xref: /openbmc/linux/drivers/usb/gadget/function/f_fs.c (revision 2bfa0719)
100a2430fSAndrzej Pietrasiewicz /*
200a2430fSAndrzej Pietrasiewicz  * f_fs.c -- user mode file system API for USB composite function controllers
300a2430fSAndrzej Pietrasiewicz  *
400a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2010 Samsung Electronics
500a2430fSAndrzej Pietrasiewicz  * Author: Michal Nazarewicz <mina86@mina86.com>
600a2430fSAndrzej Pietrasiewicz  *
700a2430fSAndrzej Pietrasiewicz  * Based on inode.c (GadgetFS) which was:
800a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2003-2004 David Brownell
900a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2003 Agilent Technologies
1000a2430fSAndrzej Pietrasiewicz  *
1100a2430fSAndrzej Pietrasiewicz  * This program is free software; you can redistribute it and/or modify
1200a2430fSAndrzej Pietrasiewicz  * it under the terms of the GNU General Public License as published by
1300a2430fSAndrzej Pietrasiewicz  * the Free Software Foundation; either version 2 of the License, or
1400a2430fSAndrzej Pietrasiewicz  * (at your option) any later version.
1500a2430fSAndrzej Pietrasiewicz  */
1600a2430fSAndrzej Pietrasiewicz 
1700a2430fSAndrzej Pietrasiewicz 
1800a2430fSAndrzej Pietrasiewicz /* #define DEBUG */
1900a2430fSAndrzej Pietrasiewicz /* #define VERBOSE_DEBUG */
2000a2430fSAndrzej Pietrasiewicz 
2100a2430fSAndrzej Pietrasiewicz #include <linux/blkdev.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/pagemap.h>
2300a2430fSAndrzej Pietrasiewicz #include <linux/export.h>
2400a2430fSAndrzej Pietrasiewicz #include <linux/hid.h>
2500a2430fSAndrzej Pietrasiewicz #include <linux/module.h>
26174cd4b1SIngo Molnar #include <linux/sched/signal.h>
27e2e40f2cSChristoph Hellwig #include <linux/uio.h>
2800a2430fSAndrzej Pietrasiewicz #include <asm/unaligned.h>
2900a2430fSAndrzej Pietrasiewicz 
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. */
4900a2430fSAndrzej Pietrasiewicz static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
5000a2430fSAndrzej Pietrasiewicz 
5100a2430fSAndrzej Pietrasiewicz /* Opened counter handling. */
5200a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs);
5300a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs);
5400a2430fSAndrzej Pietrasiewicz 
5500a2430fSAndrzej Pietrasiewicz /* Called with ffs->mutex held; take over ownership of data. */
5600a2430fSAndrzej Pietrasiewicz static int __must_check
5700a2430fSAndrzej Pietrasiewicz __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
5800a2430fSAndrzej Pietrasiewicz static int __must_check
5900a2430fSAndrzej Pietrasiewicz __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
6000a2430fSAndrzej Pietrasiewicz 
6100a2430fSAndrzej Pietrasiewicz 
6200a2430fSAndrzej Pietrasiewicz /* The function structure ***************************************************/
6300a2430fSAndrzej Pietrasiewicz 
6400a2430fSAndrzej Pietrasiewicz struct ffs_ep;
6500a2430fSAndrzej Pietrasiewicz 
6600a2430fSAndrzej Pietrasiewicz struct ffs_function {
6700a2430fSAndrzej Pietrasiewicz 	struct usb_configuration	*conf;
6800a2430fSAndrzej Pietrasiewicz 	struct usb_gadget		*gadget;
6900a2430fSAndrzej Pietrasiewicz 	struct ffs_data			*ffs;
7000a2430fSAndrzej Pietrasiewicz 
7100a2430fSAndrzej Pietrasiewicz 	struct ffs_ep			*eps;
7200a2430fSAndrzej Pietrasiewicz 	u8				eps_revmap[16];
7300a2430fSAndrzej Pietrasiewicz 	short				*interfaces_nums;
7400a2430fSAndrzej Pietrasiewicz 
7500a2430fSAndrzej Pietrasiewicz 	struct usb_function		function;
7600a2430fSAndrzej Pietrasiewicz };
7700a2430fSAndrzej Pietrasiewicz 
7800a2430fSAndrzej Pietrasiewicz 
7900a2430fSAndrzej Pietrasiewicz static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
8000a2430fSAndrzej Pietrasiewicz {
8100a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct ffs_function, function);
8200a2430fSAndrzej Pietrasiewicz }
8300a2430fSAndrzej Pietrasiewicz 
8400a2430fSAndrzej Pietrasiewicz 
8500a2430fSAndrzej Pietrasiewicz static inline enum ffs_setup_state
8600a2430fSAndrzej Pietrasiewicz ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
8700a2430fSAndrzej Pietrasiewicz {
8800a2430fSAndrzej Pietrasiewicz 	return (enum ffs_setup_state)
8900a2430fSAndrzej Pietrasiewicz 		cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
9000a2430fSAndrzej Pietrasiewicz }
9100a2430fSAndrzej Pietrasiewicz 
9200a2430fSAndrzej Pietrasiewicz 
9300a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func);
9400a2430fSAndrzej Pietrasiewicz static int __must_check ffs_func_eps_enable(struct ffs_function *func);
9500a2430fSAndrzej Pietrasiewicz 
9600a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *,
9700a2430fSAndrzej Pietrasiewicz 			 struct usb_function *);
9800a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
9900a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *);
10000a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *,
10100a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *);
10254dfce6dSFelix Hädicke static bool ffs_func_req_match(struct usb_function *,
1031a00b457SFelix Hädicke 			       const struct usb_ctrlrequest *,
1041a00b457SFelix Hädicke 			       bool config0);
10500a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *);
10600a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *);
10700a2430fSAndrzej Pietrasiewicz 
10800a2430fSAndrzej Pietrasiewicz 
10900a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
11000a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
11100a2430fSAndrzej Pietrasiewicz 
11200a2430fSAndrzej Pietrasiewicz 
11300a2430fSAndrzej Pietrasiewicz /* The endpoints structures *************************************************/
11400a2430fSAndrzej Pietrasiewicz 
11500a2430fSAndrzej Pietrasiewicz struct ffs_ep {
11600a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*ep;	/* P: ffs->eps_lock */
11700a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;	/* P: epfile->mutex */
11800a2430fSAndrzej Pietrasiewicz 
11900a2430fSAndrzej Pietrasiewicz 	/* [0]: full speed, [1]: high speed, [2]: super speed */
12000a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor	*descs[3];
12100a2430fSAndrzej Pietrasiewicz 
12200a2430fSAndrzej Pietrasiewicz 	u8				num;
12300a2430fSAndrzej Pietrasiewicz 
12400a2430fSAndrzej Pietrasiewicz 	int				status;	/* P: epfile->mutex */
12500a2430fSAndrzej Pietrasiewicz };
12600a2430fSAndrzej Pietrasiewicz 
12700a2430fSAndrzej Pietrasiewicz struct ffs_epfile {
12800a2430fSAndrzej Pietrasiewicz 	/* Protects ep->ep and ep->req. */
12900a2430fSAndrzej Pietrasiewicz 	struct mutex			mutex;
13000a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		wait;
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;
2255e33f6fdSRobert Baldyga 
2265e33f6fdSRobert Baldyga 	struct ffs_data *ffs;
22700a2430fSAndrzej Pietrasiewicz };
22800a2430fSAndrzej Pietrasiewicz 
2296d5c1c77SRobert Baldyga struct ffs_desc_helper {
2306d5c1c77SRobert Baldyga 	struct ffs_data *ffs;
2316d5c1c77SRobert Baldyga 	unsigned interfaces_count;
2326d5c1c77SRobert Baldyga 	unsigned eps_count;
2336d5c1c77SRobert Baldyga };
2346d5c1c77SRobert Baldyga 
23500a2430fSAndrzej Pietrasiewicz static int  __must_check ffs_epfiles_create(struct ffs_data *ffs);
23600a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
23700a2430fSAndrzej Pietrasiewicz 
2381bb27cacSAl Viro static struct dentry *
23900a2430fSAndrzej Pietrasiewicz ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
2401bb27cacSAl Viro 		   const struct file_operations *fops);
24100a2430fSAndrzej Pietrasiewicz 
24200a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
24300a2430fSAndrzej Pietrasiewicz 
24400a2430fSAndrzej Pietrasiewicz DEFINE_MUTEX(ffs_lock);
24500a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_lock);
24600a2430fSAndrzej Pietrasiewicz 
24700a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name);
24800a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void);
24900a2430fSAndrzej Pietrasiewicz static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
25000a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev);
25100a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name);
25200a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data);
25300a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs);
25400a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs);
25500a2430fSAndrzej Pietrasiewicz 
25600a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
25700a2430fSAndrzej Pietrasiewicz 
25800a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
25900a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
26000a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
26100a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
26200a2430fSAndrzej Pietrasiewicz 
26300a2430fSAndrzej Pietrasiewicz 
26400a2430fSAndrzej Pietrasiewicz /* Control file aka ep0 *****************************************************/
26500a2430fSAndrzej Pietrasiewicz 
26600a2430fSAndrzej Pietrasiewicz static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
26700a2430fSAndrzej Pietrasiewicz {
26800a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = req->context;
26900a2430fSAndrzej Pietrasiewicz 
2705bdcde90SDaniel Wagner 	complete(&ffs->ep0req_completion);
27100a2430fSAndrzej Pietrasiewicz }
27200a2430fSAndrzej Pietrasiewicz 
27300a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
27400a2430fSAndrzej Pietrasiewicz {
27500a2430fSAndrzej Pietrasiewicz 	struct usb_request *req = ffs->ep0req;
27600a2430fSAndrzej Pietrasiewicz 	int ret;
27700a2430fSAndrzej Pietrasiewicz 
27800a2430fSAndrzej Pietrasiewicz 	req->zero     = len < le16_to_cpu(ffs->ev.setup.wLength);
27900a2430fSAndrzej Pietrasiewicz 
28000a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
28100a2430fSAndrzej Pietrasiewicz 
28200a2430fSAndrzej Pietrasiewicz 	req->buf      = data;
28300a2430fSAndrzej Pietrasiewicz 	req->length   = len;
28400a2430fSAndrzej Pietrasiewicz 
28500a2430fSAndrzej Pietrasiewicz 	/*
28600a2430fSAndrzej Pietrasiewicz 	 * UDC layer requires to provide a buffer even for ZLP, but should
28700a2430fSAndrzej Pietrasiewicz 	 * not use it at all. Let's provide some poisoned pointer to catch
28800a2430fSAndrzej Pietrasiewicz 	 * possible bug in the driver.
28900a2430fSAndrzej Pietrasiewicz 	 */
29000a2430fSAndrzej Pietrasiewicz 	if (req->buf == NULL)
29100a2430fSAndrzej Pietrasiewicz 		req->buf = (void *)0xDEADBABE;
29200a2430fSAndrzej Pietrasiewicz 
29300a2430fSAndrzej Pietrasiewicz 	reinit_completion(&ffs->ep0req_completion);
29400a2430fSAndrzej Pietrasiewicz 
29500a2430fSAndrzej Pietrasiewicz 	ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
29600a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
29700a2430fSAndrzej Pietrasiewicz 		return ret;
29800a2430fSAndrzej Pietrasiewicz 
29900a2430fSAndrzej Pietrasiewicz 	ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
30000a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret)) {
30100a2430fSAndrzej Pietrasiewicz 		usb_ep_dequeue(ffs->gadget->ep0, req);
30200a2430fSAndrzej Pietrasiewicz 		return -EINTR;
30300a2430fSAndrzej Pietrasiewicz 	}
30400a2430fSAndrzej Pietrasiewicz 
30500a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
30600a2430fSAndrzej Pietrasiewicz 	return req->status ? req->status : req->actual;
30700a2430fSAndrzej Pietrasiewicz }
30800a2430fSAndrzej Pietrasiewicz 
30900a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_stall(struct ffs_data *ffs)
31000a2430fSAndrzej Pietrasiewicz {
31100a2430fSAndrzej Pietrasiewicz 	if (ffs->ev.can_stall) {
31200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("ep0 stall\n");
31300a2430fSAndrzej Pietrasiewicz 		usb_ep_set_halt(ffs->gadget->ep0);
31400a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_NO_SETUP;
31500a2430fSAndrzej Pietrasiewicz 		return -EL2HLT;
31600a2430fSAndrzej Pietrasiewicz 	} else {
31700a2430fSAndrzej Pietrasiewicz 		pr_debug("bogus ep0 stall!\n");
31800a2430fSAndrzej Pietrasiewicz 		return -ESRCH;
31900a2430fSAndrzej Pietrasiewicz 	}
32000a2430fSAndrzej Pietrasiewicz }
32100a2430fSAndrzej Pietrasiewicz 
32200a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
32300a2430fSAndrzej Pietrasiewicz 			     size_t len, loff_t *ptr)
32400a2430fSAndrzej Pietrasiewicz {
32500a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
32600a2430fSAndrzej Pietrasiewicz 	ssize_t ret;
32700a2430fSAndrzej Pietrasiewicz 	char *data;
32800a2430fSAndrzej Pietrasiewicz 
32900a2430fSAndrzej Pietrasiewicz 	ENTER();
33000a2430fSAndrzej Pietrasiewicz 
33100a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
33200a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
33300a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
33400a2430fSAndrzej Pietrasiewicz 
33500a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
33600a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
33700a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
33800a2430fSAndrzej Pietrasiewicz 		return ret;
33900a2430fSAndrzej Pietrasiewicz 
34000a2430fSAndrzej Pietrasiewicz 	/* Check state */
34100a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
34200a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
34300a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
34400a2430fSAndrzej Pietrasiewicz 		/* Copy data */
34500a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 16)) {
34600a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
34700a2430fSAndrzej Pietrasiewicz 			break;
34800a2430fSAndrzej Pietrasiewicz 		}
34900a2430fSAndrzej Pietrasiewicz 
35000a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
35100a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
35200a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
35300a2430fSAndrzej Pietrasiewicz 			break;
35400a2430fSAndrzej Pietrasiewicz 		}
35500a2430fSAndrzej Pietrasiewicz 
35600a2430fSAndrzej Pietrasiewicz 		/* Handle data */
35700a2430fSAndrzej Pietrasiewicz 		if (ffs->state == FFS_READ_DESCRIPTORS) {
35800a2430fSAndrzej Pietrasiewicz 			pr_info("read descriptors\n");
35900a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_descs(ffs, data, len);
36000a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
36100a2430fSAndrzej Pietrasiewicz 				break;
36200a2430fSAndrzej Pietrasiewicz 
36300a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_READ_STRINGS;
36400a2430fSAndrzej Pietrasiewicz 			ret = len;
36500a2430fSAndrzej Pietrasiewicz 		} else {
36600a2430fSAndrzej Pietrasiewicz 			pr_info("read strings\n");
36700a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_strings(ffs, data, len);
36800a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
36900a2430fSAndrzej Pietrasiewicz 				break;
37000a2430fSAndrzej Pietrasiewicz 
37100a2430fSAndrzej Pietrasiewicz 			ret = ffs_epfiles_create(ffs);
37200a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret)) {
37300a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
37400a2430fSAndrzej Pietrasiewicz 				break;
37500a2430fSAndrzej Pietrasiewicz 			}
37600a2430fSAndrzej Pietrasiewicz 
37700a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_ACTIVE;
37800a2430fSAndrzej Pietrasiewicz 			mutex_unlock(&ffs->mutex);
37900a2430fSAndrzej Pietrasiewicz 
38000a2430fSAndrzej Pietrasiewicz 			ret = ffs_ready(ffs);
38100a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0)) {
38200a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
38300a2430fSAndrzej Pietrasiewicz 				return ret;
38400a2430fSAndrzej Pietrasiewicz 			}
38500a2430fSAndrzej Pietrasiewicz 
38600a2430fSAndrzej Pietrasiewicz 			return len;
38700a2430fSAndrzej Pietrasiewicz 		}
38800a2430fSAndrzej Pietrasiewicz 		break;
38900a2430fSAndrzej Pietrasiewicz 
39000a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
39100a2430fSAndrzej Pietrasiewicz 		data = NULL;
39200a2430fSAndrzej Pietrasiewicz 		/*
39300a2430fSAndrzej Pietrasiewicz 		 * We're called from user space, we can use _irq
39400a2430fSAndrzej Pietrasiewicz 		 * rather then _irqsave
39500a2430fSAndrzej Pietrasiewicz 		 */
39600a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
39700a2430fSAndrzej Pietrasiewicz 		switch (ffs_setup_state_clear_cancelled(ffs)) {
39800a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
39900a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
40000a2430fSAndrzej Pietrasiewicz 			goto done_spin;
40100a2430fSAndrzej Pietrasiewicz 
40200a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
40300a2430fSAndrzej Pietrasiewicz 			ret = -ESRCH;
40400a2430fSAndrzej Pietrasiewicz 			goto done_spin;
40500a2430fSAndrzej Pietrasiewicz 
40600a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
40700a2430fSAndrzej Pietrasiewicz 			break;
40800a2430fSAndrzej Pietrasiewicz 		}
40900a2430fSAndrzej Pietrasiewicz 
41000a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING */
41100a2430fSAndrzej Pietrasiewicz 		if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
41200a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
41300a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
41400a2430fSAndrzej Pietrasiewicz 			break;
41500a2430fSAndrzej Pietrasiewicz 		}
41600a2430fSAndrzej Pietrasiewicz 
41700a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING and not stall */
41800a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
41900a2430fSAndrzej Pietrasiewicz 
42000a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
42100a2430fSAndrzej Pietrasiewicz 
42200a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
42300a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
42400a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
42500a2430fSAndrzej Pietrasiewicz 			break;
42600a2430fSAndrzej Pietrasiewicz 		}
42700a2430fSAndrzej Pietrasiewicz 
42800a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
42900a2430fSAndrzej Pietrasiewicz 
43000a2430fSAndrzej Pietrasiewicz 		/*
43100a2430fSAndrzej Pietrasiewicz 		 * We are guaranteed to be still in FFS_ACTIVE state
43200a2430fSAndrzej Pietrasiewicz 		 * but the state of setup could have changed from
43300a2430fSAndrzej Pietrasiewicz 		 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
43400a2430fSAndrzej Pietrasiewicz 		 * to check for that.  If that happened we copied data
43500a2430fSAndrzej Pietrasiewicz 		 * from user space in vain but it's unlikely.
43600a2430fSAndrzej Pietrasiewicz 		 *
43700a2430fSAndrzej Pietrasiewicz 		 * For sure we are not in FFS_NO_SETUP since this is
43800a2430fSAndrzej Pietrasiewicz 		 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
43900a2430fSAndrzej Pietrasiewicz 		 * transition can be performed and it's protected by
44000a2430fSAndrzej Pietrasiewicz 		 * mutex.
44100a2430fSAndrzej Pietrasiewicz 		 */
44200a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
44300a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
44400a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
44500a2430fSAndrzej Pietrasiewicz done_spin:
44600a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
44700a2430fSAndrzej Pietrasiewicz 		} else {
44800a2430fSAndrzej Pietrasiewicz 			/* unlocks spinlock */
44900a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_queue_wait(ffs, data, len);
45000a2430fSAndrzej Pietrasiewicz 		}
45100a2430fSAndrzej Pietrasiewicz 		kfree(data);
45200a2430fSAndrzej Pietrasiewicz 		break;
45300a2430fSAndrzej Pietrasiewicz 
45400a2430fSAndrzej Pietrasiewicz 	default:
45500a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
45600a2430fSAndrzej Pietrasiewicz 		break;
45700a2430fSAndrzej Pietrasiewicz 	}
45800a2430fSAndrzej Pietrasiewicz 
45900a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
46000a2430fSAndrzej Pietrasiewicz 	return ret;
46100a2430fSAndrzej Pietrasiewicz }
46200a2430fSAndrzej Pietrasiewicz 
46367913bbdSMichal Nazarewicz /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
46400a2430fSAndrzej Pietrasiewicz static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
46500a2430fSAndrzej Pietrasiewicz 				     size_t n)
46600a2430fSAndrzej Pietrasiewicz {
46700a2430fSAndrzej Pietrasiewicz 	/*
46867913bbdSMichal Nazarewicz 	 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
46967913bbdSMichal Nazarewicz 	 * size of ffs->ev.types array (which is four) so that's how much space
47067913bbdSMichal Nazarewicz 	 * we reserve.
47100a2430fSAndrzej Pietrasiewicz 	 */
47267913bbdSMichal Nazarewicz 	struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
47367913bbdSMichal Nazarewicz 	const size_t size = n * sizeof *events;
47400a2430fSAndrzej Pietrasiewicz 	unsigned i = 0;
47500a2430fSAndrzej Pietrasiewicz 
47667913bbdSMichal Nazarewicz 	memset(events, 0, size);
47700a2430fSAndrzej Pietrasiewicz 
47800a2430fSAndrzej Pietrasiewicz 	do {
47900a2430fSAndrzej Pietrasiewicz 		events[i].type = ffs->ev.types[i];
48000a2430fSAndrzej Pietrasiewicz 		if (events[i].type == FUNCTIONFS_SETUP) {
48100a2430fSAndrzej Pietrasiewicz 			events[i].u.setup = ffs->ev.setup;
48200a2430fSAndrzej Pietrasiewicz 			ffs->setup_state = FFS_SETUP_PENDING;
48300a2430fSAndrzej Pietrasiewicz 		}
48400a2430fSAndrzej Pietrasiewicz 	} while (++i < n);
48500a2430fSAndrzej Pietrasiewicz 
48600a2430fSAndrzej Pietrasiewicz 	ffs->ev.count -= n;
48767913bbdSMichal Nazarewicz 	if (ffs->ev.count)
48800a2430fSAndrzej Pietrasiewicz 		memmove(ffs->ev.types, ffs->ev.types + n,
48900a2430fSAndrzej Pietrasiewicz 			ffs->ev.count * sizeof *ffs->ev.types);
49000a2430fSAndrzej Pietrasiewicz 
49100a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
49200a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
49300a2430fSAndrzej Pietrasiewicz 
4947fe9a937SDaniel Walter 	return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
49500a2430fSAndrzej Pietrasiewicz }
49600a2430fSAndrzej Pietrasiewicz 
49700a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
49800a2430fSAndrzej Pietrasiewicz 			    size_t len, loff_t *ptr)
49900a2430fSAndrzej Pietrasiewicz {
50000a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
50100a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
50200a2430fSAndrzej Pietrasiewicz 	size_t n;
50300a2430fSAndrzej Pietrasiewicz 	int ret;
50400a2430fSAndrzej Pietrasiewicz 
50500a2430fSAndrzej Pietrasiewicz 	ENTER();
50600a2430fSAndrzej Pietrasiewicz 
50700a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
50800a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
50900a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
51000a2430fSAndrzej Pietrasiewicz 
51100a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
51200a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
51300a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
51400a2430fSAndrzej Pietrasiewicz 		return ret;
51500a2430fSAndrzej Pietrasiewicz 
51600a2430fSAndrzej Pietrasiewicz 	/* Check state */
51700a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE) {
51800a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
51900a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
52000a2430fSAndrzej Pietrasiewicz 	}
52100a2430fSAndrzej Pietrasiewicz 
52200a2430fSAndrzej Pietrasiewicz 	/*
52300a2430fSAndrzej Pietrasiewicz 	 * We're called from user space, we can use _irq rather then
52400a2430fSAndrzej Pietrasiewicz 	 * _irqsave
52500a2430fSAndrzej Pietrasiewicz 	 */
52600a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&ffs->ev.waitq.lock);
52700a2430fSAndrzej Pietrasiewicz 
52800a2430fSAndrzej Pietrasiewicz 	switch (ffs_setup_state_clear_cancelled(ffs)) {
52900a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_CANCELLED:
53000a2430fSAndrzej Pietrasiewicz 		ret = -EIDRM;
53100a2430fSAndrzej Pietrasiewicz 		break;
53200a2430fSAndrzej Pietrasiewicz 
53300a2430fSAndrzej Pietrasiewicz 	case FFS_NO_SETUP:
53400a2430fSAndrzej Pietrasiewicz 		n = len / sizeof(struct usb_functionfs_event);
53500a2430fSAndrzej Pietrasiewicz 		if (unlikely(!n)) {
53600a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
53700a2430fSAndrzej Pietrasiewicz 			break;
53800a2430fSAndrzej Pietrasiewicz 		}
53900a2430fSAndrzej Pietrasiewicz 
54000a2430fSAndrzej Pietrasiewicz 		if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
54100a2430fSAndrzej Pietrasiewicz 			ret = -EAGAIN;
54200a2430fSAndrzej Pietrasiewicz 			break;
54300a2430fSAndrzej Pietrasiewicz 		}
54400a2430fSAndrzej Pietrasiewicz 
54500a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
54600a2430fSAndrzej Pietrasiewicz 							ffs->ev.count)) {
54700a2430fSAndrzej Pietrasiewicz 			ret = -EINTR;
54800a2430fSAndrzej Pietrasiewicz 			break;
54900a2430fSAndrzej Pietrasiewicz 		}
55000a2430fSAndrzej Pietrasiewicz 
55100a2430fSAndrzej Pietrasiewicz 		return __ffs_ep0_read_events(ffs, buf,
55200a2430fSAndrzej Pietrasiewicz 					     min(n, (size_t)ffs->ev.count));
55300a2430fSAndrzej Pietrasiewicz 
55400a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_PENDING:
55500a2430fSAndrzej Pietrasiewicz 		if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
55600a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
55700a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
55800a2430fSAndrzej Pietrasiewicz 			goto done_mutex;
55900a2430fSAndrzej Pietrasiewicz 		}
56000a2430fSAndrzej Pietrasiewicz 
56100a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
56200a2430fSAndrzej Pietrasiewicz 
56300a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
56400a2430fSAndrzej Pietrasiewicz 
56500a2430fSAndrzej Pietrasiewicz 		if (likely(len)) {
56600a2430fSAndrzej Pietrasiewicz 			data = kmalloc(len, GFP_KERNEL);
56700a2430fSAndrzej Pietrasiewicz 			if (unlikely(!data)) {
56800a2430fSAndrzej Pietrasiewicz 				ret = -ENOMEM;
56900a2430fSAndrzej Pietrasiewicz 				goto done_mutex;
57000a2430fSAndrzej Pietrasiewicz 			}
57100a2430fSAndrzej Pietrasiewicz 		}
57200a2430fSAndrzej Pietrasiewicz 
57300a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
57400a2430fSAndrzej Pietrasiewicz 
57500a2430fSAndrzej Pietrasiewicz 		/* See ffs_ep0_write() */
57600a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
57700a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
57800a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
57900a2430fSAndrzej Pietrasiewicz 			break;
58000a2430fSAndrzej Pietrasiewicz 		}
58100a2430fSAndrzej Pietrasiewicz 
58200a2430fSAndrzej Pietrasiewicz 		/* unlocks spinlock */
58300a2430fSAndrzej Pietrasiewicz 		ret = __ffs_ep0_queue_wait(ffs, data, len);
5847fe9a937SDaniel Walter 		if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
58500a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
58600a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
58700a2430fSAndrzej Pietrasiewicz 
58800a2430fSAndrzej Pietrasiewicz 	default:
58900a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
59000a2430fSAndrzej Pietrasiewicz 		break;
59100a2430fSAndrzej Pietrasiewicz 	}
59200a2430fSAndrzej Pietrasiewicz 
59300a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
59400a2430fSAndrzej Pietrasiewicz done_mutex:
59500a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
59600a2430fSAndrzej Pietrasiewicz 	kfree(data);
59700a2430fSAndrzej Pietrasiewicz 	return ret;
59800a2430fSAndrzej Pietrasiewicz }
59900a2430fSAndrzej Pietrasiewicz 
60000a2430fSAndrzej Pietrasiewicz static int ffs_ep0_open(struct inode *inode, struct file *file)
60100a2430fSAndrzej Pietrasiewicz {
60200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = inode->i_private;
60300a2430fSAndrzej Pietrasiewicz 
60400a2430fSAndrzej Pietrasiewicz 	ENTER();
60500a2430fSAndrzej Pietrasiewicz 
60600a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs->state == FFS_CLOSING))
60700a2430fSAndrzej Pietrasiewicz 		return -EBUSY;
60800a2430fSAndrzej Pietrasiewicz 
60900a2430fSAndrzej Pietrasiewicz 	file->private_data = ffs;
61000a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(ffs);
61100a2430fSAndrzej Pietrasiewicz 
61200a2430fSAndrzej Pietrasiewicz 	return 0;
61300a2430fSAndrzej Pietrasiewicz }
61400a2430fSAndrzej Pietrasiewicz 
61500a2430fSAndrzej Pietrasiewicz static int ffs_ep0_release(struct inode *inode, struct file *file)
61600a2430fSAndrzej Pietrasiewicz {
61700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
61800a2430fSAndrzej Pietrasiewicz 
61900a2430fSAndrzej Pietrasiewicz 	ENTER();
62000a2430fSAndrzej Pietrasiewicz 
62100a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(ffs);
62200a2430fSAndrzej Pietrasiewicz 
62300a2430fSAndrzej Pietrasiewicz 	return 0;
62400a2430fSAndrzej Pietrasiewicz }
62500a2430fSAndrzej Pietrasiewicz 
62600a2430fSAndrzej Pietrasiewicz static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
62700a2430fSAndrzej Pietrasiewicz {
62800a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
62900a2430fSAndrzej Pietrasiewicz 	struct usb_gadget *gadget = ffs->gadget;
63000a2430fSAndrzej Pietrasiewicz 	long ret;
63100a2430fSAndrzej Pietrasiewicz 
63200a2430fSAndrzej Pietrasiewicz 	ENTER();
63300a2430fSAndrzej Pietrasiewicz 
63400a2430fSAndrzej Pietrasiewicz 	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
63500a2430fSAndrzej Pietrasiewicz 		struct ffs_function *func = ffs->func;
63600a2430fSAndrzej Pietrasiewicz 		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
63700a2430fSAndrzej Pietrasiewicz 	} else if (gadget && gadget->ops->ioctl) {
63800a2430fSAndrzej Pietrasiewicz 		ret = gadget->ops->ioctl(gadget, code, value);
63900a2430fSAndrzej Pietrasiewicz 	} else {
64000a2430fSAndrzej Pietrasiewicz 		ret = -ENOTTY;
64100a2430fSAndrzej Pietrasiewicz 	}
64200a2430fSAndrzej Pietrasiewicz 
64300a2430fSAndrzej Pietrasiewicz 	return ret;
64400a2430fSAndrzej Pietrasiewicz }
64500a2430fSAndrzej Pietrasiewicz 
64600a2430fSAndrzej Pietrasiewicz static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
64700a2430fSAndrzej Pietrasiewicz {
64800a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
64900a2430fSAndrzej Pietrasiewicz 	unsigned int mask = POLLWRNORM;
65000a2430fSAndrzej Pietrasiewicz 	int ret;
65100a2430fSAndrzej Pietrasiewicz 
65200a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &ffs->ev.waitq, wait);
65300a2430fSAndrzej Pietrasiewicz 
65400a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
65500a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
65600a2430fSAndrzej Pietrasiewicz 		return mask;
65700a2430fSAndrzej Pietrasiewicz 
65800a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
65900a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
66000a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
66100a2430fSAndrzej Pietrasiewicz 		mask |= POLLOUT;
66200a2430fSAndrzej Pietrasiewicz 		break;
66300a2430fSAndrzej Pietrasiewicz 
66400a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
66500a2430fSAndrzej Pietrasiewicz 		switch (ffs->setup_state) {
66600a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
66700a2430fSAndrzej Pietrasiewicz 			if (ffs->ev.count)
66800a2430fSAndrzej Pietrasiewicz 				mask |= POLLIN;
66900a2430fSAndrzej Pietrasiewicz 			break;
67000a2430fSAndrzej Pietrasiewicz 
67100a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
67200a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
67300a2430fSAndrzej Pietrasiewicz 			mask |= (POLLIN | POLLOUT);
67400a2430fSAndrzej Pietrasiewicz 			break;
67500a2430fSAndrzej Pietrasiewicz 		}
67600a2430fSAndrzej Pietrasiewicz 	case FFS_CLOSING:
67700a2430fSAndrzej Pietrasiewicz 		break;
67818d6b32fSRobert Baldyga 	case FFS_DEACTIVATED:
67918d6b32fSRobert Baldyga 		break;
68000a2430fSAndrzej Pietrasiewicz 	}
68100a2430fSAndrzej Pietrasiewicz 
68200a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
68300a2430fSAndrzej Pietrasiewicz 
68400a2430fSAndrzej Pietrasiewicz 	return mask;
68500a2430fSAndrzej Pietrasiewicz }
68600a2430fSAndrzej Pietrasiewicz 
68700a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_ep0_operations = {
68800a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
68900a2430fSAndrzej Pietrasiewicz 
69000a2430fSAndrzej Pietrasiewicz 	.open =		ffs_ep0_open,
69100a2430fSAndrzej Pietrasiewicz 	.write =	ffs_ep0_write,
69200a2430fSAndrzej Pietrasiewicz 	.read =		ffs_ep0_read,
69300a2430fSAndrzej Pietrasiewicz 	.release =	ffs_ep0_release,
69400a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_ep0_ioctl,
69500a2430fSAndrzej Pietrasiewicz 	.poll =		ffs_ep0_poll,
69600a2430fSAndrzej Pietrasiewicz };
69700a2430fSAndrzej Pietrasiewicz 
69800a2430fSAndrzej Pietrasiewicz 
69900a2430fSAndrzej Pietrasiewicz /* "Normal" endpoints operations ********************************************/
70000a2430fSAndrzej Pietrasiewicz 
70100a2430fSAndrzej Pietrasiewicz static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
70200a2430fSAndrzej Pietrasiewicz {
70300a2430fSAndrzej Pietrasiewicz 	ENTER();
70400a2430fSAndrzej Pietrasiewicz 	if (likely(req->context)) {
70500a2430fSAndrzej Pietrasiewicz 		struct ffs_ep *ep = _ep->driver_data;
70600a2430fSAndrzej Pietrasiewicz 		ep->status = req->status ? req->status : req->actual;
70700a2430fSAndrzej Pietrasiewicz 		complete(req->context);
70800a2430fSAndrzej Pietrasiewicz 	}
70900a2430fSAndrzej Pietrasiewicz }
71000a2430fSAndrzej Pietrasiewicz 
711c662a31bSMichal Nazarewicz static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
712c662a31bSMichal Nazarewicz {
713c662a31bSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
714c662a31bSMichal Nazarewicz 	if (likely(ret == data_len))
715c662a31bSMichal Nazarewicz 		return ret;
716c662a31bSMichal Nazarewicz 
717c662a31bSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
718c662a31bSMichal Nazarewicz 		return -EFAULT;
719c662a31bSMichal Nazarewicz 
720c662a31bSMichal Nazarewicz 	/*
721c662a31bSMichal Nazarewicz 	 * Dear user space developer!
722c662a31bSMichal Nazarewicz 	 *
723c662a31bSMichal Nazarewicz 	 * TL;DR: To stop getting below error message in your kernel log, change
724c662a31bSMichal Nazarewicz 	 * user space code using functionfs to align read buffers to a max
725c662a31bSMichal Nazarewicz 	 * packet size.
726c662a31bSMichal Nazarewicz 	 *
727c662a31bSMichal Nazarewicz 	 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
728c662a31bSMichal Nazarewicz 	 * packet size.  When unaligned buffer is passed to functionfs, it
729c662a31bSMichal Nazarewicz 	 * internally uses a larger, aligned buffer so that such UDCs are happy.
730c662a31bSMichal Nazarewicz 	 *
731c662a31bSMichal Nazarewicz 	 * Unfortunately, this means that host may send more data than was
732c662a31bSMichal Nazarewicz 	 * requested in read(2) system call.  f_fs doesn’t know what to do with
733c662a31bSMichal Nazarewicz 	 * that excess data so it simply drops it.
734c662a31bSMichal Nazarewicz 	 *
735c662a31bSMichal Nazarewicz 	 * Was the buffer aligned in the first place, no such problem would
736c662a31bSMichal Nazarewicz 	 * happen.
737c662a31bSMichal Nazarewicz 	 *
7389353afbbSMichal Nazarewicz 	 * Data may be dropped only in AIO reads.  Synchronous reads are handled
7399353afbbSMichal Nazarewicz 	 * by splitting a request into multiple parts.  This splitting may still
7409353afbbSMichal Nazarewicz 	 * be a problem though so it’s likely best to align the buffer
7419353afbbSMichal Nazarewicz 	 * regardless of it being AIO or not..
7429353afbbSMichal Nazarewicz 	 *
743c662a31bSMichal Nazarewicz 	 * This only affects OUT endpoints, i.e. reading data with a read(2),
744c662a31bSMichal Nazarewicz 	 * aio_read(2) etc. system calls.  Writing data to an IN endpoint is not
745c662a31bSMichal Nazarewicz 	 * affected.
746c662a31bSMichal Nazarewicz 	 */
747c662a31bSMichal Nazarewicz 	pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
748c662a31bSMichal Nazarewicz 	       "Align read buffer size to max packet size to avoid the problem.\n",
749c662a31bSMichal Nazarewicz 	       data_len, ret);
750c662a31bSMichal Nazarewicz 
751c662a31bSMichal Nazarewicz 	return ret;
752c662a31bSMichal Nazarewicz }
753c662a31bSMichal Nazarewicz 
75400a2430fSAndrzej Pietrasiewicz static void ffs_user_copy_worker(struct work_struct *work)
75500a2430fSAndrzej Pietrasiewicz {
75600a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
75700a2430fSAndrzej Pietrasiewicz 						   work);
75800a2430fSAndrzej Pietrasiewicz 	int ret = io_data->req->status ? io_data->req->status :
75900a2430fSAndrzej Pietrasiewicz 					 io_data->req->actual;
76038740a5bSLars-Peter Clausen 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
76100a2430fSAndrzej Pietrasiewicz 
76200a2430fSAndrzej Pietrasiewicz 	if (io_data->read && ret > 0) {
76300a2430fSAndrzej Pietrasiewicz 		use_mm(io_data->mm);
764c662a31bSMichal Nazarewicz 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
76500a2430fSAndrzej Pietrasiewicz 		unuse_mm(io_data->mm);
76600a2430fSAndrzej Pietrasiewicz 	}
76700a2430fSAndrzej Pietrasiewicz 
76804b2fa9fSChristoph Hellwig 	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
76900a2430fSAndrzej Pietrasiewicz 
77038740a5bSLars-Peter Clausen 	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
7715e33f6fdSRobert Baldyga 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);
7725e33f6fdSRobert Baldyga 
77300a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(io_data->ep, io_data->req);
77400a2430fSAndrzej Pietrasiewicz 
77500a2430fSAndrzej Pietrasiewicz 	if (io_data->read)
776c993c39bSAl Viro 		kfree(io_data->to_free);
77700a2430fSAndrzej Pietrasiewicz 	kfree(io_data->buf);
77800a2430fSAndrzej Pietrasiewicz 	kfree(io_data);
77900a2430fSAndrzej Pietrasiewicz }
78000a2430fSAndrzej Pietrasiewicz 
78100a2430fSAndrzej Pietrasiewicz static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
78200a2430fSAndrzej Pietrasiewicz 					 struct usb_request *req)
78300a2430fSAndrzej Pietrasiewicz {
78400a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = req->context;
78500a2430fSAndrzej Pietrasiewicz 
78600a2430fSAndrzej Pietrasiewicz 	ENTER();
78700a2430fSAndrzej Pietrasiewicz 
78800a2430fSAndrzej Pietrasiewicz 	INIT_WORK(&io_data->work, ffs_user_copy_worker);
78900a2430fSAndrzej Pietrasiewicz 	schedule_work(&io_data->work);
79000a2430fSAndrzej Pietrasiewicz }
79100a2430fSAndrzej Pietrasiewicz 
792a9e6f83cSMichal Nazarewicz static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
793a9e6f83cSMichal Nazarewicz {
794a9e6f83cSMichal Nazarewicz 	/*
795a9e6f83cSMichal Nazarewicz 	 * See comment in struct ffs_epfile for full read_buffer pointer
796a9e6f83cSMichal Nazarewicz 	 * synchronisation story.
797a9e6f83cSMichal Nazarewicz 	 */
798a9e6f83cSMichal Nazarewicz 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP);
799a9e6f83cSMichal Nazarewicz 	if (buf && buf != READ_BUFFER_DROP)
800a9e6f83cSMichal Nazarewicz 		kfree(buf);
801a9e6f83cSMichal Nazarewicz }
802a9e6f83cSMichal Nazarewicz 
8039353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
8049353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
8059353afbbSMichal Nazarewicz 					  struct iov_iter *iter)
8069353afbbSMichal Nazarewicz {
807a9e6f83cSMichal Nazarewicz 	/*
808a9e6f83cSMichal Nazarewicz 	 * Null out epfile->read_buffer so ffs_func_eps_disable does not free
809a9e6f83cSMichal Nazarewicz 	 * the buffer while we are using it.  See comment in struct ffs_epfile
810a9e6f83cSMichal Nazarewicz 	 * for full read_buffer pointer synchronisation story.
811a9e6f83cSMichal Nazarewicz 	 */
812a9e6f83cSMichal Nazarewicz 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL);
8139353afbbSMichal Nazarewicz 	ssize_t ret;
814a9e6f83cSMichal Nazarewicz 	if (!buf || buf == READ_BUFFER_DROP)
8159353afbbSMichal Nazarewicz 		return 0;
8169353afbbSMichal Nazarewicz 
8179353afbbSMichal Nazarewicz 	ret = copy_to_iter(buf->data, buf->length, iter);
8189353afbbSMichal Nazarewicz 	if (buf->length == ret) {
8199353afbbSMichal Nazarewicz 		kfree(buf);
820a9e6f83cSMichal Nazarewicz 		return ret;
821a9e6f83cSMichal Nazarewicz 	}
822a9e6f83cSMichal Nazarewicz 
823a9e6f83cSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter))) {
8249353afbbSMichal Nazarewicz 		ret = -EFAULT;
8259353afbbSMichal Nazarewicz 	} else {
8269353afbbSMichal Nazarewicz 		buf->length -= ret;
8279353afbbSMichal Nazarewicz 		buf->data += ret;
8289353afbbSMichal Nazarewicz 	}
829a9e6f83cSMichal Nazarewicz 
830a9e6f83cSMichal Nazarewicz 	if (cmpxchg(&epfile->read_buffer, NULL, buf))
831a9e6f83cSMichal Nazarewicz 		kfree(buf);
832a9e6f83cSMichal Nazarewicz 
8339353afbbSMichal Nazarewicz 	return ret;
8349353afbbSMichal Nazarewicz }
8359353afbbSMichal Nazarewicz 
8369353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
8379353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
8389353afbbSMichal Nazarewicz 				      void *data, int data_len,
8399353afbbSMichal Nazarewicz 				      struct iov_iter *iter)
8409353afbbSMichal Nazarewicz {
8419353afbbSMichal Nazarewicz 	struct ffs_buffer *buf;
8429353afbbSMichal Nazarewicz 
8439353afbbSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
8449353afbbSMichal Nazarewicz 	if (likely(data_len == ret))
8459353afbbSMichal Nazarewicz 		return ret;
8469353afbbSMichal Nazarewicz 
8479353afbbSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
8489353afbbSMichal Nazarewicz 		return -EFAULT;
8499353afbbSMichal Nazarewicz 
8509353afbbSMichal Nazarewicz 	/* See ffs_copy_to_iter for more context. */
8519353afbbSMichal Nazarewicz 	pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
8529353afbbSMichal Nazarewicz 		data_len, ret);
8539353afbbSMichal Nazarewicz 
8549353afbbSMichal Nazarewicz 	data_len -= ret;
8559353afbbSMichal Nazarewicz 	buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
85644963d64SDan Carpenter 	if (!buf)
85744963d64SDan Carpenter 		return -ENOMEM;
8589353afbbSMichal Nazarewicz 	buf->length = data_len;
8599353afbbSMichal Nazarewicz 	buf->data = buf->storage;
8609353afbbSMichal Nazarewicz 	memcpy(buf->storage, data + ret, data_len);
861a9e6f83cSMichal Nazarewicz 
862a9e6f83cSMichal Nazarewicz 	/*
863a9e6f83cSMichal Nazarewicz 	 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
864a9e6f83cSMichal Nazarewicz 	 * ffs_func_eps_disable has been called in the meanwhile).  See comment
865a9e6f83cSMichal Nazarewicz 	 * in struct ffs_epfile for full read_buffer pointer synchronisation
866a9e6f83cSMichal Nazarewicz 	 * story.
867a9e6f83cSMichal Nazarewicz 	 */
868a9e6f83cSMichal Nazarewicz 	if (unlikely(cmpxchg(&epfile->read_buffer, NULL, buf)))
869a9e6f83cSMichal Nazarewicz 		kfree(buf);
8709353afbbSMichal Nazarewicz 
8719353afbbSMichal Nazarewicz 	return ret;
8729353afbbSMichal Nazarewicz }
8739353afbbSMichal Nazarewicz 
87400a2430fSAndrzej Pietrasiewicz static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
87500a2430fSAndrzej Pietrasiewicz {
87600a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
877ae76e134SMichal Nazarewicz 	struct usb_request *req;
87800a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep;
87900a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
880c0d31b3cSDavid Cohen 	ssize_t ret, data_len = -EINVAL;
88100a2430fSAndrzej Pietrasiewicz 	int halt;
88200a2430fSAndrzej Pietrasiewicz 
88300a2430fSAndrzej Pietrasiewicz 	/* Are we still active? */
884b3591f67SMichal Nazarewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
885b3591f67SMichal Nazarewicz 		return -ENODEV;
88600a2430fSAndrzej Pietrasiewicz 
88700a2430fSAndrzej Pietrasiewicz 	/* Wait for endpoint to be enabled */
88800a2430fSAndrzej Pietrasiewicz 	ep = epfile->ep;
88900a2430fSAndrzej Pietrasiewicz 	if (!ep) {
890b3591f67SMichal Nazarewicz 		if (file->f_flags & O_NONBLOCK)
891b3591f67SMichal Nazarewicz 			return -EAGAIN;
89200a2430fSAndrzej Pietrasiewicz 
89300a2430fSAndrzej Pietrasiewicz 		ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
894b3591f67SMichal Nazarewicz 		if (ret)
895b3591f67SMichal Nazarewicz 			return -EINTR;
89600a2430fSAndrzej Pietrasiewicz 	}
89700a2430fSAndrzej Pietrasiewicz 
89800a2430fSAndrzej Pietrasiewicz 	/* Do we halt? */
89900a2430fSAndrzej Pietrasiewicz 	halt = (!io_data->read == !epfile->in);
900b3591f67SMichal Nazarewicz 	if (halt && epfile->isoc)
901b3591f67SMichal Nazarewicz 		return -EINVAL;
90200a2430fSAndrzej Pietrasiewicz 
9039353afbbSMichal Nazarewicz 	/* We will be using request and read_buffer */
9049353afbbSMichal Nazarewicz 	ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
9059353afbbSMichal Nazarewicz 	if (unlikely(ret))
9069353afbbSMichal Nazarewicz 		goto error;
9079353afbbSMichal Nazarewicz 
90800a2430fSAndrzej Pietrasiewicz 	/* Allocate & copy */
90900a2430fSAndrzej Pietrasiewicz 	if (!halt) {
9109353afbbSMichal Nazarewicz 		struct usb_gadget *gadget;
9119353afbbSMichal Nazarewicz 
9129353afbbSMichal Nazarewicz 		/*
9139353afbbSMichal Nazarewicz 		 * Do we have buffered data from previous partial read?  Check
9149353afbbSMichal Nazarewicz 		 * that for synchronous case only because we do not have
9159353afbbSMichal Nazarewicz 		 * facility to ‘wake up’ a pending asynchronous read and push
9169353afbbSMichal Nazarewicz 		 * buffered data to it which we would need to make things behave
9179353afbbSMichal Nazarewicz 		 * consistently.
9189353afbbSMichal Nazarewicz 		 */
9199353afbbSMichal Nazarewicz 		if (!io_data->aio && io_data->read) {
9209353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
9219353afbbSMichal Nazarewicz 			if (ret)
9229353afbbSMichal Nazarewicz 				goto error_mutex;
9239353afbbSMichal Nazarewicz 		}
9249353afbbSMichal Nazarewicz 
92500a2430fSAndrzej Pietrasiewicz 		/*
92600a2430fSAndrzej Pietrasiewicz 		 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
927ae76e134SMichal Nazarewicz 		 * before the waiting completes, so do not assign to 'gadget'
928ae76e134SMichal Nazarewicz 		 * earlier
92900a2430fSAndrzej Pietrasiewicz 		 */
9309353afbbSMichal Nazarewicz 		gadget = epfile->ffs->gadget;
93100a2430fSAndrzej Pietrasiewicz 
93200a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&epfile->ffs->eps_lock);
93300a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
93400a2430fSAndrzej Pietrasiewicz 		if (epfile->ep != ep) {
9359353afbbSMichal Nazarewicz 			ret = -ESHUTDOWN;
9369353afbbSMichal Nazarewicz 			goto error_lock;
93700a2430fSAndrzej Pietrasiewicz 		}
938c993c39bSAl Viro 		data_len = iov_iter_count(&io_data->data);
93900a2430fSAndrzej Pietrasiewicz 		/*
94000a2430fSAndrzej Pietrasiewicz 		 * Controller may require buffer size to be aligned to
94100a2430fSAndrzej Pietrasiewicz 		 * maxpacketsize of an out endpoint.
94200a2430fSAndrzej Pietrasiewicz 		 */
943c993c39bSAl Viro 		if (io_data->read)
944c993c39bSAl Viro 			data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
94500a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
94600a2430fSAndrzej Pietrasiewicz 
94700a2430fSAndrzej Pietrasiewicz 		data = kmalloc(data_len, GFP_KERNEL);
9489353afbbSMichal Nazarewicz 		if (unlikely(!data)) {
9499353afbbSMichal Nazarewicz 			ret = -ENOMEM;
9509353afbbSMichal Nazarewicz 			goto error_mutex;
9519353afbbSMichal Nazarewicz 		}
9529353afbbSMichal Nazarewicz 		if (!io_data->read &&
953cbbd26b8SAl Viro 		    !copy_from_iter_full(data, data_len, &io_data->data)) {
95400a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
9559353afbbSMichal Nazarewicz 			goto error_mutex;
95600a2430fSAndrzej Pietrasiewicz 		}
95700a2430fSAndrzej Pietrasiewicz 	}
95800a2430fSAndrzej Pietrasiewicz 
95900a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
96000a2430fSAndrzej Pietrasiewicz 
96100a2430fSAndrzej Pietrasiewicz 	if (epfile->ep != ep) {
96200a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
96300a2430fSAndrzej Pietrasiewicz 		ret = -ESHUTDOWN;
96400a2430fSAndrzej Pietrasiewicz 	} else if (halt) {
96500a2430fSAndrzej Pietrasiewicz 		/* Halt */
96600a2430fSAndrzej Pietrasiewicz 		if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
96700a2430fSAndrzej Pietrasiewicz 			usb_ep_set_halt(ep->ep);
96800a2430fSAndrzej Pietrasiewicz 		ret = -EBADMSG;
969ae76e134SMichal Nazarewicz 	} else if (unlikely(data_len == -EINVAL)) {
970c0d31b3cSDavid Cohen 		/*
971c0d31b3cSDavid Cohen 		 * Sanity Check: even though data_len can't be used
972c0d31b3cSDavid Cohen 		 * uninitialized at the time I write this comment, some
973c0d31b3cSDavid Cohen 		 * compilers complain about this situation.
974c0d31b3cSDavid Cohen 		 * In order to keep the code clean from warnings, data_len is
975c0d31b3cSDavid Cohen 		 * being initialized to -EINVAL during its declaration, which
976c0d31b3cSDavid Cohen 		 * means we can't rely on compiler anymore to warn no future
977c0d31b3cSDavid Cohen 		 * changes won't result in data_len being used uninitialized.
978c0d31b3cSDavid Cohen 		 * For such reason, we're adding this redundant sanity check
979c0d31b3cSDavid Cohen 		 * here.
980c0d31b3cSDavid Cohen 		 */
981c0d31b3cSDavid Cohen 		WARN(1, "%s: data_len == -EINVAL\n", __func__);
982c0d31b3cSDavid Cohen 		ret = -EINVAL;
983ae76e134SMichal Nazarewicz 	} else if (!io_data->aio) {
984ae76e134SMichal Nazarewicz 		DECLARE_COMPLETION_ONSTACK(done);
985ef150884SDu, Changbin 		bool interrupted = false;
986ae76e134SMichal Nazarewicz 
987ae76e134SMichal Nazarewicz 		req = ep->req;
988ae76e134SMichal Nazarewicz 		req->buf      = data;
989ae76e134SMichal Nazarewicz 		req->length   = data_len;
990ae76e134SMichal Nazarewicz 
991ae76e134SMichal Nazarewicz 		req->context  = &done;
992ae76e134SMichal Nazarewicz 		req->complete = ffs_epfile_io_complete;
993ae76e134SMichal Nazarewicz 
994ae76e134SMichal Nazarewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
995ae76e134SMichal Nazarewicz 		if (unlikely(ret < 0))
996c0d31b3cSDavid Cohen 			goto error_lock;
997ae76e134SMichal Nazarewicz 
998ae76e134SMichal Nazarewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
999ae76e134SMichal Nazarewicz 
1000ae76e134SMichal Nazarewicz 		if (unlikely(wait_for_completion_interruptible(&done))) {
1001ef150884SDu, Changbin 			/*
1002ef150884SDu, Changbin 			 * To avoid race condition with ffs_epfile_io_complete,
1003ef150884SDu, Changbin 			 * dequeue the request first then check
1004ef150884SDu, Changbin 			 * status. usb_ep_dequeue API should guarantee no race
1005ef150884SDu, Changbin 			 * condition with req->complete callback.
1006ef150884SDu, Changbin 			 */
1007ae76e134SMichal Nazarewicz 			usb_ep_dequeue(ep->ep, req);
1008ef150884SDu, Changbin 			interrupted = ep->status < 0;
1009c0d31b3cSDavid Cohen 		}
1010c0d31b3cSDavid Cohen 
1011c662a31bSMichal Nazarewicz 		if (interrupted)
1012c662a31bSMichal Nazarewicz 			ret = -EINTR;
1013c662a31bSMichal Nazarewicz 		else if (io_data->read && ep->status > 0)
10149353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_data(epfile, data, ep->status,
1015c662a31bSMichal Nazarewicz 						     &io_data->data);
1016c662a31bSMichal Nazarewicz 		else
1017c662a31bSMichal Nazarewicz 			ret = ep->status;
1018ae76e134SMichal Nazarewicz 		goto error_mutex;
1019ae76e134SMichal Nazarewicz 	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
10203163c79eSMichal Nazarewicz 		ret = -ENOMEM;
1021ae76e134SMichal Nazarewicz 	} else {
102200a2430fSAndrzej Pietrasiewicz 		req->buf      = data;
1023c0d31b3cSDavid Cohen 		req->length   = data_len;
102400a2430fSAndrzej Pietrasiewicz 
102500a2430fSAndrzej Pietrasiewicz 		io_data->buf = data;
102600a2430fSAndrzej Pietrasiewicz 		io_data->ep = ep->ep;
102700a2430fSAndrzej Pietrasiewicz 		io_data->req = req;
10285e33f6fdSRobert Baldyga 		io_data->ffs = epfile->ffs;
102900a2430fSAndrzej Pietrasiewicz 
103000a2430fSAndrzej Pietrasiewicz 		req->context  = io_data;
103100a2430fSAndrzej Pietrasiewicz 		req->complete = ffs_epfile_async_io_complete;
103200a2430fSAndrzej Pietrasiewicz 
103300a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
103400a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret)) {
103500a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, req);
103600a2430fSAndrzej Pietrasiewicz 			goto error_lock;
103700a2430fSAndrzej Pietrasiewicz 		}
1038ae76e134SMichal Nazarewicz 
103900a2430fSAndrzej Pietrasiewicz 		ret = -EIOCBQUEUED;
104000a2430fSAndrzej Pietrasiewicz 		/*
1041ae76e134SMichal Nazarewicz 		 * Do not kfree the buffer in this function.  It will be freed
1042ae76e134SMichal Nazarewicz 		 * by ffs_user_copy_worker.
104300a2430fSAndrzej Pietrasiewicz 		 */
1044ae76e134SMichal Nazarewicz 		data = NULL;
104500a2430fSAndrzej Pietrasiewicz 	}
104600a2430fSAndrzej Pietrasiewicz 
104700a2430fSAndrzej Pietrasiewicz error_lock:
104800a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
1049ae76e134SMichal Nazarewicz error_mutex:
105000a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&epfile->mutex);
105100a2430fSAndrzej Pietrasiewicz error:
105200a2430fSAndrzej Pietrasiewicz 	kfree(data);
105300a2430fSAndrzej Pietrasiewicz 	return ret;
105400a2430fSAndrzej Pietrasiewicz }
105500a2430fSAndrzej Pietrasiewicz 
105600a2430fSAndrzej Pietrasiewicz static int
105700a2430fSAndrzej Pietrasiewicz ffs_epfile_open(struct inode *inode, struct file *file)
105800a2430fSAndrzej Pietrasiewicz {
105900a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
106000a2430fSAndrzej Pietrasiewicz 
106100a2430fSAndrzej Pietrasiewicz 	ENTER();
106200a2430fSAndrzej Pietrasiewicz 
106300a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
106400a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
106500a2430fSAndrzej Pietrasiewicz 
106600a2430fSAndrzej Pietrasiewicz 	file->private_data = epfile;
106700a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(epfile->ffs);
106800a2430fSAndrzej Pietrasiewicz 
106900a2430fSAndrzej Pietrasiewicz 	return 0;
107000a2430fSAndrzej Pietrasiewicz }
107100a2430fSAndrzej Pietrasiewicz 
107200a2430fSAndrzej Pietrasiewicz static int ffs_aio_cancel(struct kiocb *kiocb)
107300a2430fSAndrzej Pietrasiewicz {
107400a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = kiocb->private;
107500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
107600a2430fSAndrzej Pietrasiewicz 	int value;
107700a2430fSAndrzej Pietrasiewicz 
107800a2430fSAndrzej Pietrasiewicz 	ENTER();
107900a2430fSAndrzej Pietrasiewicz 
108000a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
108100a2430fSAndrzej Pietrasiewicz 
108200a2430fSAndrzej Pietrasiewicz 	if (likely(io_data && io_data->ep && io_data->req))
108300a2430fSAndrzej Pietrasiewicz 		value = usb_ep_dequeue(io_data->ep, io_data->req);
108400a2430fSAndrzej Pietrasiewicz 	else
108500a2430fSAndrzej Pietrasiewicz 		value = -EINVAL;
108600a2430fSAndrzej Pietrasiewicz 
108700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
108800a2430fSAndrzej Pietrasiewicz 
108900a2430fSAndrzej Pietrasiewicz 	return value;
109000a2430fSAndrzej Pietrasiewicz }
109100a2430fSAndrzej Pietrasiewicz 
109270e60d91SAl Viro static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
109300a2430fSAndrzej Pietrasiewicz {
109470e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1095de2080d4SAl Viro 	ssize_t res;
109600a2430fSAndrzej Pietrasiewicz 
109700a2430fSAndrzej Pietrasiewicz 	ENTER();
109800a2430fSAndrzej Pietrasiewicz 
109970e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
110070e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
110170e60d91SAl Viro 		if (unlikely(!p))
110200a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
110370e60d91SAl Viro 		p->aio = true;
110470e60d91SAl Viro 	} else {
110570e60d91SAl Viro 		p->aio = false;
110670e60d91SAl Viro 	}
110700a2430fSAndrzej Pietrasiewicz 
110870e60d91SAl Viro 	p->read = false;
110970e60d91SAl Viro 	p->kiocb = kiocb;
111070e60d91SAl Viro 	p->data = *from;
111170e60d91SAl Viro 	p->mm = current->mm;
111200a2430fSAndrzej Pietrasiewicz 
111370e60d91SAl Viro 	kiocb->private = p;
111400a2430fSAndrzej Pietrasiewicz 
11154088acf1SRui Miguel Silva 	if (p->aio)
111600a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
111700a2430fSAndrzej Pietrasiewicz 
111870e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
111970e60d91SAl Viro 	if (res == -EIOCBQUEUED)
112070e60d91SAl Viro 		return res;
112170e60d91SAl Viro 	if (p->aio)
112270e60d91SAl Viro 		kfree(p);
112370e60d91SAl Viro 	else
112470e60d91SAl Viro 		*from = p->data;
1125de2080d4SAl Viro 	return res;
112600a2430fSAndrzej Pietrasiewicz }
112700a2430fSAndrzej Pietrasiewicz 
112870e60d91SAl Viro static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
112900a2430fSAndrzej Pietrasiewicz {
113070e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1131de2080d4SAl Viro 	ssize_t res;
113200a2430fSAndrzej Pietrasiewicz 
113300a2430fSAndrzej Pietrasiewicz 	ENTER();
113400a2430fSAndrzej Pietrasiewicz 
113570e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
113670e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
113770e60d91SAl Viro 		if (unlikely(!p))
113800a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
113970e60d91SAl Viro 		p->aio = true;
114070e60d91SAl Viro 	} else {
114170e60d91SAl Viro 		p->aio = false;
114200a2430fSAndrzej Pietrasiewicz 	}
114300a2430fSAndrzej Pietrasiewicz 
114470e60d91SAl Viro 	p->read = true;
114570e60d91SAl Viro 	p->kiocb = kiocb;
114670e60d91SAl Viro 	if (p->aio) {
114770e60d91SAl Viro 		p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
114870e60d91SAl Viro 		if (!p->to_free) {
114970e60d91SAl Viro 			kfree(p);
115070e60d91SAl Viro 			return -ENOMEM;
115170e60d91SAl Viro 		}
115270e60d91SAl Viro 	} else {
115370e60d91SAl Viro 		p->data = *to;
115470e60d91SAl Viro 		p->to_free = NULL;
115570e60d91SAl Viro 	}
115670e60d91SAl Viro 	p->mm = current->mm;
115700a2430fSAndrzej Pietrasiewicz 
115870e60d91SAl Viro 	kiocb->private = p;
115900a2430fSAndrzej Pietrasiewicz 
11604088acf1SRui Miguel Silva 	if (p->aio)
116100a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
116200a2430fSAndrzej Pietrasiewicz 
116370e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
116470e60d91SAl Viro 	if (res == -EIOCBQUEUED)
116570e60d91SAl Viro 		return res;
116670e60d91SAl Viro 
116770e60d91SAl Viro 	if (p->aio) {
116870e60d91SAl Viro 		kfree(p->to_free);
116970e60d91SAl Viro 		kfree(p);
117070e60d91SAl Viro 	} else {
117170e60d91SAl Viro 		*to = p->data;
1172de2080d4SAl Viro 	}
1173de2080d4SAl Viro 	return res;
117400a2430fSAndrzej Pietrasiewicz }
117500a2430fSAndrzej Pietrasiewicz 
117600a2430fSAndrzej Pietrasiewicz static int
117700a2430fSAndrzej Pietrasiewicz ffs_epfile_release(struct inode *inode, struct file *file)
117800a2430fSAndrzej Pietrasiewicz {
117900a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
118000a2430fSAndrzej Pietrasiewicz 
118100a2430fSAndrzej Pietrasiewicz 	ENTER();
118200a2430fSAndrzej Pietrasiewicz 
1183a9e6f83cSMichal Nazarewicz 	__ffs_epfile_read_buffer_free(epfile);
118400a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(epfile->ffs);
118500a2430fSAndrzej Pietrasiewicz 
118600a2430fSAndrzej Pietrasiewicz 	return 0;
118700a2430fSAndrzej Pietrasiewicz }
118800a2430fSAndrzej Pietrasiewicz 
118900a2430fSAndrzej Pietrasiewicz static long ffs_epfile_ioctl(struct file *file, unsigned code,
119000a2430fSAndrzej Pietrasiewicz 			     unsigned long value)
119100a2430fSAndrzej Pietrasiewicz {
119200a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
119300a2430fSAndrzej Pietrasiewicz 	int ret;
119400a2430fSAndrzej Pietrasiewicz 
119500a2430fSAndrzej Pietrasiewicz 	ENTER();
119600a2430fSAndrzej Pietrasiewicz 
119700a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
119800a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
119900a2430fSAndrzej Pietrasiewicz 
120000a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
120100a2430fSAndrzej Pietrasiewicz 	if (likely(epfile->ep)) {
120200a2430fSAndrzej Pietrasiewicz 		switch (code) {
120300a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_FIFO_STATUS:
120400a2430fSAndrzej Pietrasiewicz 			ret = usb_ep_fifo_status(epfile->ep->ep);
120500a2430fSAndrzej Pietrasiewicz 			break;
120600a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_FIFO_FLUSH:
120700a2430fSAndrzej Pietrasiewicz 			usb_ep_fifo_flush(epfile->ep->ep);
120800a2430fSAndrzej Pietrasiewicz 			ret = 0;
120900a2430fSAndrzej Pietrasiewicz 			break;
121000a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_CLEAR_HALT:
121100a2430fSAndrzej Pietrasiewicz 			ret = usb_ep_clear_halt(epfile->ep->ep);
121200a2430fSAndrzej Pietrasiewicz 			break;
121300a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_ENDPOINT_REVMAP:
121400a2430fSAndrzej Pietrasiewicz 			ret = epfile->ep->num;
121500a2430fSAndrzej Pietrasiewicz 			break;
1216c559a353SRobert Baldyga 		case FUNCTIONFS_ENDPOINT_DESC:
1217c559a353SRobert Baldyga 		{
1218c559a353SRobert Baldyga 			int desc_idx;
1219c559a353SRobert Baldyga 			struct usb_endpoint_descriptor *desc;
1220c559a353SRobert Baldyga 
1221c559a353SRobert Baldyga 			switch (epfile->ffs->gadget->speed) {
1222c559a353SRobert Baldyga 			case USB_SPEED_SUPER:
1223c559a353SRobert Baldyga 				desc_idx = 2;
1224c559a353SRobert Baldyga 				break;
1225c559a353SRobert Baldyga 			case USB_SPEED_HIGH:
1226c559a353SRobert Baldyga 				desc_idx = 1;
1227c559a353SRobert Baldyga 				break;
1228c559a353SRobert Baldyga 			default:
1229c559a353SRobert Baldyga 				desc_idx = 0;
1230c559a353SRobert Baldyga 			}
1231c559a353SRobert Baldyga 			desc = epfile->ep->descs[desc_idx];
1232c559a353SRobert Baldyga 
1233c559a353SRobert Baldyga 			spin_unlock_irq(&epfile->ffs->eps_lock);
1234f199a80cSVincent Pelletier 			ret = copy_to_user((void *)value, desc, desc->bLength);
1235c559a353SRobert Baldyga 			if (ret)
1236c559a353SRobert Baldyga 				ret = -EFAULT;
1237c559a353SRobert Baldyga 			return ret;
1238c559a353SRobert Baldyga 		}
123900a2430fSAndrzej Pietrasiewicz 		default:
124000a2430fSAndrzej Pietrasiewicz 			ret = -ENOTTY;
124100a2430fSAndrzej Pietrasiewicz 		}
124200a2430fSAndrzej Pietrasiewicz 	} else {
124300a2430fSAndrzej Pietrasiewicz 		ret = -ENODEV;
124400a2430fSAndrzej Pietrasiewicz 	}
124500a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
124600a2430fSAndrzej Pietrasiewicz 
124700a2430fSAndrzej Pietrasiewicz 	return ret;
124800a2430fSAndrzej Pietrasiewicz }
124900a2430fSAndrzej Pietrasiewicz 
125000a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_epfile_operations = {
125100a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
125200a2430fSAndrzej Pietrasiewicz 
125300a2430fSAndrzej Pietrasiewicz 	.open =		ffs_epfile_open,
125470e60d91SAl Viro 	.write_iter =	ffs_epfile_write_iter,
125570e60d91SAl Viro 	.read_iter =	ffs_epfile_read_iter,
125600a2430fSAndrzej Pietrasiewicz 	.release =	ffs_epfile_release,
125700a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_epfile_ioctl,
125800a2430fSAndrzej Pietrasiewicz };
125900a2430fSAndrzej Pietrasiewicz 
126000a2430fSAndrzej Pietrasiewicz 
126100a2430fSAndrzej Pietrasiewicz /* File system and super block operations ***********************************/
126200a2430fSAndrzej Pietrasiewicz 
126300a2430fSAndrzej Pietrasiewicz /*
126400a2430fSAndrzej Pietrasiewicz  * Mounting the file system creates a controller file, used first for
126500a2430fSAndrzej Pietrasiewicz  * function configuration then later for event monitoring.
126600a2430fSAndrzej Pietrasiewicz  */
126700a2430fSAndrzej Pietrasiewicz 
126800a2430fSAndrzej Pietrasiewicz static struct inode *__must_check
126900a2430fSAndrzej Pietrasiewicz ffs_sb_make_inode(struct super_block *sb, void *data,
127000a2430fSAndrzej Pietrasiewicz 		  const struct file_operations *fops,
127100a2430fSAndrzej Pietrasiewicz 		  const struct inode_operations *iops,
127200a2430fSAndrzej Pietrasiewicz 		  struct ffs_file_perms *perms)
127300a2430fSAndrzej Pietrasiewicz {
127400a2430fSAndrzej Pietrasiewicz 	struct inode *inode;
127500a2430fSAndrzej Pietrasiewicz 
127600a2430fSAndrzej Pietrasiewicz 	ENTER();
127700a2430fSAndrzej Pietrasiewicz 
127800a2430fSAndrzej Pietrasiewicz 	inode = new_inode(sb);
127900a2430fSAndrzej Pietrasiewicz 
128000a2430fSAndrzej Pietrasiewicz 	if (likely(inode)) {
1281078cd827SDeepa Dinamani 		struct timespec ts = current_time(inode);
128200a2430fSAndrzej Pietrasiewicz 
128300a2430fSAndrzej Pietrasiewicz 		inode->i_ino	 = get_next_ino();
128400a2430fSAndrzej Pietrasiewicz 		inode->i_mode    = perms->mode;
128500a2430fSAndrzej Pietrasiewicz 		inode->i_uid     = perms->uid;
128600a2430fSAndrzej Pietrasiewicz 		inode->i_gid     = perms->gid;
1287078cd827SDeepa Dinamani 		inode->i_atime   = ts;
1288078cd827SDeepa Dinamani 		inode->i_mtime   = ts;
1289078cd827SDeepa Dinamani 		inode->i_ctime   = ts;
129000a2430fSAndrzej Pietrasiewicz 		inode->i_private = data;
129100a2430fSAndrzej Pietrasiewicz 		if (fops)
129200a2430fSAndrzej Pietrasiewicz 			inode->i_fop = fops;
129300a2430fSAndrzej Pietrasiewicz 		if (iops)
129400a2430fSAndrzej Pietrasiewicz 			inode->i_op  = iops;
129500a2430fSAndrzej Pietrasiewicz 	}
129600a2430fSAndrzej Pietrasiewicz 
129700a2430fSAndrzej Pietrasiewicz 	return inode;
129800a2430fSAndrzej Pietrasiewicz }
129900a2430fSAndrzej Pietrasiewicz 
130000a2430fSAndrzej Pietrasiewicz /* Create "regular" file */
13011bb27cacSAl Viro static struct dentry *ffs_sb_create_file(struct super_block *sb,
130200a2430fSAndrzej Pietrasiewicz 					const char *name, void *data,
13031bb27cacSAl Viro 					const struct file_operations *fops)
130400a2430fSAndrzej Pietrasiewicz {
130500a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = sb->s_fs_info;
130600a2430fSAndrzej Pietrasiewicz 	struct dentry	*dentry;
130700a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
130800a2430fSAndrzej Pietrasiewicz 
130900a2430fSAndrzej Pietrasiewicz 	ENTER();
131000a2430fSAndrzej Pietrasiewicz 
131100a2430fSAndrzej Pietrasiewicz 	dentry = d_alloc_name(sb->s_root, name);
131200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!dentry))
131300a2430fSAndrzej Pietrasiewicz 		return NULL;
131400a2430fSAndrzej Pietrasiewicz 
131500a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
131600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!inode)) {
131700a2430fSAndrzej Pietrasiewicz 		dput(dentry);
131800a2430fSAndrzej Pietrasiewicz 		return NULL;
131900a2430fSAndrzej Pietrasiewicz 	}
132000a2430fSAndrzej Pietrasiewicz 
132100a2430fSAndrzej Pietrasiewicz 	d_add(dentry, inode);
13221bb27cacSAl Viro 	return dentry;
132300a2430fSAndrzej Pietrasiewicz }
132400a2430fSAndrzej Pietrasiewicz 
132500a2430fSAndrzej Pietrasiewicz /* Super block */
132600a2430fSAndrzej Pietrasiewicz static const struct super_operations ffs_sb_operations = {
132700a2430fSAndrzej Pietrasiewicz 	.statfs =	simple_statfs,
132800a2430fSAndrzej Pietrasiewicz 	.drop_inode =	generic_delete_inode,
132900a2430fSAndrzej Pietrasiewicz };
133000a2430fSAndrzej Pietrasiewicz 
133100a2430fSAndrzej Pietrasiewicz struct ffs_sb_fill_data {
133200a2430fSAndrzej Pietrasiewicz 	struct ffs_file_perms perms;
133300a2430fSAndrzej Pietrasiewicz 	umode_t root_mode;
133400a2430fSAndrzej Pietrasiewicz 	const char *dev_name;
133518d6b32fSRobert Baldyga 	bool no_disconnect;
133600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs_data;
133700a2430fSAndrzej Pietrasiewicz };
133800a2430fSAndrzej Pietrasiewicz 
133900a2430fSAndrzej Pietrasiewicz static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
134000a2430fSAndrzej Pietrasiewicz {
134100a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data *data = _data;
134200a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
134300a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = data->ffs_data;
134400a2430fSAndrzej Pietrasiewicz 
134500a2430fSAndrzej Pietrasiewicz 	ENTER();
134600a2430fSAndrzej Pietrasiewicz 
134700a2430fSAndrzej Pietrasiewicz 	ffs->sb              = sb;
134800a2430fSAndrzej Pietrasiewicz 	data->ffs_data       = NULL;
134900a2430fSAndrzej Pietrasiewicz 	sb->s_fs_info        = ffs;
135009cbfeafSKirill A. Shutemov 	sb->s_blocksize      = PAGE_SIZE;
135109cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
135200a2430fSAndrzej Pietrasiewicz 	sb->s_magic          = FUNCTIONFS_MAGIC;
135300a2430fSAndrzej Pietrasiewicz 	sb->s_op             = &ffs_sb_operations;
135400a2430fSAndrzej Pietrasiewicz 	sb->s_time_gran      = 1;
135500a2430fSAndrzej Pietrasiewicz 
135600a2430fSAndrzej Pietrasiewicz 	/* Root inode */
135700a2430fSAndrzej Pietrasiewicz 	data->perms.mode = data->root_mode;
135800a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, NULL,
135900a2430fSAndrzej Pietrasiewicz 				  &simple_dir_operations,
136000a2430fSAndrzej Pietrasiewicz 				  &simple_dir_inode_operations,
136100a2430fSAndrzej Pietrasiewicz 				  &data->perms);
136200a2430fSAndrzej Pietrasiewicz 	sb->s_root = d_make_root(inode);
136300a2430fSAndrzej Pietrasiewicz 	if (unlikely(!sb->s_root))
136400a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
136500a2430fSAndrzej Pietrasiewicz 
136600a2430fSAndrzej Pietrasiewicz 	/* EP0 file */
136700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
13681bb27cacSAl Viro 					 &ffs_ep0_operations)))
136900a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
137000a2430fSAndrzej Pietrasiewicz 
137100a2430fSAndrzej Pietrasiewicz 	return 0;
137200a2430fSAndrzej Pietrasiewicz }
137300a2430fSAndrzej Pietrasiewicz 
137400a2430fSAndrzej Pietrasiewicz static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
137500a2430fSAndrzej Pietrasiewicz {
137600a2430fSAndrzej Pietrasiewicz 	ENTER();
137700a2430fSAndrzej Pietrasiewicz 
137800a2430fSAndrzej Pietrasiewicz 	if (!opts || !*opts)
137900a2430fSAndrzej Pietrasiewicz 		return 0;
138000a2430fSAndrzej Pietrasiewicz 
138100a2430fSAndrzej Pietrasiewicz 	for (;;) {
138200a2430fSAndrzej Pietrasiewicz 		unsigned long value;
138300a2430fSAndrzej Pietrasiewicz 		char *eq, *comma;
138400a2430fSAndrzej Pietrasiewicz 
138500a2430fSAndrzej Pietrasiewicz 		/* Option limit */
138600a2430fSAndrzej Pietrasiewicz 		comma = strchr(opts, ',');
138700a2430fSAndrzej Pietrasiewicz 		if (comma)
138800a2430fSAndrzej Pietrasiewicz 			*comma = 0;
138900a2430fSAndrzej Pietrasiewicz 
139000a2430fSAndrzej Pietrasiewicz 		/* Value limit */
139100a2430fSAndrzej Pietrasiewicz 		eq = strchr(opts, '=');
139200a2430fSAndrzej Pietrasiewicz 		if (unlikely(!eq)) {
139300a2430fSAndrzej Pietrasiewicz 			pr_err("'=' missing in %s\n", opts);
139400a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
139500a2430fSAndrzej Pietrasiewicz 		}
139600a2430fSAndrzej Pietrasiewicz 		*eq = 0;
139700a2430fSAndrzej Pietrasiewicz 
139800a2430fSAndrzej Pietrasiewicz 		/* Parse value */
139900a2430fSAndrzej Pietrasiewicz 		if (kstrtoul(eq + 1, 0, &value)) {
140000a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid value: %s\n", opts, eq + 1);
140100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
140200a2430fSAndrzej Pietrasiewicz 		}
140300a2430fSAndrzej Pietrasiewicz 
140400a2430fSAndrzej Pietrasiewicz 		/* Interpret option */
140500a2430fSAndrzej Pietrasiewicz 		switch (eq - opts) {
140618d6b32fSRobert Baldyga 		case 13:
140718d6b32fSRobert Baldyga 			if (!memcmp(opts, "no_disconnect", 13))
140818d6b32fSRobert Baldyga 				data->no_disconnect = !!value;
140918d6b32fSRobert Baldyga 			else
141018d6b32fSRobert Baldyga 				goto invalid;
141118d6b32fSRobert Baldyga 			break;
141200a2430fSAndrzej Pietrasiewicz 		case 5:
141300a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "rmode", 5))
141400a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
141500a2430fSAndrzej Pietrasiewicz 			else if (!memcmp(opts, "fmode", 5))
141600a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
141700a2430fSAndrzej Pietrasiewicz 			else
141800a2430fSAndrzej Pietrasiewicz 				goto invalid;
141900a2430fSAndrzej Pietrasiewicz 			break;
142000a2430fSAndrzej Pietrasiewicz 
142100a2430fSAndrzej Pietrasiewicz 		case 4:
142200a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "mode", 4)) {
142300a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
142400a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
142500a2430fSAndrzej Pietrasiewicz 			} else {
142600a2430fSAndrzej Pietrasiewicz 				goto invalid;
142700a2430fSAndrzej Pietrasiewicz 			}
142800a2430fSAndrzej Pietrasiewicz 			break;
142900a2430fSAndrzej Pietrasiewicz 
143000a2430fSAndrzej Pietrasiewicz 		case 3:
143100a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "uid", 3)) {
143200a2430fSAndrzej Pietrasiewicz 				data->perms.uid = make_kuid(current_user_ns(), value);
143300a2430fSAndrzej Pietrasiewicz 				if (!uid_valid(data->perms.uid)) {
143400a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
143500a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
143600a2430fSAndrzej Pietrasiewicz 				}
143700a2430fSAndrzej Pietrasiewicz 			} else if (!memcmp(opts, "gid", 3)) {
143800a2430fSAndrzej Pietrasiewicz 				data->perms.gid = make_kgid(current_user_ns(), value);
143900a2430fSAndrzej Pietrasiewicz 				if (!gid_valid(data->perms.gid)) {
144000a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
144100a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
144200a2430fSAndrzej Pietrasiewicz 				}
144300a2430fSAndrzej Pietrasiewicz 			} else {
144400a2430fSAndrzej Pietrasiewicz 				goto invalid;
144500a2430fSAndrzej Pietrasiewicz 			}
144600a2430fSAndrzej Pietrasiewicz 			break;
144700a2430fSAndrzej Pietrasiewicz 
144800a2430fSAndrzej Pietrasiewicz 		default:
144900a2430fSAndrzej Pietrasiewicz invalid:
145000a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid option\n", opts);
145100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
145200a2430fSAndrzej Pietrasiewicz 		}
145300a2430fSAndrzej Pietrasiewicz 
145400a2430fSAndrzej Pietrasiewicz 		/* Next iteration */
145500a2430fSAndrzej Pietrasiewicz 		if (!comma)
145600a2430fSAndrzej Pietrasiewicz 			break;
145700a2430fSAndrzej Pietrasiewicz 		opts = comma + 1;
145800a2430fSAndrzej Pietrasiewicz 	}
145900a2430fSAndrzej Pietrasiewicz 
146000a2430fSAndrzej Pietrasiewicz 	return 0;
146100a2430fSAndrzej Pietrasiewicz }
146200a2430fSAndrzej Pietrasiewicz 
146300a2430fSAndrzej Pietrasiewicz /* "mount -t functionfs dev_name /dev/function" ends up here */
146400a2430fSAndrzej Pietrasiewicz 
146500a2430fSAndrzej Pietrasiewicz static struct dentry *
146600a2430fSAndrzej Pietrasiewicz ffs_fs_mount(struct file_system_type *t, int flags,
146700a2430fSAndrzej Pietrasiewicz 	      const char *dev_name, void *opts)
146800a2430fSAndrzej Pietrasiewicz {
146900a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data data = {
147000a2430fSAndrzej Pietrasiewicz 		.perms = {
147100a2430fSAndrzej Pietrasiewicz 			.mode = S_IFREG | 0600,
147200a2430fSAndrzej Pietrasiewicz 			.uid = GLOBAL_ROOT_UID,
147300a2430fSAndrzej Pietrasiewicz 			.gid = GLOBAL_ROOT_GID,
147400a2430fSAndrzej Pietrasiewicz 		},
147500a2430fSAndrzej Pietrasiewicz 		.root_mode = S_IFDIR | 0500,
147618d6b32fSRobert Baldyga 		.no_disconnect = false,
147700a2430fSAndrzej Pietrasiewicz 	};
147800a2430fSAndrzej Pietrasiewicz 	struct dentry *rv;
147900a2430fSAndrzej Pietrasiewicz 	int ret;
148000a2430fSAndrzej Pietrasiewicz 	void *ffs_dev;
148100a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs;
148200a2430fSAndrzej Pietrasiewicz 
148300a2430fSAndrzej Pietrasiewicz 	ENTER();
148400a2430fSAndrzej Pietrasiewicz 
148500a2430fSAndrzej Pietrasiewicz 	ret = ffs_fs_parse_opts(&data, opts);
148600a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
148700a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
148800a2430fSAndrzej Pietrasiewicz 
148900a2430fSAndrzej Pietrasiewicz 	ffs = ffs_data_new();
149000a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
149100a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
149200a2430fSAndrzej Pietrasiewicz 	ffs->file_perms = data.perms;
149318d6b32fSRobert Baldyga 	ffs->no_disconnect = data.no_disconnect;
149400a2430fSAndrzej Pietrasiewicz 
149500a2430fSAndrzej Pietrasiewicz 	ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
149600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->dev_name)) {
149700a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
149800a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
149900a2430fSAndrzej Pietrasiewicz 	}
150000a2430fSAndrzej Pietrasiewicz 
150100a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_acquire_dev(dev_name);
150200a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_dev)) {
150300a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
150400a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(ffs_dev);
150500a2430fSAndrzej Pietrasiewicz 	}
150600a2430fSAndrzej Pietrasiewicz 	ffs->private_data = ffs_dev;
150700a2430fSAndrzej Pietrasiewicz 	data.ffs_data = ffs;
150800a2430fSAndrzej Pietrasiewicz 
150900a2430fSAndrzej Pietrasiewicz 	rv = mount_nodev(t, flags, &data, ffs_sb_fill);
151000a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(rv) && data.ffs_data) {
151100a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(data.ffs_data);
151200a2430fSAndrzej Pietrasiewicz 		ffs_data_put(data.ffs_data);
151300a2430fSAndrzej Pietrasiewicz 	}
151400a2430fSAndrzej Pietrasiewicz 	return rv;
151500a2430fSAndrzej Pietrasiewicz }
151600a2430fSAndrzej Pietrasiewicz 
151700a2430fSAndrzej Pietrasiewicz static void
151800a2430fSAndrzej Pietrasiewicz ffs_fs_kill_sb(struct super_block *sb)
151900a2430fSAndrzej Pietrasiewicz {
152000a2430fSAndrzej Pietrasiewicz 	ENTER();
152100a2430fSAndrzej Pietrasiewicz 
152200a2430fSAndrzej Pietrasiewicz 	kill_litter_super(sb);
152300a2430fSAndrzej Pietrasiewicz 	if (sb->s_fs_info) {
152400a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(sb->s_fs_info);
152518d6b32fSRobert Baldyga 		ffs_data_closed(sb->s_fs_info);
152600a2430fSAndrzej Pietrasiewicz 		ffs_data_put(sb->s_fs_info);
152700a2430fSAndrzej Pietrasiewicz 	}
152800a2430fSAndrzej Pietrasiewicz }
152900a2430fSAndrzej Pietrasiewicz 
153000a2430fSAndrzej Pietrasiewicz static struct file_system_type ffs_fs_type = {
153100a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
153200a2430fSAndrzej Pietrasiewicz 	.name		= "functionfs",
153300a2430fSAndrzej Pietrasiewicz 	.mount		= ffs_fs_mount,
153400a2430fSAndrzej Pietrasiewicz 	.kill_sb	= ffs_fs_kill_sb,
153500a2430fSAndrzej Pietrasiewicz };
153600a2430fSAndrzej Pietrasiewicz MODULE_ALIAS_FS("functionfs");
153700a2430fSAndrzej Pietrasiewicz 
153800a2430fSAndrzej Pietrasiewicz 
153900a2430fSAndrzej Pietrasiewicz /* Driver's main init/cleanup functions *************************************/
154000a2430fSAndrzej Pietrasiewicz 
154100a2430fSAndrzej Pietrasiewicz static int functionfs_init(void)
154200a2430fSAndrzej Pietrasiewicz {
154300a2430fSAndrzej Pietrasiewicz 	int ret;
154400a2430fSAndrzej Pietrasiewicz 
154500a2430fSAndrzej Pietrasiewicz 	ENTER();
154600a2430fSAndrzej Pietrasiewicz 
154700a2430fSAndrzej Pietrasiewicz 	ret = register_filesystem(&ffs_fs_type);
154800a2430fSAndrzej Pietrasiewicz 	if (likely(!ret))
154900a2430fSAndrzej Pietrasiewicz 		pr_info("file system registered\n");
155000a2430fSAndrzej Pietrasiewicz 	else
155100a2430fSAndrzej Pietrasiewicz 		pr_err("failed registering file system (%d)\n", ret);
155200a2430fSAndrzej Pietrasiewicz 
155300a2430fSAndrzej Pietrasiewicz 	return ret;
155400a2430fSAndrzej Pietrasiewicz }
155500a2430fSAndrzej Pietrasiewicz 
155600a2430fSAndrzej Pietrasiewicz static void functionfs_cleanup(void)
155700a2430fSAndrzej Pietrasiewicz {
155800a2430fSAndrzej Pietrasiewicz 	ENTER();
155900a2430fSAndrzej Pietrasiewicz 
156000a2430fSAndrzej Pietrasiewicz 	pr_info("unloading\n");
156100a2430fSAndrzej Pietrasiewicz 	unregister_filesystem(&ffs_fs_type);
156200a2430fSAndrzej Pietrasiewicz }
156300a2430fSAndrzej Pietrasiewicz 
156400a2430fSAndrzej Pietrasiewicz 
156500a2430fSAndrzej Pietrasiewicz /* ffs_data and ffs_function construction and destruction code **************/
156600a2430fSAndrzej Pietrasiewicz 
156700a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs);
156800a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs);
156900a2430fSAndrzej Pietrasiewicz 
157000a2430fSAndrzej Pietrasiewicz static void ffs_data_get(struct ffs_data *ffs)
157100a2430fSAndrzej Pietrasiewicz {
157200a2430fSAndrzej Pietrasiewicz 	ENTER();
157300a2430fSAndrzej Pietrasiewicz 
157400a2430fSAndrzej Pietrasiewicz 	atomic_inc(&ffs->ref);
157500a2430fSAndrzej Pietrasiewicz }
157600a2430fSAndrzej Pietrasiewicz 
157700a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs)
157800a2430fSAndrzej Pietrasiewicz {
157900a2430fSAndrzej Pietrasiewicz 	ENTER();
158000a2430fSAndrzej Pietrasiewicz 
158100a2430fSAndrzej Pietrasiewicz 	atomic_inc(&ffs->ref);
158218d6b32fSRobert Baldyga 	if (atomic_add_return(1, &ffs->opened) == 1 &&
158318d6b32fSRobert Baldyga 			ffs->state == FFS_DEACTIVATED) {
158418d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
158518d6b32fSRobert Baldyga 		ffs_data_reset(ffs);
158618d6b32fSRobert Baldyga 	}
158700a2430fSAndrzej Pietrasiewicz }
158800a2430fSAndrzej Pietrasiewicz 
158900a2430fSAndrzej Pietrasiewicz static void ffs_data_put(struct ffs_data *ffs)
159000a2430fSAndrzej Pietrasiewicz {
159100a2430fSAndrzej Pietrasiewicz 	ENTER();
159200a2430fSAndrzej Pietrasiewicz 
159300a2430fSAndrzej Pietrasiewicz 	if (unlikely(atomic_dec_and_test(&ffs->ref))) {
159400a2430fSAndrzej Pietrasiewicz 		pr_info("%s(): freeing\n", __func__);
159500a2430fSAndrzej Pietrasiewicz 		ffs_data_clear(ffs);
159600a2430fSAndrzej Pietrasiewicz 		BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
159700a2430fSAndrzej Pietrasiewicz 		       waitqueue_active(&ffs->ep0req_completion.wait));
159800a2430fSAndrzej Pietrasiewicz 		kfree(ffs->dev_name);
159900a2430fSAndrzej Pietrasiewicz 		kfree(ffs);
160000a2430fSAndrzej Pietrasiewicz 	}
160100a2430fSAndrzej Pietrasiewicz }
160200a2430fSAndrzej Pietrasiewicz 
160300a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs)
160400a2430fSAndrzej Pietrasiewicz {
160500a2430fSAndrzej Pietrasiewicz 	ENTER();
160600a2430fSAndrzej Pietrasiewicz 
160700a2430fSAndrzej Pietrasiewicz 	if (atomic_dec_and_test(&ffs->opened)) {
160818d6b32fSRobert Baldyga 		if (ffs->no_disconnect) {
160918d6b32fSRobert Baldyga 			ffs->state = FFS_DEACTIVATED;
161018d6b32fSRobert Baldyga 			if (ffs->epfiles) {
161118d6b32fSRobert Baldyga 				ffs_epfiles_destroy(ffs->epfiles,
161218d6b32fSRobert Baldyga 						   ffs->eps_count);
161318d6b32fSRobert Baldyga 				ffs->epfiles = NULL;
161418d6b32fSRobert Baldyga 			}
161518d6b32fSRobert Baldyga 			if (ffs->setup_state == FFS_SETUP_PENDING)
161618d6b32fSRobert Baldyga 				__ffs_ep0_stall(ffs);
161718d6b32fSRobert Baldyga 		} else {
161818d6b32fSRobert Baldyga 			ffs->state = FFS_CLOSING;
161918d6b32fSRobert Baldyga 			ffs_data_reset(ffs);
162018d6b32fSRobert Baldyga 		}
162118d6b32fSRobert Baldyga 	}
162218d6b32fSRobert Baldyga 	if (atomic_read(&ffs->opened) < 0) {
162300a2430fSAndrzej Pietrasiewicz 		ffs->state = FFS_CLOSING;
162400a2430fSAndrzej Pietrasiewicz 		ffs_data_reset(ffs);
162500a2430fSAndrzej Pietrasiewicz 	}
162600a2430fSAndrzej Pietrasiewicz 
162700a2430fSAndrzej Pietrasiewicz 	ffs_data_put(ffs);
162800a2430fSAndrzej Pietrasiewicz }
162900a2430fSAndrzej Pietrasiewicz 
163000a2430fSAndrzej Pietrasiewicz static struct ffs_data *ffs_data_new(void)
163100a2430fSAndrzej Pietrasiewicz {
163200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
163300a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
163400a2430fSAndrzej Pietrasiewicz 		return NULL;
163500a2430fSAndrzej Pietrasiewicz 
163600a2430fSAndrzej Pietrasiewicz 	ENTER();
163700a2430fSAndrzej Pietrasiewicz 
163800a2430fSAndrzej Pietrasiewicz 	atomic_set(&ffs->ref, 1);
163900a2430fSAndrzej Pietrasiewicz 	atomic_set(&ffs->opened, 0);
164000a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
164100a2430fSAndrzej Pietrasiewicz 	mutex_init(&ffs->mutex);
164200a2430fSAndrzej Pietrasiewicz 	spin_lock_init(&ffs->eps_lock);
164300a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&ffs->ev.waitq);
164400a2430fSAndrzej Pietrasiewicz 	init_completion(&ffs->ep0req_completion);
164500a2430fSAndrzej Pietrasiewicz 
164600a2430fSAndrzej Pietrasiewicz 	/* XXX REVISIT need to update it in some places, or do we? */
164700a2430fSAndrzej Pietrasiewicz 	ffs->ev.can_stall = 1;
164800a2430fSAndrzej Pietrasiewicz 
164900a2430fSAndrzej Pietrasiewicz 	return ffs;
165000a2430fSAndrzej Pietrasiewicz }
165100a2430fSAndrzej Pietrasiewicz 
165200a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs)
165300a2430fSAndrzej Pietrasiewicz {
165400a2430fSAndrzej Pietrasiewicz 	ENTER();
165500a2430fSAndrzej Pietrasiewicz 
165600a2430fSAndrzej Pietrasiewicz 	ffs_closed(ffs);
165700a2430fSAndrzej Pietrasiewicz 
165800a2430fSAndrzej Pietrasiewicz 	BUG_ON(ffs->gadget);
165900a2430fSAndrzej Pietrasiewicz 
166000a2430fSAndrzej Pietrasiewicz 	if (ffs->epfiles)
166100a2430fSAndrzej Pietrasiewicz 		ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
166200a2430fSAndrzej Pietrasiewicz 
16635e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
16645e33f6fdSRobert Baldyga 		eventfd_ctx_put(ffs->ffs_eventfd);
16655e33f6fdSRobert Baldyga 
166600a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_descs_data);
166700a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_strings);
166800a2430fSAndrzej Pietrasiewicz 	kfree(ffs->stringtabs);
166900a2430fSAndrzej Pietrasiewicz }
167000a2430fSAndrzej Pietrasiewicz 
167100a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs)
167200a2430fSAndrzej Pietrasiewicz {
167300a2430fSAndrzej Pietrasiewicz 	ENTER();
167400a2430fSAndrzej Pietrasiewicz 
167500a2430fSAndrzej Pietrasiewicz 	ffs_data_clear(ffs);
167600a2430fSAndrzej Pietrasiewicz 
167700a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = NULL;
167800a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data = NULL;
167900a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs = NULL;
168000a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = NULL;
168100a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = NULL;
168200a2430fSAndrzej Pietrasiewicz 
168300a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length = 0;
168400a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count = 0;
168500a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count = 0;
168600a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count = 0;
168700a2430fSAndrzej Pietrasiewicz 
168800a2430fSAndrzej Pietrasiewicz 	ffs->strings_count = 0;
168900a2430fSAndrzej Pietrasiewicz 	ffs->interfaces_count = 0;
169000a2430fSAndrzej Pietrasiewicz 	ffs->eps_count = 0;
169100a2430fSAndrzej Pietrasiewicz 
169200a2430fSAndrzej Pietrasiewicz 	ffs->ev.count = 0;
169300a2430fSAndrzej Pietrasiewicz 
169400a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
169500a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
169600a2430fSAndrzej Pietrasiewicz 	ffs->flags = 0;
169700a2430fSAndrzej Pietrasiewicz }
169800a2430fSAndrzej Pietrasiewicz 
169900a2430fSAndrzej Pietrasiewicz 
170000a2430fSAndrzej Pietrasiewicz static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
170100a2430fSAndrzej Pietrasiewicz {
170200a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **lang;
170300a2430fSAndrzej Pietrasiewicz 	int first_id;
170400a2430fSAndrzej Pietrasiewicz 
170500a2430fSAndrzej Pietrasiewicz 	ENTER();
170600a2430fSAndrzej Pietrasiewicz 
170700a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs->state != FFS_ACTIVE
170800a2430fSAndrzej Pietrasiewicz 		 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
170900a2430fSAndrzej Pietrasiewicz 		return -EBADFD;
171000a2430fSAndrzej Pietrasiewicz 
171100a2430fSAndrzej Pietrasiewicz 	first_id = usb_string_ids_n(cdev, ffs->strings_count);
171200a2430fSAndrzej Pietrasiewicz 	if (unlikely(first_id < 0))
171300a2430fSAndrzej Pietrasiewicz 		return first_id;
171400a2430fSAndrzej Pietrasiewicz 
171500a2430fSAndrzej Pietrasiewicz 	ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
171600a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->ep0req))
171700a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
171800a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->complete = ffs_ep0_complete;
171900a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->context = ffs;
172000a2430fSAndrzej Pietrasiewicz 
172100a2430fSAndrzej Pietrasiewicz 	lang = ffs->stringtabs;
172261fe2d75SGreg Kroah-Hartman 	if (lang) {
172361fe2d75SGreg Kroah-Hartman 		for (; *lang; ++lang) {
172400a2430fSAndrzej Pietrasiewicz 			struct usb_string *str = (*lang)->strings;
172500a2430fSAndrzej Pietrasiewicz 			int id = first_id;
172600a2430fSAndrzej Pietrasiewicz 			for (; str->s; ++id, ++str)
172700a2430fSAndrzej Pietrasiewicz 				str->id = id;
172800a2430fSAndrzej Pietrasiewicz 		}
172961fe2d75SGreg Kroah-Hartman 	}
173000a2430fSAndrzej Pietrasiewicz 
173100a2430fSAndrzej Pietrasiewicz 	ffs->gadget = cdev->gadget;
173200a2430fSAndrzej Pietrasiewicz 	ffs_data_get(ffs);
173300a2430fSAndrzej Pietrasiewicz 	return 0;
173400a2430fSAndrzej Pietrasiewicz }
173500a2430fSAndrzej Pietrasiewicz 
173600a2430fSAndrzej Pietrasiewicz static void functionfs_unbind(struct ffs_data *ffs)
173700a2430fSAndrzej Pietrasiewicz {
173800a2430fSAndrzej Pietrasiewicz 	ENTER();
173900a2430fSAndrzej Pietrasiewicz 
174000a2430fSAndrzej Pietrasiewicz 	if (!WARN_ON(!ffs->gadget)) {
174100a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
174200a2430fSAndrzej Pietrasiewicz 		ffs->ep0req = NULL;
174300a2430fSAndrzej Pietrasiewicz 		ffs->gadget = NULL;
174400a2430fSAndrzej Pietrasiewicz 		clear_bit(FFS_FL_BOUND, &ffs->flags);
174500a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
174600a2430fSAndrzej Pietrasiewicz 	}
174700a2430fSAndrzej Pietrasiewicz }
174800a2430fSAndrzej Pietrasiewicz 
174900a2430fSAndrzej Pietrasiewicz static int ffs_epfiles_create(struct ffs_data *ffs)
175000a2430fSAndrzej Pietrasiewicz {
175100a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile, *epfiles;
175200a2430fSAndrzej Pietrasiewicz 	unsigned i, count;
175300a2430fSAndrzej Pietrasiewicz 
175400a2430fSAndrzej Pietrasiewicz 	ENTER();
175500a2430fSAndrzej Pietrasiewicz 
175600a2430fSAndrzej Pietrasiewicz 	count = ffs->eps_count;
175700a2430fSAndrzej Pietrasiewicz 	epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
175800a2430fSAndrzej Pietrasiewicz 	if (!epfiles)
175900a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
176000a2430fSAndrzej Pietrasiewicz 
176100a2430fSAndrzej Pietrasiewicz 	epfile = epfiles;
176200a2430fSAndrzej Pietrasiewicz 	for (i = 1; i <= count; ++i, ++epfile) {
176300a2430fSAndrzej Pietrasiewicz 		epfile->ffs = ffs;
176400a2430fSAndrzej Pietrasiewicz 		mutex_init(&epfile->mutex);
176500a2430fSAndrzej Pietrasiewicz 		init_waitqueue_head(&epfile->wait);
17661b0bf88fSRobert Baldyga 		if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1767acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
17681b0bf88fSRobert Baldyga 		else
1769acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%u", i);
1770acba23feSMario Schuknecht 		epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
17711bb27cacSAl Viro 						 epfile,
17721bb27cacSAl Viro 						 &ffs_epfile_operations);
17731bb27cacSAl Viro 		if (unlikely(!epfile->dentry)) {
177400a2430fSAndrzej Pietrasiewicz 			ffs_epfiles_destroy(epfiles, i - 1);
177500a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
177600a2430fSAndrzej Pietrasiewicz 		}
177700a2430fSAndrzej Pietrasiewicz 	}
177800a2430fSAndrzej Pietrasiewicz 
177900a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = epfiles;
178000a2430fSAndrzej Pietrasiewicz 	return 0;
178100a2430fSAndrzej Pietrasiewicz }
178200a2430fSAndrzej Pietrasiewicz 
178300a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
178400a2430fSAndrzej Pietrasiewicz {
178500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = epfiles;
178600a2430fSAndrzej Pietrasiewicz 
178700a2430fSAndrzej Pietrasiewicz 	ENTER();
178800a2430fSAndrzej Pietrasiewicz 
178900a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++epfile) {
179000a2430fSAndrzej Pietrasiewicz 		BUG_ON(mutex_is_locked(&epfile->mutex) ||
179100a2430fSAndrzej Pietrasiewicz 		       waitqueue_active(&epfile->wait));
179200a2430fSAndrzej Pietrasiewicz 		if (epfile->dentry) {
179300a2430fSAndrzej Pietrasiewicz 			d_delete(epfile->dentry);
179400a2430fSAndrzej Pietrasiewicz 			dput(epfile->dentry);
179500a2430fSAndrzej Pietrasiewicz 			epfile->dentry = NULL;
179600a2430fSAndrzej Pietrasiewicz 		}
179700a2430fSAndrzej Pietrasiewicz 	}
179800a2430fSAndrzej Pietrasiewicz 
179900a2430fSAndrzej Pietrasiewicz 	kfree(epfiles);
180000a2430fSAndrzej Pietrasiewicz }
180100a2430fSAndrzej Pietrasiewicz 
180200a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func)
180300a2430fSAndrzej Pietrasiewicz {
180400a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
180500a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = func->ffs->epfiles;
180600a2430fSAndrzej Pietrasiewicz 	unsigned count            = func->ffs->eps_count;
180700a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
180800a2430fSAndrzej Pietrasiewicz 
18099353afbbSMichal Nazarewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
181008f37148SVincent Pelletier 	while (count--) {
181100a2430fSAndrzej Pietrasiewicz 		/* pending requests get nuked */
181200a2430fSAndrzej Pietrasiewicz 		if (likely(ep->ep))
181300a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(ep->ep);
181400a2430fSAndrzej Pietrasiewicz 		++ep;
181518d6b32fSRobert Baldyga 
181618d6b32fSRobert Baldyga 		if (epfile) {
1817a9e6f83cSMichal Nazarewicz 			epfile->ep = NULL;
1818a9e6f83cSMichal Nazarewicz 			__ffs_epfile_read_buffer_free(epfile);
181900a2430fSAndrzej Pietrasiewicz 			++epfile;
182018d6b32fSRobert Baldyga 		}
182108f37148SVincent Pelletier 	}
1822a9e6f83cSMichal Nazarewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
182300a2430fSAndrzej Pietrasiewicz }
182400a2430fSAndrzej Pietrasiewicz 
182500a2430fSAndrzej Pietrasiewicz static int ffs_func_eps_enable(struct ffs_function *func)
182600a2430fSAndrzej Pietrasiewicz {
182700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs      = func->ffs;
182800a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
182900a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = ffs->epfiles;
183000a2430fSAndrzej Pietrasiewicz 	unsigned count            = ffs->eps_count;
183100a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
183200a2430fSAndrzej Pietrasiewicz 	int ret = 0;
183300a2430fSAndrzej Pietrasiewicz 
183400a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
183508f37148SVincent Pelletier 	while(count--) {
183600a2430fSAndrzej Pietrasiewicz 		struct usb_endpoint_descriptor *ds;
18372bfa0719SFelipe Balbi 		struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
18382bfa0719SFelipe Balbi 		int needs_comp_desc = false;
183900a2430fSAndrzej Pietrasiewicz 		int desc_idx;
184000a2430fSAndrzej Pietrasiewicz 
18412bfa0719SFelipe Balbi 		if (ffs->gadget->speed == USB_SPEED_SUPER) {
184200a2430fSAndrzej Pietrasiewicz 			desc_idx = 2;
18432bfa0719SFelipe Balbi 			needs_comp_desc = true;
18442bfa0719SFelipe Balbi 		} else if (ffs->gadget->speed == USB_SPEED_HIGH)
184500a2430fSAndrzej Pietrasiewicz 			desc_idx = 1;
184600a2430fSAndrzej Pietrasiewicz 		else
184700a2430fSAndrzej Pietrasiewicz 			desc_idx = 0;
184800a2430fSAndrzej Pietrasiewicz 
184900a2430fSAndrzej Pietrasiewicz 		/* fall-back to lower speed if desc missing for current speed */
185000a2430fSAndrzej Pietrasiewicz 		do {
185100a2430fSAndrzej Pietrasiewicz 			ds = ep->descs[desc_idx];
185200a2430fSAndrzej Pietrasiewicz 		} while (!ds && --desc_idx >= 0);
185300a2430fSAndrzej Pietrasiewicz 
185400a2430fSAndrzej Pietrasiewicz 		if (!ds) {
185500a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
185600a2430fSAndrzej Pietrasiewicz 			break;
185700a2430fSAndrzej Pietrasiewicz 		}
185800a2430fSAndrzej Pietrasiewicz 
185900a2430fSAndrzej Pietrasiewicz 		ep->ep->driver_data = ep;
186000a2430fSAndrzej Pietrasiewicz 		ep->ep->desc = ds;
18612bfa0719SFelipe Balbi 
18622bfa0719SFelipe Balbi 		comp_desc = (struct usb_ss_ep_comp_descriptor *)(ds +
18632bfa0719SFelipe Balbi 				USB_DT_ENDPOINT_SIZE);
18642bfa0719SFelipe Balbi 		ep->ep->maxburst = comp_desc->bMaxBurst + 1;
18652bfa0719SFelipe Balbi 
18662bfa0719SFelipe Balbi 		if (needs_comp_desc)
18672bfa0719SFelipe Balbi 			ep->ep->comp_desc = comp_desc;
18682bfa0719SFelipe Balbi 
186900a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_enable(ep->ep);
187000a2430fSAndrzej Pietrasiewicz 		if (likely(!ret)) {
187100a2430fSAndrzej Pietrasiewicz 			epfile->ep = ep;
187200a2430fSAndrzej Pietrasiewicz 			epfile->in = usb_endpoint_dir_in(ds);
187300a2430fSAndrzej Pietrasiewicz 			epfile->isoc = usb_endpoint_xfer_isoc(ds);
187400a2430fSAndrzej Pietrasiewicz 		} else {
187500a2430fSAndrzej Pietrasiewicz 			break;
187600a2430fSAndrzej Pietrasiewicz 		}
187700a2430fSAndrzej Pietrasiewicz 
187800a2430fSAndrzej Pietrasiewicz 		wake_up(&epfile->wait);
187900a2430fSAndrzej Pietrasiewicz 
188000a2430fSAndrzej Pietrasiewicz 		++ep;
188100a2430fSAndrzej Pietrasiewicz 		++epfile;
188208f37148SVincent Pelletier 	}
188300a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
188400a2430fSAndrzej Pietrasiewicz 
188500a2430fSAndrzej Pietrasiewicz 	return ret;
188600a2430fSAndrzej Pietrasiewicz }
188700a2430fSAndrzej Pietrasiewicz 
188800a2430fSAndrzej Pietrasiewicz 
188900a2430fSAndrzej Pietrasiewicz /* Parsing and building descriptors and strings *****************************/
189000a2430fSAndrzej Pietrasiewicz 
189100a2430fSAndrzej Pietrasiewicz /*
189200a2430fSAndrzej Pietrasiewicz  * This validates if data pointed by data is a valid USB descriptor as
189300a2430fSAndrzej Pietrasiewicz  * well as record how many interfaces, endpoints and strings are
189400a2430fSAndrzej Pietrasiewicz  * required by given configuration.  Returns address after the
189500a2430fSAndrzej Pietrasiewicz  * descriptor or NULL if data is invalid.
189600a2430fSAndrzej Pietrasiewicz  */
189700a2430fSAndrzej Pietrasiewicz 
189800a2430fSAndrzej Pietrasiewicz enum ffs_entity_type {
189900a2430fSAndrzej Pietrasiewicz 	FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
190000a2430fSAndrzej Pietrasiewicz };
190100a2430fSAndrzej Pietrasiewicz 
190200a2430fSAndrzej Pietrasiewicz enum ffs_os_desc_type {
190300a2430fSAndrzej Pietrasiewicz 	FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
190400a2430fSAndrzej Pietrasiewicz };
190500a2430fSAndrzej Pietrasiewicz 
190600a2430fSAndrzej Pietrasiewicz typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
190700a2430fSAndrzej Pietrasiewicz 				   u8 *valuep,
190800a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
190900a2430fSAndrzej Pietrasiewicz 				   void *priv);
191000a2430fSAndrzej Pietrasiewicz 
191100a2430fSAndrzej Pietrasiewicz typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
191200a2430fSAndrzej Pietrasiewicz 				    struct usb_os_desc_header *h, void *data,
191300a2430fSAndrzej Pietrasiewicz 				    unsigned len, void *priv);
191400a2430fSAndrzej Pietrasiewicz 
191500a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_desc(char *data, unsigned len,
191600a2430fSAndrzej Pietrasiewicz 					   ffs_entity_callback entity,
191700a2430fSAndrzej Pietrasiewicz 					   void *priv)
191800a2430fSAndrzej Pietrasiewicz {
191900a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header *_ds = (void *)data;
192000a2430fSAndrzej Pietrasiewicz 	u8 length;
192100a2430fSAndrzej Pietrasiewicz 	int ret;
192200a2430fSAndrzej Pietrasiewicz 
192300a2430fSAndrzej Pietrasiewicz 	ENTER();
192400a2430fSAndrzej Pietrasiewicz 
192500a2430fSAndrzej Pietrasiewicz 	/* At least two bytes are required: length and type */
192600a2430fSAndrzej Pietrasiewicz 	if (len < 2) {
192700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor too short\n");
192800a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
192900a2430fSAndrzej Pietrasiewicz 	}
193000a2430fSAndrzej Pietrasiewicz 
193100a2430fSAndrzej Pietrasiewicz 	/* If we have at least as many bytes as the descriptor takes? */
193200a2430fSAndrzej Pietrasiewicz 	length = _ds->bLength;
193300a2430fSAndrzej Pietrasiewicz 	if (len < length) {
193400a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor longer then available data\n");
193500a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
193600a2430fSAndrzej Pietrasiewicz 	}
193700a2430fSAndrzej Pietrasiewicz 
193800a2430fSAndrzej Pietrasiewicz #define __entity_check_INTERFACE(val)  1
193900a2430fSAndrzej Pietrasiewicz #define __entity_check_STRING(val)     (val)
194000a2430fSAndrzej Pietrasiewicz #define __entity_check_ENDPOINT(val)   ((val) & USB_ENDPOINT_NUMBER_MASK)
194100a2430fSAndrzej Pietrasiewicz #define __entity(type, val) do {					\
194200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("entity " #type "(%02x)\n", (val));		\
194300a2430fSAndrzej Pietrasiewicz 		if (unlikely(!__entity_check_ ##type(val))) {		\
194400a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid entity's value\n");		\
194500a2430fSAndrzej Pietrasiewicz 			return -EINVAL;					\
194600a2430fSAndrzej Pietrasiewicz 		}							\
194700a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_ ##type, &val, _ds, priv);		\
194800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {				\
194900a2430fSAndrzej Pietrasiewicz 			pr_debug("entity " #type "(%02x); ret = %d\n",	\
195000a2430fSAndrzej Pietrasiewicz 				 (val), ret);				\
195100a2430fSAndrzej Pietrasiewicz 			return ret;					\
195200a2430fSAndrzej Pietrasiewicz 		}							\
195300a2430fSAndrzej Pietrasiewicz 	} while (0)
195400a2430fSAndrzej Pietrasiewicz 
195500a2430fSAndrzej Pietrasiewicz 	/* Parse descriptor depending on type. */
195600a2430fSAndrzej Pietrasiewicz 	switch (_ds->bDescriptorType) {
195700a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE:
195800a2430fSAndrzej Pietrasiewicz 	case USB_DT_CONFIG:
195900a2430fSAndrzej Pietrasiewicz 	case USB_DT_STRING:
196000a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE_QUALIFIER:
196100a2430fSAndrzej Pietrasiewicz 		/* function can't have any of those */
196200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor reserved for gadget: %d\n",
196300a2430fSAndrzej Pietrasiewicz 		      _ds->bDescriptorType);
196400a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
196500a2430fSAndrzej Pietrasiewicz 
196600a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE: {
196700a2430fSAndrzej Pietrasiewicz 		struct usb_interface_descriptor *ds = (void *)_ds;
196800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface descriptor\n");
196900a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
197000a2430fSAndrzej Pietrasiewicz 			goto inv_length;
197100a2430fSAndrzej Pietrasiewicz 
197200a2430fSAndrzej Pietrasiewicz 		__entity(INTERFACE, ds->bInterfaceNumber);
197300a2430fSAndrzej Pietrasiewicz 		if (ds->iInterface)
197400a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iInterface);
197500a2430fSAndrzej Pietrasiewicz 	}
197600a2430fSAndrzej Pietrasiewicz 		break;
197700a2430fSAndrzej Pietrasiewicz 
197800a2430fSAndrzej Pietrasiewicz 	case USB_DT_ENDPOINT: {
197900a2430fSAndrzej Pietrasiewicz 		struct usb_endpoint_descriptor *ds = (void *)_ds;
198000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("endpoint descriptor\n");
198100a2430fSAndrzej Pietrasiewicz 		if (length != USB_DT_ENDPOINT_SIZE &&
198200a2430fSAndrzej Pietrasiewicz 		    length != USB_DT_ENDPOINT_AUDIO_SIZE)
198300a2430fSAndrzej Pietrasiewicz 			goto inv_length;
198400a2430fSAndrzej Pietrasiewicz 		__entity(ENDPOINT, ds->bEndpointAddress);
198500a2430fSAndrzej Pietrasiewicz 	}
198600a2430fSAndrzej Pietrasiewicz 		break;
198700a2430fSAndrzej Pietrasiewicz 
198800a2430fSAndrzej Pietrasiewicz 	case HID_DT_HID:
198900a2430fSAndrzej Pietrasiewicz 		pr_vdebug("hid descriptor\n");
199000a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct hid_descriptor))
199100a2430fSAndrzej Pietrasiewicz 			goto inv_length;
199200a2430fSAndrzej Pietrasiewicz 		break;
199300a2430fSAndrzej Pietrasiewicz 
199400a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTG:
199500a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_otg_descriptor))
199600a2430fSAndrzej Pietrasiewicz 			goto inv_length;
199700a2430fSAndrzej Pietrasiewicz 		break;
199800a2430fSAndrzej Pietrasiewicz 
199900a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_ASSOCIATION: {
200000a2430fSAndrzej Pietrasiewicz 		struct usb_interface_assoc_descriptor *ds = (void *)_ds;
200100a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface association descriptor\n");
200200a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
200300a2430fSAndrzej Pietrasiewicz 			goto inv_length;
200400a2430fSAndrzej Pietrasiewicz 		if (ds->iFunction)
200500a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iFunction);
200600a2430fSAndrzej Pietrasiewicz 	}
200700a2430fSAndrzej Pietrasiewicz 		break;
200800a2430fSAndrzej Pietrasiewicz 
200900a2430fSAndrzej Pietrasiewicz 	case USB_DT_SS_ENDPOINT_COMP:
201000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("EP SS companion descriptor\n");
201100a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_ss_ep_comp_descriptor))
201200a2430fSAndrzej Pietrasiewicz 			goto inv_length;
201300a2430fSAndrzej Pietrasiewicz 		break;
201400a2430fSAndrzej Pietrasiewicz 
201500a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTHER_SPEED_CONFIG:
201600a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_POWER:
201700a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEBUG:
201800a2430fSAndrzej Pietrasiewicz 	case USB_DT_SECURITY:
201900a2430fSAndrzej Pietrasiewicz 	case USB_DT_CS_RADIO_CONTROL:
202000a2430fSAndrzej Pietrasiewicz 		/* TODO */
202100a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
202200a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
202300a2430fSAndrzej Pietrasiewicz 
202400a2430fSAndrzej Pietrasiewicz 	default:
202500a2430fSAndrzej Pietrasiewicz 		/* We should never be here */
202600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
202700a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
202800a2430fSAndrzej Pietrasiewicz 
202900a2430fSAndrzej Pietrasiewicz inv_length:
203000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("invalid length: %d (descriptor %d)\n",
203100a2430fSAndrzej Pietrasiewicz 			  _ds->bLength, _ds->bDescriptorType);
203200a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
203300a2430fSAndrzej Pietrasiewicz 	}
203400a2430fSAndrzej Pietrasiewicz 
203500a2430fSAndrzej Pietrasiewicz #undef __entity
203600a2430fSAndrzej Pietrasiewicz #undef __entity_check_DESCRIPTOR
203700a2430fSAndrzej Pietrasiewicz #undef __entity_check_INTERFACE
203800a2430fSAndrzej Pietrasiewicz #undef __entity_check_STRING
203900a2430fSAndrzej Pietrasiewicz #undef __entity_check_ENDPOINT
204000a2430fSAndrzej Pietrasiewicz 
204100a2430fSAndrzej Pietrasiewicz 	return length;
204200a2430fSAndrzej Pietrasiewicz }
204300a2430fSAndrzej Pietrasiewicz 
204400a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
204500a2430fSAndrzej Pietrasiewicz 				     ffs_entity_callback entity, void *priv)
204600a2430fSAndrzej Pietrasiewicz {
204700a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
204800a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
204900a2430fSAndrzej Pietrasiewicz 
205000a2430fSAndrzej Pietrasiewicz 	ENTER();
205100a2430fSAndrzej Pietrasiewicz 
205200a2430fSAndrzej Pietrasiewicz 	for (;;) {
205300a2430fSAndrzej Pietrasiewicz 		int ret;
205400a2430fSAndrzej Pietrasiewicz 
205500a2430fSAndrzej Pietrasiewicz 		if (num == count)
205600a2430fSAndrzej Pietrasiewicz 			data = NULL;
205700a2430fSAndrzej Pietrasiewicz 
205800a2430fSAndrzej Pietrasiewicz 		/* Record "descriptor" entity */
205900a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
206000a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
206100a2430fSAndrzej Pietrasiewicz 			pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
206200a2430fSAndrzej Pietrasiewicz 				 num, ret);
206300a2430fSAndrzej Pietrasiewicz 			return ret;
206400a2430fSAndrzej Pietrasiewicz 		}
206500a2430fSAndrzej Pietrasiewicz 
206600a2430fSAndrzej Pietrasiewicz 		if (!data)
206700a2430fSAndrzej Pietrasiewicz 			return _len - len;
206800a2430fSAndrzej Pietrasiewicz 
206900a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_single_desc(data, len, entity, priv);
207000a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
207100a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
207200a2430fSAndrzej Pietrasiewicz 			return ret;
207300a2430fSAndrzej Pietrasiewicz 		}
207400a2430fSAndrzej Pietrasiewicz 
207500a2430fSAndrzej Pietrasiewicz 		len -= ret;
207600a2430fSAndrzej Pietrasiewicz 		data += ret;
207700a2430fSAndrzej Pietrasiewicz 		++num;
207800a2430fSAndrzej Pietrasiewicz 	}
207900a2430fSAndrzej Pietrasiewicz }
208000a2430fSAndrzej Pietrasiewicz 
208100a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_entity(enum ffs_entity_type type,
208200a2430fSAndrzej Pietrasiewicz 				u8 *valuep, struct usb_descriptor_header *desc,
208300a2430fSAndrzej Pietrasiewicz 				void *priv)
208400a2430fSAndrzej Pietrasiewicz {
20856d5c1c77SRobert Baldyga 	struct ffs_desc_helper *helper = priv;
20866d5c1c77SRobert Baldyga 	struct usb_endpoint_descriptor *d;
208700a2430fSAndrzej Pietrasiewicz 
208800a2430fSAndrzej Pietrasiewicz 	ENTER();
208900a2430fSAndrzej Pietrasiewicz 
209000a2430fSAndrzej Pietrasiewicz 	switch (type) {
209100a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
209200a2430fSAndrzej Pietrasiewicz 		break;
209300a2430fSAndrzej Pietrasiewicz 
209400a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
209500a2430fSAndrzej Pietrasiewicz 		/*
209600a2430fSAndrzej Pietrasiewicz 		 * Interfaces are indexed from zero so if we
209700a2430fSAndrzej Pietrasiewicz 		 * encountered interface "n" then there are at least
209800a2430fSAndrzej Pietrasiewicz 		 * "n+1" interfaces.
209900a2430fSAndrzej Pietrasiewicz 		 */
21006d5c1c77SRobert Baldyga 		if (*valuep >= helper->interfaces_count)
21016d5c1c77SRobert Baldyga 			helper->interfaces_count = *valuep + 1;
210200a2430fSAndrzej Pietrasiewicz 		break;
210300a2430fSAndrzej Pietrasiewicz 
210400a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
210500a2430fSAndrzej Pietrasiewicz 		/*
210696a420d2SVincent Pelletier 		 * Strings are indexed from 1 (0 is reserved
210796a420d2SVincent Pelletier 		 * for languages list)
210800a2430fSAndrzej Pietrasiewicz 		 */
21096d5c1c77SRobert Baldyga 		if (*valuep > helper->ffs->strings_count)
21106d5c1c77SRobert Baldyga 			helper->ffs->strings_count = *valuep;
211100a2430fSAndrzej Pietrasiewicz 		break;
211200a2430fSAndrzej Pietrasiewicz 
211300a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
21146d5c1c77SRobert Baldyga 		d = (void *)desc;
21156d5c1c77SRobert Baldyga 		helper->eps_count++;
211641dc9ac1SVincent Pelletier 		if (helper->eps_count >= FFS_MAX_EPS_COUNT)
21176d5c1c77SRobert Baldyga 			return -EINVAL;
21186d5c1c77SRobert Baldyga 		/* Check if descriptors for any speed were already parsed */
21196d5c1c77SRobert Baldyga 		if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
21206d5c1c77SRobert Baldyga 			helper->ffs->eps_addrmap[helper->eps_count] =
21216d5c1c77SRobert Baldyga 				d->bEndpointAddress;
21226d5c1c77SRobert Baldyga 		else if (helper->ffs->eps_addrmap[helper->eps_count] !=
21236d5c1c77SRobert Baldyga 				d->bEndpointAddress)
21246d5c1c77SRobert Baldyga 			return -EINVAL;
212500a2430fSAndrzej Pietrasiewicz 		break;
212600a2430fSAndrzej Pietrasiewicz 	}
212700a2430fSAndrzej Pietrasiewicz 
212800a2430fSAndrzej Pietrasiewicz 	return 0;
212900a2430fSAndrzej Pietrasiewicz }
213000a2430fSAndrzej Pietrasiewicz 
213100a2430fSAndrzej Pietrasiewicz static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
213200a2430fSAndrzej Pietrasiewicz 				   struct usb_os_desc_header *desc)
213300a2430fSAndrzej Pietrasiewicz {
213400a2430fSAndrzej Pietrasiewicz 	u16 bcd_version = le16_to_cpu(desc->bcdVersion);
213500a2430fSAndrzej Pietrasiewicz 	u16 w_index = le16_to_cpu(desc->wIndex);
213600a2430fSAndrzej Pietrasiewicz 
213700a2430fSAndrzej Pietrasiewicz 	if (bcd_version != 1) {
213800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptors version: %d",
213900a2430fSAndrzej Pietrasiewicz 			  bcd_version);
214000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
214100a2430fSAndrzej Pietrasiewicz 	}
214200a2430fSAndrzej Pietrasiewicz 	switch (w_index) {
214300a2430fSAndrzej Pietrasiewicz 	case 0x4:
214400a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_COMPAT;
214500a2430fSAndrzej Pietrasiewicz 		break;
214600a2430fSAndrzej Pietrasiewicz 	case 0x5:
214700a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_PROP;
214800a2430fSAndrzej Pietrasiewicz 		break;
214900a2430fSAndrzej Pietrasiewicz 	default:
215000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptor type: %d", w_index);
215100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
215200a2430fSAndrzej Pietrasiewicz 	}
215300a2430fSAndrzej Pietrasiewicz 
215400a2430fSAndrzej Pietrasiewicz 	return sizeof(*desc);
215500a2430fSAndrzej Pietrasiewicz }
215600a2430fSAndrzej Pietrasiewicz 
215700a2430fSAndrzej Pietrasiewicz /*
215800a2430fSAndrzej Pietrasiewicz  * Process all extended compatibility/extended property descriptors
215900a2430fSAndrzej Pietrasiewicz  * of a feature descriptor
216000a2430fSAndrzej Pietrasiewicz  */
216100a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
216200a2430fSAndrzej Pietrasiewicz 					      enum ffs_os_desc_type type,
216300a2430fSAndrzej Pietrasiewicz 					      u16 feature_count,
216400a2430fSAndrzej Pietrasiewicz 					      ffs_os_desc_callback entity,
216500a2430fSAndrzej Pietrasiewicz 					      void *priv,
216600a2430fSAndrzej Pietrasiewicz 					      struct usb_os_desc_header *h)
216700a2430fSAndrzej Pietrasiewicz {
216800a2430fSAndrzej Pietrasiewicz 	int ret;
216900a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
217000a2430fSAndrzej Pietrasiewicz 
217100a2430fSAndrzej Pietrasiewicz 	ENTER();
217200a2430fSAndrzej Pietrasiewicz 
217300a2430fSAndrzej Pietrasiewicz 	/* loop over all ext compat/ext prop descriptors */
217400a2430fSAndrzej Pietrasiewicz 	while (feature_count--) {
217500a2430fSAndrzej Pietrasiewicz 		ret = entity(type, h, data, len, priv);
217600a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
217700a2430fSAndrzej Pietrasiewicz 			pr_debug("bad OS descriptor, type: %d\n", type);
217800a2430fSAndrzej Pietrasiewicz 			return ret;
217900a2430fSAndrzej Pietrasiewicz 		}
218000a2430fSAndrzej Pietrasiewicz 		data += ret;
218100a2430fSAndrzej Pietrasiewicz 		len -= ret;
218200a2430fSAndrzej Pietrasiewicz 	}
218300a2430fSAndrzej Pietrasiewicz 	return _len - len;
218400a2430fSAndrzej Pietrasiewicz }
218500a2430fSAndrzej Pietrasiewicz 
218600a2430fSAndrzej Pietrasiewicz /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
218700a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_os_descs(unsigned count,
218800a2430fSAndrzej Pietrasiewicz 					char *data, unsigned len,
218900a2430fSAndrzej Pietrasiewicz 					ffs_os_desc_callback entity, void *priv)
219000a2430fSAndrzej Pietrasiewicz {
219100a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
219200a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
219300a2430fSAndrzej Pietrasiewicz 
219400a2430fSAndrzej Pietrasiewicz 	ENTER();
219500a2430fSAndrzej Pietrasiewicz 
219600a2430fSAndrzej Pietrasiewicz 	for (num = 0; num < count; ++num) {
219700a2430fSAndrzej Pietrasiewicz 		int ret;
219800a2430fSAndrzej Pietrasiewicz 		enum ffs_os_desc_type type;
219900a2430fSAndrzej Pietrasiewicz 		u16 feature_count;
220000a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_header *desc = (void *)data;
220100a2430fSAndrzej Pietrasiewicz 
220200a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*desc))
220300a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
220400a2430fSAndrzej Pietrasiewicz 
220500a2430fSAndrzej Pietrasiewicz 		/*
220600a2430fSAndrzej Pietrasiewicz 		 * Record "descriptor" entity.
220700a2430fSAndrzej Pietrasiewicz 		 * Process dwLength, bcdVersion, wIndex, get b/wCount.
220800a2430fSAndrzej Pietrasiewicz 		 * Move the data pointer to the beginning of extended
220900a2430fSAndrzej Pietrasiewicz 		 * compatibilities proper or extended properties proper
221000a2430fSAndrzej Pietrasiewicz 		 * portions of the data
221100a2430fSAndrzej Pietrasiewicz 		 */
221200a2430fSAndrzej Pietrasiewicz 		if (le32_to_cpu(desc->dwLength) > len)
221300a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
221400a2430fSAndrzej Pietrasiewicz 
221500a2430fSAndrzej Pietrasiewicz 		ret = __ffs_do_os_desc_header(&type, desc);
221600a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
221700a2430fSAndrzej Pietrasiewicz 			pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
221800a2430fSAndrzej Pietrasiewicz 				 num, ret);
221900a2430fSAndrzej Pietrasiewicz 			return ret;
222000a2430fSAndrzej Pietrasiewicz 		}
222100a2430fSAndrzej Pietrasiewicz 		/*
222200a2430fSAndrzej Pietrasiewicz 		 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
222300a2430fSAndrzej Pietrasiewicz 		 */
222400a2430fSAndrzej Pietrasiewicz 		feature_count = le16_to_cpu(desc->wCount);
222500a2430fSAndrzej Pietrasiewicz 		if (type == FFS_OS_DESC_EXT_COMPAT &&
222600a2430fSAndrzej Pietrasiewicz 		    (feature_count > 255 || desc->Reserved))
222700a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
222800a2430fSAndrzej Pietrasiewicz 		len -= ret;
222900a2430fSAndrzej Pietrasiewicz 		data += ret;
223000a2430fSAndrzej Pietrasiewicz 
223100a2430fSAndrzej Pietrasiewicz 		/*
223200a2430fSAndrzej Pietrasiewicz 		 * Process all function/property descriptors
223300a2430fSAndrzej Pietrasiewicz 		 * of this Feature Descriptor
223400a2430fSAndrzej Pietrasiewicz 		 */
223500a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_single_os_desc(data, len, type,
223600a2430fSAndrzej Pietrasiewicz 					    feature_count, entity, priv, desc);
223700a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
223800a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
223900a2430fSAndrzej Pietrasiewicz 			return ret;
224000a2430fSAndrzej Pietrasiewicz 		}
224100a2430fSAndrzej Pietrasiewicz 
224200a2430fSAndrzej Pietrasiewicz 		len -= ret;
224300a2430fSAndrzej Pietrasiewicz 		data += ret;
224400a2430fSAndrzej Pietrasiewicz 	}
224500a2430fSAndrzej Pietrasiewicz 	return _len - len;
224600a2430fSAndrzej Pietrasiewicz }
224700a2430fSAndrzej Pietrasiewicz 
224800a2430fSAndrzej Pietrasiewicz /**
224900a2430fSAndrzej Pietrasiewicz  * Validate contents of the buffer from userspace related to OS descriptors.
225000a2430fSAndrzej Pietrasiewicz  */
225100a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
225200a2430fSAndrzej Pietrasiewicz 				 struct usb_os_desc_header *h, void *data,
225300a2430fSAndrzej Pietrasiewicz 				 unsigned len, void *priv)
225400a2430fSAndrzej Pietrasiewicz {
225500a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = priv;
225600a2430fSAndrzej Pietrasiewicz 	u8 length;
225700a2430fSAndrzej Pietrasiewicz 
225800a2430fSAndrzej Pietrasiewicz 	ENTER();
225900a2430fSAndrzej Pietrasiewicz 
226000a2430fSAndrzej Pietrasiewicz 	switch (type) {
226100a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
226200a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *d = data;
226300a2430fSAndrzej Pietrasiewicz 		int i;
226400a2430fSAndrzej Pietrasiewicz 
226500a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) ||
226600a2430fSAndrzej Pietrasiewicz 		    d->bFirstInterfaceNumber >= ffs->interfaces_count ||
2267354bc45bSVincent Pelletier 		    d->Reserved1)
226800a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
226900a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
227000a2430fSAndrzej Pietrasiewicz 			if (d->Reserved2[i])
227100a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
227200a2430fSAndrzej Pietrasiewicz 
227300a2430fSAndrzej Pietrasiewicz 		length = sizeof(struct usb_ext_compat_desc);
227400a2430fSAndrzej Pietrasiewicz 	}
227500a2430fSAndrzej Pietrasiewicz 		break;
227600a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
227700a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *d = data;
227800a2430fSAndrzej Pietrasiewicz 		u32 type, pdl;
227900a2430fSAndrzej Pietrasiewicz 		u16 pnl;
228000a2430fSAndrzej Pietrasiewicz 
228100a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
228200a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
228300a2430fSAndrzej Pietrasiewicz 		length = le32_to_cpu(d->dwSize);
228483e526f2SVincent Pelletier 		if (len < length)
228583e526f2SVincent Pelletier 			return -EINVAL;
228600a2430fSAndrzej Pietrasiewicz 		type = le32_to_cpu(d->dwPropertyDataType);
228700a2430fSAndrzej Pietrasiewicz 		if (type < USB_EXT_PROP_UNICODE ||
228800a2430fSAndrzej Pietrasiewicz 		    type > USB_EXT_PROP_UNICODE_MULTI) {
228900a2430fSAndrzej Pietrasiewicz 			pr_vdebug("unsupported os descriptor property type: %d",
229000a2430fSAndrzej Pietrasiewicz 				  type);
229100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
229200a2430fSAndrzej Pietrasiewicz 		}
229300a2430fSAndrzej Pietrasiewicz 		pnl = le16_to_cpu(d->wPropertyNameLength);
229483e526f2SVincent Pelletier 		if (length < 14 + pnl) {
229583e526f2SVincent Pelletier 			pr_vdebug("invalid os descriptor length: %d pnl:%d (descriptor %d)\n",
229683e526f2SVincent Pelletier 				  length, pnl, type);
229783e526f2SVincent Pelletier 			return -EINVAL;
229883e526f2SVincent Pelletier 		}
229900a2430fSAndrzej Pietrasiewicz 		pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
230000a2430fSAndrzej Pietrasiewicz 		if (length != 14 + pnl + pdl) {
230100a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
230200a2430fSAndrzej Pietrasiewicz 				  length, pnl, pdl, type);
230300a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
230400a2430fSAndrzej Pietrasiewicz 		}
230500a2430fSAndrzej Pietrasiewicz 		++ffs->ms_os_descs_ext_prop_count;
230600a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
230700a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
230800a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_data_len += pdl;
230900a2430fSAndrzej Pietrasiewicz 	}
231000a2430fSAndrzej Pietrasiewicz 		break;
231100a2430fSAndrzej Pietrasiewicz 	default:
231200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
231300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
231400a2430fSAndrzej Pietrasiewicz 	}
231500a2430fSAndrzej Pietrasiewicz 	return length;
231600a2430fSAndrzej Pietrasiewicz }
231700a2430fSAndrzej Pietrasiewicz 
231800a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_descs(struct ffs_data *ffs,
231900a2430fSAndrzej Pietrasiewicz 				char *const _data, size_t len)
232000a2430fSAndrzej Pietrasiewicz {
232100a2430fSAndrzej Pietrasiewicz 	char *data = _data, *raw_descs;
232200a2430fSAndrzej Pietrasiewicz 	unsigned os_descs_count = 0, counts[3], flags;
232300a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL, i;
23246d5c1c77SRobert Baldyga 	struct ffs_desc_helper helper;
232500a2430fSAndrzej Pietrasiewicz 
232600a2430fSAndrzej Pietrasiewicz 	ENTER();
232700a2430fSAndrzej Pietrasiewicz 
232800a2430fSAndrzej Pietrasiewicz 	if (get_unaligned_le32(data + 4) != len)
232900a2430fSAndrzej Pietrasiewicz 		goto error;
233000a2430fSAndrzej Pietrasiewicz 
233100a2430fSAndrzej Pietrasiewicz 	switch (get_unaligned_le32(data)) {
233200a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC:
233300a2430fSAndrzej Pietrasiewicz 		flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
233400a2430fSAndrzej Pietrasiewicz 		data += 8;
233500a2430fSAndrzej Pietrasiewicz 		len  -= 8;
233600a2430fSAndrzej Pietrasiewicz 		break;
233700a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
233800a2430fSAndrzej Pietrasiewicz 		flags = get_unaligned_le32(data + 8);
23391b0bf88fSRobert Baldyga 		ffs->user_flags = flags;
234000a2430fSAndrzej Pietrasiewicz 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
234100a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_HS_DESC |
234200a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_SS_DESC |
23431b0bf88fSRobert Baldyga 			      FUNCTIONFS_HAS_MS_OS_DESC |
23445e33f6fdSRobert Baldyga 			      FUNCTIONFS_VIRTUAL_ADDR |
234554dfce6dSFelix Hädicke 			      FUNCTIONFS_EVENTFD |
23464368c28aSFelix Hädicke 			      FUNCTIONFS_ALL_CTRL_RECIP |
23474368c28aSFelix Hädicke 			      FUNCTIONFS_CONFIG0_SETUP)) {
234800a2430fSAndrzej Pietrasiewicz 			ret = -ENOSYS;
234900a2430fSAndrzej Pietrasiewicz 			goto error;
235000a2430fSAndrzej Pietrasiewicz 		}
235100a2430fSAndrzej Pietrasiewicz 		data += 12;
235200a2430fSAndrzej Pietrasiewicz 		len  -= 12;
235300a2430fSAndrzej Pietrasiewicz 		break;
235400a2430fSAndrzej Pietrasiewicz 	default:
235500a2430fSAndrzej Pietrasiewicz 		goto error;
235600a2430fSAndrzej Pietrasiewicz 	}
235700a2430fSAndrzej Pietrasiewicz 
23585e33f6fdSRobert Baldyga 	if (flags & FUNCTIONFS_EVENTFD) {
23595e33f6fdSRobert Baldyga 		if (len < 4)
23605e33f6fdSRobert Baldyga 			goto error;
23615e33f6fdSRobert Baldyga 		ffs->ffs_eventfd =
23625e33f6fdSRobert Baldyga 			eventfd_ctx_fdget((int)get_unaligned_le32(data));
23635e33f6fdSRobert Baldyga 		if (IS_ERR(ffs->ffs_eventfd)) {
23645e33f6fdSRobert Baldyga 			ret = PTR_ERR(ffs->ffs_eventfd);
23655e33f6fdSRobert Baldyga 			ffs->ffs_eventfd = NULL;
23665e33f6fdSRobert Baldyga 			goto error;
23675e33f6fdSRobert Baldyga 		}
23685e33f6fdSRobert Baldyga 		data += 4;
23695e33f6fdSRobert Baldyga 		len  -= 4;
23705e33f6fdSRobert Baldyga 	}
23715e33f6fdSRobert Baldyga 
237200a2430fSAndrzej Pietrasiewicz 	/* Read fs_count, hs_count and ss_count (if present) */
237300a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
237400a2430fSAndrzej Pietrasiewicz 		if (!(flags & (1 << i))) {
237500a2430fSAndrzej Pietrasiewicz 			counts[i] = 0;
237600a2430fSAndrzej Pietrasiewicz 		} else if (len < 4) {
237700a2430fSAndrzej Pietrasiewicz 			goto error;
237800a2430fSAndrzej Pietrasiewicz 		} else {
237900a2430fSAndrzej Pietrasiewicz 			counts[i] = get_unaligned_le32(data);
238000a2430fSAndrzej Pietrasiewicz 			data += 4;
238100a2430fSAndrzej Pietrasiewicz 			len  -= 4;
238200a2430fSAndrzej Pietrasiewicz 		}
238300a2430fSAndrzej Pietrasiewicz 	}
238400a2430fSAndrzej Pietrasiewicz 	if (flags & (1 << i)) {
238583e526f2SVincent Pelletier 		if (len < 4) {
238683e526f2SVincent Pelletier 			goto error;
238783e526f2SVincent Pelletier 		}
238800a2430fSAndrzej Pietrasiewicz 		os_descs_count = get_unaligned_le32(data);
238900a2430fSAndrzej Pietrasiewicz 		data += 4;
239000a2430fSAndrzej Pietrasiewicz 		len -= 4;
239100a2430fSAndrzej Pietrasiewicz 	};
239200a2430fSAndrzej Pietrasiewicz 
239300a2430fSAndrzej Pietrasiewicz 	/* Read descriptors */
239400a2430fSAndrzej Pietrasiewicz 	raw_descs = data;
23956d5c1c77SRobert Baldyga 	helper.ffs = ffs;
239600a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
239700a2430fSAndrzej Pietrasiewicz 		if (!counts[i])
239800a2430fSAndrzej Pietrasiewicz 			continue;
23996d5c1c77SRobert Baldyga 		helper.interfaces_count = 0;
24006d5c1c77SRobert Baldyga 		helper.eps_count = 0;
240100a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_descs(counts[i], data, len,
24026d5c1c77SRobert Baldyga 				   __ffs_data_do_entity, &helper);
240300a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
240400a2430fSAndrzej Pietrasiewicz 			goto error;
24056d5c1c77SRobert Baldyga 		if (!ffs->eps_count && !ffs->interfaces_count) {
24066d5c1c77SRobert Baldyga 			ffs->eps_count = helper.eps_count;
24076d5c1c77SRobert Baldyga 			ffs->interfaces_count = helper.interfaces_count;
24086d5c1c77SRobert Baldyga 		} else {
24096d5c1c77SRobert Baldyga 			if (ffs->eps_count != helper.eps_count) {
24106d5c1c77SRobert Baldyga 				ret = -EINVAL;
24116d5c1c77SRobert Baldyga 				goto error;
24126d5c1c77SRobert Baldyga 			}
24136d5c1c77SRobert Baldyga 			if (ffs->interfaces_count != helper.interfaces_count) {
24146d5c1c77SRobert Baldyga 				ret = -EINVAL;
24156d5c1c77SRobert Baldyga 				goto error;
24166d5c1c77SRobert Baldyga 			}
24176d5c1c77SRobert Baldyga 		}
241800a2430fSAndrzej Pietrasiewicz 		data += ret;
241900a2430fSAndrzej Pietrasiewicz 		len  -= ret;
242000a2430fSAndrzej Pietrasiewicz 	}
242100a2430fSAndrzej Pietrasiewicz 	if (os_descs_count) {
242200a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(os_descs_count, data, len,
242300a2430fSAndrzej Pietrasiewicz 				      __ffs_data_do_os_desc, ffs);
242400a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
242500a2430fSAndrzej Pietrasiewicz 			goto error;
242600a2430fSAndrzej Pietrasiewicz 		data += ret;
242700a2430fSAndrzej Pietrasiewicz 		len -= ret;
242800a2430fSAndrzej Pietrasiewicz 	}
242900a2430fSAndrzej Pietrasiewicz 
243000a2430fSAndrzej Pietrasiewicz 	if (raw_descs == data || len) {
243100a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
243200a2430fSAndrzej Pietrasiewicz 		goto error;
243300a2430fSAndrzej Pietrasiewicz 	}
243400a2430fSAndrzej Pietrasiewicz 
243500a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data	= _data;
243600a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs		= raw_descs;
243700a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length	= data - raw_descs;
243800a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count	= counts[0];
243900a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count	= counts[1];
244000a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count	= counts[2];
244100a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_count	= os_descs_count;
244200a2430fSAndrzej Pietrasiewicz 
244300a2430fSAndrzej Pietrasiewicz 	return 0;
244400a2430fSAndrzej Pietrasiewicz 
244500a2430fSAndrzej Pietrasiewicz error:
244600a2430fSAndrzej Pietrasiewicz 	kfree(_data);
244700a2430fSAndrzej Pietrasiewicz 	return ret;
244800a2430fSAndrzej Pietrasiewicz }
244900a2430fSAndrzej Pietrasiewicz 
245000a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_strings(struct ffs_data *ffs,
245100a2430fSAndrzej Pietrasiewicz 				  char *const _data, size_t len)
245200a2430fSAndrzej Pietrasiewicz {
245300a2430fSAndrzej Pietrasiewicz 	u32 str_count, needed_count, lang_count;
245400a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **stringtabs, *t;
245500a2430fSAndrzej Pietrasiewicz 	const char *data = _data;
2456872ce511SMichal Nazarewicz 	struct usb_string *s;
245700a2430fSAndrzej Pietrasiewicz 
245800a2430fSAndrzej Pietrasiewicz 	ENTER();
245900a2430fSAndrzej Pietrasiewicz 
246083e526f2SVincent Pelletier 	if (unlikely(len < 16 ||
246183e526f2SVincent Pelletier 		     get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
246200a2430fSAndrzej Pietrasiewicz 		     get_unaligned_le32(data + 4) != len))
246300a2430fSAndrzej Pietrasiewicz 		goto error;
246400a2430fSAndrzej Pietrasiewicz 	str_count  = get_unaligned_le32(data + 8);
246500a2430fSAndrzej Pietrasiewicz 	lang_count = get_unaligned_le32(data + 12);
246600a2430fSAndrzej Pietrasiewicz 
246700a2430fSAndrzej Pietrasiewicz 	/* if one is zero the other must be zero */
246800a2430fSAndrzej Pietrasiewicz 	if (unlikely(!str_count != !lang_count))
246900a2430fSAndrzej Pietrasiewicz 		goto error;
247000a2430fSAndrzej Pietrasiewicz 
247100a2430fSAndrzej Pietrasiewicz 	/* Do we have at least as many strings as descriptors need? */
247200a2430fSAndrzej Pietrasiewicz 	needed_count = ffs->strings_count;
247300a2430fSAndrzej Pietrasiewicz 	if (unlikely(str_count < needed_count))
247400a2430fSAndrzej Pietrasiewicz 		goto error;
247500a2430fSAndrzej Pietrasiewicz 
247600a2430fSAndrzej Pietrasiewicz 	/*
247700a2430fSAndrzej Pietrasiewicz 	 * If we don't need any strings just return and free all
247800a2430fSAndrzej Pietrasiewicz 	 * memory.
247900a2430fSAndrzej Pietrasiewicz 	 */
248000a2430fSAndrzej Pietrasiewicz 	if (!needed_count) {
248100a2430fSAndrzej Pietrasiewicz 		kfree(_data);
248200a2430fSAndrzej Pietrasiewicz 		return 0;
248300a2430fSAndrzej Pietrasiewicz 	}
248400a2430fSAndrzej Pietrasiewicz 
248500a2430fSAndrzej Pietrasiewicz 	/* Allocate everything in one chunk so there's less maintenance. */
248600a2430fSAndrzej Pietrasiewicz 	{
248700a2430fSAndrzej Pietrasiewicz 		unsigned i = 0;
248800a2430fSAndrzej Pietrasiewicz 		vla_group(d);
248900a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings *, stringtabs,
249000a2430fSAndrzej Pietrasiewicz 			lang_count + 1);
249100a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
249200a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_string, strings,
249300a2430fSAndrzej Pietrasiewicz 			lang_count*(needed_count+1));
249400a2430fSAndrzej Pietrasiewicz 
249500a2430fSAndrzej Pietrasiewicz 		char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
249600a2430fSAndrzej Pietrasiewicz 
249700a2430fSAndrzej Pietrasiewicz 		if (unlikely(!vlabuf)) {
249800a2430fSAndrzej Pietrasiewicz 			kfree(_data);
249900a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
250000a2430fSAndrzej Pietrasiewicz 		}
250100a2430fSAndrzej Pietrasiewicz 
250200a2430fSAndrzej Pietrasiewicz 		/* Initialize the VLA pointers */
250300a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
250400a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
250500a2430fSAndrzej Pietrasiewicz 		i = lang_count;
250600a2430fSAndrzej Pietrasiewicz 		do {
250700a2430fSAndrzej Pietrasiewicz 			*stringtabs++ = t++;
250800a2430fSAndrzej Pietrasiewicz 		} while (--i);
250900a2430fSAndrzej Pietrasiewicz 		*stringtabs = NULL;
251000a2430fSAndrzej Pietrasiewicz 
251100a2430fSAndrzej Pietrasiewicz 		/* stringtabs = vlabuf = d_stringtabs for later kfree */
251200a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
251300a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
251400a2430fSAndrzej Pietrasiewicz 		s = vla_ptr(vlabuf, d, strings);
251500a2430fSAndrzej Pietrasiewicz 	}
251600a2430fSAndrzej Pietrasiewicz 
251700a2430fSAndrzej Pietrasiewicz 	/* For each language */
251800a2430fSAndrzej Pietrasiewicz 	data += 16;
251900a2430fSAndrzej Pietrasiewicz 	len -= 16;
252000a2430fSAndrzej Pietrasiewicz 
252100a2430fSAndrzej Pietrasiewicz 	do { /* lang_count > 0 so we can use do-while */
252200a2430fSAndrzej Pietrasiewicz 		unsigned needed = needed_count;
252300a2430fSAndrzej Pietrasiewicz 
252400a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 3))
252500a2430fSAndrzej Pietrasiewicz 			goto error_free;
252600a2430fSAndrzej Pietrasiewicz 		t->language = get_unaligned_le16(data);
252700a2430fSAndrzej Pietrasiewicz 		t->strings  = s;
252800a2430fSAndrzej Pietrasiewicz 		++t;
252900a2430fSAndrzej Pietrasiewicz 
253000a2430fSAndrzej Pietrasiewicz 		data += 2;
253100a2430fSAndrzej Pietrasiewicz 		len -= 2;
253200a2430fSAndrzej Pietrasiewicz 
253300a2430fSAndrzej Pietrasiewicz 		/* For each string */
253400a2430fSAndrzej Pietrasiewicz 		do { /* str_count > 0 so we can use do-while */
253500a2430fSAndrzej Pietrasiewicz 			size_t length = strnlen(data, len);
253600a2430fSAndrzej Pietrasiewicz 
253700a2430fSAndrzej Pietrasiewicz 			if (unlikely(length == len))
253800a2430fSAndrzej Pietrasiewicz 				goto error_free;
253900a2430fSAndrzej Pietrasiewicz 
254000a2430fSAndrzej Pietrasiewicz 			/*
254100a2430fSAndrzej Pietrasiewicz 			 * User may provide more strings then we need,
254200a2430fSAndrzej Pietrasiewicz 			 * if that's the case we simply ignore the
254300a2430fSAndrzej Pietrasiewicz 			 * rest
254400a2430fSAndrzej Pietrasiewicz 			 */
254500a2430fSAndrzej Pietrasiewicz 			if (likely(needed)) {
254600a2430fSAndrzej Pietrasiewicz 				/*
254700a2430fSAndrzej Pietrasiewicz 				 * s->id will be set while adding
254800a2430fSAndrzej Pietrasiewicz 				 * function to configuration so for
254900a2430fSAndrzej Pietrasiewicz 				 * now just leave garbage here.
255000a2430fSAndrzej Pietrasiewicz 				 */
255100a2430fSAndrzej Pietrasiewicz 				s->s = data;
255200a2430fSAndrzej Pietrasiewicz 				--needed;
255300a2430fSAndrzej Pietrasiewicz 				++s;
255400a2430fSAndrzej Pietrasiewicz 			}
255500a2430fSAndrzej Pietrasiewicz 
255600a2430fSAndrzej Pietrasiewicz 			data += length + 1;
255700a2430fSAndrzej Pietrasiewicz 			len -= length + 1;
255800a2430fSAndrzej Pietrasiewicz 		} while (--str_count);
255900a2430fSAndrzej Pietrasiewicz 
256000a2430fSAndrzej Pietrasiewicz 		s->id = 0;   /* terminator */
256100a2430fSAndrzej Pietrasiewicz 		s->s = NULL;
256200a2430fSAndrzej Pietrasiewicz 		++s;
256300a2430fSAndrzej Pietrasiewicz 
256400a2430fSAndrzej Pietrasiewicz 	} while (--lang_count);
256500a2430fSAndrzej Pietrasiewicz 
256600a2430fSAndrzej Pietrasiewicz 	/* Some garbage left? */
256700a2430fSAndrzej Pietrasiewicz 	if (unlikely(len))
256800a2430fSAndrzej Pietrasiewicz 		goto error_free;
256900a2430fSAndrzej Pietrasiewicz 
257000a2430fSAndrzej Pietrasiewicz 	/* Done! */
257100a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = stringtabs;
257200a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = _data;
257300a2430fSAndrzej Pietrasiewicz 
257400a2430fSAndrzej Pietrasiewicz 	return 0;
257500a2430fSAndrzej Pietrasiewicz 
257600a2430fSAndrzej Pietrasiewicz error_free:
257700a2430fSAndrzej Pietrasiewicz 	kfree(stringtabs);
257800a2430fSAndrzej Pietrasiewicz error:
257900a2430fSAndrzej Pietrasiewicz 	kfree(_data);
258000a2430fSAndrzej Pietrasiewicz 	return -EINVAL;
258100a2430fSAndrzej Pietrasiewicz }
258200a2430fSAndrzej Pietrasiewicz 
258300a2430fSAndrzej Pietrasiewicz 
258400a2430fSAndrzej Pietrasiewicz /* Events handling and management *******************************************/
258500a2430fSAndrzej Pietrasiewicz 
258600a2430fSAndrzej Pietrasiewicz static void __ffs_event_add(struct ffs_data *ffs,
258700a2430fSAndrzej Pietrasiewicz 			    enum usb_functionfs_event_type type)
258800a2430fSAndrzej Pietrasiewicz {
258900a2430fSAndrzej Pietrasiewicz 	enum usb_functionfs_event_type rem_type1, rem_type2 = type;
259000a2430fSAndrzej Pietrasiewicz 	int neg = 0;
259100a2430fSAndrzej Pietrasiewicz 
259200a2430fSAndrzej Pietrasiewicz 	/*
259300a2430fSAndrzej Pietrasiewicz 	 * Abort any unhandled setup
259400a2430fSAndrzej Pietrasiewicz 	 *
259500a2430fSAndrzej Pietrasiewicz 	 * We do not need to worry about some cmpxchg() changing value
259600a2430fSAndrzej Pietrasiewicz 	 * of ffs->setup_state without holding the lock because when
259700a2430fSAndrzej Pietrasiewicz 	 * state is FFS_SETUP_PENDING cmpxchg() in several places in
259800a2430fSAndrzej Pietrasiewicz 	 * the source does nothing.
259900a2430fSAndrzej Pietrasiewicz 	 */
260000a2430fSAndrzej Pietrasiewicz 	if (ffs->setup_state == FFS_SETUP_PENDING)
260100a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_SETUP_CANCELLED;
260200a2430fSAndrzej Pietrasiewicz 
260367913bbdSMichal Nazarewicz 	/*
260467913bbdSMichal Nazarewicz 	 * Logic of this function guarantees that there are at most four pending
260567913bbdSMichal Nazarewicz 	 * evens on ffs->ev.types queue.  This is important because the queue
260667913bbdSMichal Nazarewicz 	 * has space for four elements only and __ffs_ep0_read_events function
260767913bbdSMichal Nazarewicz 	 * depends on that limit as well.  If more event types are added, those
260867913bbdSMichal Nazarewicz 	 * limits have to be revisited or guaranteed to still hold.
260967913bbdSMichal Nazarewicz 	 */
261000a2430fSAndrzej Pietrasiewicz 	switch (type) {
261100a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_RESUME:
261200a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_SUSPEND;
261300a2430fSAndrzej Pietrasiewicz 		/* FALL THROUGH */
261400a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SUSPEND:
261500a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SETUP:
261600a2430fSAndrzej Pietrasiewicz 		rem_type1 = type;
261700a2430fSAndrzej Pietrasiewicz 		/* Discard all similar events */
261800a2430fSAndrzej Pietrasiewicz 		break;
261900a2430fSAndrzej Pietrasiewicz 
262000a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_BIND:
262100a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_UNBIND:
262200a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DISABLE:
262300a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_ENABLE:
262400a2430fSAndrzej Pietrasiewicz 		/* Discard everything other then power management. */
262500a2430fSAndrzej Pietrasiewicz 		rem_type1 = FUNCTIONFS_SUSPEND;
262600a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_RESUME;
262700a2430fSAndrzej Pietrasiewicz 		neg = 1;
262800a2430fSAndrzej Pietrasiewicz 		break;
262900a2430fSAndrzej Pietrasiewicz 
263000a2430fSAndrzej Pietrasiewicz 	default:
2631fe00bcbfSMichal Nazarewicz 		WARN(1, "%d: unknown event, this should not happen\n", type);
2632fe00bcbfSMichal Nazarewicz 		return;
263300a2430fSAndrzej Pietrasiewicz 	}
263400a2430fSAndrzej Pietrasiewicz 
263500a2430fSAndrzej Pietrasiewicz 	{
263600a2430fSAndrzej Pietrasiewicz 		u8 *ev  = ffs->ev.types, *out = ev;
263700a2430fSAndrzej Pietrasiewicz 		unsigned n = ffs->ev.count;
263800a2430fSAndrzej Pietrasiewicz 		for (; n; --n, ++ev)
263900a2430fSAndrzej Pietrasiewicz 			if ((*ev == rem_type1 || *ev == rem_type2) == neg)
264000a2430fSAndrzej Pietrasiewicz 				*out++ = *ev;
264100a2430fSAndrzej Pietrasiewicz 			else
264200a2430fSAndrzej Pietrasiewicz 				pr_vdebug("purging event %d\n", *ev);
264300a2430fSAndrzej Pietrasiewicz 		ffs->ev.count = out - ffs->ev.types;
264400a2430fSAndrzej Pietrasiewicz 	}
264500a2430fSAndrzej Pietrasiewicz 
264600a2430fSAndrzej Pietrasiewicz 	pr_vdebug("adding event %d\n", type);
264700a2430fSAndrzej Pietrasiewicz 	ffs->ev.types[ffs->ev.count++] = type;
264800a2430fSAndrzej Pietrasiewicz 	wake_up_locked(&ffs->ev.waitq);
26495e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
26505e33f6fdSRobert Baldyga 		eventfd_signal(ffs->ffs_eventfd, 1);
265100a2430fSAndrzej Pietrasiewicz }
265200a2430fSAndrzej Pietrasiewicz 
265300a2430fSAndrzej Pietrasiewicz static void ffs_event_add(struct ffs_data *ffs,
265400a2430fSAndrzej Pietrasiewicz 			  enum usb_functionfs_event_type type)
265500a2430fSAndrzej Pietrasiewicz {
265600a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
265700a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
265800a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, type);
265900a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
266000a2430fSAndrzej Pietrasiewicz }
266100a2430fSAndrzej Pietrasiewicz 
266200a2430fSAndrzej Pietrasiewicz /* Bind/unbind USB function hooks *******************************************/
266300a2430fSAndrzej Pietrasiewicz 
26646d5c1c77SRobert Baldyga static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
26656d5c1c77SRobert Baldyga {
26666d5c1c77SRobert Baldyga 	int i;
26676d5c1c77SRobert Baldyga 
26686d5c1c77SRobert Baldyga 	for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
26696d5c1c77SRobert Baldyga 		if (ffs->eps_addrmap[i] == endpoint_address)
26706d5c1c77SRobert Baldyga 			return i;
26716d5c1c77SRobert Baldyga 	return -ENOENT;
26726d5c1c77SRobert Baldyga }
26736d5c1c77SRobert Baldyga 
267400a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
267500a2430fSAndrzej Pietrasiewicz 				    struct usb_descriptor_header *desc,
267600a2430fSAndrzej Pietrasiewicz 				    void *priv)
267700a2430fSAndrzej Pietrasiewicz {
267800a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor *ds = (void *)desc;
267900a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
268000a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ffs_ep;
268185b06f5eSDan Carpenter 	unsigned ep_desc_id;
268285b06f5eSDan Carpenter 	int idx;
268300a2430fSAndrzej Pietrasiewicz 	static const char *speed_names[] = { "full", "high", "super" };
268400a2430fSAndrzej Pietrasiewicz 
268500a2430fSAndrzej Pietrasiewicz 	if (type != FFS_DESCRIPTOR)
268600a2430fSAndrzej Pietrasiewicz 		return 0;
268700a2430fSAndrzej Pietrasiewicz 
268800a2430fSAndrzej Pietrasiewicz 	/*
268900a2430fSAndrzej Pietrasiewicz 	 * If ss_descriptors is not NULL, we are reading super speed
269000a2430fSAndrzej Pietrasiewicz 	 * descriptors; if hs_descriptors is not NULL, we are reading high
269100a2430fSAndrzej Pietrasiewicz 	 * speed descriptors; otherwise, we are reading full speed
269200a2430fSAndrzej Pietrasiewicz 	 * descriptors.
269300a2430fSAndrzej Pietrasiewicz 	 */
269400a2430fSAndrzej Pietrasiewicz 	if (func->function.ss_descriptors) {
269500a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 2;
269600a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors[(long)valuep] = desc;
269700a2430fSAndrzej Pietrasiewicz 	} else if (func->function.hs_descriptors) {
269800a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 1;
269900a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors[(long)valuep] = desc;
270000a2430fSAndrzej Pietrasiewicz 	} else {
270100a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 0;
270200a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors[(long)valuep]    = desc;
270300a2430fSAndrzej Pietrasiewicz 	}
270400a2430fSAndrzej Pietrasiewicz 
270500a2430fSAndrzej Pietrasiewicz 	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
270600a2430fSAndrzej Pietrasiewicz 		return 0;
270700a2430fSAndrzej Pietrasiewicz 
27086d5c1c77SRobert Baldyga 	idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
27096d5c1c77SRobert Baldyga 	if (idx < 0)
27106d5c1c77SRobert Baldyga 		return idx;
27116d5c1c77SRobert Baldyga 
271200a2430fSAndrzej Pietrasiewicz 	ffs_ep = func->eps + idx;
271300a2430fSAndrzej Pietrasiewicz 
271400a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs_ep->descs[ep_desc_id])) {
271500a2430fSAndrzej Pietrasiewicz 		pr_err("two %sspeed descriptors for EP %d\n",
271600a2430fSAndrzej Pietrasiewicz 			  speed_names[ep_desc_id],
271700a2430fSAndrzej Pietrasiewicz 			  ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
271800a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
271900a2430fSAndrzej Pietrasiewicz 	}
272000a2430fSAndrzej Pietrasiewicz 	ffs_ep->descs[ep_desc_id] = ds;
272100a2430fSAndrzej Pietrasiewicz 
272200a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Original  ep desc", ds, ds->bLength);
272300a2430fSAndrzej Pietrasiewicz 	if (ffs_ep->ep) {
272400a2430fSAndrzej Pietrasiewicz 		ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
272500a2430fSAndrzej Pietrasiewicz 		if (!ds->wMaxPacketSize)
272600a2430fSAndrzej Pietrasiewicz 			ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
272700a2430fSAndrzej Pietrasiewicz 	} else {
272800a2430fSAndrzej Pietrasiewicz 		struct usb_request *req;
272900a2430fSAndrzej Pietrasiewicz 		struct usb_ep *ep;
27301b0bf88fSRobert Baldyga 		u8 bEndpointAddress;
273100a2430fSAndrzej Pietrasiewicz 
27321b0bf88fSRobert Baldyga 		/*
27331b0bf88fSRobert Baldyga 		 * We back up bEndpointAddress because autoconfig overwrites
27341b0bf88fSRobert Baldyga 		 * it with physical endpoint address.
27351b0bf88fSRobert Baldyga 		 */
27361b0bf88fSRobert Baldyga 		bEndpointAddress = ds->bEndpointAddress;
273700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("autoconfig\n");
273800a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(func->gadget, ds);
273900a2430fSAndrzej Pietrasiewicz 		if (unlikely(!ep))
274000a2430fSAndrzej Pietrasiewicz 			return -ENOTSUPP;
274100a2430fSAndrzej Pietrasiewicz 		ep->driver_data = func->eps + idx;
274200a2430fSAndrzej Pietrasiewicz 
274300a2430fSAndrzej Pietrasiewicz 		req = usb_ep_alloc_request(ep, GFP_KERNEL);
274400a2430fSAndrzej Pietrasiewicz 		if (unlikely(!req))
274500a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
274600a2430fSAndrzej Pietrasiewicz 
274700a2430fSAndrzej Pietrasiewicz 		ffs_ep->ep  = ep;
274800a2430fSAndrzej Pietrasiewicz 		ffs_ep->req = req;
274900a2430fSAndrzej Pietrasiewicz 		func->eps_revmap[ds->bEndpointAddress &
275000a2430fSAndrzej Pietrasiewicz 				 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
27511b0bf88fSRobert Baldyga 		/*
27521b0bf88fSRobert Baldyga 		 * If we use virtual address mapping, we restore
27531b0bf88fSRobert Baldyga 		 * original bEndpointAddress value.
27541b0bf88fSRobert Baldyga 		 */
27551b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
27561b0bf88fSRobert Baldyga 			ds->bEndpointAddress = bEndpointAddress;
275700a2430fSAndrzej Pietrasiewicz 	}
275800a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
275900a2430fSAndrzej Pietrasiewicz 
276000a2430fSAndrzej Pietrasiewicz 	return 0;
276100a2430fSAndrzej Pietrasiewicz }
276200a2430fSAndrzej Pietrasiewicz 
276300a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
276400a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
276500a2430fSAndrzej Pietrasiewicz 				   void *priv)
276600a2430fSAndrzej Pietrasiewicz {
276700a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
276800a2430fSAndrzej Pietrasiewicz 	unsigned idx;
276900a2430fSAndrzej Pietrasiewicz 	u8 newValue;
277000a2430fSAndrzej Pietrasiewicz 
277100a2430fSAndrzej Pietrasiewicz 	switch (type) {
277200a2430fSAndrzej Pietrasiewicz 	default:
277300a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
277400a2430fSAndrzej Pietrasiewicz 		/* Handled in previous pass by __ffs_func_bind_do_descs() */
277500a2430fSAndrzej Pietrasiewicz 		return 0;
277600a2430fSAndrzej Pietrasiewicz 
277700a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
277800a2430fSAndrzej Pietrasiewicz 		idx = *valuep;
277900a2430fSAndrzej Pietrasiewicz 		if (func->interfaces_nums[idx] < 0) {
278000a2430fSAndrzej Pietrasiewicz 			int id = usb_interface_id(func->conf, &func->function);
278100a2430fSAndrzej Pietrasiewicz 			if (unlikely(id < 0))
278200a2430fSAndrzej Pietrasiewicz 				return id;
278300a2430fSAndrzej Pietrasiewicz 			func->interfaces_nums[idx] = id;
278400a2430fSAndrzej Pietrasiewicz 		}
278500a2430fSAndrzej Pietrasiewicz 		newValue = func->interfaces_nums[idx];
278600a2430fSAndrzej Pietrasiewicz 		break;
278700a2430fSAndrzej Pietrasiewicz 
278800a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
278900a2430fSAndrzej Pietrasiewicz 		/* String' IDs are allocated when fsf_data is bound to cdev */
279000a2430fSAndrzej Pietrasiewicz 		newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
279100a2430fSAndrzej Pietrasiewicz 		break;
279200a2430fSAndrzej Pietrasiewicz 
279300a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
279400a2430fSAndrzej Pietrasiewicz 		/*
279500a2430fSAndrzej Pietrasiewicz 		 * USB_DT_ENDPOINT are handled in
279600a2430fSAndrzej Pietrasiewicz 		 * __ffs_func_bind_do_descs().
279700a2430fSAndrzej Pietrasiewicz 		 */
279800a2430fSAndrzej Pietrasiewicz 		if (desc->bDescriptorType == USB_DT_ENDPOINT)
279900a2430fSAndrzej Pietrasiewicz 			return 0;
280000a2430fSAndrzej Pietrasiewicz 
280100a2430fSAndrzej Pietrasiewicz 		idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
280200a2430fSAndrzej Pietrasiewicz 		if (unlikely(!func->eps[idx].ep))
280300a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
280400a2430fSAndrzej Pietrasiewicz 
280500a2430fSAndrzej Pietrasiewicz 		{
280600a2430fSAndrzej Pietrasiewicz 			struct usb_endpoint_descriptor **descs;
280700a2430fSAndrzej Pietrasiewicz 			descs = func->eps[idx].descs;
280800a2430fSAndrzej Pietrasiewicz 			newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
280900a2430fSAndrzej Pietrasiewicz 		}
281000a2430fSAndrzej Pietrasiewicz 		break;
281100a2430fSAndrzej Pietrasiewicz 	}
281200a2430fSAndrzej Pietrasiewicz 
281300a2430fSAndrzej Pietrasiewicz 	pr_vdebug("%02x -> %02x\n", *valuep, newValue);
281400a2430fSAndrzej Pietrasiewicz 	*valuep = newValue;
281500a2430fSAndrzej Pietrasiewicz 	return 0;
281600a2430fSAndrzej Pietrasiewicz }
281700a2430fSAndrzej Pietrasiewicz 
281800a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
281900a2430fSAndrzej Pietrasiewicz 				      struct usb_os_desc_header *h, void *data,
282000a2430fSAndrzej Pietrasiewicz 				      unsigned len, void *priv)
282100a2430fSAndrzej Pietrasiewicz {
282200a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
282300a2430fSAndrzej Pietrasiewicz 	u8 length = 0;
282400a2430fSAndrzej Pietrasiewicz 
282500a2430fSAndrzej Pietrasiewicz 	switch (type) {
282600a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
282700a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *desc = data;
282800a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
282900a2430fSAndrzej Pietrasiewicz 
283000a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
283100a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
283200a2430fSAndrzej Pietrasiewicz 		memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
283300a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->CompatibleID) +
283400a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->SubCompatibleID));
283500a2430fSAndrzej Pietrasiewicz 		length = sizeof(*desc);
283600a2430fSAndrzej Pietrasiewicz 	}
283700a2430fSAndrzej Pietrasiewicz 		break;
283800a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
283900a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *desc = data;
284000a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
284100a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_ext_prop *ext_prop;
284200a2430fSAndrzej Pietrasiewicz 		char *ext_prop_name;
284300a2430fSAndrzej Pietrasiewicz 		char *ext_prop_data;
284400a2430fSAndrzej Pietrasiewicz 
284500a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[h->interface];
284600a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[h->interface];
284700a2430fSAndrzej Pietrasiewicz 
284800a2430fSAndrzej Pietrasiewicz 		ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
284900a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
285000a2430fSAndrzej Pietrasiewicz 
285100a2430fSAndrzej Pietrasiewicz 		ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
285200a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
285300a2430fSAndrzej Pietrasiewicz 		ext_prop->data_len = le32_to_cpu(*(u32 *)
285400a2430fSAndrzej Pietrasiewicz 			usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
285500a2430fSAndrzej Pietrasiewicz 		length = ext_prop->name_len + ext_prop->data_len + 14;
285600a2430fSAndrzej Pietrasiewicz 
285700a2430fSAndrzej Pietrasiewicz 		ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
285800a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_name_avail +=
285900a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len;
286000a2430fSAndrzej Pietrasiewicz 
286100a2430fSAndrzej Pietrasiewicz 		ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
286200a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_data_avail +=
286300a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len;
286400a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_data,
286500a2430fSAndrzej Pietrasiewicz 		       usb_ext_prop_data_ptr(data, ext_prop->name_len),
286600a2430fSAndrzej Pietrasiewicz 		       ext_prop->data_len);
286700a2430fSAndrzej Pietrasiewicz 		/* unicode data reported to the host as "WCHAR"s */
286800a2430fSAndrzej Pietrasiewicz 		switch (ext_prop->type) {
286900a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE:
287000a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_ENV:
287100a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_LINK:
287200a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_MULTI:
287300a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len *= 2;
287400a2430fSAndrzej Pietrasiewicz 			break;
287500a2430fSAndrzej Pietrasiewicz 		}
287600a2430fSAndrzej Pietrasiewicz 		ext_prop->data = ext_prop_data;
287700a2430fSAndrzej Pietrasiewicz 
287800a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
287900a2430fSAndrzej Pietrasiewicz 		       ext_prop->name_len);
288000a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
288100a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len *= 2;
288200a2430fSAndrzej Pietrasiewicz 		ext_prop->name = ext_prop_name;
288300a2430fSAndrzej Pietrasiewicz 
288400a2430fSAndrzej Pietrasiewicz 		t->os_desc->ext_prop_len +=
288500a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len + ext_prop->data_len + 14;
288600a2430fSAndrzej Pietrasiewicz 		++t->os_desc->ext_prop_count;
288700a2430fSAndrzej Pietrasiewicz 		list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
288800a2430fSAndrzej Pietrasiewicz 	}
288900a2430fSAndrzej Pietrasiewicz 		break;
289000a2430fSAndrzej Pietrasiewicz 	default:
289100a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
289200a2430fSAndrzej Pietrasiewicz 	}
289300a2430fSAndrzej Pietrasiewicz 
289400a2430fSAndrzej Pietrasiewicz 	return length;
289500a2430fSAndrzej Pietrasiewicz }
289600a2430fSAndrzej Pietrasiewicz 
289700a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
289800a2430fSAndrzej Pietrasiewicz 						struct usb_configuration *c)
289900a2430fSAndrzej Pietrasiewicz {
290000a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
290100a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts =
290200a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
290300a2430fSAndrzej Pietrasiewicz 	int ret;
290400a2430fSAndrzej Pietrasiewicz 
290500a2430fSAndrzej Pietrasiewicz 	ENTER();
290600a2430fSAndrzej Pietrasiewicz 
290700a2430fSAndrzej Pietrasiewicz 	/*
290800a2430fSAndrzej Pietrasiewicz 	 * Legacy gadget triggers binding in functionfs_ready_callback,
290900a2430fSAndrzej Pietrasiewicz 	 * which already uses locking; taking the same lock here would
291000a2430fSAndrzej Pietrasiewicz 	 * cause a deadlock.
291100a2430fSAndrzej Pietrasiewicz 	 *
291200a2430fSAndrzej Pietrasiewicz 	 * Configfs-enabled gadgets however do need ffs_dev_lock.
291300a2430fSAndrzej Pietrasiewicz 	 */
291400a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
291500a2430fSAndrzej Pietrasiewicz 		ffs_dev_lock();
291600a2430fSAndrzej Pietrasiewicz 	ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
291700a2430fSAndrzej Pietrasiewicz 	func->ffs = ffs_opts->dev->ffs_data;
291800a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
291900a2430fSAndrzej Pietrasiewicz 		ffs_dev_unlock();
292000a2430fSAndrzej Pietrasiewicz 	if (ret)
292100a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
292200a2430fSAndrzej Pietrasiewicz 
292300a2430fSAndrzej Pietrasiewicz 	func->conf = c;
292400a2430fSAndrzej Pietrasiewicz 	func->gadget = c->cdev->gadget;
292500a2430fSAndrzej Pietrasiewicz 
292600a2430fSAndrzej Pietrasiewicz 	/*
292700a2430fSAndrzej Pietrasiewicz 	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
292800a2430fSAndrzej Pietrasiewicz 	 * configurations are bound in sequence with list_for_each_entry,
292900a2430fSAndrzej Pietrasiewicz 	 * in each configuration its functions are bound in sequence
293000a2430fSAndrzej Pietrasiewicz 	 * with list_for_each_entry, so we assume no race condition
293100a2430fSAndrzej Pietrasiewicz 	 * with regard to ffs_opts->bound access
293200a2430fSAndrzej Pietrasiewicz 	 */
293300a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->refcnt) {
293400a2430fSAndrzej Pietrasiewicz 		ret = functionfs_bind(func->ffs, c->cdev);
293500a2430fSAndrzej Pietrasiewicz 		if (ret)
293600a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
293700a2430fSAndrzej Pietrasiewicz 	}
293800a2430fSAndrzej Pietrasiewicz 	ffs_opts->refcnt++;
293900a2430fSAndrzej Pietrasiewicz 	func->function.strings = func->ffs->stringtabs;
294000a2430fSAndrzej Pietrasiewicz 
294100a2430fSAndrzej Pietrasiewicz 	return ffs_opts;
294200a2430fSAndrzej Pietrasiewicz }
294300a2430fSAndrzej Pietrasiewicz 
294400a2430fSAndrzej Pietrasiewicz static int _ffs_func_bind(struct usb_configuration *c,
294500a2430fSAndrzej Pietrasiewicz 			  struct usb_function *f)
294600a2430fSAndrzej Pietrasiewicz {
294700a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
294800a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
294900a2430fSAndrzej Pietrasiewicz 
295000a2430fSAndrzej Pietrasiewicz 	const int full = !!func->ffs->fs_descs_count;
295100a2430fSAndrzej Pietrasiewicz 	const int high = gadget_is_dualspeed(func->gadget) &&
295200a2430fSAndrzej Pietrasiewicz 		func->ffs->hs_descs_count;
295300a2430fSAndrzej Pietrasiewicz 	const int super = gadget_is_superspeed(func->gadget) &&
295400a2430fSAndrzej Pietrasiewicz 		func->ffs->ss_descs_count;
295500a2430fSAndrzej Pietrasiewicz 
295600a2430fSAndrzej Pietrasiewicz 	int fs_len, hs_len, ss_len, ret, i;
29570015f915SDan Carpenter 	struct ffs_ep *eps_ptr;
295800a2430fSAndrzej Pietrasiewicz 
295900a2430fSAndrzej Pietrasiewicz 	/* Make it a single chunk, less management later on */
296000a2430fSAndrzej Pietrasiewicz 	vla_group(d);
296100a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
296200a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
296300a2430fSAndrzej Pietrasiewicz 		full ? ffs->fs_descs_count + 1 : 0);
296400a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
296500a2430fSAndrzej Pietrasiewicz 		high ? ffs->hs_descs_count + 1 : 0);
296600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
296700a2430fSAndrzej Pietrasiewicz 		super ? ffs->ss_descs_count + 1 : 0);
296800a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, short, inums, ffs->interfaces_count);
296900a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
297000a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
297100a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char[16], ext_compat,
297200a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
297300a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc, os_desc,
297400a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
297500a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
297600a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_count);
297700a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_name,
297800a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_name_len);
297900a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_data,
298000a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_data_len);
298100a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
298200a2430fSAndrzej Pietrasiewicz 	char *vlabuf;
298300a2430fSAndrzej Pietrasiewicz 
298400a2430fSAndrzej Pietrasiewicz 	ENTER();
298500a2430fSAndrzej Pietrasiewicz 
298600a2430fSAndrzej Pietrasiewicz 	/* Has descriptors only for speeds gadget does not support */
298700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!(full | high | super)))
298800a2430fSAndrzej Pietrasiewicz 		return -ENOTSUPP;
298900a2430fSAndrzej Pietrasiewicz 
299000a2430fSAndrzej Pietrasiewicz 	/* Allocate a single chunk, less management later on */
299100a2430fSAndrzej Pietrasiewicz 	vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
299200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!vlabuf))
299300a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
299400a2430fSAndrzej Pietrasiewicz 
299500a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
299600a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_name_avail =
299700a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_name);
299800a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_data_avail =
299900a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_data);
300000a2430fSAndrzej Pietrasiewicz 
300100a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors  */
300200a2430fSAndrzej Pietrasiewicz 	memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
300300a2430fSAndrzej Pietrasiewicz 	       ffs->raw_descs_length);
300400a2430fSAndrzej Pietrasiewicz 
300500a2430fSAndrzej Pietrasiewicz 	memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
30060015f915SDan Carpenter 	eps_ptr = vla_ptr(vlabuf, d, eps);
30070015f915SDan Carpenter 	for (i = 0; i < ffs->eps_count; i++)
30080015f915SDan Carpenter 		eps_ptr[i].num = -1;
300900a2430fSAndrzej Pietrasiewicz 
301000a2430fSAndrzej Pietrasiewicz 	/* Save pointers
301100a2430fSAndrzej Pietrasiewicz 	 * d_eps == vlabuf, func->eps used to kfree vlabuf later
301200a2430fSAndrzej Pietrasiewicz 	*/
301300a2430fSAndrzej Pietrasiewicz 	func->eps             = vla_ptr(vlabuf, d, eps);
301400a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = vla_ptr(vlabuf, d, inums);
301500a2430fSAndrzej Pietrasiewicz 
301600a2430fSAndrzej Pietrasiewicz 	/*
301700a2430fSAndrzej Pietrasiewicz 	 * Go through all the endpoint descriptors and allocate
301800a2430fSAndrzej Pietrasiewicz 	 * endpoints first, so that later we can rewrite the endpoint
301900a2430fSAndrzej Pietrasiewicz 	 * numbers without worrying that it may be described later on.
302000a2430fSAndrzej Pietrasiewicz 	 */
302100a2430fSAndrzej Pietrasiewicz 	if (likely(full)) {
302200a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
302300a2430fSAndrzej Pietrasiewicz 		fs_len = ffs_do_descs(ffs->fs_descs_count,
302400a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs),
302500a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz,
302600a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
302700a2430fSAndrzej Pietrasiewicz 		if (unlikely(fs_len < 0)) {
302800a2430fSAndrzej Pietrasiewicz 			ret = fs_len;
302900a2430fSAndrzej Pietrasiewicz 			goto error;
303000a2430fSAndrzej Pietrasiewicz 		}
303100a2430fSAndrzej Pietrasiewicz 	} else {
303200a2430fSAndrzej Pietrasiewicz 		fs_len = 0;
303300a2430fSAndrzej Pietrasiewicz 	}
303400a2430fSAndrzej Pietrasiewicz 
303500a2430fSAndrzej Pietrasiewicz 	if (likely(high)) {
303600a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
303700a2430fSAndrzej Pietrasiewicz 		hs_len = ffs_do_descs(ffs->hs_descs_count,
303800a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) + fs_len,
303900a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz - fs_len,
304000a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
304100a2430fSAndrzej Pietrasiewicz 		if (unlikely(hs_len < 0)) {
304200a2430fSAndrzej Pietrasiewicz 			ret = hs_len;
304300a2430fSAndrzej Pietrasiewicz 			goto error;
304400a2430fSAndrzej Pietrasiewicz 		}
304500a2430fSAndrzej Pietrasiewicz 	} else {
304600a2430fSAndrzej Pietrasiewicz 		hs_len = 0;
304700a2430fSAndrzej Pietrasiewicz 	}
304800a2430fSAndrzej Pietrasiewicz 
304900a2430fSAndrzej Pietrasiewicz 	if (likely(super)) {
305000a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
305100a2430fSAndrzej Pietrasiewicz 		ss_len = ffs_do_descs(ffs->ss_descs_count,
305200a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
305300a2430fSAndrzej Pietrasiewicz 				d_raw_descs__sz - fs_len - hs_len,
305400a2430fSAndrzej Pietrasiewicz 				__ffs_func_bind_do_descs, func);
305500a2430fSAndrzej Pietrasiewicz 		if (unlikely(ss_len < 0)) {
305600a2430fSAndrzej Pietrasiewicz 			ret = ss_len;
305700a2430fSAndrzej Pietrasiewicz 			goto error;
305800a2430fSAndrzej Pietrasiewicz 		}
305900a2430fSAndrzej Pietrasiewicz 	} else {
306000a2430fSAndrzej Pietrasiewicz 		ss_len = 0;
306100a2430fSAndrzej Pietrasiewicz 	}
306200a2430fSAndrzej Pietrasiewicz 
306300a2430fSAndrzej Pietrasiewicz 	/*
306400a2430fSAndrzej Pietrasiewicz 	 * Now handle interface numbers allocation and interface and
306500a2430fSAndrzej Pietrasiewicz 	 * endpoint numbers rewriting.  We can do that in one go
306600a2430fSAndrzej Pietrasiewicz 	 * now.
306700a2430fSAndrzej Pietrasiewicz 	 */
306800a2430fSAndrzej Pietrasiewicz 	ret = ffs_do_descs(ffs->fs_descs_count +
306900a2430fSAndrzej Pietrasiewicz 			   (high ? ffs->hs_descs_count : 0) +
307000a2430fSAndrzej Pietrasiewicz 			   (super ? ffs->ss_descs_count : 0),
307100a2430fSAndrzej Pietrasiewicz 			   vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
307200a2430fSAndrzej Pietrasiewicz 			   __ffs_func_bind_do_nums, func);
307300a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
307400a2430fSAndrzej Pietrasiewicz 		goto error;
307500a2430fSAndrzej Pietrasiewicz 
307600a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
3077c6010c8bSJim Lin 	if (c->cdev->use_os_string) {
307800a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ffs->interfaces_count; ++i) {
307900a2430fSAndrzej Pietrasiewicz 			struct usb_os_desc *desc;
308000a2430fSAndrzej Pietrasiewicz 
308100a2430fSAndrzej Pietrasiewicz 			desc = func->function.os_desc_table[i].os_desc =
308200a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, os_desc) +
308300a2430fSAndrzej Pietrasiewicz 				i * sizeof(struct usb_os_desc);
308400a2430fSAndrzej Pietrasiewicz 			desc->ext_compat_id =
308500a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, ext_compat) + i * 16;
308600a2430fSAndrzej Pietrasiewicz 			INIT_LIST_HEAD(&desc->ext_prop);
308700a2430fSAndrzej Pietrasiewicz 		}
308800a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(ffs->ms_os_descs_count,
308900a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) +
309000a2430fSAndrzej Pietrasiewicz 				      fs_len + hs_len + ss_len,
3091c6010c8bSJim Lin 				      d_raw_descs__sz - fs_len - hs_len -
3092c6010c8bSJim Lin 				      ss_len,
309300a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_os_desc, func);
309400a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
309500a2430fSAndrzej Pietrasiewicz 			goto error;
3096c6010c8bSJim Lin 	}
309700a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_n =
309800a2430fSAndrzej Pietrasiewicz 		c->cdev->use_os_string ? ffs->interfaces_count : 0;
309900a2430fSAndrzej Pietrasiewicz 
310000a2430fSAndrzej Pietrasiewicz 	/* And we're done */
310100a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_BIND);
310200a2430fSAndrzej Pietrasiewicz 	return 0;
310300a2430fSAndrzej Pietrasiewicz 
310400a2430fSAndrzej Pietrasiewicz error:
310500a2430fSAndrzej Pietrasiewicz 	/* XXX Do we need to release all claimed endpoints here? */
310600a2430fSAndrzej Pietrasiewicz 	return ret;
310700a2430fSAndrzej Pietrasiewicz }
310800a2430fSAndrzej Pietrasiewicz 
310900a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *c,
311000a2430fSAndrzej Pietrasiewicz 			 struct usb_function *f)
311100a2430fSAndrzej Pietrasiewicz {
311200a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
311355d81121SRobert Baldyga 	struct ffs_function *func = ffs_func_from_usb(f);
311455d81121SRobert Baldyga 	int ret;
311500a2430fSAndrzej Pietrasiewicz 
311600a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_opts))
311700a2430fSAndrzej Pietrasiewicz 		return PTR_ERR(ffs_opts);
311800a2430fSAndrzej Pietrasiewicz 
311955d81121SRobert Baldyga 	ret = _ffs_func_bind(c, f);
312055d81121SRobert Baldyga 	if (ret && !--ffs_opts->refcnt)
312155d81121SRobert Baldyga 		functionfs_unbind(func->ffs);
312255d81121SRobert Baldyga 
312355d81121SRobert Baldyga 	return ret;
312400a2430fSAndrzej Pietrasiewicz }
312500a2430fSAndrzej Pietrasiewicz 
312600a2430fSAndrzej Pietrasiewicz 
312700a2430fSAndrzej Pietrasiewicz /* Other USB function hooks *************************************************/
312800a2430fSAndrzej Pietrasiewicz 
312918d6b32fSRobert Baldyga static void ffs_reset_work(struct work_struct *work)
313018d6b32fSRobert Baldyga {
313118d6b32fSRobert Baldyga 	struct ffs_data *ffs = container_of(work,
313218d6b32fSRobert Baldyga 		struct ffs_data, reset_work);
313318d6b32fSRobert Baldyga 	ffs_data_reset(ffs);
313418d6b32fSRobert Baldyga }
313518d6b32fSRobert Baldyga 
313600a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *f,
313700a2430fSAndrzej Pietrasiewicz 			    unsigned interface, unsigned alt)
313800a2430fSAndrzej Pietrasiewicz {
313900a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
314000a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
314100a2430fSAndrzej Pietrasiewicz 	int ret = 0, intf;
314200a2430fSAndrzej Pietrasiewicz 
314300a2430fSAndrzej Pietrasiewicz 	if (alt != (unsigned)-1) {
314400a2430fSAndrzej Pietrasiewicz 		intf = ffs_func_revmap_intf(func, interface);
314500a2430fSAndrzej Pietrasiewicz 		if (unlikely(intf < 0))
314600a2430fSAndrzej Pietrasiewicz 			return intf;
314700a2430fSAndrzej Pietrasiewicz 	}
314800a2430fSAndrzej Pietrasiewicz 
314900a2430fSAndrzej Pietrasiewicz 	if (ffs->func)
315000a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(ffs->func);
315100a2430fSAndrzej Pietrasiewicz 
315218d6b32fSRobert Baldyga 	if (ffs->state == FFS_DEACTIVATED) {
315318d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
315418d6b32fSRobert Baldyga 		INIT_WORK(&ffs->reset_work, ffs_reset_work);
315518d6b32fSRobert Baldyga 		schedule_work(&ffs->reset_work);
315618d6b32fSRobert Baldyga 		return -ENODEV;
315718d6b32fSRobert Baldyga 	}
315818d6b32fSRobert Baldyga 
315900a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
316000a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
316100a2430fSAndrzej Pietrasiewicz 
316200a2430fSAndrzej Pietrasiewicz 	if (alt == (unsigned)-1) {
316300a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
316400a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_DISABLE);
316500a2430fSAndrzej Pietrasiewicz 		return 0;
316600a2430fSAndrzej Pietrasiewicz 	}
316700a2430fSAndrzej Pietrasiewicz 
316800a2430fSAndrzej Pietrasiewicz 	ffs->func = func;
316900a2430fSAndrzej Pietrasiewicz 	ret = ffs_func_eps_enable(func);
317000a2430fSAndrzej Pietrasiewicz 	if (likely(ret >= 0))
317100a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_ENABLE);
317200a2430fSAndrzej Pietrasiewicz 	return ret;
317300a2430fSAndrzej Pietrasiewicz }
317400a2430fSAndrzej Pietrasiewicz 
317500a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *f)
317600a2430fSAndrzej Pietrasiewicz {
317700a2430fSAndrzej Pietrasiewicz 	ffs_func_set_alt(f, 0, (unsigned)-1);
317800a2430fSAndrzej Pietrasiewicz }
317900a2430fSAndrzej Pietrasiewicz 
318000a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *f,
318100a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *creq)
318200a2430fSAndrzej Pietrasiewicz {
318300a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
318400a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
318500a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
318600a2430fSAndrzej Pietrasiewicz 	int ret;
318700a2430fSAndrzej Pietrasiewicz 
318800a2430fSAndrzej Pietrasiewicz 	ENTER();
318900a2430fSAndrzej Pietrasiewicz 
319000a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
319100a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequest     = %02x\n", creq->bRequest);
319200a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wValue       = %04x\n", le16_to_cpu(creq->wValue));
319300a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wIndex       = %04x\n", le16_to_cpu(creq->wIndex));
319400a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wLength      = %04x\n", le16_to_cpu(creq->wLength));
319500a2430fSAndrzej Pietrasiewicz 
319600a2430fSAndrzej Pietrasiewicz 	/*
319700a2430fSAndrzej Pietrasiewicz 	 * Most requests directed to interface go through here
319800a2430fSAndrzej Pietrasiewicz 	 * (notable exceptions are set/get interface) so we need to
319900a2430fSAndrzej Pietrasiewicz 	 * handle them.  All other either handled by composite or
320000a2430fSAndrzej Pietrasiewicz 	 * passed to usb_configuration->setup() (if one is set).  No
320100a2430fSAndrzej Pietrasiewicz 	 * matter, we will handle requests directed to endpoint here
320254dfce6dSFelix Hädicke 	 * as well (as it's straightforward).  Other request recipient
320354dfce6dSFelix Hädicke 	 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
320454dfce6dSFelix Hädicke 	 * is being used.
320500a2430fSAndrzej Pietrasiewicz 	 */
320600a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
320700a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
320800a2430fSAndrzej Pietrasiewicz 
320900a2430fSAndrzej Pietrasiewicz 	switch (creq->bRequestType & USB_RECIP_MASK) {
321000a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_INTERFACE:
321100a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
321200a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
321300a2430fSAndrzej Pietrasiewicz 			return ret;
321400a2430fSAndrzej Pietrasiewicz 		break;
321500a2430fSAndrzej Pietrasiewicz 
321600a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_ENDPOINT:
321700a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
321800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
321900a2430fSAndrzej Pietrasiewicz 			return ret;
32201b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
32211b0bf88fSRobert Baldyga 			ret = func->ffs->eps_addrmap[ret];
322200a2430fSAndrzej Pietrasiewicz 		break;
322300a2430fSAndrzej Pietrasiewicz 
322400a2430fSAndrzej Pietrasiewicz 	default:
322554dfce6dSFelix Hädicke 		if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
322654dfce6dSFelix Hädicke 			ret = le16_to_cpu(creq->wIndex);
322754dfce6dSFelix Hädicke 		else
322800a2430fSAndrzej Pietrasiewicz 			return -EOPNOTSUPP;
322900a2430fSAndrzej Pietrasiewicz 	}
323000a2430fSAndrzej Pietrasiewicz 
323100a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
323200a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup = *creq;
323300a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup.wIndex = cpu_to_le16(ret);
323400a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, FUNCTIONFS_SETUP);
323500a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
323600a2430fSAndrzej Pietrasiewicz 
323700a2430fSAndrzej Pietrasiewicz 	return 0;
323800a2430fSAndrzej Pietrasiewicz }
323900a2430fSAndrzej Pietrasiewicz 
324054dfce6dSFelix Hädicke static bool ffs_func_req_match(struct usb_function *f,
32411a00b457SFelix Hädicke 			       const struct usb_ctrlrequest *creq,
32421a00b457SFelix Hädicke 			       bool config0)
324354dfce6dSFelix Hädicke {
324454dfce6dSFelix Hädicke 	struct ffs_function *func = ffs_func_from_usb(f);
324554dfce6dSFelix Hädicke 
32464368c28aSFelix Hädicke 	if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
32471a00b457SFelix Hädicke 		return false;
32481a00b457SFelix Hädicke 
324954dfce6dSFelix Hädicke 	switch (creq->bRequestType & USB_RECIP_MASK) {
325054dfce6dSFelix Hädicke 	case USB_RECIP_INTERFACE:
325105e78c69SFelix Hädicke 		return (ffs_func_revmap_intf(func,
325205e78c69SFelix Hädicke 					     le16_to_cpu(creq->wIndex)) >= 0);
325354dfce6dSFelix Hädicke 	case USB_RECIP_ENDPOINT:
325405e78c69SFelix Hädicke 		return (ffs_func_revmap_ep(func,
325505e78c69SFelix Hädicke 					   le16_to_cpu(creq->wIndex)) >= 0);
325654dfce6dSFelix Hädicke 	default:
325754dfce6dSFelix Hädicke 		return (bool) (func->ffs->user_flags &
325854dfce6dSFelix Hädicke 			       FUNCTIONFS_ALL_CTRL_RECIP);
325954dfce6dSFelix Hädicke 	}
326054dfce6dSFelix Hädicke }
326154dfce6dSFelix Hädicke 
326200a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *f)
326300a2430fSAndrzej Pietrasiewicz {
326400a2430fSAndrzej Pietrasiewicz 	ENTER();
326500a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
326600a2430fSAndrzej Pietrasiewicz }
326700a2430fSAndrzej Pietrasiewicz 
326800a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *f)
326900a2430fSAndrzej Pietrasiewicz {
327000a2430fSAndrzej Pietrasiewicz 	ENTER();
327100a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
327200a2430fSAndrzej Pietrasiewicz }
327300a2430fSAndrzej Pietrasiewicz 
327400a2430fSAndrzej Pietrasiewicz 
327500a2430fSAndrzej Pietrasiewicz /* Endpoint and interface numbers reverse mapping ***************************/
327600a2430fSAndrzej Pietrasiewicz 
327700a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
327800a2430fSAndrzej Pietrasiewicz {
327900a2430fSAndrzej Pietrasiewicz 	num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
328000a2430fSAndrzej Pietrasiewicz 	return num ? num : -EDOM;
328100a2430fSAndrzej Pietrasiewicz }
328200a2430fSAndrzej Pietrasiewicz 
328300a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
328400a2430fSAndrzej Pietrasiewicz {
328500a2430fSAndrzej Pietrasiewicz 	short *nums = func->interfaces_nums;
328600a2430fSAndrzej Pietrasiewicz 	unsigned count = func->ffs->interfaces_count;
328700a2430fSAndrzej Pietrasiewicz 
328800a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++nums) {
328900a2430fSAndrzej Pietrasiewicz 		if (*nums >= 0 && *nums == intf)
329000a2430fSAndrzej Pietrasiewicz 			return nums - func->interfaces_nums;
329100a2430fSAndrzej Pietrasiewicz 	}
329200a2430fSAndrzej Pietrasiewicz 
329300a2430fSAndrzej Pietrasiewicz 	return -EDOM;
329400a2430fSAndrzej Pietrasiewicz }
329500a2430fSAndrzej Pietrasiewicz 
329600a2430fSAndrzej Pietrasiewicz 
329700a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
329800a2430fSAndrzej Pietrasiewicz 
329900a2430fSAndrzej Pietrasiewicz static LIST_HEAD(ffs_devices);
330000a2430fSAndrzej Pietrasiewicz 
330100a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_do_find_dev(const char *name)
330200a2430fSAndrzej Pietrasiewicz {
330300a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
330400a2430fSAndrzej Pietrasiewicz 
330500a2430fSAndrzej Pietrasiewicz 	list_for_each_entry(dev, &ffs_devices, entry) {
330600a2430fSAndrzej Pietrasiewicz 		if (!dev->name || !name)
330700a2430fSAndrzej Pietrasiewicz 			continue;
330800a2430fSAndrzej Pietrasiewicz 		if (strcmp(dev->name, name) == 0)
330900a2430fSAndrzej Pietrasiewicz 			return dev;
331000a2430fSAndrzej Pietrasiewicz 	}
331100a2430fSAndrzej Pietrasiewicz 
331200a2430fSAndrzej Pietrasiewicz 	return NULL;
331300a2430fSAndrzej Pietrasiewicz }
331400a2430fSAndrzej Pietrasiewicz 
331500a2430fSAndrzej Pietrasiewicz /*
331600a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
331700a2430fSAndrzej Pietrasiewicz  */
331800a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_get_single_dev(void)
331900a2430fSAndrzej Pietrasiewicz {
332000a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
332100a2430fSAndrzej Pietrasiewicz 
332200a2430fSAndrzej Pietrasiewicz 	if (list_is_singular(&ffs_devices)) {
332300a2430fSAndrzej Pietrasiewicz 		dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
332400a2430fSAndrzej Pietrasiewicz 		if (dev->single)
332500a2430fSAndrzej Pietrasiewicz 			return dev;
332600a2430fSAndrzej Pietrasiewicz 	}
332700a2430fSAndrzej Pietrasiewicz 
332800a2430fSAndrzej Pietrasiewicz 	return NULL;
332900a2430fSAndrzej Pietrasiewicz }
333000a2430fSAndrzej Pietrasiewicz 
333100a2430fSAndrzej Pietrasiewicz /*
333200a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
333300a2430fSAndrzej Pietrasiewicz  */
333400a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name)
333500a2430fSAndrzej Pietrasiewicz {
333600a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
333700a2430fSAndrzej Pietrasiewicz 
333800a2430fSAndrzej Pietrasiewicz 	dev = _ffs_get_single_dev();
333900a2430fSAndrzej Pietrasiewicz 	if (dev)
334000a2430fSAndrzej Pietrasiewicz 		return dev;
334100a2430fSAndrzej Pietrasiewicz 
334200a2430fSAndrzej Pietrasiewicz 	return _ffs_do_find_dev(name);
334300a2430fSAndrzej Pietrasiewicz }
334400a2430fSAndrzej Pietrasiewicz 
334500a2430fSAndrzej Pietrasiewicz /* Configfs support *********************************************************/
334600a2430fSAndrzej Pietrasiewicz 
334700a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
334800a2430fSAndrzej Pietrasiewicz {
334900a2430fSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_fs_opts,
335000a2430fSAndrzej Pietrasiewicz 			    func_inst.group);
335100a2430fSAndrzej Pietrasiewicz }
335200a2430fSAndrzej Pietrasiewicz 
335300a2430fSAndrzej Pietrasiewicz static void ffs_attr_release(struct config_item *item)
335400a2430fSAndrzej Pietrasiewicz {
335500a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts = to_ffs_opts(item);
335600a2430fSAndrzej Pietrasiewicz 
335700a2430fSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
335800a2430fSAndrzej Pietrasiewicz }
335900a2430fSAndrzej Pietrasiewicz 
336000a2430fSAndrzej Pietrasiewicz static struct configfs_item_operations ffs_item_ops = {
336100a2430fSAndrzej Pietrasiewicz 	.release	= ffs_attr_release,
336200a2430fSAndrzej Pietrasiewicz };
336300a2430fSAndrzej Pietrasiewicz 
336400a2430fSAndrzej Pietrasiewicz static struct config_item_type ffs_func_type = {
336500a2430fSAndrzej Pietrasiewicz 	.ct_item_ops	= &ffs_item_ops,
336600a2430fSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
336700a2430fSAndrzej Pietrasiewicz };
336800a2430fSAndrzej Pietrasiewicz 
336900a2430fSAndrzej Pietrasiewicz 
337000a2430fSAndrzej Pietrasiewicz /* Function registration interface ******************************************/
337100a2430fSAndrzej Pietrasiewicz 
337200a2430fSAndrzej Pietrasiewicz static void ffs_free_inst(struct usb_function_instance *f)
337300a2430fSAndrzej Pietrasiewicz {
337400a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
337500a2430fSAndrzej Pietrasiewicz 
337600a2430fSAndrzej Pietrasiewicz 	opts = to_f_fs_opts(f);
337700a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
337800a2430fSAndrzej Pietrasiewicz 	_ffs_free_dev(opts->dev);
337900a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
338000a2430fSAndrzej Pietrasiewicz 	kfree(opts);
338100a2430fSAndrzej Pietrasiewicz }
338200a2430fSAndrzej Pietrasiewicz 
338300a2430fSAndrzej Pietrasiewicz #define MAX_INST_NAME_LEN	40
338400a2430fSAndrzej Pietrasiewicz 
338500a2430fSAndrzej Pietrasiewicz static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
338600a2430fSAndrzej Pietrasiewicz {
338700a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
338800a2430fSAndrzej Pietrasiewicz 	char *ptr;
338900a2430fSAndrzej Pietrasiewicz 	const char *tmp;
339000a2430fSAndrzej Pietrasiewicz 	int name_len, ret;
339100a2430fSAndrzej Pietrasiewicz 
339200a2430fSAndrzej Pietrasiewicz 	name_len = strlen(name) + 1;
339300a2430fSAndrzej Pietrasiewicz 	if (name_len > MAX_INST_NAME_LEN)
339400a2430fSAndrzej Pietrasiewicz 		return -ENAMETOOLONG;
339500a2430fSAndrzej Pietrasiewicz 
339600a2430fSAndrzej Pietrasiewicz 	ptr = kstrndup(name, name_len, GFP_KERNEL);
339700a2430fSAndrzej Pietrasiewicz 	if (!ptr)
339800a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
339900a2430fSAndrzej Pietrasiewicz 
340000a2430fSAndrzej Pietrasiewicz 	opts = to_f_fs_opts(fi);
340100a2430fSAndrzej Pietrasiewicz 	tmp = NULL;
340200a2430fSAndrzej Pietrasiewicz 
340300a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
340400a2430fSAndrzej Pietrasiewicz 
340500a2430fSAndrzej Pietrasiewicz 	tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
340600a2430fSAndrzej Pietrasiewicz 	ret = _ffs_name_dev(opts->dev, ptr);
340700a2430fSAndrzej Pietrasiewicz 	if (ret) {
340800a2430fSAndrzej Pietrasiewicz 		kfree(ptr);
340900a2430fSAndrzej Pietrasiewicz 		ffs_dev_unlock();
341000a2430fSAndrzej Pietrasiewicz 		return ret;
341100a2430fSAndrzej Pietrasiewicz 	}
341200a2430fSAndrzej Pietrasiewicz 	opts->dev->name_allocated = true;
341300a2430fSAndrzej Pietrasiewicz 
341400a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
341500a2430fSAndrzej Pietrasiewicz 
341600a2430fSAndrzej Pietrasiewicz 	kfree(tmp);
341700a2430fSAndrzej Pietrasiewicz 
341800a2430fSAndrzej Pietrasiewicz 	return 0;
341900a2430fSAndrzej Pietrasiewicz }
342000a2430fSAndrzej Pietrasiewicz 
342100a2430fSAndrzej Pietrasiewicz static struct usb_function_instance *ffs_alloc_inst(void)
342200a2430fSAndrzej Pietrasiewicz {
342300a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
342400a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
342500a2430fSAndrzej Pietrasiewicz 
342600a2430fSAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
342700a2430fSAndrzej Pietrasiewicz 	if (!opts)
342800a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
342900a2430fSAndrzej Pietrasiewicz 
343000a2430fSAndrzej Pietrasiewicz 	opts->func_inst.set_inst_name = ffs_set_inst_name;
343100a2430fSAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = ffs_free_inst;
343200a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
343300a2430fSAndrzej Pietrasiewicz 	dev = _ffs_alloc_dev();
343400a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
343500a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(dev)) {
343600a2430fSAndrzej Pietrasiewicz 		kfree(opts);
343700a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(dev);
343800a2430fSAndrzej Pietrasiewicz 	}
343900a2430fSAndrzej Pietrasiewicz 	opts->dev = dev;
344000a2430fSAndrzej Pietrasiewicz 	dev->opts = opts;
344100a2430fSAndrzej Pietrasiewicz 
344200a2430fSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "",
344300a2430fSAndrzej Pietrasiewicz 				    &ffs_func_type);
344400a2430fSAndrzej Pietrasiewicz 	return &opts->func_inst;
344500a2430fSAndrzej Pietrasiewicz }
344600a2430fSAndrzej Pietrasiewicz 
344700a2430fSAndrzej Pietrasiewicz static void ffs_free(struct usb_function *f)
344800a2430fSAndrzej Pietrasiewicz {
344900a2430fSAndrzej Pietrasiewicz 	kfree(ffs_func_from_usb(f));
345000a2430fSAndrzej Pietrasiewicz }
345100a2430fSAndrzej Pietrasiewicz 
345200a2430fSAndrzej Pietrasiewicz static void ffs_func_unbind(struct usb_configuration *c,
345300a2430fSAndrzej Pietrasiewicz 			    struct usb_function *f)
345400a2430fSAndrzej Pietrasiewicz {
345500a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
345600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
345700a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts =
345800a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
345900a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep = func->eps;
346000a2430fSAndrzej Pietrasiewicz 	unsigned count = ffs->eps_count;
346100a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
346200a2430fSAndrzej Pietrasiewicz 
346300a2430fSAndrzej Pietrasiewicz 	ENTER();
346400a2430fSAndrzej Pietrasiewicz 	if (ffs->func == func) {
346500a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(func);
346600a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
346700a2430fSAndrzej Pietrasiewicz 	}
346800a2430fSAndrzej Pietrasiewicz 
346900a2430fSAndrzej Pietrasiewicz 	if (!--opts->refcnt)
347000a2430fSAndrzej Pietrasiewicz 		functionfs_unbind(ffs);
347100a2430fSAndrzej Pietrasiewicz 
347200a2430fSAndrzej Pietrasiewicz 	/* cleanup after autoconfig */
347300a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
347408f37148SVincent Pelletier 	while (count--) {
347500a2430fSAndrzej Pietrasiewicz 		if (ep->ep && ep->req)
347600a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, ep->req);
347700a2430fSAndrzej Pietrasiewicz 		ep->req = NULL;
347800a2430fSAndrzej Pietrasiewicz 		++ep;
347908f37148SVincent Pelletier 	}
348000a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
348100a2430fSAndrzej Pietrasiewicz 	kfree(func->eps);
348200a2430fSAndrzej Pietrasiewicz 	func->eps = NULL;
348300a2430fSAndrzej Pietrasiewicz 	/*
348400a2430fSAndrzej Pietrasiewicz 	 * eps, descriptors and interfaces_nums are allocated in the
348500a2430fSAndrzej Pietrasiewicz 	 * same chunk so only one free is required.
348600a2430fSAndrzej Pietrasiewicz 	 */
348700a2430fSAndrzej Pietrasiewicz 	func->function.fs_descriptors = NULL;
348800a2430fSAndrzej Pietrasiewicz 	func->function.hs_descriptors = NULL;
348900a2430fSAndrzej Pietrasiewicz 	func->function.ss_descriptors = NULL;
349000a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = NULL;
349100a2430fSAndrzej Pietrasiewicz 
349200a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_UNBIND);
349300a2430fSAndrzej Pietrasiewicz }
349400a2430fSAndrzej Pietrasiewicz 
349500a2430fSAndrzej Pietrasiewicz static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
349600a2430fSAndrzej Pietrasiewicz {
349700a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func;
349800a2430fSAndrzej Pietrasiewicz 
349900a2430fSAndrzej Pietrasiewicz 	ENTER();
350000a2430fSAndrzej Pietrasiewicz 
350100a2430fSAndrzej Pietrasiewicz 	func = kzalloc(sizeof(*func), GFP_KERNEL);
350200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!func))
350300a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
350400a2430fSAndrzej Pietrasiewicz 
350500a2430fSAndrzej Pietrasiewicz 	func->function.name    = "Function FS Gadget";
350600a2430fSAndrzej Pietrasiewicz 
350700a2430fSAndrzej Pietrasiewicz 	func->function.bind    = ffs_func_bind;
350800a2430fSAndrzej Pietrasiewicz 	func->function.unbind  = ffs_func_unbind;
350900a2430fSAndrzej Pietrasiewicz 	func->function.set_alt = ffs_func_set_alt;
351000a2430fSAndrzej Pietrasiewicz 	func->function.disable = ffs_func_disable;
351100a2430fSAndrzej Pietrasiewicz 	func->function.setup   = ffs_func_setup;
351254dfce6dSFelix Hädicke 	func->function.req_match = ffs_func_req_match;
351300a2430fSAndrzej Pietrasiewicz 	func->function.suspend = ffs_func_suspend;
351400a2430fSAndrzej Pietrasiewicz 	func->function.resume  = ffs_func_resume;
351500a2430fSAndrzej Pietrasiewicz 	func->function.free_func = ffs_free;
351600a2430fSAndrzej Pietrasiewicz 
351700a2430fSAndrzej Pietrasiewicz 	return &func->function;
351800a2430fSAndrzej Pietrasiewicz }
351900a2430fSAndrzej Pietrasiewicz 
352000a2430fSAndrzej Pietrasiewicz /*
352100a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
352200a2430fSAndrzej Pietrasiewicz  */
352300a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void)
352400a2430fSAndrzej Pietrasiewicz {
352500a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
352600a2430fSAndrzej Pietrasiewicz 	int ret;
352700a2430fSAndrzej Pietrasiewicz 
352800a2430fSAndrzej Pietrasiewicz 	if (_ffs_get_single_dev())
352900a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(-EBUSY);
353000a2430fSAndrzej Pietrasiewicz 
353100a2430fSAndrzej Pietrasiewicz 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
353200a2430fSAndrzej Pietrasiewicz 	if (!dev)
353300a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
353400a2430fSAndrzej Pietrasiewicz 
353500a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices)) {
353600a2430fSAndrzej Pietrasiewicz 		ret = functionfs_init();
353700a2430fSAndrzej Pietrasiewicz 		if (ret) {
353800a2430fSAndrzej Pietrasiewicz 			kfree(dev);
353900a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
354000a2430fSAndrzej Pietrasiewicz 		}
354100a2430fSAndrzej Pietrasiewicz 	}
354200a2430fSAndrzej Pietrasiewicz 
354300a2430fSAndrzej Pietrasiewicz 	list_add(&dev->entry, &ffs_devices);
354400a2430fSAndrzej Pietrasiewicz 
354500a2430fSAndrzej Pietrasiewicz 	return dev;
354600a2430fSAndrzej Pietrasiewicz }
354700a2430fSAndrzej Pietrasiewicz 
354800a2430fSAndrzej Pietrasiewicz /*
354900a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
355000a2430fSAndrzej Pietrasiewicz  * The caller is responsible for "name" being available whenever f_fs needs it
355100a2430fSAndrzej Pietrasiewicz  */
355200a2430fSAndrzej Pietrasiewicz static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
355300a2430fSAndrzej Pietrasiewicz {
355400a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *existing;
355500a2430fSAndrzej Pietrasiewicz 
355600a2430fSAndrzej Pietrasiewicz 	existing = _ffs_do_find_dev(name);
355700a2430fSAndrzej Pietrasiewicz 	if (existing)
355800a2430fSAndrzej Pietrasiewicz 		return -EBUSY;
355900a2430fSAndrzej Pietrasiewicz 
356000a2430fSAndrzej Pietrasiewicz 	dev->name = name;
356100a2430fSAndrzej Pietrasiewicz 
356200a2430fSAndrzej Pietrasiewicz 	return 0;
356300a2430fSAndrzej Pietrasiewicz }
356400a2430fSAndrzej Pietrasiewicz 
356500a2430fSAndrzej Pietrasiewicz /*
356600a2430fSAndrzej Pietrasiewicz  * The caller is responsible for "name" being available whenever f_fs needs it
356700a2430fSAndrzej Pietrasiewicz  */
356800a2430fSAndrzej Pietrasiewicz int ffs_name_dev(struct ffs_dev *dev, const char *name)
356900a2430fSAndrzej Pietrasiewicz {
357000a2430fSAndrzej Pietrasiewicz 	int ret;
357100a2430fSAndrzej Pietrasiewicz 
357200a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
357300a2430fSAndrzej Pietrasiewicz 	ret = _ffs_name_dev(dev, name);
357400a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
357500a2430fSAndrzej Pietrasiewicz 
357600a2430fSAndrzej Pietrasiewicz 	return ret;
357700a2430fSAndrzej Pietrasiewicz }
357800a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_name_dev);
357900a2430fSAndrzej Pietrasiewicz 
358000a2430fSAndrzej Pietrasiewicz int ffs_single_dev(struct ffs_dev *dev)
358100a2430fSAndrzej Pietrasiewicz {
358200a2430fSAndrzej Pietrasiewicz 	int ret;
358300a2430fSAndrzej Pietrasiewicz 
358400a2430fSAndrzej Pietrasiewicz 	ret = 0;
358500a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
358600a2430fSAndrzej Pietrasiewicz 
358700a2430fSAndrzej Pietrasiewicz 	if (!list_is_singular(&ffs_devices))
358800a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
358900a2430fSAndrzej Pietrasiewicz 	else
359000a2430fSAndrzej Pietrasiewicz 		dev->single = true;
359100a2430fSAndrzej Pietrasiewicz 
359200a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
359300a2430fSAndrzej Pietrasiewicz 	return ret;
359400a2430fSAndrzej Pietrasiewicz }
359500a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_single_dev);
359600a2430fSAndrzej Pietrasiewicz 
359700a2430fSAndrzej Pietrasiewicz /*
359800a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
359900a2430fSAndrzej Pietrasiewicz  */
360000a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev)
360100a2430fSAndrzej Pietrasiewicz {
360200a2430fSAndrzej Pietrasiewicz 	list_del(&dev->entry);
360300a2430fSAndrzej Pietrasiewicz 	if (dev->name_allocated)
360400a2430fSAndrzej Pietrasiewicz 		kfree(dev->name);
36053262ad82SJim Baxter 
36063262ad82SJim Baxter 	/* Clear the private_data pointer to stop incorrect dev access */
36073262ad82SJim Baxter 	if (dev->ffs_data)
36083262ad82SJim Baxter 		dev->ffs_data->private_data = NULL;
36093262ad82SJim Baxter 
361000a2430fSAndrzej Pietrasiewicz 	kfree(dev);
361100a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices))
361200a2430fSAndrzej Pietrasiewicz 		functionfs_cleanup();
361300a2430fSAndrzej Pietrasiewicz }
361400a2430fSAndrzej Pietrasiewicz 
361500a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name)
361600a2430fSAndrzej Pietrasiewicz {
361700a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
361800a2430fSAndrzej Pietrasiewicz 
361900a2430fSAndrzej Pietrasiewicz 	ENTER();
362000a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
362100a2430fSAndrzej Pietrasiewicz 
362200a2430fSAndrzej Pietrasiewicz 	ffs_dev = _ffs_find_dev(dev_name);
362300a2430fSAndrzej Pietrasiewicz 	if (!ffs_dev)
362400a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
362500a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->mounted)
362600a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-EBUSY);
362700a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->ffs_acquire_dev_callback &&
362800a2430fSAndrzej Pietrasiewicz 	    ffs_dev->ffs_acquire_dev_callback(ffs_dev))
362900a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
363000a2430fSAndrzej Pietrasiewicz 	else
363100a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = true;
363200a2430fSAndrzej Pietrasiewicz 
363300a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
363400a2430fSAndrzej Pietrasiewicz 	return ffs_dev;
363500a2430fSAndrzej Pietrasiewicz }
363600a2430fSAndrzej Pietrasiewicz 
363700a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data)
363800a2430fSAndrzej Pietrasiewicz {
363900a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
364000a2430fSAndrzej Pietrasiewicz 
364100a2430fSAndrzej Pietrasiewicz 	ENTER();
364200a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
364300a2430fSAndrzej Pietrasiewicz 
364400a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_data->private_data;
364500a2430fSAndrzej Pietrasiewicz 	if (ffs_dev) {
364600a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = false;
364700a2430fSAndrzej Pietrasiewicz 
364800a2430fSAndrzej Pietrasiewicz 		if (ffs_dev->ffs_release_dev_callback)
364900a2430fSAndrzej Pietrasiewicz 			ffs_dev->ffs_release_dev_callback(ffs_dev);
365000a2430fSAndrzej Pietrasiewicz 	}
365100a2430fSAndrzej Pietrasiewicz 
365200a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
365300a2430fSAndrzej Pietrasiewicz }
365400a2430fSAndrzej Pietrasiewicz 
365500a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs)
365600a2430fSAndrzej Pietrasiewicz {
365700a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
365800a2430fSAndrzej Pietrasiewicz 	int ret = 0;
365900a2430fSAndrzej Pietrasiewicz 
366000a2430fSAndrzej Pietrasiewicz 	ENTER();
366100a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
366200a2430fSAndrzej Pietrasiewicz 
366300a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
366400a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj) {
366500a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
366600a2430fSAndrzej Pietrasiewicz 		goto done;
366700a2430fSAndrzej Pietrasiewicz 	}
366800a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs_obj->desc_ready)) {
366900a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
367000a2430fSAndrzej Pietrasiewicz 		goto done;
367100a2430fSAndrzej Pietrasiewicz 	}
367200a2430fSAndrzej Pietrasiewicz 
367300a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = true;
367400a2430fSAndrzej Pietrasiewicz 	ffs_obj->ffs_data = ffs;
367500a2430fSAndrzej Pietrasiewicz 
367649a79d8bSKrzysztof Opasiak 	if (ffs_obj->ffs_ready_callback) {
367700a2430fSAndrzej Pietrasiewicz 		ret = ffs_obj->ffs_ready_callback(ffs);
367849a79d8bSKrzysztof Opasiak 		if (ret)
367949a79d8bSKrzysztof Opasiak 			goto done;
368049a79d8bSKrzysztof Opasiak 	}
368100a2430fSAndrzej Pietrasiewicz 
368249a79d8bSKrzysztof Opasiak 	set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
368300a2430fSAndrzej Pietrasiewicz done:
368400a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
368500a2430fSAndrzej Pietrasiewicz 	return ret;
368600a2430fSAndrzej Pietrasiewicz }
368700a2430fSAndrzej Pietrasiewicz 
368800a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs)
368900a2430fSAndrzej Pietrasiewicz {
369000a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
3691f14e9ad1SRui Miguel Silva 	struct f_fs_opts *opts;
3692b3ce3ce0SBaolin Wang 	struct config_item *ci;
369300a2430fSAndrzej Pietrasiewicz 
369400a2430fSAndrzej Pietrasiewicz 	ENTER();
369500a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
369600a2430fSAndrzej Pietrasiewicz 
369700a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
369800a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj)
369900a2430fSAndrzej Pietrasiewicz 		goto done;
370000a2430fSAndrzej Pietrasiewicz 
370100a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = false;
370200a2430fSAndrzej Pietrasiewicz 
370349a79d8bSKrzysztof Opasiak 	if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
370449a79d8bSKrzysztof Opasiak 	    ffs_obj->ffs_closed_callback)
370500a2430fSAndrzej Pietrasiewicz 		ffs_obj->ffs_closed_callback(ffs);
370600a2430fSAndrzej Pietrasiewicz 
3707f14e9ad1SRui Miguel Silva 	if (ffs_obj->opts)
3708f14e9ad1SRui Miguel Silva 		opts = ffs_obj->opts;
3709f14e9ad1SRui Miguel Silva 	else
3710f14e9ad1SRui Miguel Silva 		goto done;
3711f14e9ad1SRui Miguel Silva 
3712f14e9ad1SRui Miguel Silva 	if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
37132c935bc5SPeter Zijlstra 	    || !kref_read(&opts->func_inst.group.cg_item.ci_kref))
371400a2430fSAndrzej Pietrasiewicz 		goto done;
371500a2430fSAndrzej Pietrasiewicz 
3716b3ce3ce0SBaolin Wang 	ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
3717b3ce3ce0SBaolin Wang 	ffs_dev_unlock();
3718b3ce3ce0SBaolin Wang 
3719b3ce3ce0SBaolin Wang 	unregister_gadget_item(ci);
3720b3ce3ce0SBaolin Wang 	return;
372100a2430fSAndrzej Pietrasiewicz done:
372200a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
372300a2430fSAndrzej Pietrasiewicz }
372400a2430fSAndrzej Pietrasiewicz 
372500a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
372600a2430fSAndrzej Pietrasiewicz 
372700a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
372800a2430fSAndrzej Pietrasiewicz {
372900a2430fSAndrzej Pietrasiewicz 	return nonblock
373000a2430fSAndrzej Pietrasiewicz 		? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
373100a2430fSAndrzej Pietrasiewicz 		: mutex_lock_interruptible(mutex);
373200a2430fSAndrzej Pietrasiewicz }
373300a2430fSAndrzej Pietrasiewicz 
373400a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
373500a2430fSAndrzej Pietrasiewicz {
373600a2430fSAndrzej Pietrasiewicz 	char *data;
373700a2430fSAndrzej Pietrasiewicz 
373800a2430fSAndrzej Pietrasiewicz 	if (unlikely(!len))
373900a2430fSAndrzej Pietrasiewicz 		return NULL;
374000a2430fSAndrzej Pietrasiewicz 
374100a2430fSAndrzej Pietrasiewicz 	data = kmalloc(len, GFP_KERNEL);
374200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!data))
374300a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
374400a2430fSAndrzej Pietrasiewicz 
37457fe9a937SDaniel Walter 	if (unlikely(copy_from_user(data, buf, len))) {
374600a2430fSAndrzej Pietrasiewicz 		kfree(data);
374700a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-EFAULT);
374800a2430fSAndrzej Pietrasiewicz 	}
374900a2430fSAndrzej Pietrasiewicz 
375000a2430fSAndrzej Pietrasiewicz 	pr_vdebug("Buffer from user space:\n");
375100a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem("", data, len);
375200a2430fSAndrzej Pietrasiewicz 
375300a2430fSAndrzej Pietrasiewicz 	return data;
375400a2430fSAndrzej Pietrasiewicz }
375500a2430fSAndrzej Pietrasiewicz 
375600a2430fSAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
375700a2430fSAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
375800a2430fSAndrzej Pietrasiewicz MODULE_AUTHOR("Michal Nazarewicz");
3759