xref: /openbmc/linux/drivers/usb/gadget/function/f_fs.c (revision 4833a94e)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
200a2430fSAndrzej Pietrasiewicz /*
300a2430fSAndrzej Pietrasiewicz  * f_fs.c -- user mode file system API for USB composite function controllers
400a2430fSAndrzej Pietrasiewicz  *
500a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2010 Samsung Electronics
600a2430fSAndrzej Pietrasiewicz  * Author: Michal Nazarewicz <mina86@mina86.com>
700a2430fSAndrzej Pietrasiewicz  *
800a2430fSAndrzej Pietrasiewicz  * Based on inode.c (GadgetFS) which was:
900a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2003-2004 David Brownell
1000a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2003 Agilent Technologies
1100a2430fSAndrzej Pietrasiewicz  */
1200a2430fSAndrzej Pietrasiewicz 
1300a2430fSAndrzej Pietrasiewicz 
1400a2430fSAndrzej Pietrasiewicz /* #define DEBUG */
1500a2430fSAndrzej Pietrasiewicz /* #define VERBOSE_DEBUG */
1600a2430fSAndrzej Pietrasiewicz 
1700a2430fSAndrzej Pietrasiewicz #include <linux/blkdev.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/pagemap.h>
1900a2430fSAndrzej Pietrasiewicz #include <linux/export.h>
2000a2430fSAndrzej Pietrasiewicz #include <linux/hid.h>
21772a7a72SAndrzej Pietrasiewicz #include <linux/mm.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/module.h>
23772a7a72SAndrzej Pietrasiewicz #include <linux/scatterlist.h>
24174cd4b1SIngo Molnar #include <linux/sched/signal.h>
25e2e40f2cSChristoph Hellwig #include <linux/uio.h>
26772a7a72SAndrzej Pietrasiewicz #include <linux/vmalloc.h>
2700a2430fSAndrzej Pietrasiewicz #include <asm/unaligned.h>
2800a2430fSAndrzej Pietrasiewicz 
297f7c548cSVincent Pelletier #include <linux/usb/ccid.h>
3000a2430fSAndrzej Pietrasiewicz #include <linux/usb/composite.h>
3100a2430fSAndrzej Pietrasiewicz #include <linux/usb/functionfs.h>
3200a2430fSAndrzej Pietrasiewicz 
3300a2430fSAndrzej Pietrasiewicz #include <linux/aio.h>
3400a2430fSAndrzej Pietrasiewicz #include <linux/mmu_context.h>
3500a2430fSAndrzej Pietrasiewicz #include <linux/poll.h>
365e33f6fdSRobert Baldyga #include <linux/eventfd.h>
3700a2430fSAndrzej Pietrasiewicz 
3800a2430fSAndrzej Pietrasiewicz #include "u_fs.h"
3900a2430fSAndrzej Pietrasiewicz #include "u_f.h"
4000a2430fSAndrzej Pietrasiewicz #include "u_os_desc.h"
4100a2430fSAndrzej Pietrasiewicz #include "configfs.h"
4200a2430fSAndrzej Pietrasiewicz 
4300a2430fSAndrzej Pietrasiewicz #define FUNCTIONFS_MAGIC	0xa647361 /* Chosen by a honest dice roll ;) */
4400a2430fSAndrzej Pietrasiewicz 
4500a2430fSAndrzej Pietrasiewicz /* Reference counter handling */
4600a2430fSAndrzej Pietrasiewicz static void ffs_data_get(struct ffs_data *ffs);
4700a2430fSAndrzej Pietrasiewicz static void ffs_data_put(struct ffs_data *ffs);
4800a2430fSAndrzej Pietrasiewicz /* Creates new ffs_data object. */
49addfc582SJohn Keeping static struct ffs_data *__must_check ffs_data_new(const char *dev_name)
50addfc582SJohn Keeping 	__attribute__((malloc));
5100a2430fSAndrzej Pietrasiewicz 
5200a2430fSAndrzej Pietrasiewicz /* Opened counter handling. */
5300a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs);
5400a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs);
5500a2430fSAndrzej Pietrasiewicz 
5600a2430fSAndrzej Pietrasiewicz /* Called with ffs->mutex held; take over ownership of data. */
5700a2430fSAndrzej Pietrasiewicz static int __must_check
5800a2430fSAndrzej Pietrasiewicz __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
5900a2430fSAndrzej Pietrasiewicz static int __must_check
6000a2430fSAndrzej Pietrasiewicz __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
6100a2430fSAndrzej Pietrasiewicz 
6200a2430fSAndrzej Pietrasiewicz 
6300a2430fSAndrzej Pietrasiewicz /* The function structure ***************************************************/
6400a2430fSAndrzej Pietrasiewicz 
6500a2430fSAndrzej Pietrasiewicz struct ffs_ep;
6600a2430fSAndrzej Pietrasiewicz 
6700a2430fSAndrzej Pietrasiewicz struct ffs_function {
6800a2430fSAndrzej Pietrasiewicz 	struct usb_configuration	*conf;
6900a2430fSAndrzej Pietrasiewicz 	struct usb_gadget		*gadget;
7000a2430fSAndrzej Pietrasiewicz 	struct ffs_data			*ffs;
7100a2430fSAndrzej Pietrasiewicz 
7200a2430fSAndrzej Pietrasiewicz 	struct ffs_ep			*eps;
7300a2430fSAndrzej Pietrasiewicz 	u8				eps_revmap[16];
7400a2430fSAndrzej Pietrasiewicz 	short				*interfaces_nums;
7500a2430fSAndrzej Pietrasiewicz 
7600a2430fSAndrzej Pietrasiewicz 	struct usb_function		function;
7700a2430fSAndrzej Pietrasiewicz };
7800a2430fSAndrzej Pietrasiewicz 
7900a2430fSAndrzej Pietrasiewicz 
8000a2430fSAndrzej Pietrasiewicz static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
8100a2430fSAndrzej Pietrasiewicz {
8200a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct ffs_function, function);
8300a2430fSAndrzej Pietrasiewicz }
8400a2430fSAndrzej Pietrasiewicz 
8500a2430fSAndrzej Pietrasiewicz 
8600a2430fSAndrzej Pietrasiewicz static inline enum ffs_setup_state
8700a2430fSAndrzej Pietrasiewicz ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
8800a2430fSAndrzej Pietrasiewicz {
8900a2430fSAndrzej Pietrasiewicz 	return (enum ffs_setup_state)
9000a2430fSAndrzej Pietrasiewicz 		cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
9100a2430fSAndrzej Pietrasiewicz }
9200a2430fSAndrzej Pietrasiewicz 
9300a2430fSAndrzej Pietrasiewicz 
9400a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func);
9500a2430fSAndrzej Pietrasiewicz static int __must_check ffs_func_eps_enable(struct ffs_function *func);
9600a2430fSAndrzej Pietrasiewicz 
9700a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *,
9800a2430fSAndrzej Pietrasiewicz 			 struct usb_function *);
9900a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
10000a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *);
10100a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *,
10200a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *);
10354dfce6dSFelix Hädicke static bool ffs_func_req_match(struct usb_function *,
1041a00b457SFelix Hädicke 			       const struct usb_ctrlrequest *,
1051a00b457SFelix Hädicke 			       bool config0);
10600a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *);
10700a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *);
10800a2430fSAndrzej Pietrasiewicz 
10900a2430fSAndrzej Pietrasiewicz 
11000a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
11100a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
11200a2430fSAndrzej Pietrasiewicz 
11300a2430fSAndrzej Pietrasiewicz 
11400a2430fSAndrzej Pietrasiewicz /* The endpoints structures *************************************************/
11500a2430fSAndrzej Pietrasiewicz 
11600a2430fSAndrzej Pietrasiewicz struct ffs_ep {
11700a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*ep;	/* P: ffs->eps_lock */
11800a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;	/* P: epfile->mutex */
11900a2430fSAndrzej Pietrasiewicz 
12000a2430fSAndrzej Pietrasiewicz 	/* [0]: full speed, [1]: high speed, [2]: super speed */
12100a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor	*descs[3];
12200a2430fSAndrzej Pietrasiewicz 
12300a2430fSAndrzej Pietrasiewicz 	u8				num;
12400a2430fSAndrzej Pietrasiewicz 
12500a2430fSAndrzej Pietrasiewicz 	int				status;	/* P: epfile->mutex */
12600a2430fSAndrzej Pietrasiewicz };
12700a2430fSAndrzej Pietrasiewicz 
12800a2430fSAndrzej Pietrasiewicz struct ffs_epfile {
12900a2430fSAndrzej Pietrasiewicz 	/* Protects ep->ep and ep->req. */
13000a2430fSAndrzej Pietrasiewicz 	struct mutex			mutex;
13100a2430fSAndrzej Pietrasiewicz 
13200a2430fSAndrzej Pietrasiewicz 	struct ffs_data			*ffs;
13300a2430fSAndrzej Pietrasiewicz 	struct ffs_ep			*ep;	/* P: ffs->eps_lock */
13400a2430fSAndrzej Pietrasiewicz 
13500a2430fSAndrzej Pietrasiewicz 	struct dentry			*dentry;
13600a2430fSAndrzej Pietrasiewicz 
1379353afbbSMichal Nazarewicz 	/*
1389353afbbSMichal Nazarewicz 	 * Buffer for holding data from partial reads which may happen since
1399353afbbSMichal Nazarewicz 	 * we’re rounding user read requests to a multiple of a max packet size.
140a9e6f83cSMichal Nazarewicz 	 *
141a9e6f83cSMichal Nazarewicz 	 * The pointer is initialised with NULL value and may be set by
142a9e6f83cSMichal Nazarewicz 	 * __ffs_epfile_read_data function to point to a temporary buffer.
143a9e6f83cSMichal Nazarewicz 	 *
144a9e6f83cSMichal Nazarewicz 	 * In normal operation, calls to __ffs_epfile_read_buffered will consume
145a9e6f83cSMichal Nazarewicz 	 * data from said buffer and eventually free it.  Importantly, while the
146a9e6f83cSMichal Nazarewicz 	 * function is using the buffer, it sets the pointer to NULL.  This is
147a9e6f83cSMichal Nazarewicz 	 * all right since __ffs_epfile_read_data and __ffs_epfile_read_buffered
148a9e6f83cSMichal Nazarewicz 	 * can never run concurrently (they are synchronised by epfile->mutex)
149a9e6f83cSMichal Nazarewicz 	 * so the latter will not assign a new value to the pointer.
150a9e6f83cSMichal Nazarewicz 	 *
151a9e6f83cSMichal Nazarewicz 	 * Meanwhile ffs_func_eps_disable frees the buffer (if the pointer is
152a9e6f83cSMichal Nazarewicz 	 * valid) and sets the pointer to READ_BUFFER_DROP value.  This special
153a9e6f83cSMichal Nazarewicz 	 * value is crux of the synchronisation between ffs_func_eps_disable and
154a9e6f83cSMichal Nazarewicz 	 * __ffs_epfile_read_data.
155a9e6f83cSMichal Nazarewicz 	 *
156a9e6f83cSMichal Nazarewicz 	 * Once __ffs_epfile_read_data is about to finish it will try to set the
157a9e6f83cSMichal Nazarewicz 	 * pointer back to its old value (as described above), but seeing as the
158a9e6f83cSMichal Nazarewicz 	 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
159a9e6f83cSMichal Nazarewicz 	 * the buffer.
160a9e6f83cSMichal Nazarewicz 	 *
161a9e6f83cSMichal Nazarewicz 	 * == State transitions ==
162a9e6f83cSMichal Nazarewicz 	 *
163a9e6f83cSMichal Nazarewicz 	 * • ptr == NULL:  (initial state)
164a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP
165a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffered:    nop
166a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_data allocates temp buffer: go to ptr == buf
167a9e6f83cSMichal Nazarewicz 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
168a9e6f83cSMichal Nazarewicz 	 * • ptr == DROP:
169a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffer_free: nop
170a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffered:    go to ptr == NULL
171a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_data allocates temp buffer: free buf, nop
172a9e6f83cSMichal Nazarewicz 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
173a9e6f83cSMichal Nazarewicz 	 * • ptr == buf:
174a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffer_free: free buf, go to ptr == DROP
175a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffered:    go to ptr == NULL and reading
176a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_data:        n/a, __ffs_epfile_read_buffered
177a9e6f83cSMichal Nazarewicz 	 *                                    is always called first
178a9e6f83cSMichal Nazarewicz 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
179a9e6f83cSMichal Nazarewicz 	 * • ptr == NULL and reading:
180a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP and reading
181a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffered:    n/a, mutex is held
182a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_data:        n/a, mutex is held
183a9e6f83cSMichal Nazarewicz 	 *   ◦ reading finishes and …
184a9e6f83cSMichal Nazarewicz 	 *     … all data read:               free buf, go to ptr == NULL
185a9e6f83cSMichal Nazarewicz 	 *     … otherwise:                   go to ptr == buf and reading
186a9e6f83cSMichal Nazarewicz 	 * • ptr == DROP and reading:
187a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffer_free: nop
188a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_buffered:    n/a, mutex is held
189a9e6f83cSMichal Nazarewicz 	 *   ◦ __ffs_epfile_read_data:        n/a, mutex is held
190a9e6f83cSMichal Nazarewicz 	 *   ◦ reading finishes:              free buf, go to ptr == DROP
1919353afbbSMichal Nazarewicz 	 */
192a9e6f83cSMichal Nazarewicz 	struct ffs_buffer		*read_buffer;
193a9e6f83cSMichal Nazarewicz #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
1949353afbbSMichal Nazarewicz 
19500a2430fSAndrzej Pietrasiewicz 	char				name[5];
19600a2430fSAndrzej Pietrasiewicz 
19700a2430fSAndrzej Pietrasiewicz 	unsigned char			in;	/* P: ffs->eps_lock */
19800a2430fSAndrzej Pietrasiewicz 	unsigned char			isoc;	/* P: ffs->eps_lock */
19900a2430fSAndrzej Pietrasiewicz 
20000a2430fSAndrzej Pietrasiewicz 	unsigned char			_pad;
20100a2430fSAndrzej Pietrasiewicz };
20200a2430fSAndrzej Pietrasiewicz 
2039353afbbSMichal Nazarewicz struct ffs_buffer {
2049353afbbSMichal Nazarewicz 	size_t length;
2059353afbbSMichal Nazarewicz 	char *data;
2069353afbbSMichal Nazarewicz 	char storage[];
2079353afbbSMichal Nazarewicz };
2089353afbbSMichal Nazarewicz 
20900a2430fSAndrzej Pietrasiewicz /*  ffs_io_data structure ***************************************************/
21000a2430fSAndrzej Pietrasiewicz 
21100a2430fSAndrzej Pietrasiewicz struct ffs_io_data {
21200a2430fSAndrzej Pietrasiewicz 	bool aio;
21300a2430fSAndrzej Pietrasiewicz 	bool read;
21400a2430fSAndrzej Pietrasiewicz 
21500a2430fSAndrzej Pietrasiewicz 	struct kiocb *kiocb;
216c993c39bSAl Viro 	struct iov_iter data;
217c993c39bSAl Viro 	const void *to_free;
218c993c39bSAl Viro 	char *buf;
21900a2430fSAndrzej Pietrasiewicz 
22000a2430fSAndrzej Pietrasiewicz 	struct mm_struct *mm;
22100a2430fSAndrzej Pietrasiewicz 	struct work_struct work;
22200a2430fSAndrzej Pietrasiewicz 
22300a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
22400a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
225772a7a72SAndrzej Pietrasiewicz 	struct sg_table sgt;
226772a7a72SAndrzej Pietrasiewicz 	bool use_sg;
2275e33f6fdSRobert Baldyga 
2285e33f6fdSRobert Baldyga 	struct ffs_data *ffs;
22900a2430fSAndrzej Pietrasiewicz };
23000a2430fSAndrzej Pietrasiewicz 
2316d5c1c77SRobert Baldyga struct ffs_desc_helper {
2326d5c1c77SRobert Baldyga 	struct ffs_data *ffs;
2336d5c1c77SRobert Baldyga 	unsigned interfaces_count;
2346d5c1c77SRobert Baldyga 	unsigned eps_count;
2356d5c1c77SRobert Baldyga };
2366d5c1c77SRobert Baldyga 
23700a2430fSAndrzej Pietrasiewicz static int  __must_check ffs_epfiles_create(struct ffs_data *ffs);
23800a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
23900a2430fSAndrzej Pietrasiewicz 
2401bb27cacSAl Viro static struct dentry *
24100a2430fSAndrzej Pietrasiewicz ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
2421bb27cacSAl Viro 		   const struct file_operations *fops);
24300a2430fSAndrzej Pietrasiewicz 
24400a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
24500a2430fSAndrzej Pietrasiewicz 
24600a2430fSAndrzej Pietrasiewicz DEFINE_MUTEX(ffs_lock);
24700a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_lock);
24800a2430fSAndrzej Pietrasiewicz 
24900a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name);
25000a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void);
25100a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev);
25200a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name);
25300a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data);
25400a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs);
25500a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs);
25600a2430fSAndrzej Pietrasiewicz 
25700a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
25800a2430fSAndrzej Pietrasiewicz 
25900a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
26000a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
26100a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
26200a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
26300a2430fSAndrzej Pietrasiewicz 
26400a2430fSAndrzej Pietrasiewicz 
26500a2430fSAndrzej Pietrasiewicz /* Control file aka ep0 *****************************************************/
26600a2430fSAndrzej Pietrasiewicz 
26700a2430fSAndrzej Pietrasiewicz static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
26800a2430fSAndrzej Pietrasiewicz {
26900a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = req->context;
27000a2430fSAndrzej Pietrasiewicz 
2715bdcde90SDaniel Wagner 	complete(&ffs->ep0req_completion);
27200a2430fSAndrzej Pietrasiewicz }
27300a2430fSAndrzej Pietrasiewicz 
27400a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
275c40619bbSVincent Pelletier 	__releases(&ffs->ev.waitq.lock)
27600a2430fSAndrzej Pietrasiewicz {
27700a2430fSAndrzej Pietrasiewicz 	struct usb_request *req = ffs->ep0req;
27800a2430fSAndrzej Pietrasiewicz 	int ret;
27900a2430fSAndrzej Pietrasiewicz 
28000a2430fSAndrzej Pietrasiewicz 	req->zero     = len < le16_to_cpu(ffs->ev.setup.wLength);
28100a2430fSAndrzej Pietrasiewicz 
28200a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
28300a2430fSAndrzej Pietrasiewicz 
28400a2430fSAndrzej Pietrasiewicz 	req->buf      = data;
28500a2430fSAndrzej Pietrasiewicz 	req->length   = len;
28600a2430fSAndrzej Pietrasiewicz 
28700a2430fSAndrzej Pietrasiewicz 	/*
28800a2430fSAndrzej Pietrasiewicz 	 * UDC layer requires to provide a buffer even for ZLP, but should
28900a2430fSAndrzej Pietrasiewicz 	 * not use it at all. Let's provide some poisoned pointer to catch
29000a2430fSAndrzej Pietrasiewicz 	 * possible bug in the driver.
29100a2430fSAndrzej Pietrasiewicz 	 */
29200a2430fSAndrzej Pietrasiewicz 	if (req->buf == NULL)
29300a2430fSAndrzej Pietrasiewicz 		req->buf = (void *)0xDEADBABE;
29400a2430fSAndrzej Pietrasiewicz 
29500a2430fSAndrzej Pietrasiewicz 	reinit_completion(&ffs->ep0req_completion);
29600a2430fSAndrzej Pietrasiewicz 
29700a2430fSAndrzej Pietrasiewicz 	ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
29800a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
29900a2430fSAndrzej Pietrasiewicz 		return ret;
30000a2430fSAndrzej Pietrasiewicz 
30100a2430fSAndrzej Pietrasiewicz 	ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
30200a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret)) {
30300a2430fSAndrzej Pietrasiewicz 		usb_ep_dequeue(ffs->gadget->ep0, req);
30400a2430fSAndrzej Pietrasiewicz 		return -EINTR;
30500a2430fSAndrzej Pietrasiewicz 	}
30600a2430fSAndrzej Pietrasiewicz 
30700a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
30800a2430fSAndrzej Pietrasiewicz 	return req->status ? req->status : req->actual;
30900a2430fSAndrzej Pietrasiewicz }
31000a2430fSAndrzej Pietrasiewicz 
31100a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_stall(struct ffs_data *ffs)
31200a2430fSAndrzej Pietrasiewicz {
31300a2430fSAndrzej Pietrasiewicz 	if (ffs->ev.can_stall) {
31400a2430fSAndrzej Pietrasiewicz 		pr_vdebug("ep0 stall\n");
31500a2430fSAndrzej Pietrasiewicz 		usb_ep_set_halt(ffs->gadget->ep0);
31600a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_NO_SETUP;
31700a2430fSAndrzej Pietrasiewicz 		return -EL2HLT;
31800a2430fSAndrzej Pietrasiewicz 	} else {
31900a2430fSAndrzej Pietrasiewicz 		pr_debug("bogus ep0 stall!\n");
32000a2430fSAndrzej Pietrasiewicz 		return -ESRCH;
32100a2430fSAndrzej Pietrasiewicz 	}
32200a2430fSAndrzej Pietrasiewicz }
32300a2430fSAndrzej Pietrasiewicz 
32400a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
32500a2430fSAndrzej Pietrasiewicz 			     size_t len, loff_t *ptr)
32600a2430fSAndrzej Pietrasiewicz {
32700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
32800a2430fSAndrzej Pietrasiewicz 	ssize_t ret;
32900a2430fSAndrzej Pietrasiewicz 	char *data;
33000a2430fSAndrzej Pietrasiewicz 
33100a2430fSAndrzej Pietrasiewicz 	ENTER();
33200a2430fSAndrzej Pietrasiewicz 
33300a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
33400a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
33500a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
33600a2430fSAndrzej Pietrasiewicz 
33700a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
33800a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
33900a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
34000a2430fSAndrzej Pietrasiewicz 		return ret;
34100a2430fSAndrzej Pietrasiewicz 
34200a2430fSAndrzej Pietrasiewicz 	/* Check state */
34300a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
34400a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
34500a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
34600a2430fSAndrzej Pietrasiewicz 		/* Copy data */
34700a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 16)) {
34800a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
34900a2430fSAndrzej Pietrasiewicz 			break;
35000a2430fSAndrzej Pietrasiewicz 		}
35100a2430fSAndrzej Pietrasiewicz 
35200a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
35300a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
35400a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
35500a2430fSAndrzej Pietrasiewicz 			break;
35600a2430fSAndrzej Pietrasiewicz 		}
35700a2430fSAndrzej Pietrasiewicz 
35800a2430fSAndrzej Pietrasiewicz 		/* Handle data */
35900a2430fSAndrzej Pietrasiewicz 		if (ffs->state == FFS_READ_DESCRIPTORS) {
36000a2430fSAndrzej Pietrasiewicz 			pr_info("read descriptors\n");
36100a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_descs(ffs, data, len);
36200a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
36300a2430fSAndrzej Pietrasiewicz 				break;
36400a2430fSAndrzej Pietrasiewicz 
36500a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_READ_STRINGS;
36600a2430fSAndrzej Pietrasiewicz 			ret = len;
36700a2430fSAndrzej Pietrasiewicz 		} else {
36800a2430fSAndrzej Pietrasiewicz 			pr_info("read strings\n");
36900a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_strings(ffs, data, len);
37000a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
37100a2430fSAndrzej Pietrasiewicz 				break;
37200a2430fSAndrzej Pietrasiewicz 
37300a2430fSAndrzej Pietrasiewicz 			ret = ffs_epfiles_create(ffs);
37400a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret)) {
37500a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
37600a2430fSAndrzej Pietrasiewicz 				break;
37700a2430fSAndrzej Pietrasiewicz 			}
37800a2430fSAndrzej Pietrasiewicz 
37900a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_ACTIVE;
38000a2430fSAndrzej Pietrasiewicz 			mutex_unlock(&ffs->mutex);
38100a2430fSAndrzej Pietrasiewicz 
38200a2430fSAndrzej Pietrasiewicz 			ret = ffs_ready(ffs);
38300a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0)) {
38400a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
38500a2430fSAndrzej Pietrasiewicz 				return ret;
38600a2430fSAndrzej Pietrasiewicz 			}
38700a2430fSAndrzej Pietrasiewicz 
38800a2430fSAndrzej Pietrasiewicz 			return len;
38900a2430fSAndrzej Pietrasiewicz 		}
39000a2430fSAndrzej Pietrasiewicz 		break;
39100a2430fSAndrzej Pietrasiewicz 
39200a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
39300a2430fSAndrzej Pietrasiewicz 		data = NULL;
39400a2430fSAndrzej Pietrasiewicz 		/*
39500a2430fSAndrzej Pietrasiewicz 		 * We're called from user space, we can use _irq
39600a2430fSAndrzej Pietrasiewicz 		 * rather then _irqsave
39700a2430fSAndrzej Pietrasiewicz 		 */
39800a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
39900a2430fSAndrzej Pietrasiewicz 		switch (ffs_setup_state_clear_cancelled(ffs)) {
40000a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
40100a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
40200a2430fSAndrzej Pietrasiewicz 			goto done_spin;
40300a2430fSAndrzej Pietrasiewicz 
40400a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
40500a2430fSAndrzej Pietrasiewicz 			ret = -ESRCH;
40600a2430fSAndrzej Pietrasiewicz 			goto done_spin;
40700a2430fSAndrzej Pietrasiewicz 
40800a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
40900a2430fSAndrzej Pietrasiewicz 			break;
41000a2430fSAndrzej Pietrasiewicz 		}
41100a2430fSAndrzej Pietrasiewicz 
41200a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING */
41300a2430fSAndrzej Pietrasiewicz 		if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
41400a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
41500a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
41600a2430fSAndrzej Pietrasiewicz 			break;
41700a2430fSAndrzej Pietrasiewicz 		}
41800a2430fSAndrzej Pietrasiewicz 
41900a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING and not stall */
42000a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
42100a2430fSAndrzej Pietrasiewicz 
42200a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
42300a2430fSAndrzej Pietrasiewicz 
42400a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
42500a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
42600a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
42700a2430fSAndrzej Pietrasiewicz 			break;
42800a2430fSAndrzej Pietrasiewicz 		}
42900a2430fSAndrzej Pietrasiewicz 
43000a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
43100a2430fSAndrzej Pietrasiewicz 
43200a2430fSAndrzej Pietrasiewicz 		/*
43300a2430fSAndrzej Pietrasiewicz 		 * We are guaranteed to be still in FFS_ACTIVE state
43400a2430fSAndrzej Pietrasiewicz 		 * but the state of setup could have changed from
43500a2430fSAndrzej Pietrasiewicz 		 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
43600a2430fSAndrzej Pietrasiewicz 		 * to check for that.  If that happened we copied data
43700a2430fSAndrzej Pietrasiewicz 		 * from user space in vain but it's unlikely.
43800a2430fSAndrzej Pietrasiewicz 		 *
43900a2430fSAndrzej Pietrasiewicz 		 * For sure we are not in FFS_NO_SETUP since this is
44000a2430fSAndrzej Pietrasiewicz 		 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
44100a2430fSAndrzej Pietrasiewicz 		 * transition can be performed and it's protected by
44200a2430fSAndrzej Pietrasiewicz 		 * mutex.
44300a2430fSAndrzej Pietrasiewicz 		 */
44400a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
44500a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
44600a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
44700a2430fSAndrzej Pietrasiewicz done_spin:
44800a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
44900a2430fSAndrzej Pietrasiewicz 		} else {
45000a2430fSAndrzej Pietrasiewicz 			/* unlocks spinlock */
45100a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_queue_wait(ffs, data, len);
45200a2430fSAndrzej Pietrasiewicz 		}
45300a2430fSAndrzej Pietrasiewicz 		kfree(data);
45400a2430fSAndrzej Pietrasiewicz 		break;
45500a2430fSAndrzej Pietrasiewicz 
45600a2430fSAndrzej Pietrasiewicz 	default:
45700a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
45800a2430fSAndrzej Pietrasiewicz 		break;
45900a2430fSAndrzej Pietrasiewicz 	}
46000a2430fSAndrzej Pietrasiewicz 
46100a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
46200a2430fSAndrzej Pietrasiewicz 	return ret;
46300a2430fSAndrzej Pietrasiewicz }
46400a2430fSAndrzej Pietrasiewicz 
46567913bbdSMichal Nazarewicz /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
46600a2430fSAndrzej Pietrasiewicz static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
46700a2430fSAndrzej Pietrasiewicz 				     size_t n)
468c40619bbSVincent Pelletier 	__releases(&ffs->ev.waitq.lock)
46900a2430fSAndrzej Pietrasiewicz {
47000a2430fSAndrzej Pietrasiewicz 	/*
47167913bbdSMichal Nazarewicz 	 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
47267913bbdSMichal Nazarewicz 	 * size of ffs->ev.types array (which is four) so that's how much space
47367913bbdSMichal Nazarewicz 	 * we reserve.
47400a2430fSAndrzej Pietrasiewicz 	 */
47567913bbdSMichal Nazarewicz 	struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
47667913bbdSMichal Nazarewicz 	const size_t size = n * sizeof *events;
47700a2430fSAndrzej Pietrasiewicz 	unsigned i = 0;
47800a2430fSAndrzej Pietrasiewicz 
47967913bbdSMichal Nazarewicz 	memset(events, 0, size);
48000a2430fSAndrzej Pietrasiewicz 
48100a2430fSAndrzej Pietrasiewicz 	do {
48200a2430fSAndrzej Pietrasiewicz 		events[i].type = ffs->ev.types[i];
48300a2430fSAndrzej Pietrasiewicz 		if (events[i].type == FUNCTIONFS_SETUP) {
48400a2430fSAndrzej Pietrasiewicz 			events[i].u.setup = ffs->ev.setup;
48500a2430fSAndrzej Pietrasiewicz 			ffs->setup_state = FFS_SETUP_PENDING;
48600a2430fSAndrzej Pietrasiewicz 		}
48700a2430fSAndrzej Pietrasiewicz 	} while (++i < n);
48800a2430fSAndrzej Pietrasiewicz 
48900a2430fSAndrzej Pietrasiewicz 	ffs->ev.count -= n;
49067913bbdSMichal Nazarewicz 	if (ffs->ev.count)
49100a2430fSAndrzej Pietrasiewicz 		memmove(ffs->ev.types, ffs->ev.types + n,
49200a2430fSAndrzej Pietrasiewicz 			ffs->ev.count * sizeof *ffs->ev.types);
49300a2430fSAndrzej Pietrasiewicz 
49400a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
49500a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
49600a2430fSAndrzej Pietrasiewicz 
4977fe9a937SDaniel Walter 	return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
49800a2430fSAndrzej Pietrasiewicz }
49900a2430fSAndrzej Pietrasiewicz 
50000a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
50100a2430fSAndrzej Pietrasiewicz 			    size_t len, loff_t *ptr)
50200a2430fSAndrzej Pietrasiewicz {
50300a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
50400a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
50500a2430fSAndrzej Pietrasiewicz 	size_t n;
50600a2430fSAndrzej Pietrasiewicz 	int ret;
50700a2430fSAndrzej Pietrasiewicz 
50800a2430fSAndrzej Pietrasiewicz 	ENTER();
50900a2430fSAndrzej Pietrasiewicz 
51000a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
51100a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
51200a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
51300a2430fSAndrzej Pietrasiewicz 
51400a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
51500a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
51600a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
51700a2430fSAndrzej Pietrasiewicz 		return ret;
51800a2430fSAndrzej Pietrasiewicz 
51900a2430fSAndrzej Pietrasiewicz 	/* Check state */
52000a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE) {
52100a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
52200a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
52300a2430fSAndrzej Pietrasiewicz 	}
52400a2430fSAndrzej Pietrasiewicz 
52500a2430fSAndrzej Pietrasiewicz 	/*
52600a2430fSAndrzej Pietrasiewicz 	 * We're called from user space, we can use _irq rather then
52700a2430fSAndrzej Pietrasiewicz 	 * _irqsave
52800a2430fSAndrzej Pietrasiewicz 	 */
52900a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&ffs->ev.waitq.lock);
53000a2430fSAndrzej Pietrasiewicz 
53100a2430fSAndrzej Pietrasiewicz 	switch (ffs_setup_state_clear_cancelled(ffs)) {
53200a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_CANCELLED:
53300a2430fSAndrzej Pietrasiewicz 		ret = -EIDRM;
53400a2430fSAndrzej Pietrasiewicz 		break;
53500a2430fSAndrzej Pietrasiewicz 
53600a2430fSAndrzej Pietrasiewicz 	case FFS_NO_SETUP:
53700a2430fSAndrzej Pietrasiewicz 		n = len / sizeof(struct usb_functionfs_event);
53800a2430fSAndrzej Pietrasiewicz 		if (unlikely(!n)) {
53900a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
54000a2430fSAndrzej Pietrasiewicz 			break;
54100a2430fSAndrzej Pietrasiewicz 		}
54200a2430fSAndrzej Pietrasiewicz 
54300a2430fSAndrzej Pietrasiewicz 		if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
54400a2430fSAndrzej Pietrasiewicz 			ret = -EAGAIN;
54500a2430fSAndrzej Pietrasiewicz 			break;
54600a2430fSAndrzej Pietrasiewicz 		}
54700a2430fSAndrzej Pietrasiewicz 
54800a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
54900a2430fSAndrzej Pietrasiewicz 							ffs->ev.count)) {
55000a2430fSAndrzej Pietrasiewicz 			ret = -EINTR;
55100a2430fSAndrzej Pietrasiewicz 			break;
55200a2430fSAndrzej Pietrasiewicz 		}
55300a2430fSAndrzej Pietrasiewicz 
554c40619bbSVincent Pelletier 		/* unlocks spinlock */
55500a2430fSAndrzej Pietrasiewicz 		return __ffs_ep0_read_events(ffs, buf,
55600a2430fSAndrzej Pietrasiewicz 					     min(n, (size_t)ffs->ev.count));
55700a2430fSAndrzej Pietrasiewicz 
55800a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_PENDING:
55900a2430fSAndrzej Pietrasiewicz 		if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
56000a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
56100a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
56200a2430fSAndrzej Pietrasiewicz 			goto done_mutex;
56300a2430fSAndrzej Pietrasiewicz 		}
56400a2430fSAndrzej Pietrasiewicz 
56500a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
56600a2430fSAndrzej Pietrasiewicz 
56700a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
56800a2430fSAndrzej Pietrasiewicz 
56900a2430fSAndrzej Pietrasiewicz 		if (likely(len)) {
57000a2430fSAndrzej Pietrasiewicz 			data = kmalloc(len, GFP_KERNEL);
57100a2430fSAndrzej Pietrasiewicz 			if (unlikely(!data)) {
57200a2430fSAndrzej Pietrasiewicz 				ret = -ENOMEM;
57300a2430fSAndrzej Pietrasiewicz 				goto done_mutex;
57400a2430fSAndrzej Pietrasiewicz 			}
57500a2430fSAndrzej Pietrasiewicz 		}
57600a2430fSAndrzej Pietrasiewicz 
57700a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
57800a2430fSAndrzej Pietrasiewicz 
57900a2430fSAndrzej Pietrasiewicz 		/* See ffs_ep0_write() */
58000a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
58100a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
58200a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
58300a2430fSAndrzej Pietrasiewicz 			break;
58400a2430fSAndrzej Pietrasiewicz 		}
58500a2430fSAndrzej Pietrasiewicz 
58600a2430fSAndrzej Pietrasiewicz 		/* unlocks spinlock */
58700a2430fSAndrzej Pietrasiewicz 		ret = __ffs_ep0_queue_wait(ffs, data, len);
5887fe9a937SDaniel Walter 		if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
58900a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
59000a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
59100a2430fSAndrzej Pietrasiewicz 
59200a2430fSAndrzej Pietrasiewicz 	default:
59300a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
59400a2430fSAndrzej Pietrasiewicz 		break;
59500a2430fSAndrzej Pietrasiewicz 	}
59600a2430fSAndrzej Pietrasiewicz 
59700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
59800a2430fSAndrzej Pietrasiewicz done_mutex:
59900a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
60000a2430fSAndrzej Pietrasiewicz 	kfree(data);
60100a2430fSAndrzej Pietrasiewicz 	return ret;
60200a2430fSAndrzej Pietrasiewicz }
60300a2430fSAndrzej Pietrasiewicz 
60400a2430fSAndrzej Pietrasiewicz static int ffs_ep0_open(struct inode *inode, struct file *file)
60500a2430fSAndrzej Pietrasiewicz {
60600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = inode->i_private;
60700a2430fSAndrzej Pietrasiewicz 
60800a2430fSAndrzej Pietrasiewicz 	ENTER();
60900a2430fSAndrzej Pietrasiewicz 
61000a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs->state == FFS_CLOSING))
61100a2430fSAndrzej Pietrasiewicz 		return -EBUSY;
61200a2430fSAndrzej Pietrasiewicz 
61300a2430fSAndrzej Pietrasiewicz 	file->private_data = ffs;
61400a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(ffs);
61500a2430fSAndrzej Pietrasiewicz 
61600a2430fSAndrzej Pietrasiewicz 	return 0;
61700a2430fSAndrzej Pietrasiewicz }
61800a2430fSAndrzej Pietrasiewicz 
61900a2430fSAndrzej Pietrasiewicz static int ffs_ep0_release(struct inode *inode, struct file *file)
62000a2430fSAndrzej Pietrasiewicz {
62100a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
62200a2430fSAndrzej Pietrasiewicz 
62300a2430fSAndrzej Pietrasiewicz 	ENTER();
62400a2430fSAndrzej Pietrasiewicz 
62500a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(ffs);
62600a2430fSAndrzej Pietrasiewicz 
62700a2430fSAndrzej Pietrasiewicz 	return 0;
62800a2430fSAndrzej Pietrasiewicz }
62900a2430fSAndrzej Pietrasiewicz 
63000a2430fSAndrzej Pietrasiewicz static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
63100a2430fSAndrzej Pietrasiewicz {
63200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
63300a2430fSAndrzej Pietrasiewicz 	struct usb_gadget *gadget = ffs->gadget;
63400a2430fSAndrzej Pietrasiewicz 	long ret;
63500a2430fSAndrzej Pietrasiewicz 
63600a2430fSAndrzej Pietrasiewicz 	ENTER();
63700a2430fSAndrzej Pietrasiewicz 
63800a2430fSAndrzej Pietrasiewicz 	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
63900a2430fSAndrzej Pietrasiewicz 		struct ffs_function *func = ffs->func;
64000a2430fSAndrzej Pietrasiewicz 		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
64100a2430fSAndrzej Pietrasiewicz 	} else if (gadget && gadget->ops->ioctl) {
64200a2430fSAndrzej Pietrasiewicz 		ret = gadget->ops->ioctl(gadget, code, value);
64300a2430fSAndrzej Pietrasiewicz 	} else {
64400a2430fSAndrzej Pietrasiewicz 		ret = -ENOTTY;
64500a2430fSAndrzej Pietrasiewicz 	}
64600a2430fSAndrzej Pietrasiewicz 
64700a2430fSAndrzej Pietrasiewicz 	return ret;
64800a2430fSAndrzej Pietrasiewicz }
64900a2430fSAndrzej Pietrasiewicz 
650afc9a42bSAl Viro static __poll_t ffs_ep0_poll(struct file *file, poll_table *wait)
65100a2430fSAndrzej Pietrasiewicz {
65200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
653a9a08845SLinus Torvalds 	__poll_t mask = EPOLLWRNORM;
65400a2430fSAndrzej Pietrasiewicz 	int ret;
65500a2430fSAndrzej Pietrasiewicz 
65600a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &ffs->ev.waitq, wait);
65700a2430fSAndrzej Pietrasiewicz 
65800a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
65900a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
66000a2430fSAndrzej Pietrasiewicz 		return mask;
66100a2430fSAndrzej Pietrasiewicz 
66200a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
66300a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
66400a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
665a9a08845SLinus Torvalds 		mask |= EPOLLOUT;
66600a2430fSAndrzej Pietrasiewicz 		break;
66700a2430fSAndrzej Pietrasiewicz 
66800a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
66900a2430fSAndrzej Pietrasiewicz 		switch (ffs->setup_state) {
67000a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
67100a2430fSAndrzej Pietrasiewicz 			if (ffs->ev.count)
672a9a08845SLinus Torvalds 				mask |= EPOLLIN;
67300a2430fSAndrzej Pietrasiewicz 			break;
67400a2430fSAndrzej Pietrasiewicz 
67500a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
67600a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
677a9a08845SLinus Torvalds 			mask |= (EPOLLIN | EPOLLOUT);
67800a2430fSAndrzej Pietrasiewicz 			break;
67900a2430fSAndrzej Pietrasiewicz 		}
68000a2430fSAndrzej Pietrasiewicz 	case FFS_CLOSING:
68100a2430fSAndrzej Pietrasiewicz 		break;
68218d6b32fSRobert Baldyga 	case FFS_DEACTIVATED:
68318d6b32fSRobert Baldyga 		break;
68400a2430fSAndrzej Pietrasiewicz 	}
68500a2430fSAndrzej Pietrasiewicz 
68600a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
68700a2430fSAndrzej Pietrasiewicz 
68800a2430fSAndrzej Pietrasiewicz 	return mask;
68900a2430fSAndrzej Pietrasiewicz }
69000a2430fSAndrzej Pietrasiewicz 
69100a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_ep0_operations = {
69200a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
69300a2430fSAndrzej Pietrasiewicz 
69400a2430fSAndrzej Pietrasiewicz 	.open =		ffs_ep0_open,
69500a2430fSAndrzej Pietrasiewicz 	.write =	ffs_ep0_write,
69600a2430fSAndrzej Pietrasiewicz 	.read =		ffs_ep0_read,
69700a2430fSAndrzej Pietrasiewicz 	.release =	ffs_ep0_release,
69800a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_ep0_ioctl,
69900a2430fSAndrzej Pietrasiewicz 	.poll =		ffs_ep0_poll,
70000a2430fSAndrzej Pietrasiewicz };
70100a2430fSAndrzej Pietrasiewicz 
70200a2430fSAndrzej Pietrasiewicz 
70300a2430fSAndrzej Pietrasiewicz /* "Normal" endpoints operations ********************************************/
70400a2430fSAndrzej Pietrasiewicz 
70500a2430fSAndrzej Pietrasiewicz static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
70600a2430fSAndrzej Pietrasiewicz {
70700a2430fSAndrzej Pietrasiewicz 	ENTER();
70800a2430fSAndrzej Pietrasiewicz 	if (likely(req->context)) {
70900a2430fSAndrzej Pietrasiewicz 		struct ffs_ep *ep = _ep->driver_data;
71000a2430fSAndrzej Pietrasiewicz 		ep->status = req->status ? req->status : req->actual;
71100a2430fSAndrzej Pietrasiewicz 		complete(req->context);
71200a2430fSAndrzej Pietrasiewicz 	}
71300a2430fSAndrzej Pietrasiewicz }
71400a2430fSAndrzej Pietrasiewicz 
715c662a31bSMichal Nazarewicz static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
716c662a31bSMichal Nazarewicz {
717c662a31bSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
718c662a31bSMichal Nazarewicz 	if (likely(ret == data_len))
719c662a31bSMichal Nazarewicz 		return ret;
720c662a31bSMichal Nazarewicz 
721c662a31bSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
722c662a31bSMichal Nazarewicz 		return -EFAULT;
723c662a31bSMichal Nazarewicz 
724c662a31bSMichal Nazarewicz 	/*
725c662a31bSMichal Nazarewicz 	 * Dear user space developer!
726c662a31bSMichal Nazarewicz 	 *
727c662a31bSMichal Nazarewicz 	 * TL;DR: To stop getting below error message in your kernel log, change
728c662a31bSMichal Nazarewicz 	 * user space code using functionfs to align read buffers to a max
729c662a31bSMichal Nazarewicz 	 * packet size.
730c662a31bSMichal Nazarewicz 	 *
731c662a31bSMichal Nazarewicz 	 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
732c662a31bSMichal Nazarewicz 	 * packet size.  When unaligned buffer is passed to functionfs, it
733c662a31bSMichal Nazarewicz 	 * internally uses a larger, aligned buffer so that such UDCs are happy.
734c662a31bSMichal Nazarewicz 	 *
735c662a31bSMichal Nazarewicz 	 * Unfortunately, this means that host may send more data than was
736c662a31bSMichal Nazarewicz 	 * requested in read(2) system call.  f_fs doesn’t know what to do with
737c662a31bSMichal Nazarewicz 	 * that excess data so it simply drops it.
738c662a31bSMichal Nazarewicz 	 *
739c662a31bSMichal Nazarewicz 	 * Was the buffer aligned in the first place, no such problem would
740c662a31bSMichal Nazarewicz 	 * happen.
741c662a31bSMichal Nazarewicz 	 *
7429353afbbSMichal Nazarewicz 	 * Data may be dropped only in AIO reads.  Synchronous reads are handled
7439353afbbSMichal Nazarewicz 	 * by splitting a request into multiple parts.  This splitting may still
7449353afbbSMichal Nazarewicz 	 * be a problem though so it’s likely best to align the buffer
7459353afbbSMichal Nazarewicz 	 * regardless of it being AIO or not..
7469353afbbSMichal Nazarewicz 	 *
747c662a31bSMichal Nazarewicz 	 * This only affects OUT endpoints, i.e. reading data with a read(2),
748c662a31bSMichal Nazarewicz 	 * aio_read(2) etc. system calls.  Writing data to an IN endpoint is not
749c662a31bSMichal Nazarewicz 	 * affected.
750c662a31bSMichal Nazarewicz 	 */
751c662a31bSMichal Nazarewicz 	pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
752c662a31bSMichal Nazarewicz 	       "Align read buffer size to max packet size to avoid the problem.\n",
753c662a31bSMichal Nazarewicz 	       data_len, ret);
754c662a31bSMichal Nazarewicz 
755c662a31bSMichal Nazarewicz 	return ret;
756c662a31bSMichal Nazarewicz }
757c662a31bSMichal Nazarewicz 
758772a7a72SAndrzej Pietrasiewicz /*
759772a7a72SAndrzej Pietrasiewicz  * allocate a virtually contiguous buffer and create a scatterlist describing it
760772a7a72SAndrzej Pietrasiewicz  * @sg_table	- pointer to a place to be filled with sg_table contents
761772a7a72SAndrzej Pietrasiewicz  * @size	- required buffer size
762772a7a72SAndrzej Pietrasiewicz  */
763772a7a72SAndrzej Pietrasiewicz static void *ffs_build_sg_list(struct sg_table *sgt, size_t sz)
764772a7a72SAndrzej Pietrasiewicz {
765772a7a72SAndrzej Pietrasiewicz 	struct page **pages;
766772a7a72SAndrzej Pietrasiewicz 	void *vaddr, *ptr;
767772a7a72SAndrzej Pietrasiewicz 	unsigned int n_pages;
768772a7a72SAndrzej Pietrasiewicz 	int i;
769772a7a72SAndrzej Pietrasiewicz 
770772a7a72SAndrzej Pietrasiewicz 	vaddr = vmalloc(sz);
771772a7a72SAndrzej Pietrasiewicz 	if (!vaddr)
772772a7a72SAndrzej Pietrasiewicz 		return NULL;
773772a7a72SAndrzej Pietrasiewicz 
774772a7a72SAndrzej Pietrasiewicz 	n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
775772a7a72SAndrzej Pietrasiewicz 	pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
776772a7a72SAndrzej Pietrasiewicz 	if (!pages) {
777772a7a72SAndrzej Pietrasiewicz 		vfree(vaddr);
778772a7a72SAndrzej Pietrasiewicz 
779772a7a72SAndrzej Pietrasiewicz 		return NULL;
780772a7a72SAndrzej Pietrasiewicz 	}
781772a7a72SAndrzej Pietrasiewicz 	for (i = 0, ptr = vaddr; i < n_pages; ++i, ptr += PAGE_SIZE)
782772a7a72SAndrzej Pietrasiewicz 		pages[i] = vmalloc_to_page(ptr);
783772a7a72SAndrzej Pietrasiewicz 
784772a7a72SAndrzej Pietrasiewicz 	if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL)) {
785772a7a72SAndrzej Pietrasiewicz 		kvfree(pages);
786772a7a72SAndrzej Pietrasiewicz 		vfree(vaddr);
787772a7a72SAndrzej Pietrasiewicz 
788772a7a72SAndrzej Pietrasiewicz 		return NULL;
789772a7a72SAndrzej Pietrasiewicz 	}
790772a7a72SAndrzej Pietrasiewicz 	kvfree(pages);
791772a7a72SAndrzej Pietrasiewicz 
792772a7a72SAndrzej Pietrasiewicz 	return vaddr;
793772a7a72SAndrzej Pietrasiewicz }
794772a7a72SAndrzej Pietrasiewicz 
795772a7a72SAndrzej Pietrasiewicz static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
796772a7a72SAndrzej Pietrasiewicz 	size_t data_len)
797772a7a72SAndrzej Pietrasiewicz {
798772a7a72SAndrzej Pietrasiewicz 	if (io_data->use_sg)
799772a7a72SAndrzej Pietrasiewicz 		return ffs_build_sg_list(&io_data->sgt, data_len);
800772a7a72SAndrzej Pietrasiewicz 
801772a7a72SAndrzej Pietrasiewicz 	return kmalloc(data_len, GFP_KERNEL);
802772a7a72SAndrzej Pietrasiewicz }
803772a7a72SAndrzej Pietrasiewicz 
804772a7a72SAndrzej Pietrasiewicz static inline void ffs_free_buffer(struct ffs_io_data *io_data)
805772a7a72SAndrzej Pietrasiewicz {
806772a7a72SAndrzej Pietrasiewicz 	if (!io_data->buf)
807772a7a72SAndrzej Pietrasiewicz 		return;
808772a7a72SAndrzej Pietrasiewicz 
809772a7a72SAndrzej Pietrasiewicz 	if (io_data->use_sg) {
810772a7a72SAndrzej Pietrasiewicz 		sg_free_table(&io_data->sgt);
811772a7a72SAndrzej Pietrasiewicz 		vfree(io_data->buf);
812772a7a72SAndrzej Pietrasiewicz 	} else {
813772a7a72SAndrzej Pietrasiewicz 		kfree(io_data->buf);
814772a7a72SAndrzej Pietrasiewicz 	}
815772a7a72SAndrzej Pietrasiewicz }
816772a7a72SAndrzej Pietrasiewicz 
81700a2430fSAndrzej Pietrasiewicz static void ffs_user_copy_worker(struct work_struct *work)
81800a2430fSAndrzej Pietrasiewicz {
81900a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
82000a2430fSAndrzej Pietrasiewicz 						   work);
82100a2430fSAndrzej Pietrasiewicz 	int ret = io_data->req->status ? io_data->req->status :
82200a2430fSAndrzej Pietrasiewicz 					 io_data->req->actual;
82338740a5bSLars-Peter Clausen 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
82400a2430fSAndrzej Pietrasiewicz 
82500a2430fSAndrzej Pietrasiewicz 	if (io_data->read && ret > 0) {
8264058ebf3SLars-Peter Clausen 		mm_segment_t oldfs = get_fs();
8274058ebf3SLars-Peter Clausen 
8284058ebf3SLars-Peter Clausen 		set_fs(USER_DS);
82900a2430fSAndrzej Pietrasiewicz 		use_mm(io_data->mm);
830c662a31bSMichal Nazarewicz 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
83100a2430fSAndrzej Pietrasiewicz 		unuse_mm(io_data->mm);
8324058ebf3SLars-Peter Clausen 		set_fs(oldfs);
83300a2430fSAndrzej Pietrasiewicz 	}
83400a2430fSAndrzej Pietrasiewicz 
83504b2fa9fSChristoph Hellwig 	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
83600a2430fSAndrzej Pietrasiewicz 
83738740a5bSLars-Peter Clausen 	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
8385e33f6fdSRobert Baldyga 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);
8395e33f6fdSRobert Baldyga 
84000a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(io_data->ep, io_data->req);
84100a2430fSAndrzej Pietrasiewicz 
84200a2430fSAndrzej Pietrasiewicz 	if (io_data->read)
843c993c39bSAl Viro 		kfree(io_data->to_free);
844772a7a72SAndrzej Pietrasiewicz 	ffs_free_buffer(io_data);
84500a2430fSAndrzej Pietrasiewicz 	kfree(io_data);
84600a2430fSAndrzej Pietrasiewicz }
84700a2430fSAndrzej Pietrasiewicz 
84800a2430fSAndrzej Pietrasiewicz static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
84900a2430fSAndrzej Pietrasiewicz 					 struct usb_request *req)
85000a2430fSAndrzej Pietrasiewicz {
85100a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = req->context;
852addfc582SJohn Keeping 	struct ffs_data *ffs = io_data->ffs;
85300a2430fSAndrzej Pietrasiewicz 
85400a2430fSAndrzej Pietrasiewicz 	ENTER();
85500a2430fSAndrzej Pietrasiewicz 
85600a2430fSAndrzej Pietrasiewicz 	INIT_WORK(&io_data->work, ffs_user_copy_worker);
857addfc582SJohn Keeping 	queue_work(ffs->io_completion_wq, &io_data->work);
85800a2430fSAndrzej Pietrasiewicz }
85900a2430fSAndrzej Pietrasiewicz 
860a9e6f83cSMichal Nazarewicz static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
861a9e6f83cSMichal Nazarewicz {
862a9e6f83cSMichal Nazarewicz 	/*
863a9e6f83cSMichal Nazarewicz 	 * See comment in struct ffs_epfile for full read_buffer pointer
864a9e6f83cSMichal Nazarewicz 	 * synchronisation story.
865a9e6f83cSMichal Nazarewicz 	 */
866a9e6f83cSMichal Nazarewicz 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP);
867a9e6f83cSMichal Nazarewicz 	if (buf && buf != READ_BUFFER_DROP)
868a9e6f83cSMichal Nazarewicz 		kfree(buf);
869a9e6f83cSMichal Nazarewicz }
870a9e6f83cSMichal Nazarewicz 
8719353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
8729353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
8739353afbbSMichal Nazarewicz 					  struct iov_iter *iter)
8749353afbbSMichal Nazarewicz {
875a9e6f83cSMichal Nazarewicz 	/*
876a9e6f83cSMichal Nazarewicz 	 * Null out epfile->read_buffer so ffs_func_eps_disable does not free
877a9e6f83cSMichal Nazarewicz 	 * the buffer while we are using it.  See comment in struct ffs_epfile
878a9e6f83cSMichal Nazarewicz 	 * for full read_buffer pointer synchronisation story.
879a9e6f83cSMichal Nazarewicz 	 */
880a9e6f83cSMichal Nazarewicz 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL);
8819353afbbSMichal Nazarewicz 	ssize_t ret;
882a9e6f83cSMichal Nazarewicz 	if (!buf || buf == READ_BUFFER_DROP)
8839353afbbSMichal Nazarewicz 		return 0;
8849353afbbSMichal Nazarewicz 
8859353afbbSMichal Nazarewicz 	ret = copy_to_iter(buf->data, buf->length, iter);
8869353afbbSMichal Nazarewicz 	if (buf->length == ret) {
8879353afbbSMichal Nazarewicz 		kfree(buf);
888a9e6f83cSMichal Nazarewicz 		return ret;
889a9e6f83cSMichal Nazarewicz 	}
890a9e6f83cSMichal Nazarewicz 
891a9e6f83cSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter))) {
8929353afbbSMichal Nazarewicz 		ret = -EFAULT;
8939353afbbSMichal Nazarewicz 	} else {
8949353afbbSMichal Nazarewicz 		buf->length -= ret;
8959353afbbSMichal Nazarewicz 		buf->data += ret;
8969353afbbSMichal Nazarewicz 	}
897a9e6f83cSMichal Nazarewicz 
898a9e6f83cSMichal Nazarewicz 	if (cmpxchg(&epfile->read_buffer, NULL, buf))
899a9e6f83cSMichal Nazarewicz 		kfree(buf);
900a9e6f83cSMichal Nazarewicz 
9019353afbbSMichal Nazarewicz 	return ret;
9029353afbbSMichal Nazarewicz }
9039353afbbSMichal Nazarewicz 
9049353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
9059353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
9069353afbbSMichal Nazarewicz 				      void *data, int data_len,
9079353afbbSMichal Nazarewicz 				      struct iov_iter *iter)
9089353afbbSMichal Nazarewicz {
9099353afbbSMichal Nazarewicz 	struct ffs_buffer *buf;
9109353afbbSMichal Nazarewicz 
9119353afbbSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
9129353afbbSMichal Nazarewicz 	if (likely(data_len == ret))
9139353afbbSMichal Nazarewicz 		return ret;
9149353afbbSMichal Nazarewicz 
9159353afbbSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
9169353afbbSMichal Nazarewicz 		return -EFAULT;
9179353afbbSMichal Nazarewicz 
9189353afbbSMichal Nazarewicz 	/* See ffs_copy_to_iter for more context. */
9199353afbbSMichal Nazarewicz 	pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
9209353afbbSMichal Nazarewicz 		data_len, ret);
9219353afbbSMichal Nazarewicz 
9229353afbbSMichal Nazarewicz 	data_len -= ret;
9239353afbbSMichal Nazarewicz 	buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
92444963d64SDan Carpenter 	if (!buf)
92544963d64SDan Carpenter 		return -ENOMEM;
9269353afbbSMichal Nazarewicz 	buf->length = data_len;
9279353afbbSMichal Nazarewicz 	buf->data = buf->storage;
9289353afbbSMichal Nazarewicz 	memcpy(buf->storage, data + ret, data_len);
929a9e6f83cSMichal Nazarewicz 
930a9e6f83cSMichal Nazarewicz 	/*
931a9e6f83cSMichal Nazarewicz 	 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
932a9e6f83cSMichal Nazarewicz 	 * ffs_func_eps_disable has been called in the meanwhile).  See comment
933a9e6f83cSMichal Nazarewicz 	 * in struct ffs_epfile for full read_buffer pointer synchronisation
934a9e6f83cSMichal Nazarewicz 	 * story.
935a9e6f83cSMichal Nazarewicz 	 */
936a9e6f83cSMichal Nazarewicz 	if (unlikely(cmpxchg(&epfile->read_buffer, NULL, buf)))
937a9e6f83cSMichal Nazarewicz 		kfree(buf);
9389353afbbSMichal Nazarewicz 
9399353afbbSMichal Nazarewicz 	return ret;
9409353afbbSMichal Nazarewicz }
9419353afbbSMichal Nazarewicz 
94200a2430fSAndrzej Pietrasiewicz static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
94300a2430fSAndrzej Pietrasiewicz {
94400a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
945ae76e134SMichal Nazarewicz 	struct usb_request *req;
94600a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep;
94700a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
948c0d31b3cSDavid Cohen 	ssize_t ret, data_len = -EINVAL;
94900a2430fSAndrzej Pietrasiewicz 	int halt;
95000a2430fSAndrzej Pietrasiewicz 
95100a2430fSAndrzej Pietrasiewicz 	/* Are we still active? */
952b3591f67SMichal Nazarewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
953b3591f67SMichal Nazarewicz 		return -ENODEV;
95400a2430fSAndrzej Pietrasiewicz 
95500a2430fSAndrzej Pietrasiewicz 	/* Wait for endpoint to be enabled */
95600a2430fSAndrzej Pietrasiewicz 	ep = epfile->ep;
95700a2430fSAndrzej Pietrasiewicz 	if (!ep) {
958b3591f67SMichal Nazarewicz 		if (file->f_flags & O_NONBLOCK)
959b3591f67SMichal Nazarewicz 			return -EAGAIN;
96000a2430fSAndrzej Pietrasiewicz 
961e16828cfSJerry Zhang 		ret = wait_event_interruptible(
962e16828cfSJerry Zhang 				epfile->ffs->wait, (ep = epfile->ep));
963b3591f67SMichal Nazarewicz 		if (ret)
964b3591f67SMichal Nazarewicz 			return -EINTR;
96500a2430fSAndrzej Pietrasiewicz 	}
96600a2430fSAndrzej Pietrasiewicz 
96700a2430fSAndrzej Pietrasiewicz 	/* Do we halt? */
96800a2430fSAndrzej Pietrasiewicz 	halt = (!io_data->read == !epfile->in);
969b3591f67SMichal Nazarewicz 	if (halt && epfile->isoc)
970b3591f67SMichal Nazarewicz 		return -EINVAL;
97100a2430fSAndrzej Pietrasiewicz 
9729353afbbSMichal Nazarewicz 	/* We will be using request and read_buffer */
9739353afbbSMichal Nazarewicz 	ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
9749353afbbSMichal Nazarewicz 	if (unlikely(ret))
9759353afbbSMichal Nazarewicz 		goto error;
9769353afbbSMichal Nazarewicz 
97700a2430fSAndrzej Pietrasiewicz 	/* Allocate & copy */
97800a2430fSAndrzej Pietrasiewicz 	if (!halt) {
9799353afbbSMichal Nazarewicz 		struct usb_gadget *gadget;
9809353afbbSMichal Nazarewicz 
9819353afbbSMichal Nazarewicz 		/*
9829353afbbSMichal Nazarewicz 		 * Do we have buffered data from previous partial read?  Check
9839353afbbSMichal Nazarewicz 		 * that for synchronous case only because we do not have
9849353afbbSMichal Nazarewicz 		 * facility to ‘wake up’ a pending asynchronous read and push
9859353afbbSMichal Nazarewicz 		 * buffered data to it which we would need to make things behave
9869353afbbSMichal Nazarewicz 		 * consistently.
9879353afbbSMichal Nazarewicz 		 */
9889353afbbSMichal Nazarewicz 		if (!io_data->aio && io_data->read) {
9899353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
9909353afbbSMichal Nazarewicz 			if (ret)
9919353afbbSMichal Nazarewicz 				goto error_mutex;
9929353afbbSMichal Nazarewicz 		}
9939353afbbSMichal Nazarewicz 
99400a2430fSAndrzej Pietrasiewicz 		/*
99500a2430fSAndrzej Pietrasiewicz 		 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
996ae76e134SMichal Nazarewicz 		 * before the waiting completes, so do not assign to 'gadget'
997ae76e134SMichal Nazarewicz 		 * earlier
99800a2430fSAndrzej Pietrasiewicz 		 */
9999353afbbSMichal Nazarewicz 		gadget = epfile->ffs->gadget;
100000a2430fSAndrzej Pietrasiewicz 
100100a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&epfile->ffs->eps_lock);
100200a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
100300a2430fSAndrzej Pietrasiewicz 		if (epfile->ep != ep) {
10049353afbbSMichal Nazarewicz 			ret = -ESHUTDOWN;
10059353afbbSMichal Nazarewicz 			goto error_lock;
100600a2430fSAndrzej Pietrasiewicz 		}
1007c993c39bSAl Viro 		data_len = iov_iter_count(&io_data->data);
100800a2430fSAndrzej Pietrasiewicz 		/*
100900a2430fSAndrzej Pietrasiewicz 		 * Controller may require buffer size to be aligned to
101000a2430fSAndrzej Pietrasiewicz 		 * maxpacketsize of an out endpoint.
101100a2430fSAndrzej Pietrasiewicz 		 */
1012c993c39bSAl Viro 		if (io_data->read)
1013c993c39bSAl Viro 			data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
10144833a94eSFei Yang 
10154833a94eSFei Yang 		io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
101600a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
101700a2430fSAndrzej Pietrasiewicz 
1018772a7a72SAndrzej Pietrasiewicz 		data = ffs_alloc_buffer(io_data, data_len);
10199353afbbSMichal Nazarewicz 		if (unlikely(!data)) {
10209353afbbSMichal Nazarewicz 			ret = -ENOMEM;
10219353afbbSMichal Nazarewicz 			goto error_mutex;
10229353afbbSMichal Nazarewicz 		}
10239353afbbSMichal Nazarewicz 		if (!io_data->read &&
1024cbbd26b8SAl Viro 		    !copy_from_iter_full(data, data_len, &io_data->data)) {
102500a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
10269353afbbSMichal Nazarewicz 			goto error_mutex;
102700a2430fSAndrzej Pietrasiewicz 		}
102800a2430fSAndrzej Pietrasiewicz 	}
102900a2430fSAndrzej Pietrasiewicz 
103000a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
103100a2430fSAndrzej Pietrasiewicz 
103200a2430fSAndrzej Pietrasiewicz 	if (epfile->ep != ep) {
103300a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
103400a2430fSAndrzej Pietrasiewicz 		ret = -ESHUTDOWN;
103500a2430fSAndrzej Pietrasiewicz 	} else if (halt) {
1036cdff9f8eSJerry Zhang 		ret = usb_ep_set_halt(ep->ep);
1037cdff9f8eSJerry Zhang 		if (!ret)
103800a2430fSAndrzej Pietrasiewicz 			ret = -EBADMSG;
1039ae76e134SMichal Nazarewicz 	} else if (unlikely(data_len == -EINVAL)) {
1040c0d31b3cSDavid Cohen 		/*
1041c0d31b3cSDavid Cohen 		 * Sanity Check: even though data_len can't be used
1042c0d31b3cSDavid Cohen 		 * uninitialized at the time I write this comment, some
1043c0d31b3cSDavid Cohen 		 * compilers complain about this situation.
1044c0d31b3cSDavid Cohen 		 * In order to keep the code clean from warnings, data_len is
1045c0d31b3cSDavid Cohen 		 * being initialized to -EINVAL during its declaration, which
1046c0d31b3cSDavid Cohen 		 * means we can't rely on compiler anymore to warn no future
1047c0d31b3cSDavid Cohen 		 * changes won't result in data_len being used uninitialized.
1048c0d31b3cSDavid Cohen 		 * For such reason, we're adding this redundant sanity check
1049c0d31b3cSDavid Cohen 		 * here.
1050c0d31b3cSDavid Cohen 		 */
1051c0d31b3cSDavid Cohen 		WARN(1, "%s: data_len == -EINVAL\n", __func__);
1052c0d31b3cSDavid Cohen 		ret = -EINVAL;
1053ae76e134SMichal Nazarewicz 	} else if (!io_data->aio) {
1054ae76e134SMichal Nazarewicz 		DECLARE_COMPLETION_ONSTACK(done);
1055ef150884SDu, Changbin 		bool interrupted = false;
1056ae76e134SMichal Nazarewicz 
1057ae76e134SMichal Nazarewicz 		req = ep->req;
1058772a7a72SAndrzej Pietrasiewicz 		if (io_data->use_sg) {
1059772a7a72SAndrzej Pietrasiewicz 			req->buf = NULL;
1060772a7a72SAndrzej Pietrasiewicz 			req->sg	= io_data->sgt.sgl;
1061772a7a72SAndrzej Pietrasiewicz 			req->num_sgs = io_data->sgt.nents;
1062772a7a72SAndrzej Pietrasiewicz 		} else {
1063ae76e134SMichal Nazarewicz 			req->buf = data;
1064772a7a72SAndrzej Pietrasiewicz 		}
1065ae76e134SMichal Nazarewicz 		req->length = data_len;
1066ae76e134SMichal Nazarewicz 
1067772a7a72SAndrzej Pietrasiewicz 		io_data->buf = data;
1068772a7a72SAndrzej Pietrasiewicz 
1069ae76e134SMichal Nazarewicz 		req->context  = &done;
1070ae76e134SMichal Nazarewicz 		req->complete = ffs_epfile_io_complete;
1071ae76e134SMichal Nazarewicz 
1072ae76e134SMichal Nazarewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
1073ae76e134SMichal Nazarewicz 		if (unlikely(ret < 0))
1074c0d31b3cSDavid Cohen 			goto error_lock;
1075ae76e134SMichal Nazarewicz 
1076ae76e134SMichal Nazarewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
1077ae76e134SMichal Nazarewicz 
1078ae76e134SMichal Nazarewicz 		if (unlikely(wait_for_completion_interruptible(&done))) {
1079ef150884SDu, Changbin 			/*
1080ef150884SDu, Changbin 			 * To avoid race condition with ffs_epfile_io_complete,
1081ef150884SDu, Changbin 			 * dequeue the request first then check
1082ef150884SDu, Changbin 			 * status. usb_ep_dequeue API should guarantee no race
1083ef150884SDu, Changbin 			 * condition with req->complete callback.
1084ef150884SDu, Changbin 			 */
1085ae76e134SMichal Nazarewicz 			usb_ep_dequeue(ep->ep, req);
108654f64d5cSJohn Stultz 			wait_for_completion(&done);
1087ef150884SDu, Changbin 			interrupted = ep->status < 0;
1088c0d31b3cSDavid Cohen 		}
1089c0d31b3cSDavid Cohen 
1090c662a31bSMichal Nazarewicz 		if (interrupted)
1091c662a31bSMichal Nazarewicz 			ret = -EINTR;
1092c662a31bSMichal Nazarewicz 		else if (io_data->read && ep->status > 0)
10939353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_data(epfile, data, ep->status,
1094c662a31bSMichal Nazarewicz 						     &io_data->data);
1095c662a31bSMichal Nazarewicz 		else
1096c662a31bSMichal Nazarewicz 			ret = ep->status;
1097ae76e134SMichal Nazarewicz 		goto error_mutex;
109830bf90ccSVincent Pelletier 	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
10993163c79eSMichal Nazarewicz 		ret = -ENOMEM;
1100ae76e134SMichal Nazarewicz 	} else {
1101772a7a72SAndrzej Pietrasiewicz 		if (io_data->use_sg) {
1102772a7a72SAndrzej Pietrasiewicz 			req->buf = NULL;
1103772a7a72SAndrzej Pietrasiewicz 			req->sg	= io_data->sgt.sgl;
1104772a7a72SAndrzej Pietrasiewicz 			req->num_sgs = io_data->sgt.nents;
1105772a7a72SAndrzej Pietrasiewicz 		} else {
110600a2430fSAndrzej Pietrasiewicz 			req->buf = data;
1107772a7a72SAndrzej Pietrasiewicz 		}
1108c0d31b3cSDavid Cohen 		req->length = data_len;
110900a2430fSAndrzej Pietrasiewicz 
111000a2430fSAndrzej Pietrasiewicz 		io_data->buf = data;
111100a2430fSAndrzej Pietrasiewicz 		io_data->ep = ep->ep;
111200a2430fSAndrzej Pietrasiewicz 		io_data->req = req;
11135e33f6fdSRobert Baldyga 		io_data->ffs = epfile->ffs;
111400a2430fSAndrzej Pietrasiewicz 
111500a2430fSAndrzej Pietrasiewicz 		req->context  = io_data;
111600a2430fSAndrzej Pietrasiewicz 		req->complete = ffs_epfile_async_io_complete;
111700a2430fSAndrzej Pietrasiewicz 
111800a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
111900a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret)) {
112000a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, req);
112100a2430fSAndrzej Pietrasiewicz 			goto error_lock;
112200a2430fSAndrzej Pietrasiewicz 		}
1123ae76e134SMichal Nazarewicz 
112400a2430fSAndrzej Pietrasiewicz 		ret = -EIOCBQUEUED;
112500a2430fSAndrzej Pietrasiewicz 		/*
1126ae76e134SMichal Nazarewicz 		 * Do not kfree the buffer in this function.  It will be freed
1127ae76e134SMichal Nazarewicz 		 * by ffs_user_copy_worker.
112800a2430fSAndrzej Pietrasiewicz 		 */
1129ae76e134SMichal Nazarewicz 		data = NULL;
113000a2430fSAndrzej Pietrasiewicz 	}
113100a2430fSAndrzej Pietrasiewicz 
113200a2430fSAndrzej Pietrasiewicz error_lock:
113300a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
1134ae76e134SMichal Nazarewicz error_mutex:
113500a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&epfile->mutex);
113600a2430fSAndrzej Pietrasiewicz error:
113773103c7fSFei Yang 	if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */
1138772a7a72SAndrzej Pietrasiewicz 		ffs_free_buffer(io_data);
113900a2430fSAndrzej Pietrasiewicz 	return ret;
114000a2430fSAndrzej Pietrasiewicz }
114100a2430fSAndrzej Pietrasiewicz 
114200a2430fSAndrzej Pietrasiewicz static int
114300a2430fSAndrzej Pietrasiewicz ffs_epfile_open(struct inode *inode, struct file *file)
114400a2430fSAndrzej Pietrasiewicz {
114500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
114600a2430fSAndrzej Pietrasiewicz 
114700a2430fSAndrzej Pietrasiewicz 	ENTER();
114800a2430fSAndrzej Pietrasiewicz 
114900a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
115000a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
115100a2430fSAndrzej Pietrasiewicz 
115200a2430fSAndrzej Pietrasiewicz 	file->private_data = epfile;
115300a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(epfile->ffs);
115400a2430fSAndrzej Pietrasiewicz 
115500a2430fSAndrzej Pietrasiewicz 	return 0;
115600a2430fSAndrzej Pietrasiewicz }
115700a2430fSAndrzej Pietrasiewicz 
115800a2430fSAndrzej Pietrasiewicz static int ffs_aio_cancel(struct kiocb *kiocb)
115900a2430fSAndrzej Pietrasiewicz {
116000a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = kiocb->private;
1161a9c85903SShen Jing 	struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
116200a2430fSAndrzej Pietrasiewicz 	int value;
116300a2430fSAndrzej Pietrasiewicz 
116400a2430fSAndrzej Pietrasiewicz 	ENTER();
116500a2430fSAndrzej Pietrasiewicz 
1166a9c85903SShen Jing 	spin_lock_irq(&epfile->ffs->eps_lock);
1167a9c85903SShen Jing 
1168a9c85903SShen Jing 	if (likely(io_data && io_data->ep && io_data->req))
1169a9c85903SShen Jing 		value = usb_ep_dequeue(io_data->ep, io_data->req);
1170a9c85903SShen Jing 	else
117100a2430fSAndrzej Pietrasiewicz 		value = -EINVAL;
1172a9c85903SShen Jing 
1173a9c85903SShen Jing 	spin_unlock_irq(&epfile->ffs->eps_lock);
117400a2430fSAndrzej Pietrasiewicz 
117500a2430fSAndrzej Pietrasiewicz 	return value;
117600a2430fSAndrzej Pietrasiewicz }
117700a2430fSAndrzej Pietrasiewicz 
117870e60d91SAl Viro static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
117900a2430fSAndrzej Pietrasiewicz {
118070e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1181de2080d4SAl Viro 	ssize_t res;
118200a2430fSAndrzej Pietrasiewicz 
118300a2430fSAndrzej Pietrasiewicz 	ENTER();
118400a2430fSAndrzej Pietrasiewicz 
118570e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
118670e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
118770e60d91SAl Viro 		if (unlikely(!p))
118800a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
118970e60d91SAl Viro 		p->aio = true;
119070e60d91SAl Viro 	} else {
119170e60d91SAl Viro 		p->aio = false;
119270e60d91SAl Viro 	}
119300a2430fSAndrzej Pietrasiewicz 
119470e60d91SAl Viro 	p->read = false;
119570e60d91SAl Viro 	p->kiocb = kiocb;
119670e60d91SAl Viro 	p->data = *from;
119770e60d91SAl Viro 	p->mm = current->mm;
119800a2430fSAndrzej Pietrasiewicz 
119970e60d91SAl Viro 	kiocb->private = p;
120000a2430fSAndrzej Pietrasiewicz 
12014088acf1SRui Miguel Silva 	if (p->aio)
120200a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
120300a2430fSAndrzej Pietrasiewicz 
120470e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
120570e60d91SAl Viro 	if (res == -EIOCBQUEUED)
120670e60d91SAl Viro 		return res;
120770e60d91SAl Viro 	if (p->aio)
120870e60d91SAl Viro 		kfree(p);
120970e60d91SAl Viro 	else
121070e60d91SAl Viro 		*from = p->data;
1211de2080d4SAl Viro 	return res;
121200a2430fSAndrzej Pietrasiewicz }
121300a2430fSAndrzej Pietrasiewicz 
121470e60d91SAl Viro static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
121500a2430fSAndrzej Pietrasiewicz {
121670e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1217de2080d4SAl Viro 	ssize_t res;
121800a2430fSAndrzej Pietrasiewicz 
121900a2430fSAndrzej Pietrasiewicz 	ENTER();
122000a2430fSAndrzej Pietrasiewicz 
122170e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
122270e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
122370e60d91SAl Viro 		if (unlikely(!p))
122400a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
122570e60d91SAl Viro 		p->aio = true;
122670e60d91SAl Viro 	} else {
122770e60d91SAl Viro 		p->aio = false;
122800a2430fSAndrzej Pietrasiewicz 	}
122900a2430fSAndrzej Pietrasiewicz 
123070e60d91SAl Viro 	p->read = true;
123170e60d91SAl Viro 	p->kiocb = kiocb;
123270e60d91SAl Viro 	if (p->aio) {
123370e60d91SAl Viro 		p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
123470e60d91SAl Viro 		if (!p->to_free) {
123570e60d91SAl Viro 			kfree(p);
123670e60d91SAl Viro 			return -ENOMEM;
123770e60d91SAl Viro 		}
123870e60d91SAl Viro 	} else {
123970e60d91SAl Viro 		p->data = *to;
124070e60d91SAl Viro 		p->to_free = NULL;
124170e60d91SAl Viro 	}
124270e60d91SAl Viro 	p->mm = current->mm;
124300a2430fSAndrzej Pietrasiewicz 
124470e60d91SAl Viro 	kiocb->private = p;
124500a2430fSAndrzej Pietrasiewicz 
12464088acf1SRui Miguel Silva 	if (p->aio)
124700a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
124800a2430fSAndrzej Pietrasiewicz 
124970e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
125070e60d91SAl Viro 	if (res == -EIOCBQUEUED)
125170e60d91SAl Viro 		return res;
125270e60d91SAl Viro 
125370e60d91SAl Viro 	if (p->aio) {
125470e60d91SAl Viro 		kfree(p->to_free);
125570e60d91SAl Viro 		kfree(p);
125670e60d91SAl Viro 	} else {
125770e60d91SAl Viro 		*to = p->data;
1258de2080d4SAl Viro 	}
1259de2080d4SAl Viro 	return res;
126000a2430fSAndrzej Pietrasiewicz }
126100a2430fSAndrzej Pietrasiewicz 
126200a2430fSAndrzej Pietrasiewicz static int
126300a2430fSAndrzej Pietrasiewicz ffs_epfile_release(struct inode *inode, struct file *file)
126400a2430fSAndrzej Pietrasiewicz {
126500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
126600a2430fSAndrzej Pietrasiewicz 
126700a2430fSAndrzej Pietrasiewicz 	ENTER();
126800a2430fSAndrzej Pietrasiewicz 
1269a9e6f83cSMichal Nazarewicz 	__ffs_epfile_read_buffer_free(epfile);
127000a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(epfile->ffs);
127100a2430fSAndrzej Pietrasiewicz 
127200a2430fSAndrzej Pietrasiewicz 	return 0;
127300a2430fSAndrzej Pietrasiewicz }
127400a2430fSAndrzej Pietrasiewicz 
127500a2430fSAndrzej Pietrasiewicz static long ffs_epfile_ioctl(struct file *file, unsigned code,
127600a2430fSAndrzej Pietrasiewicz 			     unsigned long value)
127700a2430fSAndrzej Pietrasiewicz {
127800a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
1279222155deSJerry Zhang 	struct ffs_ep *ep;
128000a2430fSAndrzej Pietrasiewicz 	int ret;
128100a2430fSAndrzej Pietrasiewicz 
128200a2430fSAndrzej Pietrasiewicz 	ENTER();
128300a2430fSAndrzej Pietrasiewicz 
128400a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
128500a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
128600a2430fSAndrzej Pietrasiewicz 
1287222155deSJerry Zhang 	/* Wait for endpoint to be enabled */
1288222155deSJerry Zhang 	ep = epfile->ep;
1289222155deSJerry Zhang 	if (!ep) {
1290222155deSJerry Zhang 		if (file->f_flags & O_NONBLOCK)
1291222155deSJerry Zhang 			return -EAGAIN;
1292222155deSJerry Zhang 
1293e16828cfSJerry Zhang 		ret = wait_event_interruptible(
1294e16828cfSJerry Zhang 				epfile->ffs->wait, (ep = epfile->ep));
1295222155deSJerry Zhang 		if (ret)
1296222155deSJerry Zhang 			return -EINTR;
1297222155deSJerry Zhang 	}
1298222155deSJerry Zhang 
129900a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
1300222155deSJerry Zhang 
1301222155deSJerry Zhang 	/* In the meantime, endpoint got disabled or changed. */
1302222155deSJerry Zhang 	if (epfile->ep != ep) {
1303222155deSJerry Zhang 		spin_unlock_irq(&epfile->ffs->eps_lock);
1304222155deSJerry Zhang 		return -ESHUTDOWN;
1305222155deSJerry Zhang 	}
1306222155deSJerry Zhang 
130700a2430fSAndrzej Pietrasiewicz 	switch (code) {
130800a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_FIFO_STATUS:
130900a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_fifo_status(epfile->ep->ep);
131000a2430fSAndrzej Pietrasiewicz 		break;
131100a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_FIFO_FLUSH:
131200a2430fSAndrzej Pietrasiewicz 		usb_ep_fifo_flush(epfile->ep->ep);
131300a2430fSAndrzej Pietrasiewicz 		ret = 0;
131400a2430fSAndrzej Pietrasiewicz 		break;
131500a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_CLEAR_HALT:
131600a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_clear_halt(epfile->ep->ep);
131700a2430fSAndrzej Pietrasiewicz 		break;
131800a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_ENDPOINT_REVMAP:
131900a2430fSAndrzej Pietrasiewicz 		ret = epfile->ep->num;
132000a2430fSAndrzej Pietrasiewicz 		break;
1321c559a353SRobert Baldyga 	case FUNCTIONFS_ENDPOINT_DESC:
1322c559a353SRobert Baldyga 	{
1323c559a353SRobert Baldyga 		int desc_idx;
1324c559a353SRobert Baldyga 		struct usb_endpoint_descriptor *desc;
1325c559a353SRobert Baldyga 
1326c559a353SRobert Baldyga 		switch (epfile->ffs->gadget->speed) {
1327c559a353SRobert Baldyga 		case USB_SPEED_SUPER:
1328c559a353SRobert Baldyga 			desc_idx = 2;
1329c559a353SRobert Baldyga 			break;
1330c559a353SRobert Baldyga 		case USB_SPEED_HIGH:
1331c559a353SRobert Baldyga 			desc_idx = 1;
1332c559a353SRobert Baldyga 			break;
1333c559a353SRobert Baldyga 		default:
1334c559a353SRobert Baldyga 			desc_idx = 0;
1335c559a353SRobert Baldyga 		}
1336c559a353SRobert Baldyga 		desc = epfile->ep->descs[desc_idx];
1337c559a353SRobert Baldyga 
1338c559a353SRobert Baldyga 		spin_unlock_irq(&epfile->ffs->eps_lock);
1339c40619bbSVincent Pelletier 		ret = copy_to_user((void __user *)value, desc, desc->bLength);
1340c559a353SRobert Baldyga 		if (ret)
1341c559a353SRobert Baldyga 			ret = -EFAULT;
1342c559a353SRobert Baldyga 		return ret;
1343c559a353SRobert Baldyga 	}
134400a2430fSAndrzej Pietrasiewicz 	default:
134500a2430fSAndrzej Pietrasiewicz 		ret = -ENOTTY;
134600a2430fSAndrzej Pietrasiewicz 	}
134700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
134800a2430fSAndrzej Pietrasiewicz 
134900a2430fSAndrzej Pietrasiewicz 	return ret;
135000a2430fSAndrzej Pietrasiewicz }
135100a2430fSAndrzej Pietrasiewicz 
13526819e323SJerry Zhang #ifdef CONFIG_COMPAT
13536819e323SJerry Zhang static long ffs_epfile_compat_ioctl(struct file *file, unsigned code,
13546819e323SJerry Zhang 		unsigned long value)
13556819e323SJerry Zhang {
13566819e323SJerry Zhang 	return ffs_epfile_ioctl(file, code, value);
13576819e323SJerry Zhang }
13586819e323SJerry Zhang #endif
13596819e323SJerry Zhang 
136000a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_epfile_operations = {
136100a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
136200a2430fSAndrzej Pietrasiewicz 
136300a2430fSAndrzej Pietrasiewicz 	.open =		ffs_epfile_open,
136470e60d91SAl Viro 	.write_iter =	ffs_epfile_write_iter,
136570e60d91SAl Viro 	.read_iter =	ffs_epfile_read_iter,
136600a2430fSAndrzej Pietrasiewicz 	.release =	ffs_epfile_release,
136700a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_epfile_ioctl,
13686819e323SJerry Zhang #ifdef CONFIG_COMPAT
13696819e323SJerry Zhang 	.compat_ioctl = ffs_epfile_compat_ioctl,
13706819e323SJerry Zhang #endif
137100a2430fSAndrzej Pietrasiewicz };
137200a2430fSAndrzej Pietrasiewicz 
137300a2430fSAndrzej Pietrasiewicz 
137400a2430fSAndrzej Pietrasiewicz /* File system and super block operations ***********************************/
137500a2430fSAndrzej Pietrasiewicz 
137600a2430fSAndrzej Pietrasiewicz /*
137700a2430fSAndrzej Pietrasiewicz  * Mounting the file system creates a controller file, used first for
137800a2430fSAndrzej Pietrasiewicz  * function configuration then later for event monitoring.
137900a2430fSAndrzej Pietrasiewicz  */
138000a2430fSAndrzej Pietrasiewicz 
138100a2430fSAndrzej Pietrasiewicz static struct inode *__must_check
138200a2430fSAndrzej Pietrasiewicz ffs_sb_make_inode(struct super_block *sb, void *data,
138300a2430fSAndrzej Pietrasiewicz 		  const struct file_operations *fops,
138400a2430fSAndrzej Pietrasiewicz 		  const struct inode_operations *iops,
138500a2430fSAndrzej Pietrasiewicz 		  struct ffs_file_perms *perms)
138600a2430fSAndrzej Pietrasiewicz {
138700a2430fSAndrzej Pietrasiewicz 	struct inode *inode;
138800a2430fSAndrzej Pietrasiewicz 
138900a2430fSAndrzej Pietrasiewicz 	ENTER();
139000a2430fSAndrzej Pietrasiewicz 
139100a2430fSAndrzej Pietrasiewicz 	inode = new_inode(sb);
139200a2430fSAndrzej Pietrasiewicz 
139300a2430fSAndrzej Pietrasiewicz 	if (likely(inode)) {
139495582b00SDeepa Dinamani 		struct timespec64 ts = current_time(inode);
139500a2430fSAndrzej Pietrasiewicz 
139600a2430fSAndrzej Pietrasiewicz 		inode->i_ino	 = get_next_ino();
139700a2430fSAndrzej Pietrasiewicz 		inode->i_mode    = perms->mode;
139800a2430fSAndrzej Pietrasiewicz 		inode->i_uid     = perms->uid;
139900a2430fSAndrzej Pietrasiewicz 		inode->i_gid     = perms->gid;
1400078cd827SDeepa Dinamani 		inode->i_atime   = ts;
1401078cd827SDeepa Dinamani 		inode->i_mtime   = ts;
1402078cd827SDeepa Dinamani 		inode->i_ctime   = ts;
140300a2430fSAndrzej Pietrasiewicz 		inode->i_private = data;
140400a2430fSAndrzej Pietrasiewicz 		if (fops)
140500a2430fSAndrzej Pietrasiewicz 			inode->i_fop = fops;
140600a2430fSAndrzej Pietrasiewicz 		if (iops)
140700a2430fSAndrzej Pietrasiewicz 			inode->i_op  = iops;
140800a2430fSAndrzej Pietrasiewicz 	}
140900a2430fSAndrzej Pietrasiewicz 
141000a2430fSAndrzej Pietrasiewicz 	return inode;
141100a2430fSAndrzej Pietrasiewicz }
141200a2430fSAndrzej Pietrasiewicz 
141300a2430fSAndrzej Pietrasiewicz /* Create "regular" file */
14141bb27cacSAl Viro static struct dentry *ffs_sb_create_file(struct super_block *sb,
141500a2430fSAndrzej Pietrasiewicz 					const char *name, void *data,
14161bb27cacSAl Viro 					const struct file_operations *fops)
141700a2430fSAndrzej Pietrasiewicz {
141800a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = sb->s_fs_info;
141900a2430fSAndrzej Pietrasiewicz 	struct dentry	*dentry;
142000a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
142100a2430fSAndrzej Pietrasiewicz 
142200a2430fSAndrzej Pietrasiewicz 	ENTER();
142300a2430fSAndrzej Pietrasiewicz 
142400a2430fSAndrzej Pietrasiewicz 	dentry = d_alloc_name(sb->s_root, name);
142500a2430fSAndrzej Pietrasiewicz 	if (unlikely(!dentry))
142600a2430fSAndrzej Pietrasiewicz 		return NULL;
142700a2430fSAndrzej Pietrasiewicz 
142800a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
142900a2430fSAndrzej Pietrasiewicz 	if (unlikely(!inode)) {
143000a2430fSAndrzej Pietrasiewicz 		dput(dentry);
143100a2430fSAndrzej Pietrasiewicz 		return NULL;
143200a2430fSAndrzej Pietrasiewicz 	}
143300a2430fSAndrzej Pietrasiewicz 
143400a2430fSAndrzej Pietrasiewicz 	d_add(dentry, inode);
14351bb27cacSAl Viro 	return dentry;
143600a2430fSAndrzej Pietrasiewicz }
143700a2430fSAndrzej Pietrasiewicz 
143800a2430fSAndrzej Pietrasiewicz /* Super block */
143900a2430fSAndrzej Pietrasiewicz static const struct super_operations ffs_sb_operations = {
144000a2430fSAndrzej Pietrasiewicz 	.statfs =	simple_statfs,
144100a2430fSAndrzej Pietrasiewicz 	.drop_inode =	generic_delete_inode,
144200a2430fSAndrzej Pietrasiewicz };
144300a2430fSAndrzej Pietrasiewicz 
144400a2430fSAndrzej Pietrasiewicz struct ffs_sb_fill_data {
144500a2430fSAndrzej Pietrasiewicz 	struct ffs_file_perms perms;
144600a2430fSAndrzej Pietrasiewicz 	umode_t root_mode;
144700a2430fSAndrzej Pietrasiewicz 	const char *dev_name;
144818d6b32fSRobert Baldyga 	bool no_disconnect;
144900a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs_data;
145000a2430fSAndrzej Pietrasiewicz };
145100a2430fSAndrzej Pietrasiewicz 
145200a2430fSAndrzej Pietrasiewicz static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
145300a2430fSAndrzej Pietrasiewicz {
145400a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data *data = _data;
145500a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
145600a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = data->ffs_data;
145700a2430fSAndrzej Pietrasiewicz 
145800a2430fSAndrzej Pietrasiewicz 	ENTER();
145900a2430fSAndrzej Pietrasiewicz 
146000a2430fSAndrzej Pietrasiewicz 	ffs->sb              = sb;
146100a2430fSAndrzej Pietrasiewicz 	data->ffs_data       = NULL;
146200a2430fSAndrzej Pietrasiewicz 	sb->s_fs_info        = ffs;
146309cbfeafSKirill A. Shutemov 	sb->s_blocksize      = PAGE_SIZE;
146409cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
146500a2430fSAndrzej Pietrasiewicz 	sb->s_magic          = FUNCTIONFS_MAGIC;
146600a2430fSAndrzej Pietrasiewicz 	sb->s_op             = &ffs_sb_operations;
146700a2430fSAndrzej Pietrasiewicz 	sb->s_time_gran      = 1;
146800a2430fSAndrzej Pietrasiewicz 
146900a2430fSAndrzej Pietrasiewicz 	/* Root inode */
147000a2430fSAndrzej Pietrasiewicz 	data->perms.mode = data->root_mode;
147100a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, NULL,
147200a2430fSAndrzej Pietrasiewicz 				  &simple_dir_operations,
147300a2430fSAndrzej Pietrasiewicz 				  &simple_dir_inode_operations,
147400a2430fSAndrzej Pietrasiewicz 				  &data->perms);
147500a2430fSAndrzej Pietrasiewicz 	sb->s_root = d_make_root(inode);
147600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!sb->s_root))
147700a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
147800a2430fSAndrzej Pietrasiewicz 
147900a2430fSAndrzej Pietrasiewicz 	/* EP0 file */
148000a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
14811bb27cacSAl Viro 					 &ffs_ep0_operations)))
148200a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
148300a2430fSAndrzej Pietrasiewicz 
148400a2430fSAndrzej Pietrasiewicz 	return 0;
148500a2430fSAndrzej Pietrasiewicz }
148600a2430fSAndrzej Pietrasiewicz 
148700a2430fSAndrzej Pietrasiewicz static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
148800a2430fSAndrzej Pietrasiewicz {
148900a2430fSAndrzej Pietrasiewicz 	ENTER();
149000a2430fSAndrzej Pietrasiewicz 
149100a2430fSAndrzej Pietrasiewicz 	if (!opts || !*opts)
149200a2430fSAndrzej Pietrasiewicz 		return 0;
149300a2430fSAndrzej Pietrasiewicz 
149400a2430fSAndrzej Pietrasiewicz 	for (;;) {
149500a2430fSAndrzej Pietrasiewicz 		unsigned long value;
149600a2430fSAndrzej Pietrasiewicz 		char *eq, *comma;
149700a2430fSAndrzej Pietrasiewicz 
149800a2430fSAndrzej Pietrasiewicz 		/* Option limit */
149900a2430fSAndrzej Pietrasiewicz 		comma = strchr(opts, ',');
150000a2430fSAndrzej Pietrasiewicz 		if (comma)
150100a2430fSAndrzej Pietrasiewicz 			*comma = 0;
150200a2430fSAndrzej Pietrasiewicz 
150300a2430fSAndrzej Pietrasiewicz 		/* Value limit */
150400a2430fSAndrzej Pietrasiewicz 		eq = strchr(opts, '=');
150500a2430fSAndrzej Pietrasiewicz 		if (unlikely(!eq)) {
150600a2430fSAndrzej Pietrasiewicz 			pr_err("'=' missing in %s\n", opts);
150700a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
150800a2430fSAndrzej Pietrasiewicz 		}
150900a2430fSAndrzej Pietrasiewicz 		*eq = 0;
151000a2430fSAndrzej Pietrasiewicz 
151100a2430fSAndrzej Pietrasiewicz 		/* Parse value */
151200a2430fSAndrzej Pietrasiewicz 		if (kstrtoul(eq + 1, 0, &value)) {
151300a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid value: %s\n", opts, eq + 1);
151400a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
151500a2430fSAndrzej Pietrasiewicz 		}
151600a2430fSAndrzej Pietrasiewicz 
151700a2430fSAndrzej Pietrasiewicz 		/* Interpret option */
151800a2430fSAndrzej Pietrasiewicz 		switch (eq - opts) {
151918d6b32fSRobert Baldyga 		case 13:
152018d6b32fSRobert Baldyga 			if (!memcmp(opts, "no_disconnect", 13))
152118d6b32fSRobert Baldyga 				data->no_disconnect = !!value;
152218d6b32fSRobert Baldyga 			else
152318d6b32fSRobert Baldyga 				goto invalid;
152418d6b32fSRobert Baldyga 			break;
152500a2430fSAndrzej Pietrasiewicz 		case 5:
152600a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "rmode", 5))
152700a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
152800a2430fSAndrzej Pietrasiewicz 			else if (!memcmp(opts, "fmode", 5))
152900a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
153000a2430fSAndrzej Pietrasiewicz 			else
153100a2430fSAndrzej Pietrasiewicz 				goto invalid;
153200a2430fSAndrzej Pietrasiewicz 			break;
153300a2430fSAndrzej Pietrasiewicz 
153400a2430fSAndrzej Pietrasiewicz 		case 4:
153500a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "mode", 4)) {
153600a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
153700a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
153800a2430fSAndrzej Pietrasiewicz 			} else {
153900a2430fSAndrzej Pietrasiewicz 				goto invalid;
154000a2430fSAndrzej Pietrasiewicz 			}
154100a2430fSAndrzej Pietrasiewicz 			break;
154200a2430fSAndrzej Pietrasiewicz 
154300a2430fSAndrzej Pietrasiewicz 		case 3:
154400a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "uid", 3)) {
154500a2430fSAndrzej Pietrasiewicz 				data->perms.uid = make_kuid(current_user_ns(), value);
154600a2430fSAndrzej Pietrasiewicz 				if (!uid_valid(data->perms.uid)) {
154700a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
154800a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
154900a2430fSAndrzej Pietrasiewicz 				}
155000a2430fSAndrzej Pietrasiewicz 			} else if (!memcmp(opts, "gid", 3)) {
155100a2430fSAndrzej Pietrasiewicz 				data->perms.gid = make_kgid(current_user_ns(), value);
155200a2430fSAndrzej Pietrasiewicz 				if (!gid_valid(data->perms.gid)) {
155300a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
155400a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
155500a2430fSAndrzej Pietrasiewicz 				}
155600a2430fSAndrzej Pietrasiewicz 			} else {
155700a2430fSAndrzej Pietrasiewicz 				goto invalid;
155800a2430fSAndrzej Pietrasiewicz 			}
155900a2430fSAndrzej Pietrasiewicz 			break;
156000a2430fSAndrzej Pietrasiewicz 
156100a2430fSAndrzej Pietrasiewicz 		default:
156200a2430fSAndrzej Pietrasiewicz invalid:
156300a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid option\n", opts);
156400a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
156500a2430fSAndrzej Pietrasiewicz 		}
156600a2430fSAndrzej Pietrasiewicz 
156700a2430fSAndrzej Pietrasiewicz 		/* Next iteration */
156800a2430fSAndrzej Pietrasiewicz 		if (!comma)
156900a2430fSAndrzej Pietrasiewicz 			break;
157000a2430fSAndrzej Pietrasiewicz 		opts = comma + 1;
157100a2430fSAndrzej Pietrasiewicz 	}
157200a2430fSAndrzej Pietrasiewicz 
157300a2430fSAndrzej Pietrasiewicz 	return 0;
157400a2430fSAndrzej Pietrasiewicz }
157500a2430fSAndrzej Pietrasiewicz 
157600a2430fSAndrzej Pietrasiewicz /* "mount -t functionfs dev_name /dev/function" ends up here */
157700a2430fSAndrzej Pietrasiewicz 
157800a2430fSAndrzej Pietrasiewicz static struct dentry *
157900a2430fSAndrzej Pietrasiewicz ffs_fs_mount(struct file_system_type *t, int flags,
158000a2430fSAndrzej Pietrasiewicz 	      const char *dev_name, void *opts)
158100a2430fSAndrzej Pietrasiewicz {
158200a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data data = {
158300a2430fSAndrzej Pietrasiewicz 		.perms = {
158400a2430fSAndrzej Pietrasiewicz 			.mode = S_IFREG | 0600,
158500a2430fSAndrzej Pietrasiewicz 			.uid = GLOBAL_ROOT_UID,
158600a2430fSAndrzej Pietrasiewicz 			.gid = GLOBAL_ROOT_GID,
158700a2430fSAndrzej Pietrasiewicz 		},
158800a2430fSAndrzej Pietrasiewicz 		.root_mode = S_IFDIR | 0500,
158918d6b32fSRobert Baldyga 		.no_disconnect = false,
159000a2430fSAndrzej Pietrasiewicz 	};
159100a2430fSAndrzej Pietrasiewicz 	struct dentry *rv;
159200a2430fSAndrzej Pietrasiewicz 	int ret;
159300a2430fSAndrzej Pietrasiewicz 	void *ffs_dev;
159400a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs;
159500a2430fSAndrzej Pietrasiewicz 
159600a2430fSAndrzej Pietrasiewicz 	ENTER();
159700a2430fSAndrzej Pietrasiewicz 
159800a2430fSAndrzej Pietrasiewicz 	ret = ffs_fs_parse_opts(&data, opts);
159900a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
160000a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
160100a2430fSAndrzej Pietrasiewicz 
1602addfc582SJohn Keeping 	ffs = ffs_data_new(dev_name);
160300a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
160400a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
160500a2430fSAndrzej Pietrasiewicz 	ffs->file_perms = data.perms;
160618d6b32fSRobert Baldyga 	ffs->no_disconnect = data.no_disconnect;
160700a2430fSAndrzej Pietrasiewicz 
160800a2430fSAndrzej Pietrasiewicz 	ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
160900a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->dev_name)) {
161000a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
161100a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
161200a2430fSAndrzej Pietrasiewicz 	}
161300a2430fSAndrzej Pietrasiewicz 
161400a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_acquire_dev(dev_name);
161500a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_dev)) {
161600a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
161700a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(ffs_dev);
161800a2430fSAndrzej Pietrasiewicz 	}
161900a2430fSAndrzej Pietrasiewicz 	ffs->private_data = ffs_dev;
162000a2430fSAndrzej Pietrasiewicz 	data.ffs_data = ffs;
162100a2430fSAndrzej Pietrasiewicz 
162200a2430fSAndrzej Pietrasiewicz 	rv = mount_nodev(t, flags, &data, ffs_sb_fill);
162300a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(rv) && data.ffs_data) {
162400a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(data.ffs_data);
162500a2430fSAndrzej Pietrasiewicz 		ffs_data_put(data.ffs_data);
162600a2430fSAndrzej Pietrasiewicz 	}
162700a2430fSAndrzej Pietrasiewicz 	return rv;
162800a2430fSAndrzej Pietrasiewicz }
162900a2430fSAndrzej Pietrasiewicz 
163000a2430fSAndrzej Pietrasiewicz static void
163100a2430fSAndrzej Pietrasiewicz ffs_fs_kill_sb(struct super_block *sb)
163200a2430fSAndrzej Pietrasiewicz {
163300a2430fSAndrzej Pietrasiewicz 	ENTER();
163400a2430fSAndrzej Pietrasiewicz 
163500a2430fSAndrzej Pietrasiewicz 	kill_litter_super(sb);
163600a2430fSAndrzej Pietrasiewicz 	if (sb->s_fs_info) {
163700a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(sb->s_fs_info);
163818d6b32fSRobert Baldyga 		ffs_data_closed(sb->s_fs_info);
163900a2430fSAndrzej Pietrasiewicz 	}
164000a2430fSAndrzej Pietrasiewicz }
164100a2430fSAndrzej Pietrasiewicz 
164200a2430fSAndrzej Pietrasiewicz static struct file_system_type ffs_fs_type = {
164300a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
164400a2430fSAndrzej Pietrasiewicz 	.name		= "functionfs",
164500a2430fSAndrzej Pietrasiewicz 	.mount		= ffs_fs_mount,
164600a2430fSAndrzej Pietrasiewicz 	.kill_sb	= ffs_fs_kill_sb,
164700a2430fSAndrzej Pietrasiewicz };
164800a2430fSAndrzej Pietrasiewicz MODULE_ALIAS_FS("functionfs");
164900a2430fSAndrzej Pietrasiewicz 
165000a2430fSAndrzej Pietrasiewicz 
165100a2430fSAndrzej Pietrasiewicz /* Driver's main init/cleanup functions *************************************/
165200a2430fSAndrzej Pietrasiewicz 
165300a2430fSAndrzej Pietrasiewicz static int functionfs_init(void)
165400a2430fSAndrzej Pietrasiewicz {
165500a2430fSAndrzej Pietrasiewicz 	int ret;
165600a2430fSAndrzej Pietrasiewicz 
165700a2430fSAndrzej Pietrasiewicz 	ENTER();
165800a2430fSAndrzej Pietrasiewicz 
165900a2430fSAndrzej Pietrasiewicz 	ret = register_filesystem(&ffs_fs_type);
166000a2430fSAndrzej Pietrasiewicz 	if (likely(!ret))
166100a2430fSAndrzej Pietrasiewicz 		pr_info("file system registered\n");
166200a2430fSAndrzej Pietrasiewicz 	else
166300a2430fSAndrzej Pietrasiewicz 		pr_err("failed registering file system (%d)\n", ret);
166400a2430fSAndrzej Pietrasiewicz 
166500a2430fSAndrzej Pietrasiewicz 	return ret;
166600a2430fSAndrzej Pietrasiewicz }
166700a2430fSAndrzej Pietrasiewicz 
166800a2430fSAndrzej Pietrasiewicz static void functionfs_cleanup(void)
166900a2430fSAndrzej Pietrasiewicz {
167000a2430fSAndrzej Pietrasiewicz 	ENTER();
167100a2430fSAndrzej Pietrasiewicz 
167200a2430fSAndrzej Pietrasiewicz 	pr_info("unloading\n");
167300a2430fSAndrzej Pietrasiewicz 	unregister_filesystem(&ffs_fs_type);
167400a2430fSAndrzej Pietrasiewicz }
167500a2430fSAndrzej Pietrasiewicz 
167600a2430fSAndrzej Pietrasiewicz 
167700a2430fSAndrzej Pietrasiewicz /* ffs_data and ffs_function construction and destruction code **************/
167800a2430fSAndrzej Pietrasiewicz 
167900a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs);
168000a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs);
168100a2430fSAndrzej Pietrasiewicz 
168200a2430fSAndrzej Pietrasiewicz static void ffs_data_get(struct ffs_data *ffs)
168300a2430fSAndrzej Pietrasiewicz {
168400a2430fSAndrzej Pietrasiewicz 	ENTER();
168500a2430fSAndrzej Pietrasiewicz 
168643938613SElena Reshetova 	refcount_inc(&ffs->ref);
168700a2430fSAndrzej Pietrasiewicz }
168800a2430fSAndrzej Pietrasiewicz 
168900a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs)
169000a2430fSAndrzej Pietrasiewicz {
169100a2430fSAndrzej Pietrasiewicz 	ENTER();
169200a2430fSAndrzej Pietrasiewicz 
169343938613SElena Reshetova 	refcount_inc(&ffs->ref);
169418d6b32fSRobert Baldyga 	if (atomic_add_return(1, &ffs->opened) == 1 &&
169518d6b32fSRobert Baldyga 			ffs->state == FFS_DEACTIVATED) {
169618d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
169718d6b32fSRobert Baldyga 		ffs_data_reset(ffs);
169818d6b32fSRobert Baldyga 	}
169900a2430fSAndrzej Pietrasiewicz }
170000a2430fSAndrzej Pietrasiewicz 
170100a2430fSAndrzej Pietrasiewicz static void ffs_data_put(struct ffs_data *ffs)
170200a2430fSAndrzej Pietrasiewicz {
170300a2430fSAndrzej Pietrasiewicz 	ENTER();
170400a2430fSAndrzej Pietrasiewicz 
170543938613SElena Reshetova 	if (unlikely(refcount_dec_and_test(&ffs->ref))) {
170600a2430fSAndrzej Pietrasiewicz 		pr_info("%s(): freeing\n", __func__);
170700a2430fSAndrzej Pietrasiewicz 		ffs_data_clear(ffs);
170800a2430fSAndrzej Pietrasiewicz 		BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1709e16828cfSJerry Zhang 		       waitqueue_active(&ffs->ep0req_completion.wait) ||
1710e16828cfSJerry Zhang 		       waitqueue_active(&ffs->wait));
1711addfc582SJohn Keeping 		destroy_workqueue(ffs->io_completion_wq);
171200a2430fSAndrzej Pietrasiewicz 		kfree(ffs->dev_name);
171300a2430fSAndrzej Pietrasiewicz 		kfree(ffs);
171400a2430fSAndrzej Pietrasiewicz 	}
171500a2430fSAndrzej Pietrasiewicz }
171600a2430fSAndrzej Pietrasiewicz 
171700a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs)
171800a2430fSAndrzej Pietrasiewicz {
171900a2430fSAndrzej Pietrasiewicz 	ENTER();
172000a2430fSAndrzej Pietrasiewicz 
172100a2430fSAndrzej Pietrasiewicz 	if (atomic_dec_and_test(&ffs->opened)) {
172218d6b32fSRobert Baldyga 		if (ffs->no_disconnect) {
172318d6b32fSRobert Baldyga 			ffs->state = FFS_DEACTIVATED;
172418d6b32fSRobert Baldyga 			if (ffs->epfiles) {
172518d6b32fSRobert Baldyga 				ffs_epfiles_destroy(ffs->epfiles,
172618d6b32fSRobert Baldyga 						   ffs->eps_count);
172718d6b32fSRobert Baldyga 				ffs->epfiles = NULL;
172818d6b32fSRobert Baldyga 			}
172918d6b32fSRobert Baldyga 			if (ffs->setup_state == FFS_SETUP_PENDING)
173018d6b32fSRobert Baldyga 				__ffs_ep0_stall(ffs);
173118d6b32fSRobert Baldyga 		} else {
173218d6b32fSRobert Baldyga 			ffs->state = FFS_CLOSING;
173318d6b32fSRobert Baldyga 			ffs_data_reset(ffs);
173418d6b32fSRobert Baldyga 		}
173518d6b32fSRobert Baldyga 	}
173618d6b32fSRobert Baldyga 	if (atomic_read(&ffs->opened) < 0) {
173700a2430fSAndrzej Pietrasiewicz 		ffs->state = FFS_CLOSING;
173800a2430fSAndrzej Pietrasiewicz 		ffs_data_reset(ffs);
173900a2430fSAndrzej Pietrasiewicz 	}
174000a2430fSAndrzej Pietrasiewicz 
174100a2430fSAndrzej Pietrasiewicz 	ffs_data_put(ffs);
174200a2430fSAndrzej Pietrasiewicz }
174300a2430fSAndrzej Pietrasiewicz 
1744addfc582SJohn Keeping static struct ffs_data *ffs_data_new(const char *dev_name)
174500a2430fSAndrzej Pietrasiewicz {
174600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
174700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
174800a2430fSAndrzej Pietrasiewicz 		return NULL;
174900a2430fSAndrzej Pietrasiewicz 
175000a2430fSAndrzej Pietrasiewicz 	ENTER();
175100a2430fSAndrzej Pietrasiewicz 
1752addfc582SJohn Keeping 	ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name);
1753addfc582SJohn Keeping 	if (!ffs->io_completion_wq) {
1754addfc582SJohn Keeping 		kfree(ffs);
1755addfc582SJohn Keeping 		return NULL;
1756addfc582SJohn Keeping 	}
1757addfc582SJohn Keeping 
175843938613SElena Reshetova 	refcount_set(&ffs->ref, 1);
175900a2430fSAndrzej Pietrasiewicz 	atomic_set(&ffs->opened, 0);
176000a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
176100a2430fSAndrzej Pietrasiewicz 	mutex_init(&ffs->mutex);
176200a2430fSAndrzej Pietrasiewicz 	spin_lock_init(&ffs->eps_lock);
176300a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&ffs->ev.waitq);
1764e16828cfSJerry Zhang 	init_waitqueue_head(&ffs->wait);
176500a2430fSAndrzej Pietrasiewicz 	init_completion(&ffs->ep0req_completion);
176600a2430fSAndrzej Pietrasiewicz 
176700a2430fSAndrzej Pietrasiewicz 	/* XXX REVISIT need to update it in some places, or do we? */
176800a2430fSAndrzej Pietrasiewicz 	ffs->ev.can_stall = 1;
176900a2430fSAndrzej Pietrasiewicz 
177000a2430fSAndrzej Pietrasiewicz 	return ffs;
177100a2430fSAndrzej Pietrasiewicz }
177200a2430fSAndrzej Pietrasiewicz 
177300a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs)
177400a2430fSAndrzej Pietrasiewicz {
177500a2430fSAndrzej Pietrasiewicz 	ENTER();
177600a2430fSAndrzej Pietrasiewicz 
177700a2430fSAndrzej Pietrasiewicz 	ffs_closed(ffs);
177800a2430fSAndrzej Pietrasiewicz 
177900a2430fSAndrzej Pietrasiewicz 	BUG_ON(ffs->gadget);
178000a2430fSAndrzej Pietrasiewicz 
178100a2430fSAndrzej Pietrasiewicz 	if (ffs->epfiles)
178200a2430fSAndrzej Pietrasiewicz 		ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
178300a2430fSAndrzej Pietrasiewicz 
17845e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
17855e33f6fdSRobert Baldyga 		eventfd_ctx_put(ffs->ffs_eventfd);
17865e33f6fdSRobert Baldyga 
178700a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_descs_data);
178800a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_strings);
178900a2430fSAndrzej Pietrasiewicz 	kfree(ffs->stringtabs);
179000a2430fSAndrzej Pietrasiewicz }
179100a2430fSAndrzej Pietrasiewicz 
179200a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs)
179300a2430fSAndrzej Pietrasiewicz {
179400a2430fSAndrzej Pietrasiewicz 	ENTER();
179500a2430fSAndrzej Pietrasiewicz 
179600a2430fSAndrzej Pietrasiewicz 	ffs_data_clear(ffs);
179700a2430fSAndrzej Pietrasiewicz 
179800a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = NULL;
179900a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data = NULL;
180000a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs = NULL;
180100a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = NULL;
180200a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = NULL;
180300a2430fSAndrzej Pietrasiewicz 
180400a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length = 0;
180500a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count = 0;
180600a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count = 0;
180700a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count = 0;
180800a2430fSAndrzej Pietrasiewicz 
180900a2430fSAndrzej Pietrasiewicz 	ffs->strings_count = 0;
181000a2430fSAndrzej Pietrasiewicz 	ffs->interfaces_count = 0;
181100a2430fSAndrzej Pietrasiewicz 	ffs->eps_count = 0;
181200a2430fSAndrzej Pietrasiewicz 
181300a2430fSAndrzej Pietrasiewicz 	ffs->ev.count = 0;
181400a2430fSAndrzej Pietrasiewicz 
181500a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
181600a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
181700a2430fSAndrzej Pietrasiewicz 	ffs->flags = 0;
181800a2430fSAndrzej Pietrasiewicz }
181900a2430fSAndrzej Pietrasiewicz 
182000a2430fSAndrzej Pietrasiewicz 
182100a2430fSAndrzej Pietrasiewicz static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
182200a2430fSAndrzej Pietrasiewicz {
182300a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **lang;
182400a2430fSAndrzej Pietrasiewicz 	int first_id;
182500a2430fSAndrzej Pietrasiewicz 
182600a2430fSAndrzej Pietrasiewicz 	ENTER();
182700a2430fSAndrzej Pietrasiewicz 
182800a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs->state != FFS_ACTIVE
182900a2430fSAndrzej Pietrasiewicz 		 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
183000a2430fSAndrzej Pietrasiewicz 		return -EBADFD;
183100a2430fSAndrzej Pietrasiewicz 
183200a2430fSAndrzej Pietrasiewicz 	first_id = usb_string_ids_n(cdev, ffs->strings_count);
183300a2430fSAndrzej Pietrasiewicz 	if (unlikely(first_id < 0))
183400a2430fSAndrzej Pietrasiewicz 		return first_id;
183500a2430fSAndrzej Pietrasiewicz 
183600a2430fSAndrzej Pietrasiewicz 	ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
183700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->ep0req))
183800a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
183900a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->complete = ffs_ep0_complete;
184000a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->context = ffs;
184100a2430fSAndrzej Pietrasiewicz 
184200a2430fSAndrzej Pietrasiewicz 	lang = ffs->stringtabs;
184361fe2d75SGreg Kroah-Hartman 	if (lang) {
184461fe2d75SGreg Kroah-Hartman 		for (; *lang; ++lang) {
184500a2430fSAndrzej Pietrasiewicz 			struct usb_string *str = (*lang)->strings;
184600a2430fSAndrzej Pietrasiewicz 			int id = first_id;
184700a2430fSAndrzej Pietrasiewicz 			for (; str->s; ++id, ++str)
184800a2430fSAndrzej Pietrasiewicz 				str->id = id;
184900a2430fSAndrzej Pietrasiewicz 		}
185061fe2d75SGreg Kroah-Hartman 	}
185100a2430fSAndrzej Pietrasiewicz 
185200a2430fSAndrzej Pietrasiewicz 	ffs->gadget = cdev->gadget;
185300a2430fSAndrzej Pietrasiewicz 	ffs_data_get(ffs);
185400a2430fSAndrzej Pietrasiewicz 	return 0;
185500a2430fSAndrzej Pietrasiewicz }
185600a2430fSAndrzej Pietrasiewicz 
185700a2430fSAndrzej Pietrasiewicz static void functionfs_unbind(struct ffs_data *ffs)
185800a2430fSAndrzej Pietrasiewicz {
185900a2430fSAndrzej Pietrasiewicz 	ENTER();
186000a2430fSAndrzej Pietrasiewicz 
186100a2430fSAndrzej Pietrasiewicz 	if (!WARN_ON(!ffs->gadget)) {
186200a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
186300a2430fSAndrzej Pietrasiewicz 		ffs->ep0req = NULL;
186400a2430fSAndrzej Pietrasiewicz 		ffs->gadget = NULL;
186500a2430fSAndrzej Pietrasiewicz 		clear_bit(FFS_FL_BOUND, &ffs->flags);
186600a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
186700a2430fSAndrzej Pietrasiewicz 	}
186800a2430fSAndrzej Pietrasiewicz }
186900a2430fSAndrzej Pietrasiewicz 
187000a2430fSAndrzej Pietrasiewicz static int ffs_epfiles_create(struct ffs_data *ffs)
187100a2430fSAndrzej Pietrasiewicz {
187200a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile, *epfiles;
187300a2430fSAndrzej Pietrasiewicz 	unsigned i, count;
187400a2430fSAndrzej Pietrasiewicz 
187500a2430fSAndrzej Pietrasiewicz 	ENTER();
187600a2430fSAndrzej Pietrasiewicz 
187700a2430fSAndrzej Pietrasiewicz 	count = ffs->eps_count;
187800a2430fSAndrzej Pietrasiewicz 	epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
187900a2430fSAndrzej Pietrasiewicz 	if (!epfiles)
188000a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
188100a2430fSAndrzej Pietrasiewicz 
188200a2430fSAndrzej Pietrasiewicz 	epfile = epfiles;
188300a2430fSAndrzej Pietrasiewicz 	for (i = 1; i <= count; ++i, ++epfile) {
188400a2430fSAndrzej Pietrasiewicz 		epfile->ffs = ffs;
188500a2430fSAndrzej Pietrasiewicz 		mutex_init(&epfile->mutex);
18861b0bf88fSRobert Baldyga 		if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1887acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
18881b0bf88fSRobert Baldyga 		else
1889acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%u", i);
1890acba23feSMario Schuknecht 		epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
18911bb27cacSAl Viro 						 epfile,
18921bb27cacSAl Viro 						 &ffs_epfile_operations);
18931bb27cacSAl Viro 		if (unlikely(!epfile->dentry)) {
189400a2430fSAndrzej Pietrasiewicz 			ffs_epfiles_destroy(epfiles, i - 1);
189500a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
189600a2430fSAndrzej Pietrasiewicz 		}
189700a2430fSAndrzej Pietrasiewicz 	}
189800a2430fSAndrzej Pietrasiewicz 
189900a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = epfiles;
190000a2430fSAndrzej Pietrasiewicz 	return 0;
190100a2430fSAndrzej Pietrasiewicz }
190200a2430fSAndrzej Pietrasiewicz 
190300a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
190400a2430fSAndrzej Pietrasiewicz {
190500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = epfiles;
190600a2430fSAndrzej Pietrasiewicz 
190700a2430fSAndrzej Pietrasiewicz 	ENTER();
190800a2430fSAndrzej Pietrasiewicz 
190900a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++epfile) {
1910e16828cfSJerry Zhang 		BUG_ON(mutex_is_locked(&epfile->mutex));
191100a2430fSAndrzej Pietrasiewicz 		if (epfile->dentry) {
191200a2430fSAndrzej Pietrasiewicz 			d_delete(epfile->dentry);
191300a2430fSAndrzej Pietrasiewicz 			dput(epfile->dentry);
191400a2430fSAndrzej Pietrasiewicz 			epfile->dentry = NULL;
191500a2430fSAndrzej Pietrasiewicz 		}
191600a2430fSAndrzej Pietrasiewicz 	}
191700a2430fSAndrzej Pietrasiewicz 
191800a2430fSAndrzej Pietrasiewicz 	kfree(epfiles);
191900a2430fSAndrzej Pietrasiewicz }
192000a2430fSAndrzej Pietrasiewicz 
192100a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func)
192200a2430fSAndrzej Pietrasiewicz {
192300a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
192400a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = func->ffs->epfiles;
192500a2430fSAndrzej Pietrasiewicz 	unsigned count            = func->ffs->eps_count;
192600a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
192700a2430fSAndrzej Pietrasiewicz 
19289353afbbSMichal Nazarewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
192908f37148SVincent Pelletier 	while (count--) {
193000a2430fSAndrzej Pietrasiewicz 		/* pending requests get nuked */
193100a2430fSAndrzej Pietrasiewicz 		if (likely(ep->ep))
193200a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(ep->ep);
193300a2430fSAndrzej Pietrasiewicz 		++ep;
193418d6b32fSRobert Baldyga 
193518d6b32fSRobert Baldyga 		if (epfile) {
1936a9e6f83cSMichal Nazarewicz 			epfile->ep = NULL;
1937a9e6f83cSMichal Nazarewicz 			__ffs_epfile_read_buffer_free(epfile);
193800a2430fSAndrzej Pietrasiewicz 			++epfile;
193918d6b32fSRobert Baldyga 		}
194008f37148SVincent Pelletier 	}
1941a9e6f83cSMichal Nazarewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
194200a2430fSAndrzej Pietrasiewicz }
194300a2430fSAndrzej Pietrasiewicz 
194400a2430fSAndrzej Pietrasiewicz static int ffs_func_eps_enable(struct ffs_function *func)
194500a2430fSAndrzej Pietrasiewicz {
194600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs      = func->ffs;
194700a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
194800a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = ffs->epfiles;
194900a2430fSAndrzej Pietrasiewicz 	unsigned count            = ffs->eps_count;
195000a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
195100a2430fSAndrzej Pietrasiewicz 	int ret = 0;
195200a2430fSAndrzej Pietrasiewicz 
195300a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
195408f37148SVincent Pelletier 	while(count--) {
195500a2430fSAndrzej Pietrasiewicz 		ep->ep->driver_data = ep;
19562bfa0719SFelipe Balbi 
1957675272d0SJack Pham 		ret = config_ep_by_speed(func->gadget, &func->function, ep->ep);
1958675272d0SJack Pham 		if (ret) {
1959675272d0SJack Pham 			pr_err("%s: config_ep_by_speed(%s) returned %d\n",
1960675272d0SJack Pham 					__func__, ep->ep->name, ret);
1961675272d0SJack Pham 			break;
1962b7f73850SWilliam Wu 		}
19632bfa0719SFelipe Balbi 
196400a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_enable(ep->ep);
196500a2430fSAndrzej Pietrasiewicz 		if (likely(!ret)) {
196600a2430fSAndrzej Pietrasiewicz 			epfile->ep = ep;
1967675272d0SJack Pham 			epfile->in = usb_endpoint_dir_in(ep->ep->desc);
1968675272d0SJack Pham 			epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc);
196900a2430fSAndrzej Pietrasiewicz 		} else {
197000a2430fSAndrzej Pietrasiewicz 			break;
197100a2430fSAndrzej Pietrasiewicz 		}
197200a2430fSAndrzej Pietrasiewicz 
197300a2430fSAndrzej Pietrasiewicz 		++ep;
197400a2430fSAndrzej Pietrasiewicz 		++epfile;
197508f37148SVincent Pelletier 	}
1976e16828cfSJerry Zhang 
1977e16828cfSJerry Zhang 	wake_up_interruptible(&ffs->wait);
197800a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
197900a2430fSAndrzej Pietrasiewicz 
198000a2430fSAndrzej Pietrasiewicz 	return ret;
198100a2430fSAndrzej Pietrasiewicz }
198200a2430fSAndrzej Pietrasiewicz 
198300a2430fSAndrzej Pietrasiewicz 
198400a2430fSAndrzej Pietrasiewicz /* Parsing and building descriptors and strings *****************************/
198500a2430fSAndrzej Pietrasiewicz 
198600a2430fSAndrzej Pietrasiewicz /*
198700a2430fSAndrzej Pietrasiewicz  * This validates if data pointed by data is a valid USB descriptor as
198800a2430fSAndrzej Pietrasiewicz  * well as record how many interfaces, endpoints and strings are
198900a2430fSAndrzej Pietrasiewicz  * required by given configuration.  Returns address after the
199000a2430fSAndrzej Pietrasiewicz  * descriptor or NULL if data is invalid.
199100a2430fSAndrzej Pietrasiewicz  */
199200a2430fSAndrzej Pietrasiewicz 
199300a2430fSAndrzej Pietrasiewicz enum ffs_entity_type {
199400a2430fSAndrzej Pietrasiewicz 	FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
199500a2430fSAndrzej Pietrasiewicz };
199600a2430fSAndrzej Pietrasiewicz 
199700a2430fSAndrzej Pietrasiewicz enum ffs_os_desc_type {
199800a2430fSAndrzej Pietrasiewicz 	FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
199900a2430fSAndrzej Pietrasiewicz };
200000a2430fSAndrzej Pietrasiewicz 
200100a2430fSAndrzej Pietrasiewicz typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
200200a2430fSAndrzej Pietrasiewicz 				   u8 *valuep,
200300a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
200400a2430fSAndrzej Pietrasiewicz 				   void *priv);
200500a2430fSAndrzej Pietrasiewicz 
200600a2430fSAndrzej Pietrasiewicz typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
200700a2430fSAndrzej Pietrasiewicz 				    struct usb_os_desc_header *h, void *data,
200800a2430fSAndrzej Pietrasiewicz 				    unsigned len, void *priv);
200900a2430fSAndrzej Pietrasiewicz 
201000a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_desc(char *data, unsigned len,
201100a2430fSAndrzej Pietrasiewicz 					   ffs_entity_callback entity,
20127f7c548cSVincent Pelletier 					   void *priv, int *current_class)
201300a2430fSAndrzej Pietrasiewicz {
201400a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header *_ds = (void *)data;
201500a2430fSAndrzej Pietrasiewicz 	u8 length;
201600a2430fSAndrzej Pietrasiewicz 	int ret;
201700a2430fSAndrzej Pietrasiewicz 
201800a2430fSAndrzej Pietrasiewicz 	ENTER();
201900a2430fSAndrzej Pietrasiewicz 
202000a2430fSAndrzej Pietrasiewicz 	/* At least two bytes are required: length and type */
202100a2430fSAndrzej Pietrasiewicz 	if (len < 2) {
202200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor too short\n");
202300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
202400a2430fSAndrzej Pietrasiewicz 	}
202500a2430fSAndrzej Pietrasiewicz 
202600a2430fSAndrzej Pietrasiewicz 	/* If we have at least as many bytes as the descriptor takes? */
202700a2430fSAndrzej Pietrasiewicz 	length = _ds->bLength;
202800a2430fSAndrzej Pietrasiewicz 	if (len < length) {
202900a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor longer then available data\n");
203000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
203100a2430fSAndrzej Pietrasiewicz 	}
203200a2430fSAndrzej Pietrasiewicz 
203300a2430fSAndrzej Pietrasiewicz #define __entity_check_INTERFACE(val)  1
203400a2430fSAndrzej Pietrasiewicz #define __entity_check_STRING(val)     (val)
203500a2430fSAndrzej Pietrasiewicz #define __entity_check_ENDPOINT(val)   ((val) & USB_ENDPOINT_NUMBER_MASK)
203600a2430fSAndrzej Pietrasiewicz #define __entity(type, val) do {					\
203700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("entity " #type "(%02x)\n", (val));		\
203800a2430fSAndrzej Pietrasiewicz 		if (unlikely(!__entity_check_ ##type(val))) {		\
203900a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid entity's value\n");		\
204000a2430fSAndrzej Pietrasiewicz 			return -EINVAL;					\
204100a2430fSAndrzej Pietrasiewicz 		}							\
204200a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_ ##type, &val, _ds, priv);		\
204300a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {				\
204400a2430fSAndrzej Pietrasiewicz 			pr_debug("entity " #type "(%02x); ret = %d\n",	\
204500a2430fSAndrzej Pietrasiewicz 				 (val), ret);				\
204600a2430fSAndrzej Pietrasiewicz 			return ret;					\
204700a2430fSAndrzej Pietrasiewicz 		}							\
204800a2430fSAndrzej Pietrasiewicz 	} while (0)
204900a2430fSAndrzej Pietrasiewicz 
205000a2430fSAndrzej Pietrasiewicz 	/* Parse descriptor depending on type. */
205100a2430fSAndrzej Pietrasiewicz 	switch (_ds->bDescriptorType) {
205200a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE:
205300a2430fSAndrzej Pietrasiewicz 	case USB_DT_CONFIG:
205400a2430fSAndrzej Pietrasiewicz 	case USB_DT_STRING:
205500a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE_QUALIFIER:
205600a2430fSAndrzej Pietrasiewicz 		/* function can't have any of those */
205700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor reserved for gadget: %d\n",
205800a2430fSAndrzej Pietrasiewicz 		      _ds->bDescriptorType);
205900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
206000a2430fSAndrzej Pietrasiewicz 
206100a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE: {
206200a2430fSAndrzej Pietrasiewicz 		struct usb_interface_descriptor *ds = (void *)_ds;
206300a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface descriptor\n");
206400a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
206500a2430fSAndrzej Pietrasiewicz 			goto inv_length;
206600a2430fSAndrzej Pietrasiewicz 
206700a2430fSAndrzej Pietrasiewicz 		__entity(INTERFACE, ds->bInterfaceNumber);
206800a2430fSAndrzej Pietrasiewicz 		if (ds->iInterface)
206900a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iInterface);
20707f7c548cSVincent Pelletier 		*current_class = ds->bInterfaceClass;
207100a2430fSAndrzej Pietrasiewicz 	}
207200a2430fSAndrzej Pietrasiewicz 		break;
207300a2430fSAndrzej Pietrasiewicz 
207400a2430fSAndrzej Pietrasiewicz 	case USB_DT_ENDPOINT: {
207500a2430fSAndrzej Pietrasiewicz 		struct usb_endpoint_descriptor *ds = (void *)_ds;
207600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("endpoint descriptor\n");
207700a2430fSAndrzej Pietrasiewicz 		if (length != USB_DT_ENDPOINT_SIZE &&
207800a2430fSAndrzej Pietrasiewicz 		    length != USB_DT_ENDPOINT_AUDIO_SIZE)
207900a2430fSAndrzej Pietrasiewicz 			goto inv_length;
208000a2430fSAndrzej Pietrasiewicz 		__entity(ENDPOINT, ds->bEndpointAddress);
208100a2430fSAndrzej Pietrasiewicz 	}
208200a2430fSAndrzej Pietrasiewicz 		break;
208300a2430fSAndrzej Pietrasiewicz 
20847f7c548cSVincent Pelletier 	case USB_TYPE_CLASS | 0x01:
20857f7c548cSVincent Pelletier                 if (*current_class == USB_INTERFACE_CLASS_HID) {
208600a2430fSAndrzej Pietrasiewicz 			pr_vdebug("hid descriptor\n");
208700a2430fSAndrzej Pietrasiewicz 			if (length != sizeof(struct hid_descriptor))
208800a2430fSAndrzej Pietrasiewicz 				goto inv_length;
208900a2430fSAndrzej Pietrasiewicz 			break;
20907f7c548cSVincent Pelletier 		} else if (*current_class == USB_INTERFACE_CLASS_CCID) {
20917f7c548cSVincent Pelletier 			pr_vdebug("ccid descriptor\n");
20927f7c548cSVincent Pelletier 			if (length != sizeof(struct ccid_descriptor))
20937f7c548cSVincent Pelletier 				goto inv_length;
20947f7c548cSVincent Pelletier 			break;
20957f7c548cSVincent Pelletier 		} else {
20967f7c548cSVincent Pelletier 			pr_vdebug("unknown descriptor: %d for class %d\n",
20977f7c548cSVincent Pelletier 			      _ds->bDescriptorType, *current_class);
20987f7c548cSVincent Pelletier 			return -EINVAL;
20997f7c548cSVincent Pelletier 		}
210000a2430fSAndrzej Pietrasiewicz 
210100a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTG:
210200a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_otg_descriptor))
210300a2430fSAndrzej Pietrasiewicz 			goto inv_length;
210400a2430fSAndrzej Pietrasiewicz 		break;
210500a2430fSAndrzej Pietrasiewicz 
210600a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_ASSOCIATION: {
210700a2430fSAndrzej Pietrasiewicz 		struct usb_interface_assoc_descriptor *ds = (void *)_ds;
210800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface association descriptor\n");
210900a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
211000a2430fSAndrzej Pietrasiewicz 			goto inv_length;
211100a2430fSAndrzej Pietrasiewicz 		if (ds->iFunction)
211200a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iFunction);
211300a2430fSAndrzej Pietrasiewicz 	}
211400a2430fSAndrzej Pietrasiewicz 		break;
211500a2430fSAndrzej Pietrasiewicz 
211600a2430fSAndrzej Pietrasiewicz 	case USB_DT_SS_ENDPOINT_COMP:
211700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("EP SS companion descriptor\n");
211800a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_ss_ep_comp_descriptor))
211900a2430fSAndrzej Pietrasiewicz 			goto inv_length;
212000a2430fSAndrzej Pietrasiewicz 		break;
212100a2430fSAndrzej Pietrasiewicz 
212200a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTHER_SPEED_CONFIG:
212300a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_POWER:
212400a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEBUG:
212500a2430fSAndrzej Pietrasiewicz 	case USB_DT_SECURITY:
212600a2430fSAndrzej Pietrasiewicz 	case USB_DT_CS_RADIO_CONTROL:
212700a2430fSAndrzej Pietrasiewicz 		/* TODO */
212800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
212900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
213000a2430fSAndrzej Pietrasiewicz 
213100a2430fSAndrzej Pietrasiewicz 	default:
213200a2430fSAndrzej Pietrasiewicz 		/* We should never be here */
213300a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
213400a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
213500a2430fSAndrzej Pietrasiewicz 
213600a2430fSAndrzej Pietrasiewicz inv_length:
213700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("invalid length: %d (descriptor %d)\n",
213800a2430fSAndrzej Pietrasiewicz 			  _ds->bLength, _ds->bDescriptorType);
213900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
214000a2430fSAndrzej Pietrasiewicz 	}
214100a2430fSAndrzej Pietrasiewicz 
214200a2430fSAndrzej Pietrasiewicz #undef __entity
214300a2430fSAndrzej Pietrasiewicz #undef __entity_check_DESCRIPTOR
214400a2430fSAndrzej Pietrasiewicz #undef __entity_check_INTERFACE
214500a2430fSAndrzej Pietrasiewicz #undef __entity_check_STRING
214600a2430fSAndrzej Pietrasiewicz #undef __entity_check_ENDPOINT
214700a2430fSAndrzej Pietrasiewicz 
214800a2430fSAndrzej Pietrasiewicz 	return length;
214900a2430fSAndrzej Pietrasiewicz }
215000a2430fSAndrzej Pietrasiewicz 
215100a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
215200a2430fSAndrzej Pietrasiewicz 				     ffs_entity_callback entity, void *priv)
215300a2430fSAndrzej Pietrasiewicz {
215400a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
215500a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
21567f7c548cSVincent Pelletier 	int current_class = -1;
215700a2430fSAndrzej Pietrasiewicz 
215800a2430fSAndrzej Pietrasiewicz 	ENTER();
215900a2430fSAndrzej Pietrasiewicz 
216000a2430fSAndrzej Pietrasiewicz 	for (;;) {
216100a2430fSAndrzej Pietrasiewicz 		int ret;
216200a2430fSAndrzej Pietrasiewicz 
216300a2430fSAndrzej Pietrasiewicz 		if (num == count)
216400a2430fSAndrzej Pietrasiewicz 			data = NULL;
216500a2430fSAndrzej Pietrasiewicz 
216600a2430fSAndrzej Pietrasiewicz 		/* Record "descriptor" entity */
216700a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
216800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
216900a2430fSAndrzej Pietrasiewicz 			pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
217000a2430fSAndrzej Pietrasiewicz 				 num, ret);
217100a2430fSAndrzej Pietrasiewicz 			return ret;
217200a2430fSAndrzej Pietrasiewicz 		}
217300a2430fSAndrzej Pietrasiewicz 
217400a2430fSAndrzej Pietrasiewicz 		if (!data)
217500a2430fSAndrzej Pietrasiewicz 			return _len - len;
217600a2430fSAndrzej Pietrasiewicz 
21777f7c548cSVincent Pelletier 		ret = ffs_do_single_desc(data, len, entity, priv,
21787f7c548cSVincent Pelletier 			&current_class);
217900a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
218000a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
218100a2430fSAndrzej Pietrasiewicz 			return ret;
218200a2430fSAndrzej Pietrasiewicz 		}
218300a2430fSAndrzej Pietrasiewicz 
218400a2430fSAndrzej Pietrasiewicz 		len -= ret;
218500a2430fSAndrzej Pietrasiewicz 		data += ret;
218600a2430fSAndrzej Pietrasiewicz 		++num;
218700a2430fSAndrzej Pietrasiewicz 	}
218800a2430fSAndrzej Pietrasiewicz }
218900a2430fSAndrzej Pietrasiewicz 
219000a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_entity(enum ffs_entity_type type,
219100a2430fSAndrzej Pietrasiewicz 				u8 *valuep, struct usb_descriptor_header *desc,
219200a2430fSAndrzej Pietrasiewicz 				void *priv)
219300a2430fSAndrzej Pietrasiewicz {
21946d5c1c77SRobert Baldyga 	struct ffs_desc_helper *helper = priv;
21956d5c1c77SRobert Baldyga 	struct usb_endpoint_descriptor *d;
219600a2430fSAndrzej Pietrasiewicz 
219700a2430fSAndrzej Pietrasiewicz 	ENTER();
219800a2430fSAndrzej Pietrasiewicz 
219900a2430fSAndrzej Pietrasiewicz 	switch (type) {
220000a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
220100a2430fSAndrzej Pietrasiewicz 		break;
220200a2430fSAndrzej Pietrasiewicz 
220300a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
220400a2430fSAndrzej Pietrasiewicz 		/*
220500a2430fSAndrzej Pietrasiewicz 		 * Interfaces are indexed from zero so if we
220600a2430fSAndrzej Pietrasiewicz 		 * encountered interface "n" then there are at least
220700a2430fSAndrzej Pietrasiewicz 		 * "n+1" interfaces.
220800a2430fSAndrzej Pietrasiewicz 		 */
22096d5c1c77SRobert Baldyga 		if (*valuep >= helper->interfaces_count)
22106d5c1c77SRobert Baldyga 			helper->interfaces_count = *valuep + 1;
221100a2430fSAndrzej Pietrasiewicz 		break;
221200a2430fSAndrzej Pietrasiewicz 
221300a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
221400a2430fSAndrzej Pietrasiewicz 		/*
221596a420d2SVincent Pelletier 		 * Strings are indexed from 1 (0 is reserved
221696a420d2SVincent Pelletier 		 * for languages list)
221700a2430fSAndrzej Pietrasiewicz 		 */
22186d5c1c77SRobert Baldyga 		if (*valuep > helper->ffs->strings_count)
22196d5c1c77SRobert Baldyga 			helper->ffs->strings_count = *valuep;
222000a2430fSAndrzej Pietrasiewicz 		break;
222100a2430fSAndrzej Pietrasiewicz 
222200a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
22236d5c1c77SRobert Baldyga 		d = (void *)desc;
22246d5c1c77SRobert Baldyga 		helper->eps_count++;
222541dc9ac1SVincent Pelletier 		if (helper->eps_count >= FFS_MAX_EPS_COUNT)
22266d5c1c77SRobert Baldyga 			return -EINVAL;
22276d5c1c77SRobert Baldyga 		/* Check if descriptors for any speed were already parsed */
22286d5c1c77SRobert Baldyga 		if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
22296d5c1c77SRobert Baldyga 			helper->ffs->eps_addrmap[helper->eps_count] =
22306d5c1c77SRobert Baldyga 				d->bEndpointAddress;
22316d5c1c77SRobert Baldyga 		else if (helper->ffs->eps_addrmap[helper->eps_count] !=
22326d5c1c77SRobert Baldyga 				d->bEndpointAddress)
22336d5c1c77SRobert Baldyga 			return -EINVAL;
223400a2430fSAndrzej Pietrasiewicz 		break;
223500a2430fSAndrzej Pietrasiewicz 	}
223600a2430fSAndrzej Pietrasiewicz 
223700a2430fSAndrzej Pietrasiewicz 	return 0;
223800a2430fSAndrzej Pietrasiewicz }
223900a2430fSAndrzej Pietrasiewicz 
224000a2430fSAndrzej Pietrasiewicz static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
224100a2430fSAndrzej Pietrasiewicz 				   struct usb_os_desc_header *desc)
224200a2430fSAndrzej Pietrasiewicz {
224300a2430fSAndrzej Pietrasiewicz 	u16 bcd_version = le16_to_cpu(desc->bcdVersion);
224400a2430fSAndrzej Pietrasiewicz 	u16 w_index = le16_to_cpu(desc->wIndex);
224500a2430fSAndrzej Pietrasiewicz 
224600a2430fSAndrzej Pietrasiewicz 	if (bcd_version != 1) {
224700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptors version: %d",
224800a2430fSAndrzej Pietrasiewicz 			  bcd_version);
224900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
225000a2430fSAndrzej Pietrasiewicz 	}
225100a2430fSAndrzej Pietrasiewicz 	switch (w_index) {
225200a2430fSAndrzej Pietrasiewicz 	case 0x4:
225300a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_COMPAT;
225400a2430fSAndrzej Pietrasiewicz 		break;
225500a2430fSAndrzej Pietrasiewicz 	case 0x5:
225600a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_PROP;
225700a2430fSAndrzej Pietrasiewicz 		break;
225800a2430fSAndrzej Pietrasiewicz 	default:
225900a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptor type: %d", w_index);
226000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
226100a2430fSAndrzej Pietrasiewicz 	}
226200a2430fSAndrzej Pietrasiewicz 
226300a2430fSAndrzej Pietrasiewicz 	return sizeof(*desc);
226400a2430fSAndrzej Pietrasiewicz }
226500a2430fSAndrzej Pietrasiewicz 
226600a2430fSAndrzej Pietrasiewicz /*
226700a2430fSAndrzej Pietrasiewicz  * Process all extended compatibility/extended property descriptors
226800a2430fSAndrzej Pietrasiewicz  * of a feature descriptor
226900a2430fSAndrzej Pietrasiewicz  */
227000a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
227100a2430fSAndrzej Pietrasiewicz 					      enum ffs_os_desc_type type,
227200a2430fSAndrzej Pietrasiewicz 					      u16 feature_count,
227300a2430fSAndrzej Pietrasiewicz 					      ffs_os_desc_callback entity,
227400a2430fSAndrzej Pietrasiewicz 					      void *priv,
227500a2430fSAndrzej Pietrasiewicz 					      struct usb_os_desc_header *h)
227600a2430fSAndrzej Pietrasiewicz {
227700a2430fSAndrzej Pietrasiewicz 	int ret;
227800a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
227900a2430fSAndrzej Pietrasiewicz 
228000a2430fSAndrzej Pietrasiewicz 	ENTER();
228100a2430fSAndrzej Pietrasiewicz 
228200a2430fSAndrzej Pietrasiewicz 	/* loop over all ext compat/ext prop descriptors */
228300a2430fSAndrzej Pietrasiewicz 	while (feature_count--) {
228400a2430fSAndrzej Pietrasiewicz 		ret = entity(type, h, data, len, priv);
228500a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
228600a2430fSAndrzej Pietrasiewicz 			pr_debug("bad OS descriptor, type: %d\n", type);
228700a2430fSAndrzej Pietrasiewicz 			return ret;
228800a2430fSAndrzej Pietrasiewicz 		}
228900a2430fSAndrzej Pietrasiewicz 		data += ret;
229000a2430fSAndrzej Pietrasiewicz 		len -= ret;
229100a2430fSAndrzej Pietrasiewicz 	}
229200a2430fSAndrzej Pietrasiewicz 	return _len - len;
229300a2430fSAndrzej Pietrasiewicz }
229400a2430fSAndrzej Pietrasiewicz 
229500a2430fSAndrzej Pietrasiewicz /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
229600a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_os_descs(unsigned count,
229700a2430fSAndrzej Pietrasiewicz 					char *data, unsigned len,
229800a2430fSAndrzej Pietrasiewicz 					ffs_os_desc_callback entity, void *priv)
229900a2430fSAndrzej Pietrasiewicz {
230000a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
230100a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
230200a2430fSAndrzej Pietrasiewicz 
230300a2430fSAndrzej Pietrasiewicz 	ENTER();
230400a2430fSAndrzej Pietrasiewicz 
230500a2430fSAndrzej Pietrasiewicz 	for (num = 0; num < count; ++num) {
230600a2430fSAndrzej Pietrasiewicz 		int ret;
230700a2430fSAndrzej Pietrasiewicz 		enum ffs_os_desc_type type;
230800a2430fSAndrzej Pietrasiewicz 		u16 feature_count;
230900a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_header *desc = (void *)data;
231000a2430fSAndrzej Pietrasiewicz 
231100a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*desc))
231200a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
231300a2430fSAndrzej Pietrasiewicz 
231400a2430fSAndrzej Pietrasiewicz 		/*
231500a2430fSAndrzej Pietrasiewicz 		 * Record "descriptor" entity.
231600a2430fSAndrzej Pietrasiewicz 		 * Process dwLength, bcdVersion, wIndex, get b/wCount.
231700a2430fSAndrzej Pietrasiewicz 		 * Move the data pointer to the beginning of extended
231800a2430fSAndrzej Pietrasiewicz 		 * compatibilities proper or extended properties proper
231900a2430fSAndrzej Pietrasiewicz 		 * portions of the data
232000a2430fSAndrzej Pietrasiewicz 		 */
232100a2430fSAndrzej Pietrasiewicz 		if (le32_to_cpu(desc->dwLength) > len)
232200a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
232300a2430fSAndrzej Pietrasiewicz 
232400a2430fSAndrzej Pietrasiewicz 		ret = __ffs_do_os_desc_header(&type, desc);
232500a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
232600a2430fSAndrzej Pietrasiewicz 			pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
232700a2430fSAndrzej Pietrasiewicz 				 num, ret);
232800a2430fSAndrzej Pietrasiewicz 			return ret;
232900a2430fSAndrzej Pietrasiewicz 		}
233000a2430fSAndrzej Pietrasiewicz 		/*
233100a2430fSAndrzej Pietrasiewicz 		 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
233200a2430fSAndrzej Pietrasiewicz 		 */
233300a2430fSAndrzej Pietrasiewicz 		feature_count = le16_to_cpu(desc->wCount);
233400a2430fSAndrzej Pietrasiewicz 		if (type == FFS_OS_DESC_EXT_COMPAT &&
233500a2430fSAndrzej Pietrasiewicz 		    (feature_count > 255 || desc->Reserved))
233600a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
233700a2430fSAndrzej Pietrasiewicz 		len -= ret;
233800a2430fSAndrzej Pietrasiewicz 		data += ret;
233900a2430fSAndrzej Pietrasiewicz 
234000a2430fSAndrzej Pietrasiewicz 		/*
234100a2430fSAndrzej Pietrasiewicz 		 * Process all function/property descriptors
234200a2430fSAndrzej Pietrasiewicz 		 * of this Feature Descriptor
234300a2430fSAndrzej Pietrasiewicz 		 */
234400a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_single_os_desc(data, len, type,
234500a2430fSAndrzej Pietrasiewicz 					    feature_count, entity, priv, desc);
234600a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
234700a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
234800a2430fSAndrzej Pietrasiewicz 			return ret;
234900a2430fSAndrzej Pietrasiewicz 		}
235000a2430fSAndrzej Pietrasiewicz 
235100a2430fSAndrzej Pietrasiewicz 		len -= ret;
235200a2430fSAndrzej Pietrasiewicz 		data += ret;
235300a2430fSAndrzej Pietrasiewicz 	}
235400a2430fSAndrzej Pietrasiewicz 	return _len - len;
235500a2430fSAndrzej Pietrasiewicz }
235600a2430fSAndrzej Pietrasiewicz 
235700a2430fSAndrzej Pietrasiewicz /**
235800a2430fSAndrzej Pietrasiewicz  * Validate contents of the buffer from userspace related to OS descriptors.
235900a2430fSAndrzej Pietrasiewicz  */
236000a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
236100a2430fSAndrzej Pietrasiewicz 				 struct usb_os_desc_header *h, void *data,
236200a2430fSAndrzej Pietrasiewicz 				 unsigned len, void *priv)
236300a2430fSAndrzej Pietrasiewicz {
236400a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = priv;
236500a2430fSAndrzej Pietrasiewicz 	u8 length;
236600a2430fSAndrzej Pietrasiewicz 
236700a2430fSAndrzej Pietrasiewicz 	ENTER();
236800a2430fSAndrzej Pietrasiewicz 
236900a2430fSAndrzej Pietrasiewicz 	switch (type) {
237000a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
237100a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *d = data;
237200a2430fSAndrzej Pietrasiewicz 		int i;
237300a2430fSAndrzej Pietrasiewicz 
237400a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) ||
2375a3acc696SJohn Keeping 		    d->bFirstInterfaceNumber >= ffs->interfaces_count)
237600a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
2377a3acc696SJohn Keeping 		if (d->Reserved1 != 1) {
2378a3acc696SJohn Keeping 			/*
2379a3acc696SJohn Keeping 			 * According to the spec, Reserved1 must be set to 1
2380a3acc696SJohn Keeping 			 * but older kernels incorrectly rejected non-zero
2381a3acc696SJohn Keeping 			 * values.  We fix it here to avoid returning EINVAL
2382a3acc696SJohn Keeping 			 * in response to values we used to accept.
2383a3acc696SJohn Keeping 			 */
2384a3acc696SJohn Keeping 			pr_debug("usb_ext_compat_desc::Reserved1 forced to 1\n");
2385a3acc696SJohn Keeping 			d->Reserved1 = 1;
2386a3acc696SJohn Keeping 		}
238700a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
238800a2430fSAndrzej Pietrasiewicz 			if (d->Reserved2[i])
238900a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
239000a2430fSAndrzej Pietrasiewicz 
239100a2430fSAndrzej Pietrasiewicz 		length = sizeof(struct usb_ext_compat_desc);
239200a2430fSAndrzej Pietrasiewicz 	}
239300a2430fSAndrzej Pietrasiewicz 		break;
239400a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
239500a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *d = data;
239600a2430fSAndrzej Pietrasiewicz 		u32 type, pdl;
239700a2430fSAndrzej Pietrasiewicz 		u16 pnl;
239800a2430fSAndrzej Pietrasiewicz 
239900a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
240000a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
240100a2430fSAndrzej Pietrasiewicz 		length = le32_to_cpu(d->dwSize);
240283e526f2SVincent Pelletier 		if (len < length)
240383e526f2SVincent Pelletier 			return -EINVAL;
240400a2430fSAndrzej Pietrasiewicz 		type = le32_to_cpu(d->dwPropertyDataType);
240500a2430fSAndrzej Pietrasiewicz 		if (type < USB_EXT_PROP_UNICODE ||
240600a2430fSAndrzej Pietrasiewicz 		    type > USB_EXT_PROP_UNICODE_MULTI) {
240700a2430fSAndrzej Pietrasiewicz 			pr_vdebug("unsupported os descriptor property type: %d",
240800a2430fSAndrzej Pietrasiewicz 				  type);
240900a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
241000a2430fSAndrzej Pietrasiewicz 		}
241100a2430fSAndrzej Pietrasiewicz 		pnl = le16_to_cpu(d->wPropertyNameLength);
241283e526f2SVincent Pelletier 		if (length < 14 + pnl) {
241383e526f2SVincent Pelletier 			pr_vdebug("invalid os descriptor length: %d pnl:%d (descriptor %d)\n",
241483e526f2SVincent Pelletier 				  length, pnl, type);
241583e526f2SVincent Pelletier 			return -EINVAL;
241683e526f2SVincent Pelletier 		}
2417c40619bbSVincent Pelletier 		pdl = le32_to_cpu(*(__le32 *)((u8 *)data + 10 + pnl));
241800a2430fSAndrzej Pietrasiewicz 		if (length != 14 + pnl + pdl) {
241900a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
242000a2430fSAndrzej Pietrasiewicz 				  length, pnl, pdl, type);
242100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
242200a2430fSAndrzej Pietrasiewicz 		}
242300a2430fSAndrzej Pietrasiewicz 		++ffs->ms_os_descs_ext_prop_count;
242400a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
242500a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
242600a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_data_len += pdl;
242700a2430fSAndrzej Pietrasiewicz 	}
242800a2430fSAndrzej Pietrasiewicz 		break;
242900a2430fSAndrzej Pietrasiewicz 	default:
243000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
243100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
243200a2430fSAndrzej Pietrasiewicz 	}
243300a2430fSAndrzej Pietrasiewicz 	return length;
243400a2430fSAndrzej Pietrasiewicz }
243500a2430fSAndrzej Pietrasiewicz 
243600a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_descs(struct ffs_data *ffs,
243700a2430fSAndrzej Pietrasiewicz 				char *const _data, size_t len)
243800a2430fSAndrzej Pietrasiewicz {
243900a2430fSAndrzej Pietrasiewicz 	char *data = _data, *raw_descs;
244000a2430fSAndrzej Pietrasiewicz 	unsigned os_descs_count = 0, counts[3], flags;
244100a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL, i;
24426d5c1c77SRobert Baldyga 	struct ffs_desc_helper helper;
244300a2430fSAndrzej Pietrasiewicz 
244400a2430fSAndrzej Pietrasiewicz 	ENTER();
244500a2430fSAndrzej Pietrasiewicz 
244600a2430fSAndrzej Pietrasiewicz 	if (get_unaligned_le32(data + 4) != len)
244700a2430fSAndrzej Pietrasiewicz 		goto error;
244800a2430fSAndrzej Pietrasiewicz 
244900a2430fSAndrzej Pietrasiewicz 	switch (get_unaligned_le32(data)) {
245000a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC:
245100a2430fSAndrzej Pietrasiewicz 		flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
245200a2430fSAndrzej Pietrasiewicz 		data += 8;
245300a2430fSAndrzej Pietrasiewicz 		len  -= 8;
245400a2430fSAndrzej Pietrasiewicz 		break;
245500a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
245600a2430fSAndrzej Pietrasiewicz 		flags = get_unaligned_le32(data + 8);
24571b0bf88fSRobert Baldyga 		ffs->user_flags = flags;
245800a2430fSAndrzej Pietrasiewicz 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
245900a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_HS_DESC |
246000a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_SS_DESC |
24611b0bf88fSRobert Baldyga 			      FUNCTIONFS_HAS_MS_OS_DESC |
24625e33f6fdSRobert Baldyga 			      FUNCTIONFS_VIRTUAL_ADDR |
246354dfce6dSFelix Hädicke 			      FUNCTIONFS_EVENTFD |
24644368c28aSFelix Hädicke 			      FUNCTIONFS_ALL_CTRL_RECIP |
24654368c28aSFelix Hädicke 			      FUNCTIONFS_CONFIG0_SETUP)) {
246600a2430fSAndrzej Pietrasiewicz 			ret = -ENOSYS;
246700a2430fSAndrzej Pietrasiewicz 			goto error;
246800a2430fSAndrzej Pietrasiewicz 		}
246900a2430fSAndrzej Pietrasiewicz 		data += 12;
247000a2430fSAndrzej Pietrasiewicz 		len  -= 12;
247100a2430fSAndrzej Pietrasiewicz 		break;
247200a2430fSAndrzej Pietrasiewicz 	default:
247300a2430fSAndrzej Pietrasiewicz 		goto error;
247400a2430fSAndrzej Pietrasiewicz 	}
247500a2430fSAndrzej Pietrasiewicz 
24765e33f6fdSRobert Baldyga 	if (flags & FUNCTIONFS_EVENTFD) {
24775e33f6fdSRobert Baldyga 		if (len < 4)
24785e33f6fdSRobert Baldyga 			goto error;
24795e33f6fdSRobert Baldyga 		ffs->ffs_eventfd =
24805e33f6fdSRobert Baldyga 			eventfd_ctx_fdget((int)get_unaligned_le32(data));
24815e33f6fdSRobert Baldyga 		if (IS_ERR(ffs->ffs_eventfd)) {
24825e33f6fdSRobert Baldyga 			ret = PTR_ERR(ffs->ffs_eventfd);
24835e33f6fdSRobert Baldyga 			ffs->ffs_eventfd = NULL;
24845e33f6fdSRobert Baldyga 			goto error;
24855e33f6fdSRobert Baldyga 		}
24865e33f6fdSRobert Baldyga 		data += 4;
24875e33f6fdSRobert Baldyga 		len  -= 4;
24885e33f6fdSRobert Baldyga 	}
24895e33f6fdSRobert Baldyga 
249000a2430fSAndrzej Pietrasiewicz 	/* Read fs_count, hs_count and ss_count (if present) */
249100a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
249200a2430fSAndrzej Pietrasiewicz 		if (!(flags & (1 << i))) {
249300a2430fSAndrzej Pietrasiewicz 			counts[i] = 0;
249400a2430fSAndrzej Pietrasiewicz 		} else if (len < 4) {
249500a2430fSAndrzej Pietrasiewicz 			goto error;
249600a2430fSAndrzej Pietrasiewicz 		} else {
249700a2430fSAndrzej Pietrasiewicz 			counts[i] = get_unaligned_le32(data);
249800a2430fSAndrzej Pietrasiewicz 			data += 4;
249900a2430fSAndrzej Pietrasiewicz 			len  -= 4;
250000a2430fSAndrzej Pietrasiewicz 		}
250100a2430fSAndrzej Pietrasiewicz 	}
250200a2430fSAndrzej Pietrasiewicz 	if (flags & (1 << i)) {
250383e526f2SVincent Pelletier 		if (len < 4) {
250483e526f2SVincent Pelletier 			goto error;
250583e526f2SVincent Pelletier 		}
250600a2430fSAndrzej Pietrasiewicz 		os_descs_count = get_unaligned_le32(data);
250700a2430fSAndrzej Pietrasiewicz 		data += 4;
250800a2430fSAndrzej Pietrasiewicz 		len -= 4;
250900a2430fSAndrzej Pietrasiewicz 	};
251000a2430fSAndrzej Pietrasiewicz 
251100a2430fSAndrzej Pietrasiewicz 	/* Read descriptors */
251200a2430fSAndrzej Pietrasiewicz 	raw_descs = data;
25136d5c1c77SRobert Baldyga 	helper.ffs = ffs;
251400a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
251500a2430fSAndrzej Pietrasiewicz 		if (!counts[i])
251600a2430fSAndrzej Pietrasiewicz 			continue;
25176d5c1c77SRobert Baldyga 		helper.interfaces_count = 0;
25186d5c1c77SRobert Baldyga 		helper.eps_count = 0;
251900a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_descs(counts[i], data, len,
25206d5c1c77SRobert Baldyga 				   __ffs_data_do_entity, &helper);
252100a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
252200a2430fSAndrzej Pietrasiewicz 			goto error;
25236d5c1c77SRobert Baldyga 		if (!ffs->eps_count && !ffs->interfaces_count) {
25246d5c1c77SRobert Baldyga 			ffs->eps_count = helper.eps_count;
25256d5c1c77SRobert Baldyga 			ffs->interfaces_count = helper.interfaces_count;
25266d5c1c77SRobert Baldyga 		} else {
25276d5c1c77SRobert Baldyga 			if (ffs->eps_count != helper.eps_count) {
25286d5c1c77SRobert Baldyga 				ret = -EINVAL;
25296d5c1c77SRobert Baldyga 				goto error;
25306d5c1c77SRobert Baldyga 			}
25316d5c1c77SRobert Baldyga 			if (ffs->interfaces_count != helper.interfaces_count) {
25326d5c1c77SRobert Baldyga 				ret = -EINVAL;
25336d5c1c77SRobert Baldyga 				goto error;
25346d5c1c77SRobert Baldyga 			}
25356d5c1c77SRobert Baldyga 		}
253600a2430fSAndrzej Pietrasiewicz 		data += ret;
253700a2430fSAndrzej Pietrasiewicz 		len  -= ret;
253800a2430fSAndrzej Pietrasiewicz 	}
253900a2430fSAndrzej Pietrasiewicz 	if (os_descs_count) {
254000a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(os_descs_count, data, len,
254100a2430fSAndrzej Pietrasiewicz 				      __ffs_data_do_os_desc, ffs);
254200a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
254300a2430fSAndrzej Pietrasiewicz 			goto error;
254400a2430fSAndrzej Pietrasiewicz 		data += ret;
254500a2430fSAndrzej Pietrasiewicz 		len -= ret;
254600a2430fSAndrzej Pietrasiewicz 	}
254700a2430fSAndrzej Pietrasiewicz 
254800a2430fSAndrzej Pietrasiewicz 	if (raw_descs == data || len) {
254900a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
255000a2430fSAndrzej Pietrasiewicz 		goto error;
255100a2430fSAndrzej Pietrasiewicz 	}
255200a2430fSAndrzej Pietrasiewicz 
255300a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data	= _data;
255400a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs		= raw_descs;
255500a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length	= data - raw_descs;
255600a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count	= counts[0];
255700a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count	= counts[1];
255800a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count	= counts[2];
255900a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_count	= os_descs_count;
256000a2430fSAndrzej Pietrasiewicz 
256100a2430fSAndrzej Pietrasiewicz 	return 0;
256200a2430fSAndrzej Pietrasiewicz 
256300a2430fSAndrzej Pietrasiewicz error:
256400a2430fSAndrzej Pietrasiewicz 	kfree(_data);
256500a2430fSAndrzej Pietrasiewicz 	return ret;
256600a2430fSAndrzej Pietrasiewicz }
256700a2430fSAndrzej Pietrasiewicz 
256800a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_strings(struct ffs_data *ffs,
256900a2430fSAndrzej Pietrasiewicz 				  char *const _data, size_t len)
257000a2430fSAndrzej Pietrasiewicz {
257100a2430fSAndrzej Pietrasiewicz 	u32 str_count, needed_count, lang_count;
257200a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **stringtabs, *t;
257300a2430fSAndrzej Pietrasiewicz 	const char *data = _data;
2574872ce511SMichal Nazarewicz 	struct usb_string *s;
257500a2430fSAndrzej Pietrasiewicz 
257600a2430fSAndrzej Pietrasiewicz 	ENTER();
257700a2430fSAndrzej Pietrasiewicz 
257883e526f2SVincent Pelletier 	if (unlikely(len < 16 ||
257983e526f2SVincent Pelletier 		     get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
258000a2430fSAndrzej Pietrasiewicz 		     get_unaligned_le32(data + 4) != len))
258100a2430fSAndrzej Pietrasiewicz 		goto error;
258200a2430fSAndrzej Pietrasiewicz 	str_count  = get_unaligned_le32(data + 8);
258300a2430fSAndrzej Pietrasiewicz 	lang_count = get_unaligned_le32(data + 12);
258400a2430fSAndrzej Pietrasiewicz 
258500a2430fSAndrzej Pietrasiewicz 	/* if one is zero the other must be zero */
258600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!str_count != !lang_count))
258700a2430fSAndrzej Pietrasiewicz 		goto error;
258800a2430fSAndrzej Pietrasiewicz 
258900a2430fSAndrzej Pietrasiewicz 	/* Do we have at least as many strings as descriptors need? */
259000a2430fSAndrzej Pietrasiewicz 	needed_count = ffs->strings_count;
259100a2430fSAndrzej Pietrasiewicz 	if (unlikely(str_count < needed_count))
259200a2430fSAndrzej Pietrasiewicz 		goto error;
259300a2430fSAndrzej Pietrasiewicz 
259400a2430fSAndrzej Pietrasiewicz 	/*
259500a2430fSAndrzej Pietrasiewicz 	 * If we don't need any strings just return and free all
259600a2430fSAndrzej Pietrasiewicz 	 * memory.
259700a2430fSAndrzej Pietrasiewicz 	 */
259800a2430fSAndrzej Pietrasiewicz 	if (!needed_count) {
259900a2430fSAndrzej Pietrasiewicz 		kfree(_data);
260000a2430fSAndrzej Pietrasiewicz 		return 0;
260100a2430fSAndrzej Pietrasiewicz 	}
260200a2430fSAndrzej Pietrasiewicz 
260300a2430fSAndrzej Pietrasiewicz 	/* Allocate everything in one chunk so there's less maintenance. */
260400a2430fSAndrzej Pietrasiewicz 	{
260500a2430fSAndrzej Pietrasiewicz 		unsigned i = 0;
260600a2430fSAndrzej Pietrasiewicz 		vla_group(d);
260700a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings *, stringtabs,
260800a2430fSAndrzej Pietrasiewicz 			lang_count + 1);
260900a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
261000a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_string, strings,
261100a2430fSAndrzej Pietrasiewicz 			lang_count*(needed_count+1));
261200a2430fSAndrzej Pietrasiewicz 
261300a2430fSAndrzej Pietrasiewicz 		char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
261400a2430fSAndrzej Pietrasiewicz 
261500a2430fSAndrzej Pietrasiewicz 		if (unlikely(!vlabuf)) {
261600a2430fSAndrzej Pietrasiewicz 			kfree(_data);
261700a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
261800a2430fSAndrzej Pietrasiewicz 		}
261900a2430fSAndrzej Pietrasiewicz 
262000a2430fSAndrzej Pietrasiewicz 		/* Initialize the VLA pointers */
262100a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
262200a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
262300a2430fSAndrzej Pietrasiewicz 		i = lang_count;
262400a2430fSAndrzej Pietrasiewicz 		do {
262500a2430fSAndrzej Pietrasiewicz 			*stringtabs++ = t++;
262600a2430fSAndrzej Pietrasiewicz 		} while (--i);
262700a2430fSAndrzej Pietrasiewicz 		*stringtabs = NULL;
262800a2430fSAndrzej Pietrasiewicz 
262900a2430fSAndrzej Pietrasiewicz 		/* stringtabs = vlabuf = d_stringtabs for later kfree */
263000a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
263100a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
263200a2430fSAndrzej Pietrasiewicz 		s = vla_ptr(vlabuf, d, strings);
263300a2430fSAndrzej Pietrasiewicz 	}
263400a2430fSAndrzej Pietrasiewicz 
263500a2430fSAndrzej Pietrasiewicz 	/* For each language */
263600a2430fSAndrzej Pietrasiewicz 	data += 16;
263700a2430fSAndrzej Pietrasiewicz 	len -= 16;
263800a2430fSAndrzej Pietrasiewicz 
263900a2430fSAndrzej Pietrasiewicz 	do { /* lang_count > 0 so we can use do-while */
264000a2430fSAndrzej Pietrasiewicz 		unsigned needed = needed_count;
264100a2430fSAndrzej Pietrasiewicz 
264200a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 3))
264300a2430fSAndrzej Pietrasiewicz 			goto error_free;
264400a2430fSAndrzej Pietrasiewicz 		t->language = get_unaligned_le16(data);
264500a2430fSAndrzej Pietrasiewicz 		t->strings  = s;
264600a2430fSAndrzej Pietrasiewicz 		++t;
264700a2430fSAndrzej Pietrasiewicz 
264800a2430fSAndrzej Pietrasiewicz 		data += 2;
264900a2430fSAndrzej Pietrasiewicz 		len -= 2;
265000a2430fSAndrzej Pietrasiewicz 
265100a2430fSAndrzej Pietrasiewicz 		/* For each string */
265200a2430fSAndrzej Pietrasiewicz 		do { /* str_count > 0 so we can use do-while */
265300a2430fSAndrzej Pietrasiewicz 			size_t length = strnlen(data, len);
265400a2430fSAndrzej Pietrasiewicz 
265500a2430fSAndrzej Pietrasiewicz 			if (unlikely(length == len))
265600a2430fSAndrzej Pietrasiewicz 				goto error_free;
265700a2430fSAndrzej Pietrasiewicz 
265800a2430fSAndrzej Pietrasiewicz 			/*
265900a2430fSAndrzej Pietrasiewicz 			 * User may provide more strings then we need,
266000a2430fSAndrzej Pietrasiewicz 			 * if that's the case we simply ignore the
266100a2430fSAndrzej Pietrasiewicz 			 * rest
266200a2430fSAndrzej Pietrasiewicz 			 */
266300a2430fSAndrzej Pietrasiewicz 			if (likely(needed)) {
266400a2430fSAndrzej Pietrasiewicz 				/*
266500a2430fSAndrzej Pietrasiewicz 				 * s->id will be set while adding
266600a2430fSAndrzej Pietrasiewicz 				 * function to configuration so for
266700a2430fSAndrzej Pietrasiewicz 				 * now just leave garbage here.
266800a2430fSAndrzej Pietrasiewicz 				 */
266900a2430fSAndrzej Pietrasiewicz 				s->s = data;
267000a2430fSAndrzej Pietrasiewicz 				--needed;
267100a2430fSAndrzej Pietrasiewicz 				++s;
267200a2430fSAndrzej Pietrasiewicz 			}
267300a2430fSAndrzej Pietrasiewicz 
267400a2430fSAndrzej Pietrasiewicz 			data += length + 1;
267500a2430fSAndrzej Pietrasiewicz 			len -= length + 1;
267600a2430fSAndrzej Pietrasiewicz 		} while (--str_count);
267700a2430fSAndrzej Pietrasiewicz 
267800a2430fSAndrzej Pietrasiewicz 		s->id = 0;   /* terminator */
267900a2430fSAndrzej Pietrasiewicz 		s->s = NULL;
268000a2430fSAndrzej Pietrasiewicz 		++s;
268100a2430fSAndrzej Pietrasiewicz 
268200a2430fSAndrzej Pietrasiewicz 	} while (--lang_count);
268300a2430fSAndrzej Pietrasiewicz 
268400a2430fSAndrzej Pietrasiewicz 	/* Some garbage left? */
268500a2430fSAndrzej Pietrasiewicz 	if (unlikely(len))
268600a2430fSAndrzej Pietrasiewicz 		goto error_free;
268700a2430fSAndrzej Pietrasiewicz 
268800a2430fSAndrzej Pietrasiewicz 	/* Done! */
268900a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = stringtabs;
269000a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = _data;
269100a2430fSAndrzej Pietrasiewicz 
269200a2430fSAndrzej Pietrasiewicz 	return 0;
269300a2430fSAndrzej Pietrasiewicz 
269400a2430fSAndrzej Pietrasiewicz error_free:
269500a2430fSAndrzej Pietrasiewicz 	kfree(stringtabs);
269600a2430fSAndrzej Pietrasiewicz error:
269700a2430fSAndrzej Pietrasiewicz 	kfree(_data);
269800a2430fSAndrzej Pietrasiewicz 	return -EINVAL;
269900a2430fSAndrzej Pietrasiewicz }
270000a2430fSAndrzej Pietrasiewicz 
270100a2430fSAndrzej Pietrasiewicz 
270200a2430fSAndrzej Pietrasiewicz /* Events handling and management *******************************************/
270300a2430fSAndrzej Pietrasiewicz 
270400a2430fSAndrzej Pietrasiewicz static void __ffs_event_add(struct ffs_data *ffs,
270500a2430fSAndrzej Pietrasiewicz 			    enum usb_functionfs_event_type type)
270600a2430fSAndrzej Pietrasiewicz {
270700a2430fSAndrzej Pietrasiewicz 	enum usb_functionfs_event_type rem_type1, rem_type2 = type;
270800a2430fSAndrzej Pietrasiewicz 	int neg = 0;
270900a2430fSAndrzej Pietrasiewicz 
271000a2430fSAndrzej Pietrasiewicz 	/*
271100a2430fSAndrzej Pietrasiewicz 	 * Abort any unhandled setup
271200a2430fSAndrzej Pietrasiewicz 	 *
271300a2430fSAndrzej Pietrasiewicz 	 * We do not need to worry about some cmpxchg() changing value
271400a2430fSAndrzej Pietrasiewicz 	 * of ffs->setup_state without holding the lock because when
271500a2430fSAndrzej Pietrasiewicz 	 * state is FFS_SETUP_PENDING cmpxchg() in several places in
271600a2430fSAndrzej Pietrasiewicz 	 * the source does nothing.
271700a2430fSAndrzej Pietrasiewicz 	 */
271800a2430fSAndrzej Pietrasiewicz 	if (ffs->setup_state == FFS_SETUP_PENDING)
271900a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_SETUP_CANCELLED;
272000a2430fSAndrzej Pietrasiewicz 
272167913bbdSMichal Nazarewicz 	/*
272267913bbdSMichal Nazarewicz 	 * Logic of this function guarantees that there are at most four pending
272367913bbdSMichal Nazarewicz 	 * evens on ffs->ev.types queue.  This is important because the queue
272467913bbdSMichal Nazarewicz 	 * has space for four elements only and __ffs_ep0_read_events function
272567913bbdSMichal Nazarewicz 	 * depends on that limit as well.  If more event types are added, those
272667913bbdSMichal Nazarewicz 	 * limits have to be revisited or guaranteed to still hold.
272767913bbdSMichal Nazarewicz 	 */
272800a2430fSAndrzej Pietrasiewicz 	switch (type) {
272900a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_RESUME:
273000a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_SUSPEND;
273100a2430fSAndrzej Pietrasiewicz 		/* FALL THROUGH */
273200a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SUSPEND:
273300a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SETUP:
273400a2430fSAndrzej Pietrasiewicz 		rem_type1 = type;
273500a2430fSAndrzej Pietrasiewicz 		/* Discard all similar events */
273600a2430fSAndrzej Pietrasiewicz 		break;
273700a2430fSAndrzej Pietrasiewicz 
273800a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_BIND:
273900a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_UNBIND:
274000a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DISABLE:
274100a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_ENABLE:
274200a2430fSAndrzej Pietrasiewicz 		/* Discard everything other then power management. */
274300a2430fSAndrzej Pietrasiewicz 		rem_type1 = FUNCTIONFS_SUSPEND;
274400a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_RESUME;
274500a2430fSAndrzej Pietrasiewicz 		neg = 1;
274600a2430fSAndrzej Pietrasiewicz 		break;
274700a2430fSAndrzej Pietrasiewicz 
274800a2430fSAndrzej Pietrasiewicz 	default:
2749fe00bcbfSMichal Nazarewicz 		WARN(1, "%d: unknown event, this should not happen\n", type);
2750fe00bcbfSMichal Nazarewicz 		return;
275100a2430fSAndrzej Pietrasiewicz 	}
275200a2430fSAndrzej Pietrasiewicz 
275300a2430fSAndrzej Pietrasiewicz 	{
275400a2430fSAndrzej Pietrasiewicz 		u8 *ev  = ffs->ev.types, *out = ev;
275500a2430fSAndrzej Pietrasiewicz 		unsigned n = ffs->ev.count;
275600a2430fSAndrzej Pietrasiewicz 		for (; n; --n, ++ev)
275700a2430fSAndrzej Pietrasiewicz 			if ((*ev == rem_type1 || *ev == rem_type2) == neg)
275800a2430fSAndrzej Pietrasiewicz 				*out++ = *ev;
275900a2430fSAndrzej Pietrasiewicz 			else
276000a2430fSAndrzej Pietrasiewicz 				pr_vdebug("purging event %d\n", *ev);
276100a2430fSAndrzej Pietrasiewicz 		ffs->ev.count = out - ffs->ev.types;
276200a2430fSAndrzej Pietrasiewicz 	}
276300a2430fSAndrzej Pietrasiewicz 
276400a2430fSAndrzej Pietrasiewicz 	pr_vdebug("adding event %d\n", type);
276500a2430fSAndrzej Pietrasiewicz 	ffs->ev.types[ffs->ev.count++] = type;
276600a2430fSAndrzej Pietrasiewicz 	wake_up_locked(&ffs->ev.waitq);
27675e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
27685e33f6fdSRobert Baldyga 		eventfd_signal(ffs->ffs_eventfd, 1);
276900a2430fSAndrzej Pietrasiewicz }
277000a2430fSAndrzej Pietrasiewicz 
277100a2430fSAndrzej Pietrasiewicz static void ffs_event_add(struct ffs_data *ffs,
277200a2430fSAndrzej Pietrasiewicz 			  enum usb_functionfs_event_type type)
277300a2430fSAndrzej Pietrasiewicz {
277400a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
277500a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
277600a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, type);
277700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
277800a2430fSAndrzej Pietrasiewicz }
277900a2430fSAndrzej Pietrasiewicz 
278000a2430fSAndrzej Pietrasiewicz /* Bind/unbind USB function hooks *******************************************/
278100a2430fSAndrzej Pietrasiewicz 
27826d5c1c77SRobert Baldyga static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
27836d5c1c77SRobert Baldyga {
27846d5c1c77SRobert Baldyga 	int i;
27856d5c1c77SRobert Baldyga 
27866d5c1c77SRobert Baldyga 	for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
27876d5c1c77SRobert Baldyga 		if (ffs->eps_addrmap[i] == endpoint_address)
27886d5c1c77SRobert Baldyga 			return i;
27896d5c1c77SRobert Baldyga 	return -ENOENT;
27906d5c1c77SRobert Baldyga }
27916d5c1c77SRobert Baldyga 
279200a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
279300a2430fSAndrzej Pietrasiewicz 				    struct usb_descriptor_header *desc,
279400a2430fSAndrzej Pietrasiewicz 				    void *priv)
279500a2430fSAndrzej Pietrasiewicz {
279600a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor *ds = (void *)desc;
279700a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
279800a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ffs_ep;
279985b06f5eSDan Carpenter 	unsigned ep_desc_id;
280085b06f5eSDan Carpenter 	int idx;
280100a2430fSAndrzej Pietrasiewicz 	static const char *speed_names[] = { "full", "high", "super" };
280200a2430fSAndrzej Pietrasiewicz 
280300a2430fSAndrzej Pietrasiewicz 	if (type != FFS_DESCRIPTOR)
280400a2430fSAndrzej Pietrasiewicz 		return 0;
280500a2430fSAndrzej Pietrasiewicz 
280600a2430fSAndrzej Pietrasiewicz 	/*
280700a2430fSAndrzej Pietrasiewicz 	 * If ss_descriptors is not NULL, we are reading super speed
280800a2430fSAndrzej Pietrasiewicz 	 * descriptors; if hs_descriptors is not NULL, we are reading high
280900a2430fSAndrzej Pietrasiewicz 	 * speed descriptors; otherwise, we are reading full speed
281000a2430fSAndrzej Pietrasiewicz 	 * descriptors.
281100a2430fSAndrzej Pietrasiewicz 	 */
281200a2430fSAndrzej Pietrasiewicz 	if (func->function.ss_descriptors) {
281300a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 2;
281400a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors[(long)valuep] = desc;
281500a2430fSAndrzej Pietrasiewicz 	} else if (func->function.hs_descriptors) {
281600a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 1;
281700a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors[(long)valuep] = desc;
281800a2430fSAndrzej Pietrasiewicz 	} else {
281900a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 0;
282000a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors[(long)valuep]    = desc;
282100a2430fSAndrzej Pietrasiewicz 	}
282200a2430fSAndrzej Pietrasiewicz 
282300a2430fSAndrzej Pietrasiewicz 	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
282400a2430fSAndrzej Pietrasiewicz 		return 0;
282500a2430fSAndrzej Pietrasiewicz 
28266d5c1c77SRobert Baldyga 	idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
28276d5c1c77SRobert Baldyga 	if (idx < 0)
28286d5c1c77SRobert Baldyga 		return idx;
28296d5c1c77SRobert Baldyga 
283000a2430fSAndrzej Pietrasiewicz 	ffs_ep = func->eps + idx;
283100a2430fSAndrzej Pietrasiewicz 
283200a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs_ep->descs[ep_desc_id])) {
283300a2430fSAndrzej Pietrasiewicz 		pr_err("two %sspeed descriptors for EP %d\n",
283400a2430fSAndrzej Pietrasiewicz 			  speed_names[ep_desc_id],
283500a2430fSAndrzej Pietrasiewicz 			  ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
283600a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
283700a2430fSAndrzej Pietrasiewicz 	}
283800a2430fSAndrzej Pietrasiewicz 	ffs_ep->descs[ep_desc_id] = ds;
283900a2430fSAndrzej Pietrasiewicz 
284000a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Original  ep desc", ds, ds->bLength);
284100a2430fSAndrzej Pietrasiewicz 	if (ffs_ep->ep) {
284200a2430fSAndrzej Pietrasiewicz 		ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
284300a2430fSAndrzej Pietrasiewicz 		if (!ds->wMaxPacketSize)
284400a2430fSAndrzej Pietrasiewicz 			ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
284500a2430fSAndrzej Pietrasiewicz 	} else {
284600a2430fSAndrzej Pietrasiewicz 		struct usb_request *req;
284700a2430fSAndrzej Pietrasiewicz 		struct usb_ep *ep;
28481b0bf88fSRobert Baldyga 		u8 bEndpointAddress;
2849bdcc03ceSAndrzej Pietrasiewicz 		u16 wMaxPacketSize;
285000a2430fSAndrzej Pietrasiewicz 
28511b0bf88fSRobert Baldyga 		/*
28521b0bf88fSRobert Baldyga 		 * We back up bEndpointAddress because autoconfig overwrites
28531b0bf88fSRobert Baldyga 		 * it with physical endpoint address.
28541b0bf88fSRobert Baldyga 		 */
28551b0bf88fSRobert Baldyga 		bEndpointAddress = ds->bEndpointAddress;
2856bdcc03ceSAndrzej Pietrasiewicz 		/*
2857bdcc03ceSAndrzej Pietrasiewicz 		 * We back up wMaxPacketSize because autoconfig treats
2858bdcc03ceSAndrzej Pietrasiewicz 		 * endpoint descriptors as if they were full speed.
2859bdcc03ceSAndrzej Pietrasiewicz 		 */
2860bdcc03ceSAndrzej Pietrasiewicz 		wMaxPacketSize = ds->wMaxPacketSize;
286100a2430fSAndrzej Pietrasiewicz 		pr_vdebug("autoconfig\n");
286200a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(func->gadget, ds);
286300a2430fSAndrzej Pietrasiewicz 		if (unlikely(!ep))
286400a2430fSAndrzej Pietrasiewicz 			return -ENOTSUPP;
286500a2430fSAndrzej Pietrasiewicz 		ep->driver_data = func->eps + idx;
286600a2430fSAndrzej Pietrasiewicz 
286700a2430fSAndrzej Pietrasiewicz 		req = usb_ep_alloc_request(ep, GFP_KERNEL);
286800a2430fSAndrzej Pietrasiewicz 		if (unlikely(!req))
286900a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
287000a2430fSAndrzej Pietrasiewicz 
287100a2430fSAndrzej Pietrasiewicz 		ffs_ep->ep  = ep;
287200a2430fSAndrzej Pietrasiewicz 		ffs_ep->req = req;
287300a2430fSAndrzej Pietrasiewicz 		func->eps_revmap[ds->bEndpointAddress &
287400a2430fSAndrzej Pietrasiewicz 				 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
28751b0bf88fSRobert Baldyga 		/*
28761b0bf88fSRobert Baldyga 		 * If we use virtual address mapping, we restore
28771b0bf88fSRobert Baldyga 		 * original bEndpointAddress value.
28781b0bf88fSRobert Baldyga 		 */
28791b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
28801b0bf88fSRobert Baldyga 			ds->bEndpointAddress = bEndpointAddress;
2881bdcc03ceSAndrzej Pietrasiewicz 		/*
2882bdcc03ceSAndrzej Pietrasiewicz 		 * Restore wMaxPacketSize which was potentially
2883bdcc03ceSAndrzej Pietrasiewicz 		 * overwritten by autoconfig.
2884bdcc03ceSAndrzej Pietrasiewicz 		 */
2885bdcc03ceSAndrzej Pietrasiewicz 		ds->wMaxPacketSize = wMaxPacketSize;
288600a2430fSAndrzej Pietrasiewicz 	}
288700a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
288800a2430fSAndrzej Pietrasiewicz 
288900a2430fSAndrzej Pietrasiewicz 	return 0;
289000a2430fSAndrzej Pietrasiewicz }
289100a2430fSAndrzej Pietrasiewicz 
289200a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
289300a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
289400a2430fSAndrzej Pietrasiewicz 				   void *priv)
289500a2430fSAndrzej Pietrasiewicz {
289600a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
289700a2430fSAndrzej Pietrasiewicz 	unsigned idx;
289800a2430fSAndrzej Pietrasiewicz 	u8 newValue;
289900a2430fSAndrzej Pietrasiewicz 
290000a2430fSAndrzej Pietrasiewicz 	switch (type) {
290100a2430fSAndrzej Pietrasiewicz 	default:
290200a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
290300a2430fSAndrzej Pietrasiewicz 		/* Handled in previous pass by __ffs_func_bind_do_descs() */
290400a2430fSAndrzej Pietrasiewicz 		return 0;
290500a2430fSAndrzej Pietrasiewicz 
290600a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
290700a2430fSAndrzej Pietrasiewicz 		idx = *valuep;
290800a2430fSAndrzej Pietrasiewicz 		if (func->interfaces_nums[idx] < 0) {
290900a2430fSAndrzej Pietrasiewicz 			int id = usb_interface_id(func->conf, &func->function);
291000a2430fSAndrzej Pietrasiewicz 			if (unlikely(id < 0))
291100a2430fSAndrzej Pietrasiewicz 				return id;
291200a2430fSAndrzej Pietrasiewicz 			func->interfaces_nums[idx] = id;
291300a2430fSAndrzej Pietrasiewicz 		}
291400a2430fSAndrzej Pietrasiewicz 		newValue = func->interfaces_nums[idx];
291500a2430fSAndrzej Pietrasiewicz 		break;
291600a2430fSAndrzej Pietrasiewicz 
291700a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
291800a2430fSAndrzej Pietrasiewicz 		/* String' IDs are allocated when fsf_data is bound to cdev */
291900a2430fSAndrzej Pietrasiewicz 		newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
292000a2430fSAndrzej Pietrasiewicz 		break;
292100a2430fSAndrzej Pietrasiewicz 
292200a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
292300a2430fSAndrzej Pietrasiewicz 		/*
292400a2430fSAndrzej Pietrasiewicz 		 * USB_DT_ENDPOINT are handled in
292500a2430fSAndrzej Pietrasiewicz 		 * __ffs_func_bind_do_descs().
292600a2430fSAndrzej Pietrasiewicz 		 */
292700a2430fSAndrzej Pietrasiewicz 		if (desc->bDescriptorType == USB_DT_ENDPOINT)
292800a2430fSAndrzej Pietrasiewicz 			return 0;
292900a2430fSAndrzej Pietrasiewicz 
293000a2430fSAndrzej Pietrasiewicz 		idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
293100a2430fSAndrzej Pietrasiewicz 		if (unlikely(!func->eps[idx].ep))
293200a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
293300a2430fSAndrzej Pietrasiewicz 
293400a2430fSAndrzej Pietrasiewicz 		{
293500a2430fSAndrzej Pietrasiewicz 			struct usb_endpoint_descriptor **descs;
293600a2430fSAndrzej Pietrasiewicz 			descs = func->eps[idx].descs;
293700a2430fSAndrzej Pietrasiewicz 			newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
293800a2430fSAndrzej Pietrasiewicz 		}
293900a2430fSAndrzej Pietrasiewicz 		break;
294000a2430fSAndrzej Pietrasiewicz 	}
294100a2430fSAndrzej Pietrasiewicz 
294200a2430fSAndrzej Pietrasiewicz 	pr_vdebug("%02x -> %02x\n", *valuep, newValue);
294300a2430fSAndrzej Pietrasiewicz 	*valuep = newValue;
294400a2430fSAndrzej Pietrasiewicz 	return 0;
294500a2430fSAndrzej Pietrasiewicz }
294600a2430fSAndrzej Pietrasiewicz 
294700a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
294800a2430fSAndrzej Pietrasiewicz 				      struct usb_os_desc_header *h, void *data,
294900a2430fSAndrzej Pietrasiewicz 				      unsigned len, void *priv)
295000a2430fSAndrzej Pietrasiewicz {
295100a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
295200a2430fSAndrzej Pietrasiewicz 	u8 length = 0;
295300a2430fSAndrzej Pietrasiewicz 
295400a2430fSAndrzej Pietrasiewicz 	switch (type) {
295500a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
295600a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *desc = data;
295700a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
295800a2430fSAndrzej Pietrasiewicz 
295900a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
296000a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
296100a2430fSAndrzej Pietrasiewicz 		memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
296200a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->CompatibleID) +
296300a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->SubCompatibleID));
296400a2430fSAndrzej Pietrasiewicz 		length = sizeof(*desc);
296500a2430fSAndrzej Pietrasiewicz 	}
296600a2430fSAndrzej Pietrasiewicz 		break;
296700a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
296800a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *desc = data;
296900a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
297000a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_ext_prop *ext_prop;
297100a2430fSAndrzej Pietrasiewicz 		char *ext_prop_name;
297200a2430fSAndrzej Pietrasiewicz 		char *ext_prop_data;
297300a2430fSAndrzej Pietrasiewicz 
297400a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[h->interface];
297500a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[h->interface];
297600a2430fSAndrzej Pietrasiewicz 
297700a2430fSAndrzej Pietrasiewicz 		ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
297800a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
297900a2430fSAndrzej Pietrasiewicz 
298000a2430fSAndrzej Pietrasiewicz 		ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
298100a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2982c40619bbSVincent Pelletier 		ext_prop->data_len = le32_to_cpu(*(__le32 *)
298300a2430fSAndrzej Pietrasiewicz 			usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
298400a2430fSAndrzej Pietrasiewicz 		length = ext_prop->name_len + ext_prop->data_len + 14;
298500a2430fSAndrzej Pietrasiewicz 
298600a2430fSAndrzej Pietrasiewicz 		ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
298700a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_name_avail +=
298800a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len;
298900a2430fSAndrzej Pietrasiewicz 
299000a2430fSAndrzej Pietrasiewicz 		ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
299100a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_data_avail +=
299200a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len;
299300a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_data,
299400a2430fSAndrzej Pietrasiewicz 		       usb_ext_prop_data_ptr(data, ext_prop->name_len),
299500a2430fSAndrzej Pietrasiewicz 		       ext_prop->data_len);
299600a2430fSAndrzej Pietrasiewicz 		/* unicode data reported to the host as "WCHAR"s */
299700a2430fSAndrzej Pietrasiewicz 		switch (ext_prop->type) {
299800a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE:
299900a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_ENV:
300000a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_LINK:
300100a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_MULTI:
300200a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len *= 2;
300300a2430fSAndrzej Pietrasiewicz 			break;
300400a2430fSAndrzej Pietrasiewicz 		}
300500a2430fSAndrzej Pietrasiewicz 		ext_prop->data = ext_prop_data;
300600a2430fSAndrzej Pietrasiewicz 
300700a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
300800a2430fSAndrzej Pietrasiewicz 		       ext_prop->name_len);
300900a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
301000a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len *= 2;
301100a2430fSAndrzej Pietrasiewicz 		ext_prop->name = ext_prop_name;
301200a2430fSAndrzej Pietrasiewicz 
301300a2430fSAndrzej Pietrasiewicz 		t->os_desc->ext_prop_len +=
301400a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len + ext_prop->data_len + 14;
301500a2430fSAndrzej Pietrasiewicz 		++t->os_desc->ext_prop_count;
301600a2430fSAndrzej Pietrasiewicz 		list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
301700a2430fSAndrzej Pietrasiewicz 	}
301800a2430fSAndrzej Pietrasiewicz 		break;
301900a2430fSAndrzej Pietrasiewicz 	default:
302000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
302100a2430fSAndrzej Pietrasiewicz 	}
302200a2430fSAndrzej Pietrasiewicz 
302300a2430fSAndrzej Pietrasiewicz 	return length;
302400a2430fSAndrzej Pietrasiewicz }
302500a2430fSAndrzej Pietrasiewicz 
302600a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
302700a2430fSAndrzej Pietrasiewicz 						struct usb_configuration *c)
302800a2430fSAndrzej Pietrasiewicz {
302900a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
303000a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts =
303100a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
303200a2430fSAndrzej Pietrasiewicz 	int ret;
303300a2430fSAndrzej Pietrasiewicz 
303400a2430fSAndrzej Pietrasiewicz 	ENTER();
303500a2430fSAndrzej Pietrasiewicz 
303600a2430fSAndrzej Pietrasiewicz 	/*
303700a2430fSAndrzej Pietrasiewicz 	 * Legacy gadget triggers binding in functionfs_ready_callback,
303800a2430fSAndrzej Pietrasiewicz 	 * which already uses locking; taking the same lock here would
303900a2430fSAndrzej Pietrasiewicz 	 * cause a deadlock.
304000a2430fSAndrzej Pietrasiewicz 	 *
304100a2430fSAndrzej Pietrasiewicz 	 * Configfs-enabled gadgets however do need ffs_dev_lock.
304200a2430fSAndrzej Pietrasiewicz 	 */
304300a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
304400a2430fSAndrzej Pietrasiewicz 		ffs_dev_lock();
304500a2430fSAndrzej Pietrasiewicz 	ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
304600a2430fSAndrzej Pietrasiewicz 	func->ffs = ffs_opts->dev->ffs_data;
304700a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
304800a2430fSAndrzej Pietrasiewicz 		ffs_dev_unlock();
304900a2430fSAndrzej Pietrasiewicz 	if (ret)
305000a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
305100a2430fSAndrzej Pietrasiewicz 
305200a2430fSAndrzej Pietrasiewicz 	func->conf = c;
305300a2430fSAndrzej Pietrasiewicz 	func->gadget = c->cdev->gadget;
305400a2430fSAndrzej Pietrasiewicz 
305500a2430fSAndrzej Pietrasiewicz 	/*
305600a2430fSAndrzej Pietrasiewicz 	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
305700a2430fSAndrzej Pietrasiewicz 	 * configurations are bound in sequence with list_for_each_entry,
305800a2430fSAndrzej Pietrasiewicz 	 * in each configuration its functions are bound in sequence
305900a2430fSAndrzej Pietrasiewicz 	 * with list_for_each_entry, so we assume no race condition
306000a2430fSAndrzej Pietrasiewicz 	 * with regard to ffs_opts->bound access
306100a2430fSAndrzej Pietrasiewicz 	 */
306200a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->refcnt) {
306300a2430fSAndrzej Pietrasiewicz 		ret = functionfs_bind(func->ffs, c->cdev);
306400a2430fSAndrzej Pietrasiewicz 		if (ret)
306500a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
306600a2430fSAndrzej Pietrasiewicz 	}
306700a2430fSAndrzej Pietrasiewicz 	ffs_opts->refcnt++;
306800a2430fSAndrzej Pietrasiewicz 	func->function.strings = func->ffs->stringtabs;
306900a2430fSAndrzej Pietrasiewicz 
307000a2430fSAndrzej Pietrasiewicz 	return ffs_opts;
307100a2430fSAndrzej Pietrasiewicz }
307200a2430fSAndrzej Pietrasiewicz 
307300a2430fSAndrzej Pietrasiewicz static int _ffs_func_bind(struct usb_configuration *c,
307400a2430fSAndrzej Pietrasiewicz 			  struct usb_function *f)
307500a2430fSAndrzej Pietrasiewicz {
307600a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
307700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
307800a2430fSAndrzej Pietrasiewicz 
307900a2430fSAndrzej Pietrasiewicz 	const int full = !!func->ffs->fs_descs_count;
30806cf439e0SJack Pham 	const int high = !!func->ffs->hs_descs_count;
30816cf439e0SJack Pham 	const int super = !!func->ffs->ss_descs_count;
308200a2430fSAndrzej Pietrasiewicz 
308300a2430fSAndrzej Pietrasiewicz 	int fs_len, hs_len, ss_len, ret, i;
30840015f915SDan Carpenter 	struct ffs_ep *eps_ptr;
308500a2430fSAndrzej Pietrasiewicz 
308600a2430fSAndrzej Pietrasiewicz 	/* Make it a single chunk, less management later on */
308700a2430fSAndrzej Pietrasiewicz 	vla_group(d);
308800a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
308900a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
309000a2430fSAndrzej Pietrasiewicz 		full ? ffs->fs_descs_count + 1 : 0);
309100a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
309200a2430fSAndrzej Pietrasiewicz 		high ? ffs->hs_descs_count + 1 : 0);
309300a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
309400a2430fSAndrzej Pietrasiewicz 		super ? ffs->ss_descs_count + 1 : 0);
309500a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, short, inums, ffs->interfaces_count);
309600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
309700a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
309800a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char[16], ext_compat,
309900a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
310000a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc, os_desc,
310100a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
310200a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
310300a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_count);
310400a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_name,
310500a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_name_len);
310600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_data,
310700a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_data_len);
310800a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
310900a2430fSAndrzej Pietrasiewicz 	char *vlabuf;
311000a2430fSAndrzej Pietrasiewicz 
311100a2430fSAndrzej Pietrasiewicz 	ENTER();
311200a2430fSAndrzej Pietrasiewicz 
311300a2430fSAndrzej Pietrasiewicz 	/* Has descriptors only for speeds gadget does not support */
311400a2430fSAndrzej Pietrasiewicz 	if (unlikely(!(full | high | super)))
311500a2430fSAndrzej Pietrasiewicz 		return -ENOTSUPP;
311600a2430fSAndrzej Pietrasiewicz 
311700a2430fSAndrzej Pietrasiewicz 	/* Allocate a single chunk, less management later on */
311800a2430fSAndrzej Pietrasiewicz 	vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
311900a2430fSAndrzej Pietrasiewicz 	if (unlikely(!vlabuf))
312000a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
312100a2430fSAndrzej Pietrasiewicz 
312200a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
312300a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_name_avail =
312400a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_name);
312500a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_data_avail =
312600a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_data);
312700a2430fSAndrzej Pietrasiewicz 
312800a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors  */
312900a2430fSAndrzej Pietrasiewicz 	memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
313000a2430fSAndrzej Pietrasiewicz 	       ffs->raw_descs_length);
313100a2430fSAndrzej Pietrasiewicz 
313200a2430fSAndrzej Pietrasiewicz 	memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
31330015f915SDan Carpenter 	eps_ptr = vla_ptr(vlabuf, d, eps);
31340015f915SDan Carpenter 	for (i = 0; i < ffs->eps_count; i++)
31350015f915SDan Carpenter 		eps_ptr[i].num = -1;
313600a2430fSAndrzej Pietrasiewicz 
313700a2430fSAndrzej Pietrasiewicz 	/* Save pointers
313800a2430fSAndrzej Pietrasiewicz 	 * d_eps == vlabuf, func->eps used to kfree vlabuf later
313900a2430fSAndrzej Pietrasiewicz 	*/
314000a2430fSAndrzej Pietrasiewicz 	func->eps             = vla_ptr(vlabuf, d, eps);
314100a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = vla_ptr(vlabuf, d, inums);
314200a2430fSAndrzej Pietrasiewicz 
314300a2430fSAndrzej Pietrasiewicz 	/*
314400a2430fSAndrzej Pietrasiewicz 	 * Go through all the endpoint descriptors and allocate
314500a2430fSAndrzej Pietrasiewicz 	 * endpoints first, so that later we can rewrite the endpoint
314600a2430fSAndrzej Pietrasiewicz 	 * numbers without worrying that it may be described later on.
314700a2430fSAndrzej Pietrasiewicz 	 */
314800a2430fSAndrzej Pietrasiewicz 	if (likely(full)) {
314900a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
315000a2430fSAndrzej Pietrasiewicz 		fs_len = ffs_do_descs(ffs->fs_descs_count,
315100a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs),
315200a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz,
315300a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
315400a2430fSAndrzej Pietrasiewicz 		if (unlikely(fs_len < 0)) {
315500a2430fSAndrzej Pietrasiewicz 			ret = fs_len;
315600a2430fSAndrzej Pietrasiewicz 			goto error;
315700a2430fSAndrzej Pietrasiewicz 		}
315800a2430fSAndrzej Pietrasiewicz 	} else {
315900a2430fSAndrzej Pietrasiewicz 		fs_len = 0;
316000a2430fSAndrzej Pietrasiewicz 	}
316100a2430fSAndrzej Pietrasiewicz 
316200a2430fSAndrzej Pietrasiewicz 	if (likely(high)) {
316300a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
316400a2430fSAndrzej Pietrasiewicz 		hs_len = ffs_do_descs(ffs->hs_descs_count,
316500a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) + fs_len,
316600a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz - fs_len,
316700a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
316800a2430fSAndrzej Pietrasiewicz 		if (unlikely(hs_len < 0)) {
316900a2430fSAndrzej Pietrasiewicz 			ret = hs_len;
317000a2430fSAndrzej Pietrasiewicz 			goto error;
317100a2430fSAndrzej Pietrasiewicz 		}
317200a2430fSAndrzej Pietrasiewicz 	} else {
317300a2430fSAndrzej Pietrasiewicz 		hs_len = 0;
317400a2430fSAndrzej Pietrasiewicz 	}
317500a2430fSAndrzej Pietrasiewicz 
317600a2430fSAndrzej Pietrasiewicz 	if (likely(super)) {
317700a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
317800a2430fSAndrzej Pietrasiewicz 		ss_len = ffs_do_descs(ffs->ss_descs_count,
317900a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
318000a2430fSAndrzej Pietrasiewicz 				d_raw_descs__sz - fs_len - hs_len,
318100a2430fSAndrzej Pietrasiewicz 				__ffs_func_bind_do_descs, func);
318200a2430fSAndrzej Pietrasiewicz 		if (unlikely(ss_len < 0)) {
318300a2430fSAndrzej Pietrasiewicz 			ret = ss_len;
318400a2430fSAndrzej Pietrasiewicz 			goto error;
318500a2430fSAndrzej Pietrasiewicz 		}
318600a2430fSAndrzej Pietrasiewicz 	} else {
318700a2430fSAndrzej Pietrasiewicz 		ss_len = 0;
318800a2430fSAndrzej Pietrasiewicz 	}
318900a2430fSAndrzej Pietrasiewicz 
319000a2430fSAndrzej Pietrasiewicz 	/*
319100a2430fSAndrzej Pietrasiewicz 	 * Now handle interface numbers allocation and interface and
319200a2430fSAndrzej Pietrasiewicz 	 * endpoint numbers rewriting.  We can do that in one go
319300a2430fSAndrzej Pietrasiewicz 	 * now.
319400a2430fSAndrzej Pietrasiewicz 	 */
319500a2430fSAndrzej Pietrasiewicz 	ret = ffs_do_descs(ffs->fs_descs_count +
319600a2430fSAndrzej Pietrasiewicz 			   (high ? ffs->hs_descs_count : 0) +
319700a2430fSAndrzej Pietrasiewicz 			   (super ? ffs->ss_descs_count : 0),
319800a2430fSAndrzej Pietrasiewicz 			   vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
319900a2430fSAndrzej Pietrasiewicz 			   __ffs_func_bind_do_nums, func);
320000a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
320100a2430fSAndrzej Pietrasiewicz 		goto error;
320200a2430fSAndrzej Pietrasiewicz 
320300a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
3204c6010c8bSJim Lin 	if (c->cdev->use_os_string) {
320500a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ffs->interfaces_count; ++i) {
320600a2430fSAndrzej Pietrasiewicz 			struct usb_os_desc *desc;
320700a2430fSAndrzej Pietrasiewicz 
320800a2430fSAndrzej Pietrasiewicz 			desc = func->function.os_desc_table[i].os_desc =
320900a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, os_desc) +
321000a2430fSAndrzej Pietrasiewicz 				i * sizeof(struct usb_os_desc);
321100a2430fSAndrzej Pietrasiewicz 			desc->ext_compat_id =
321200a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, ext_compat) + i * 16;
321300a2430fSAndrzej Pietrasiewicz 			INIT_LIST_HEAD(&desc->ext_prop);
321400a2430fSAndrzej Pietrasiewicz 		}
321500a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(ffs->ms_os_descs_count,
321600a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) +
321700a2430fSAndrzej Pietrasiewicz 				      fs_len + hs_len + ss_len,
3218c6010c8bSJim Lin 				      d_raw_descs__sz - fs_len - hs_len -
3219c6010c8bSJim Lin 				      ss_len,
322000a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_os_desc, func);
322100a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
322200a2430fSAndrzej Pietrasiewicz 			goto error;
3223c6010c8bSJim Lin 	}
322400a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_n =
322500a2430fSAndrzej Pietrasiewicz 		c->cdev->use_os_string ? ffs->interfaces_count : 0;
322600a2430fSAndrzej Pietrasiewicz 
322700a2430fSAndrzej Pietrasiewicz 	/* And we're done */
322800a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_BIND);
322900a2430fSAndrzej Pietrasiewicz 	return 0;
323000a2430fSAndrzej Pietrasiewicz 
323100a2430fSAndrzej Pietrasiewicz error:
323200a2430fSAndrzej Pietrasiewicz 	/* XXX Do we need to release all claimed endpoints here? */
323300a2430fSAndrzej Pietrasiewicz 	return ret;
323400a2430fSAndrzej Pietrasiewicz }
323500a2430fSAndrzej Pietrasiewicz 
323600a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *c,
323700a2430fSAndrzej Pietrasiewicz 			 struct usb_function *f)
323800a2430fSAndrzej Pietrasiewicz {
323900a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
324055d81121SRobert Baldyga 	struct ffs_function *func = ffs_func_from_usb(f);
324155d81121SRobert Baldyga 	int ret;
324200a2430fSAndrzej Pietrasiewicz 
324300a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_opts))
324400a2430fSAndrzej Pietrasiewicz 		return PTR_ERR(ffs_opts);
324500a2430fSAndrzej Pietrasiewicz 
324655d81121SRobert Baldyga 	ret = _ffs_func_bind(c, f);
324755d81121SRobert Baldyga 	if (ret && !--ffs_opts->refcnt)
324855d81121SRobert Baldyga 		functionfs_unbind(func->ffs);
324955d81121SRobert Baldyga 
325055d81121SRobert Baldyga 	return ret;
325100a2430fSAndrzej Pietrasiewicz }
325200a2430fSAndrzej Pietrasiewicz 
325300a2430fSAndrzej Pietrasiewicz 
325400a2430fSAndrzej Pietrasiewicz /* Other USB function hooks *************************************************/
325500a2430fSAndrzej Pietrasiewicz 
325618d6b32fSRobert Baldyga static void ffs_reset_work(struct work_struct *work)
325718d6b32fSRobert Baldyga {
325818d6b32fSRobert Baldyga 	struct ffs_data *ffs = container_of(work,
325918d6b32fSRobert Baldyga 		struct ffs_data, reset_work);
326018d6b32fSRobert Baldyga 	ffs_data_reset(ffs);
326118d6b32fSRobert Baldyga }
326218d6b32fSRobert Baldyga 
326300a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *f,
326400a2430fSAndrzej Pietrasiewicz 			    unsigned interface, unsigned alt)
326500a2430fSAndrzej Pietrasiewicz {
326600a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
326700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
326800a2430fSAndrzej Pietrasiewicz 	int ret = 0, intf;
326900a2430fSAndrzej Pietrasiewicz 
327000a2430fSAndrzej Pietrasiewicz 	if (alt != (unsigned)-1) {
327100a2430fSAndrzej Pietrasiewicz 		intf = ffs_func_revmap_intf(func, interface);
327200a2430fSAndrzej Pietrasiewicz 		if (unlikely(intf < 0))
327300a2430fSAndrzej Pietrasiewicz 			return intf;
327400a2430fSAndrzej Pietrasiewicz 	}
327500a2430fSAndrzej Pietrasiewicz 
327600a2430fSAndrzej Pietrasiewicz 	if (ffs->func)
327700a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(ffs->func);
327800a2430fSAndrzej Pietrasiewicz 
327918d6b32fSRobert Baldyga 	if (ffs->state == FFS_DEACTIVATED) {
328018d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
328118d6b32fSRobert Baldyga 		INIT_WORK(&ffs->reset_work, ffs_reset_work);
328218d6b32fSRobert Baldyga 		schedule_work(&ffs->reset_work);
328318d6b32fSRobert Baldyga 		return -ENODEV;
328418d6b32fSRobert Baldyga 	}
328518d6b32fSRobert Baldyga 
328600a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
328700a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
328800a2430fSAndrzej Pietrasiewicz 
328900a2430fSAndrzej Pietrasiewicz 	if (alt == (unsigned)-1) {
329000a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
329100a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_DISABLE);
329200a2430fSAndrzej Pietrasiewicz 		return 0;
329300a2430fSAndrzej Pietrasiewicz 	}
329400a2430fSAndrzej Pietrasiewicz 
329500a2430fSAndrzej Pietrasiewicz 	ffs->func = func;
329600a2430fSAndrzej Pietrasiewicz 	ret = ffs_func_eps_enable(func);
329700a2430fSAndrzej Pietrasiewicz 	if (likely(ret >= 0))
329800a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_ENABLE);
329900a2430fSAndrzej Pietrasiewicz 	return ret;
330000a2430fSAndrzej Pietrasiewicz }
330100a2430fSAndrzej Pietrasiewicz 
330200a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *f)
330300a2430fSAndrzej Pietrasiewicz {
330400a2430fSAndrzej Pietrasiewicz 	ffs_func_set_alt(f, 0, (unsigned)-1);
330500a2430fSAndrzej Pietrasiewicz }
330600a2430fSAndrzej Pietrasiewicz 
330700a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *f,
330800a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *creq)
330900a2430fSAndrzej Pietrasiewicz {
331000a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
331100a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
331200a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
331300a2430fSAndrzej Pietrasiewicz 	int ret;
331400a2430fSAndrzej Pietrasiewicz 
331500a2430fSAndrzej Pietrasiewicz 	ENTER();
331600a2430fSAndrzej Pietrasiewicz 
331700a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
331800a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequest     = %02x\n", creq->bRequest);
331900a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wValue       = %04x\n", le16_to_cpu(creq->wValue));
332000a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wIndex       = %04x\n", le16_to_cpu(creq->wIndex));
332100a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wLength      = %04x\n", le16_to_cpu(creq->wLength));
332200a2430fSAndrzej Pietrasiewicz 
332300a2430fSAndrzej Pietrasiewicz 	/*
332400a2430fSAndrzej Pietrasiewicz 	 * Most requests directed to interface go through here
332500a2430fSAndrzej Pietrasiewicz 	 * (notable exceptions are set/get interface) so we need to
332600a2430fSAndrzej Pietrasiewicz 	 * handle them.  All other either handled by composite or
332700a2430fSAndrzej Pietrasiewicz 	 * passed to usb_configuration->setup() (if one is set).  No
332800a2430fSAndrzej Pietrasiewicz 	 * matter, we will handle requests directed to endpoint here
332954dfce6dSFelix Hädicke 	 * as well (as it's straightforward).  Other request recipient
333054dfce6dSFelix Hädicke 	 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
333154dfce6dSFelix Hädicke 	 * is being used.
333200a2430fSAndrzej Pietrasiewicz 	 */
333300a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
333400a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
333500a2430fSAndrzej Pietrasiewicz 
333600a2430fSAndrzej Pietrasiewicz 	switch (creq->bRequestType & USB_RECIP_MASK) {
333700a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_INTERFACE:
333800a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
333900a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
334000a2430fSAndrzej Pietrasiewicz 			return ret;
334100a2430fSAndrzej Pietrasiewicz 		break;
334200a2430fSAndrzej Pietrasiewicz 
334300a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_ENDPOINT:
334400a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
334500a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
334600a2430fSAndrzej Pietrasiewicz 			return ret;
33471b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
33481b0bf88fSRobert Baldyga 			ret = func->ffs->eps_addrmap[ret];
334900a2430fSAndrzej Pietrasiewicz 		break;
335000a2430fSAndrzej Pietrasiewicz 
335100a2430fSAndrzej Pietrasiewicz 	default:
335254dfce6dSFelix Hädicke 		if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
335354dfce6dSFelix Hädicke 			ret = le16_to_cpu(creq->wIndex);
335454dfce6dSFelix Hädicke 		else
335500a2430fSAndrzej Pietrasiewicz 			return -EOPNOTSUPP;
335600a2430fSAndrzej Pietrasiewicz 	}
335700a2430fSAndrzej Pietrasiewicz 
335800a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
335900a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup = *creq;
336000a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup.wIndex = cpu_to_le16(ret);
336100a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, FUNCTIONFS_SETUP);
336200a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
336300a2430fSAndrzej Pietrasiewicz 
33644d644abfSJerry Zhang 	return creq->wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0;
336500a2430fSAndrzej Pietrasiewicz }
336600a2430fSAndrzej Pietrasiewicz 
336754dfce6dSFelix Hädicke static bool ffs_func_req_match(struct usb_function *f,
33681a00b457SFelix Hädicke 			       const struct usb_ctrlrequest *creq,
33691a00b457SFelix Hädicke 			       bool config0)
337054dfce6dSFelix Hädicke {
337154dfce6dSFelix Hädicke 	struct ffs_function *func = ffs_func_from_usb(f);
337254dfce6dSFelix Hädicke 
33734368c28aSFelix Hädicke 	if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
33741a00b457SFelix Hädicke 		return false;
33751a00b457SFelix Hädicke 
337654dfce6dSFelix Hädicke 	switch (creq->bRequestType & USB_RECIP_MASK) {
337754dfce6dSFelix Hädicke 	case USB_RECIP_INTERFACE:
337805e78c69SFelix Hädicke 		return (ffs_func_revmap_intf(func,
337905e78c69SFelix Hädicke 					     le16_to_cpu(creq->wIndex)) >= 0);
338054dfce6dSFelix Hädicke 	case USB_RECIP_ENDPOINT:
338105e78c69SFelix Hädicke 		return (ffs_func_revmap_ep(func,
338205e78c69SFelix Hädicke 					   le16_to_cpu(creq->wIndex)) >= 0);
338354dfce6dSFelix Hädicke 	default:
338454dfce6dSFelix Hädicke 		return (bool) (func->ffs->user_flags &
338554dfce6dSFelix Hädicke 			       FUNCTIONFS_ALL_CTRL_RECIP);
338654dfce6dSFelix Hädicke 	}
338754dfce6dSFelix Hädicke }
338854dfce6dSFelix Hädicke 
338900a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *f)
339000a2430fSAndrzej Pietrasiewicz {
339100a2430fSAndrzej Pietrasiewicz 	ENTER();
339200a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
339300a2430fSAndrzej Pietrasiewicz }
339400a2430fSAndrzej Pietrasiewicz 
339500a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *f)
339600a2430fSAndrzej Pietrasiewicz {
339700a2430fSAndrzej Pietrasiewicz 	ENTER();
339800a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
339900a2430fSAndrzej Pietrasiewicz }
340000a2430fSAndrzej Pietrasiewicz 
340100a2430fSAndrzej Pietrasiewicz 
340200a2430fSAndrzej Pietrasiewicz /* Endpoint and interface numbers reverse mapping ***************************/
340300a2430fSAndrzej Pietrasiewicz 
340400a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
340500a2430fSAndrzej Pietrasiewicz {
340600a2430fSAndrzej Pietrasiewicz 	num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
340700a2430fSAndrzej Pietrasiewicz 	return num ? num : -EDOM;
340800a2430fSAndrzej Pietrasiewicz }
340900a2430fSAndrzej Pietrasiewicz 
341000a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
341100a2430fSAndrzej Pietrasiewicz {
341200a2430fSAndrzej Pietrasiewicz 	short *nums = func->interfaces_nums;
341300a2430fSAndrzej Pietrasiewicz 	unsigned count = func->ffs->interfaces_count;
341400a2430fSAndrzej Pietrasiewicz 
341500a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++nums) {
341600a2430fSAndrzej Pietrasiewicz 		if (*nums >= 0 && *nums == intf)
341700a2430fSAndrzej Pietrasiewicz 			return nums - func->interfaces_nums;
341800a2430fSAndrzej Pietrasiewicz 	}
341900a2430fSAndrzej Pietrasiewicz 
342000a2430fSAndrzej Pietrasiewicz 	return -EDOM;
342100a2430fSAndrzej Pietrasiewicz }
342200a2430fSAndrzej Pietrasiewicz 
342300a2430fSAndrzej Pietrasiewicz 
342400a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
342500a2430fSAndrzej Pietrasiewicz 
342600a2430fSAndrzej Pietrasiewicz static LIST_HEAD(ffs_devices);
342700a2430fSAndrzej Pietrasiewicz 
342800a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_do_find_dev(const char *name)
342900a2430fSAndrzej Pietrasiewicz {
343000a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
343100a2430fSAndrzej Pietrasiewicz 
3432ea920bb4SMichal Nazarewicz 	if (!name)
3433ea920bb4SMichal Nazarewicz 		return NULL;
3434ea920bb4SMichal Nazarewicz 
343500a2430fSAndrzej Pietrasiewicz 	list_for_each_entry(dev, &ffs_devices, entry) {
343600a2430fSAndrzej Pietrasiewicz 		if (strcmp(dev->name, name) == 0)
343700a2430fSAndrzej Pietrasiewicz 			return dev;
343800a2430fSAndrzej Pietrasiewicz 	}
343900a2430fSAndrzej Pietrasiewicz 
344000a2430fSAndrzej Pietrasiewicz 	return NULL;
344100a2430fSAndrzej Pietrasiewicz }
344200a2430fSAndrzej Pietrasiewicz 
344300a2430fSAndrzej Pietrasiewicz /*
344400a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
344500a2430fSAndrzej Pietrasiewicz  */
344600a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_get_single_dev(void)
344700a2430fSAndrzej Pietrasiewicz {
344800a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
344900a2430fSAndrzej Pietrasiewicz 
345000a2430fSAndrzej Pietrasiewicz 	if (list_is_singular(&ffs_devices)) {
345100a2430fSAndrzej Pietrasiewicz 		dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
345200a2430fSAndrzej Pietrasiewicz 		if (dev->single)
345300a2430fSAndrzej Pietrasiewicz 			return dev;
345400a2430fSAndrzej Pietrasiewicz 	}
345500a2430fSAndrzej Pietrasiewicz 
345600a2430fSAndrzej Pietrasiewicz 	return NULL;
345700a2430fSAndrzej Pietrasiewicz }
345800a2430fSAndrzej Pietrasiewicz 
345900a2430fSAndrzej Pietrasiewicz /*
346000a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
346100a2430fSAndrzej Pietrasiewicz  */
346200a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name)
346300a2430fSAndrzej Pietrasiewicz {
346400a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
346500a2430fSAndrzej Pietrasiewicz 
346600a2430fSAndrzej Pietrasiewicz 	dev = _ffs_get_single_dev();
346700a2430fSAndrzej Pietrasiewicz 	if (dev)
346800a2430fSAndrzej Pietrasiewicz 		return dev;
346900a2430fSAndrzej Pietrasiewicz 
347000a2430fSAndrzej Pietrasiewicz 	return _ffs_do_find_dev(name);
347100a2430fSAndrzej Pietrasiewicz }
347200a2430fSAndrzej Pietrasiewicz 
347300a2430fSAndrzej Pietrasiewicz /* Configfs support *********************************************************/
347400a2430fSAndrzej Pietrasiewicz 
347500a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
347600a2430fSAndrzej Pietrasiewicz {
347700a2430fSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_fs_opts,
347800a2430fSAndrzej Pietrasiewicz 			    func_inst.group);
347900a2430fSAndrzej Pietrasiewicz }
348000a2430fSAndrzej Pietrasiewicz 
348100a2430fSAndrzej Pietrasiewicz static void ffs_attr_release(struct config_item *item)
348200a2430fSAndrzej Pietrasiewicz {
348300a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts = to_ffs_opts(item);
348400a2430fSAndrzej Pietrasiewicz 
348500a2430fSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
348600a2430fSAndrzej Pietrasiewicz }
348700a2430fSAndrzej Pietrasiewicz 
348800a2430fSAndrzej Pietrasiewicz static struct configfs_item_operations ffs_item_ops = {
348900a2430fSAndrzej Pietrasiewicz 	.release	= ffs_attr_release,
349000a2430fSAndrzej Pietrasiewicz };
349100a2430fSAndrzej Pietrasiewicz 
349297363902SBhumika Goyal static const struct config_item_type ffs_func_type = {
349300a2430fSAndrzej Pietrasiewicz 	.ct_item_ops	= &ffs_item_ops,
349400a2430fSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
349500a2430fSAndrzej Pietrasiewicz };
349600a2430fSAndrzej Pietrasiewicz 
349700a2430fSAndrzej Pietrasiewicz 
349800a2430fSAndrzej Pietrasiewicz /* Function registration interface ******************************************/
349900a2430fSAndrzej Pietrasiewicz 
350000a2430fSAndrzej Pietrasiewicz static void ffs_free_inst(struct usb_function_instance *f)
350100a2430fSAndrzej Pietrasiewicz {
350200a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
350300a2430fSAndrzej Pietrasiewicz 
350400a2430fSAndrzej Pietrasiewicz 	opts = to_f_fs_opts(f);
350500a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
350600a2430fSAndrzej Pietrasiewicz 	_ffs_free_dev(opts->dev);
350700a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
350800a2430fSAndrzej Pietrasiewicz 	kfree(opts);
350900a2430fSAndrzej Pietrasiewicz }
351000a2430fSAndrzej Pietrasiewicz 
351100a2430fSAndrzej Pietrasiewicz static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
351200a2430fSAndrzej Pietrasiewicz {
3513ea920bb4SMichal Nazarewicz 	if (strlen(name) >= FIELD_SIZEOF(struct ffs_dev, name))
351400a2430fSAndrzej Pietrasiewicz 		return -ENAMETOOLONG;
3515ea920bb4SMichal Nazarewicz 	return ffs_name_dev(to_f_fs_opts(fi)->dev, name);
351600a2430fSAndrzej Pietrasiewicz }
351700a2430fSAndrzej Pietrasiewicz 
351800a2430fSAndrzej Pietrasiewicz static struct usb_function_instance *ffs_alloc_inst(void)
351900a2430fSAndrzej Pietrasiewicz {
352000a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
352100a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
352200a2430fSAndrzej Pietrasiewicz 
352300a2430fSAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
352400a2430fSAndrzej Pietrasiewicz 	if (!opts)
352500a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
352600a2430fSAndrzej Pietrasiewicz 
352700a2430fSAndrzej Pietrasiewicz 	opts->func_inst.set_inst_name = ffs_set_inst_name;
352800a2430fSAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = ffs_free_inst;
352900a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
353000a2430fSAndrzej Pietrasiewicz 	dev = _ffs_alloc_dev();
353100a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
353200a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(dev)) {
353300a2430fSAndrzej Pietrasiewicz 		kfree(opts);
353400a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(dev);
353500a2430fSAndrzej Pietrasiewicz 	}
353600a2430fSAndrzej Pietrasiewicz 	opts->dev = dev;
353700a2430fSAndrzej Pietrasiewicz 	dev->opts = opts;
353800a2430fSAndrzej Pietrasiewicz 
353900a2430fSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "",
354000a2430fSAndrzej Pietrasiewicz 				    &ffs_func_type);
354100a2430fSAndrzej Pietrasiewicz 	return &opts->func_inst;
354200a2430fSAndrzej Pietrasiewicz }
354300a2430fSAndrzej Pietrasiewicz 
354400a2430fSAndrzej Pietrasiewicz static void ffs_free(struct usb_function *f)
354500a2430fSAndrzej Pietrasiewicz {
354600a2430fSAndrzej Pietrasiewicz 	kfree(ffs_func_from_usb(f));
354700a2430fSAndrzej Pietrasiewicz }
354800a2430fSAndrzej Pietrasiewicz 
354900a2430fSAndrzej Pietrasiewicz static void ffs_func_unbind(struct usb_configuration *c,
355000a2430fSAndrzej Pietrasiewicz 			    struct usb_function *f)
355100a2430fSAndrzej Pietrasiewicz {
355200a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
355300a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
355400a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts =
355500a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
355600a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep = func->eps;
355700a2430fSAndrzej Pietrasiewicz 	unsigned count = ffs->eps_count;
355800a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
355900a2430fSAndrzej Pietrasiewicz 
356000a2430fSAndrzej Pietrasiewicz 	ENTER();
356100a2430fSAndrzej Pietrasiewicz 	if (ffs->func == func) {
356200a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(func);
356300a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
356400a2430fSAndrzej Pietrasiewicz 	}
356500a2430fSAndrzej Pietrasiewicz 
356600a2430fSAndrzej Pietrasiewicz 	if (!--opts->refcnt)
356700a2430fSAndrzej Pietrasiewicz 		functionfs_unbind(ffs);
356800a2430fSAndrzej Pietrasiewicz 
356900a2430fSAndrzej Pietrasiewicz 	/* cleanup after autoconfig */
357000a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
357108f37148SVincent Pelletier 	while (count--) {
357200a2430fSAndrzej Pietrasiewicz 		if (ep->ep && ep->req)
357300a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, ep->req);
357400a2430fSAndrzej Pietrasiewicz 		ep->req = NULL;
357500a2430fSAndrzej Pietrasiewicz 		++ep;
357608f37148SVincent Pelletier 	}
357700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
357800a2430fSAndrzej Pietrasiewicz 	kfree(func->eps);
357900a2430fSAndrzej Pietrasiewicz 	func->eps = NULL;
358000a2430fSAndrzej Pietrasiewicz 	/*
358100a2430fSAndrzej Pietrasiewicz 	 * eps, descriptors and interfaces_nums are allocated in the
358200a2430fSAndrzej Pietrasiewicz 	 * same chunk so only one free is required.
358300a2430fSAndrzej Pietrasiewicz 	 */
358400a2430fSAndrzej Pietrasiewicz 	func->function.fs_descriptors = NULL;
358500a2430fSAndrzej Pietrasiewicz 	func->function.hs_descriptors = NULL;
358600a2430fSAndrzej Pietrasiewicz 	func->function.ss_descriptors = NULL;
358700a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = NULL;
358800a2430fSAndrzej Pietrasiewicz 
358900a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_UNBIND);
359000a2430fSAndrzej Pietrasiewicz }
359100a2430fSAndrzej Pietrasiewicz 
359200a2430fSAndrzej Pietrasiewicz static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
359300a2430fSAndrzej Pietrasiewicz {
359400a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func;
359500a2430fSAndrzej Pietrasiewicz 
359600a2430fSAndrzej Pietrasiewicz 	ENTER();
359700a2430fSAndrzej Pietrasiewicz 
359800a2430fSAndrzej Pietrasiewicz 	func = kzalloc(sizeof(*func), GFP_KERNEL);
359900a2430fSAndrzej Pietrasiewicz 	if (unlikely(!func))
360000a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
360100a2430fSAndrzej Pietrasiewicz 
360200a2430fSAndrzej Pietrasiewicz 	func->function.name    = "Function FS Gadget";
360300a2430fSAndrzej Pietrasiewicz 
360400a2430fSAndrzej Pietrasiewicz 	func->function.bind    = ffs_func_bind;
360500a2430fSAndrzej Pietrasiewicz 	func->function.unbind  = ffs_func_unbind;
360600a2430fSAndrzej Pietrasiewicz 	func->function.set_alt = ffs_func_set_alt;
360700a2430fSAndrzej Pietrasiewicz 	func->function.disable = ffs_func_disable;
360800a2430fSAndrzej Pietrasiewicz 	func->function.setup   = ffs_func_setup;
360954dfce6dSFelix Hädicke 	func->function.req_match = ffs_func_req_match;
361000a2430fSAndrzej Pietrasiewicz 	func->function.suspend = ffs_func_suspend;
361100a2430fSAndrzej Pietrasiewicz 	func->function.resume  = ffs_func_resume;
361200a2430fSAndrzej Pietrasiewicz 	func->function.free_func = ffs_free;
361300a2430fSAndrzej Pietrasiewicz 
361400a2430fSAndrzej Pietrasiewicz 	return &func->function;
361500a2430fSAndrzej Pietrasiewicz }
361600a2430fSAndrzej Pietrasiewicz 
361700a2430fSAndrzej Pietrasiewicz /*
361800a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
361900a2430fSAndrzej Pietrasiewicz  */
362000a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void)
362100a2430fSAndrzej Pietrasiewicz {
362200a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
362300a2430fSAndrzej Pietrasiewicz 	int ret;
362400a2430fSAndrzej Pietrasiewicz 
362500a2430fSAndrzej Pietrasiewicz 	if (_ffs_get_single_dev())
362600a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(-EBUSY);
362700a2430fSAndrzej Pietrasiewicz 
362800a2430fSAndrzej Pietrasiewicz 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
362900a2430fSAndrzej Pietrasiewicz 	if (!dev)
363000a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
363100a2430fSAndrzej Pietrasiewicz 
363200a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices)) {
363300a2430fSAndrzej Pietrasiewicz 		ret = functionfs_init();
363400a2430fSAndrzej Pietrasiewicz 		if (ret) {
363500a2430fSAndrzej Pietrasiewicz 			kfree(dev);
363600a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
363700a2430fSAndrzej Pietrasiewicz 		}
363800a2430fSAndrzej Pietrasiewicz 	}
363900a2430fSAndrzej Pietrasiewicz 
364000a2430fSAndrzej Pietrasiewicz 	list_add(&dev->entry, &ffs_devices);
364100a2430fSAndrzej Pietrasiewicz 
364200a2430fSAndrzej Pietrasiewicz 	return dev;
364300a2430fSAndrzej Pietrasiewicz }
364400a2430fSAndrzej Pietrasiewicz 
364500a2430fSAndrzej Pietrasiewicz int ffs_name_dev(struct ffs_dev *dev, const char *name)
364600a2430fSAndrzej Pietrasiewicz {
3647ea920bb4SMichal Nazarewicz 	struct ffs_dev *existing;
3648ea920bb4SMichal Nazarewicz 	int ret = 0;
364900a2430fSAndrzej Pietrasiewicz 
365000a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
3651ea920bb4SMichal Nazarewicz 
3652ea920bb4SMichal Nazarewicz 	existing = _ffs_do_find_dev(name);
3653ea920bb4SMichal Nazarewicz 	if (!existing)
3654ea920bb4SMichal Nazarewicz 		strlcpy(dev->name, name, ARRAY_SIZE(dev->name));
3655ea920bb4SMichal Nazarewicz 	else if (existing != dev)
3656ea920bb4SMichal Nazarewicz 		ret = -EBUSY;
3657ea920bb4SMichal Nazarewicz 
365800a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
365900a2430fSAndrzej Pietrasiewicz 
366000a2430fSAndrzej Pietrasiewicz 	return ret;
366100a2430fSAndrzej Pietrasiewicz }
366200a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_name_dev);
366300a2430fSAndrzej Pietrasiewicz 
366400a2430fSAndrzej Pietrasiewicz int ffs_single_dev(struct ffs_dev *dev)
366500a2430fSAndrzej Pietrasiewicz {
366600a2430fSAndrzej Pietrasiewicz 	int ret;
366700a2430fSAndrzej Pietrasiewicz 
366800a2430fSAndrzej Pietrasiewicz 	ret = 0;
366900a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
367000a2430fSAndrzej Pietrasiewicz 
367100a2430fSAndrzej Pietrasiewicz 	if (!list_is_singular(&ffs_devices))
367200a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
367300a2430fSAndrzej Pietrasiewicz 	else
367400a2430fSAndrzej Pietrasiewicz 		dev->single = true;
367500a2430fSAndrzej Pietrasiewicz 
367600a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
367700a2430fSAndrzej Pietrasiewicz 	return ret;
367800a2430fSAndrzej Pietrasiewicz }
367900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_single_dev);
368000a2430fSAndrzej Pietrasiewicz 
368100a2430fSAndrzej Pietrasiewicz /*
368200a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
368300a2430fSAndrzej Pietrasiewicz  */
368400a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev)
368500a2430fSAndrzej Pietrasiewicz {
368600a2430fSAndrzej Pietrasiewicz 	list_del(&dev->entry);
36873262ad82SJim Baxter 
36883262ad82SJim Baxter 	/* Clear the private_data pointer to stop incorrect dev access */
36893262ad82SJim Baxter 	if (dev->ffs_data)
36903262ad82SJim Baxter 		dev->ffs_data->private_data = NULL;
36913262ad82SJim Baxter 
369200a2430fSAndrzej Pietrasiewicz 	kfree(dev);
369300a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices))
369400a2430fSAndrzej Pietrasiewicz 		functionfs_cleanup();
369500a2430fSAndrzej Pietrasiewicz }
369600a2430fSAndrzej Pietrasiewicz 
369700a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name)
369800a2430fSAndrzej Pietrasiewicz {
369900a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
370000a2430fSAndrzej Pietrasiewicz 
370100a2430fSAndrzej Pietrasiewicz 	ENTER();
370200a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
370300a2430fSAndrzej Pietrasiewicz 
370400a2430fSAndrzej Pietrasiewicz 	ffs_dev = _ffs_find_dev(dev_name);
370500a2430fSAndrzej Pietrasiewicz 	if (!ffs_dev)
370600a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
370700a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->mounted)
370800a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-EBUSY);
370900a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->ffs_acquire_dev_callback &&
371000a2430fSAndrzej Pietrasiewicz 	    ffs_dev->ffs_acquire_dev_callback(ffs_dev))
371100a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
371200a2430fSAndrzej Pietrasiewicz 	else
371300a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = true;
371400a2430fSAndrzej Pietrasiewicz 
371500a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
371600a2430fSAndrzej Pietrasiewicz 	return ffs_dev;
371700a2430fSAndrzej Pietrasiewicz }
371800a2430fSAndrzej Pietrasiewicz 
371900a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data)
372000a2430fSAndrzej Pietrasiewicz {
372100a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
372200a2430fSAndrzej Pietrasiewicz 
372300a2430fSAndrzej Pietrasiewicz 	ENTER();
372400a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
372500a2430fSAndrzej Pietrasiewicz 
372600a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_data->private_data;
372700a2430fSAndrzej Pietrasiewicz 	if (ffs_dev) {
372800a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = false;
372900a2430fSAndrzej Pietrasiewicz 
373000a2430fSAndrzej Pietrasiewicz 		if (ffs_dev->ffs_release_dev_callback)
373100a2430fSAndrzej Pietrasiewicz 			ffs_dev->ffs_release_dev_callback(ffs_dev);
373200a2430fSAndrzej Pietrasiewicz 	}
373300a2430fSAndrzej Pietrasiewicz 
373400a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
373500a2430fSAndrzej Pietrasiewicz }
373600a2430fSAndrzej Pietrasiewicz 
373700a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs)
373800a2430fSAndrzej Pietrasiewicz {
373900a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
374000a2430fSAndrzej Pietrasiewicz 	int ret = 0;
374100a2430fSAndrzej Pietrasiewicz 
374200a2430fSAndrzej Pietrasiewicz 	ENTER();
374300a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
374400a2430fSAndrzej Pietrasiewicz 
374500a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
374600a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj) {
374700a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
374800a2430fSAndrzej Pietrasiewicz 		goto done;
374900a2430fSAndrzej Pietrasiewicz 	}
375000a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs_obj->desc_ready)) {
375100a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
375200a2430fSAndrzej Pietrasiewicz 		goto done;
375300a2430fSAndrzej Pietrasiewicz 	}
375400a2430fSAndrzej Pietrasiewicz 
375500a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = true;
375600a2430fSAndrzej Pietrasiewicz 	ffs_obj->ffs_data = ffs;
375700a2430fSAndrzej Pietrasiewicz 
375849a79d8bSKrzysztof Opasiak 	if (ffs_obj->ffs_ready_callback) {
375900a2430fSAndrzej Pietrasiewicz 		ret = ffs_obj->ffs_ready_callback(ffs);
376049a79d8bSKrzysztof Opasiak 		if (ret)
376149a79d8bSKrzysztof Opasiak 			goto done;
376249a79d8bSKrzysztof Opasiak 	}
376300a2430fSAndrzej Pietrasiewicz 
376449a79d8bSKrzysztof Opasiak 	set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
376500a2430fSAndrzej Pietrasiewicz done:
376600a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
376700a2430fSAndrzej Pietrasiewicz 	return ret;
376800a2430fSAndrzej Pietrasiewicz }
376900a2430fSAndrzej Pietrasiewicz 
377000a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs)
377100a2430fSAndrzej Pietrasiewicz {
377200a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
3773f14e9ad1SRui Miguel Silva 	struct f_fs_opts *opts;
3774b3ce3ce0SBaolin Wang 	struct config_item *ci;
377500a2430fSAndrzej Pietrasiewicz 
377600a2430fSAndrzej Pietrasiewicz 	ENTER();
377700a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
377800a2430fSAndrzej Pietrasiewicz 
377900a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
378000a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj)
378100a2430fSAndrzej Pietrasiewicz 		goto done;
378200a2430fSAndrzej Pietrasiewicz 
378300a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = false;
3784cdafb6d8SAndrew Gabbasov 	ffs_obj->ffs_data = NULL;
378500a2430fSAndrzej Pietrasiewicz 
378649a79d8bSKrzysztof Opasiak 	if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
378749a79d8bSKrzysztof Opasiak 	    ffs_obj->ffs_closed_callback)
378800a2430fSAndrzej Pietrasiewicz 		ffs_obj->ffs_closed_callback(ffs);
378900a2430fSAndrzej Pietrasiewicz 
3790f14e9ad1SRui Miguel Silva 	if (ffs_obj->opts)
3791f14e9ad1SRui Miguel Silva 		opts = ffs_obj->opts;
3792f14e9ad1SRui Miguel Silva 	else
3793f14e9ad1SRui Miguel Silva 		goto done;
3794f14e9ad1SRui Miguel Silva 
3795f14e9ad1SRui Miguel Silva 	if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
37962c935bc5SPeter Zijlstra 	    || !kref_read(&opts->func_inst.group.cg_item.ci_kref))
379700a2430fSAndrzej Pietrasiewicz 		goto done;
379800a2430fSAndrzej Pietrasiewicz 
3799b3ce3ce0SBaolin Wang 	ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
3800b3ce3ce0SBaolin Wang 	ffs_dev_unlock();
3801b3ce3ce0SBaolin Wang 
3802ce5bf9a5SHemant Kumar 	if (test_bit(FFS_FL_BOUND, &ffs->flags))
3803b3ce3ce0SBaolin Wang 		unregister_gadget_item(ci);
3804b3ce3ce0SBaolin Wang 	return;
380500a2430fSAndrzej Pietrasiewicz done:
380600a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
380700a2430fSAndrzej Pietrasiewicz }
380800a2430fSAndrzej Pietrasiewicz 
380900a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
381000a2430fSAndrzej Pietrasiewicz 
381100a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
381200a2430fSAndrzej Pietrasiewicz {
381300a2430fSAndrzej Pietrasiewicz 	return nonblock
381400a2430fSAndrzej Pietrasiewicz 		? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
381500a2430fSAndrzej Pietrasiewicz 		: mutex_lock_interruptible(mutex);
381600a2430fSAndrzej Pietrasiewicz }
381700a2430fSAndrzej Pietrasiewicz 
381800a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
381900a2430fSAndrzej Pietrasiewicz {
382000a2430fSAndrzej Pietrasiewicz 	char *data;
382100a2430fSAndrzej Pietrasiewicz 
382200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!len))
382300a2430fSAndrzej Pietrasiewicz 		return NULL;
382400a2430fSAndrzej Pietrasiewicz 
382500a2430fSAndrzej Pietrasiewicz 	data = kmalloc(len, GFP_KERNEL);
382600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!data))
382700a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
382800a2430fSAndrzej Pietrasiewicz 
38297fe9a937SDaniel Walter 	if (unlikely(copy_from_user(data, buf, len))) {
383000a2430fSAndrzej Pietrasiewicz 		kfree(data);
383100a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-EFAULT);
383200a2430fSAndrzej Pietrasiewicz 	}
383300a2430fSAndrzej Pietrasiewicz 
383400a2430fSAndrzej Pietrasiewicz 	pr_vdebug("Buffer from user space:\n");
383500a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem("", data, len);
383600a2430fSAndrzej Pietrasiewicz 
383700a2430fSAndrzej Pietrasiewicz 	return data;
383800a2430fSAndrzej Pietrasiewicz }
383900a2430fSAndrzej Pietrasiewicz 
384000a2430fSAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
384100a2430fSAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
384200a2430fSAndrzej Pietrasiewicz MODULE_AUTHOR("Michal Nazarewicz");
3843