xref: /openbmc/linux/drivers/usb/gadget/function/f_fs.c (revision 078cd827)
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>
26e2e40f2cSChristoph Hellwig #include <linux/uio.h>
2700a2430fSAndrzej Pietrasiewicz #include <asm/unaligned.h>
2800a2430fSAndrzej Pietrasiewicz 
2900a2430fSAndrzej Pietrasiewicz #include <linux/usb/composite.h>
3000a2430fSAndrzej Pietrasiewicz #include <linux/usb/functionfs.h>
3100a2430fSAndrzej Pietrasiewicz 
3200a2430fSAndrzej Pietrasiewicz #include <linux/aio.h>
3300a2430fSAndrzej Pietrasiewicz #include <linux/mmu_context.h>
3400a2430fSAndrzej Pietrasiewicz #include <linux/poll.h>
355e33f6fdSRobert Baldyga #include <linux/eventfd.h>
3600a2430fSAndrzej Pietrasiewicz 
3700a2430fSAndrzej Pietrasiewicz #include "u_fs.h"
3800a2430fSAndrzej Pietrasiewicz #include "u_f.h"
3900a2430fSAndrzej Pietrasiewicz #include "u_os_desc.h"
4000a2430fSAndrzej Pietrasiewicz #include "configfs.h"
4100a2430fSAndrzej Pietrasiewicz 
4200a2430fSAndrzej Pietrasiewicz #define FUNCTIONFS_MAGIC	0xa647361 /* Chosen by a honest dice roll ;) */
4300a2430fSAndrzej Pietrasiewicz 
4400a2430fSAndrzej Pietrasiewicz /* Reference counter handling */
4500a2430fSAndrzej Pietrasiewicz static void ffs_data_get(struct ffs_data *ffs);
4600a2430fSAndrzej Pietrasiewicz static void ffs_data_put(struct ffs_data *ffs);
4700a2430fSAndrzej Pietrasiewicz /* Creates new ffs_data object. */
4800a2430fSAndrzej Pietrasiewicz static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
4900a2430fSAndrzej Pietrasiewicz 
5000a2430fSAndrzej Pietrasiewicz /* Opened counter handling. */
5100a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs);
5200a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs);
5300a2430fSAndrzej Pietrasiewicz 
5400a2430fSAndrzej Pietrasiewicz /* Called with ffs->mutex held; take over ownership of data. */
5500a2430fSAndrzej Pietrasiewicz static int __must_check
5600a2430fSAndrzej Pietrasiewicz __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
5700a2430fSAndrzej Pietrasiewicz static int __must_check
5800a2430fSAndrzej Pietrasiewicz __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
5900a2430fSAndrzej Pietrasiewicz 
6000a2430fSAndrzej Pietrasiewicz 
6100a2430fSAndrzej Pietrasiewicz /* The function structure ***************************************************/
6200a2430fSAndrzej Pietrasiewicz 
6300a2430fSAndrzej Pietrasiewicz struct ffs_ep;
6400a2430fSAndrzej Pietrasiewicz 
6500a2430fSAndrzej Pietrasiewicz struct ffs_function {
6600a2430fSAndrzej Pietrasiewicz 	struct usb_configuration	*conf;
6700a2430fSAndrzej Pietrasiewicz 	struct usb_gadget		*gadget;
6800a2430fSAndrzej Pietrasiewicz 	struct ffs_data			*ffs;
6900a2430fSAndrzej Pietrasiewicz 
7000a2430fSAndrzej Pietrasiewicz 	struct ffs_ep			*eps;
7100a2430fSAndrzej Pietrasiewicz 	u8				eps_revmap[16];
7200a2430fSAndrzej Pietrasiewicz 	short				*interfaces_nums;
7300a2430fSAndrzej Pietrasiewicz 
7400a2430fSAndrzej Pietrasiewicz 	struct usb_function		function;
7500a2430fSAndrzej Pietrasiewicz };
7600a2430fSAndrzej Pietrasiewicz 
7700a2430fSAndrzej Pietrasiewicz 
7800a2430fSAndrzej Pietrasiewicz static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
7900a2430fSAndrzej Pietrasiewicz {
8000a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct ffs_function, function);
8100a2430fSAndrzej Pietrasiewicz }
8200a2430fSAndrzej Pietrasiewicz 
8300a2430fSAndrzej Pietrasiewicz 
8400a2430fSAndrzej Pietrasiewicz static inline enum ffs_setup_state
8500a2430fSAndrzej Pietrasiewicz ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
8600a2430fSAndrzej Pietrasiewicz {
8700a2430fSAndrzej Pietrasiewicz 	return (enum ffs_setup_state)
8800a2430fSAndrzej Pietrasiewicz 		cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
8900a2430fSAndrzej Pietrasiewicz }
9000a2430fSAndrzej Pietrasiewicz 
9100a2430fSAndrzej Pietrasiewicz 
9200a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func);
9300a2430fSAndrzej Pietrasiewicz static int __must_check ffs_func_eps_enable(struct ffs_function *func);
9400a2430fSAndrzej Pietrasiewicz 
9500a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *,
9600a2430fSAndrzej Pietrasiewicz 			 struct usb_function *);
9700a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
9800a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *);
9900a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *,
10000a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *);
10100a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *);
10200a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *);
10300a2430fSAndrzej Pietrasiewicz 
10400a2430fSAndrzej Pietrasiewicz 
10500a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
10600a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
10700a2430fSAndrzej Pietrasiewicz 
10800a2430fSAndrzej Pietrasiewicz 
10900a2430fSAndrzej Pietrasiewicz /* The endpoints structures *************************************************/
11000a2430fSAndrzej Pietrasiewicz 
11100a2430fSAndrzej Pietrasiewicz struct ffs_ep {
11200a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*ep;	/* P: ffs->eps_lock */
11300a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;	/* P: epfile->mutex */
11400a2430fSAndrzej Pietrasiewicz 
11500a2430fSAndrzej Pietrasiewicz 	/* [0]: full speed, [1]: high speed, [2]: super speed */
11600a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor	*descs[3];
11700a2430fSAndrzej Pietrasiewicz 
11800a2430fSAndrzej Pietrasiewicz 	u8				num;
11900a2430fSAndrzej Pietrasiewicz 
12000a2430fSAndrzej Pietrasiewicz 	int				status;	/* P: epfile->mutex */
12100a2430fSAndrzej Pietrasiewicz };
12200a2430fSAndrzej Pietrasiewicz 
12300a2430fSAndrzej Pietrasiewicz struct ffs_epfile {
12400a2430fSAndrzej Pietrasiewicz 	/* Protects ep->ep and ep->req. */
12500a2430fSAndrzej Pietrasiewicz 	struct mutex			mutex;
12600a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		wait;
12700a2430fSAndrzej Pietrasiewicz 
12800a2430fSAndrzej Pietrasiewicz 	struct ffs_data			*ffs;
12900a2430fSAndrzej Pietrasiewicz 	struct ffs_ep			*ep;	/* P: ffs->eps_lock */
13000a2430fSAndrzej Pietrasiewicz 
13100a2430fSAndrzej Pietrasiewicz 	struct dentry			*dentry;
13200a2430fSAndrzej Pietrasiewicz 
1339353afbbSMichal Nazarewicz 	/*
1349353afbbSMichal Nazarewicz 	 * Buffer for holding data from partial reads which may happen since
1359353afbbSMichal Nazarewicz 	 * we’re rounding user read requests to a multiple of a max packet size.
1369353afbbSMichal Nazarewicz 	 */
1379353afbbSMichal Nazarewicz 	struct ffs_buffer		*read_buffer;	/* P: epfile->mutex */
1389353afbbSMichal Nazarewicz 
13900a2430fSAndrzej Pietrasiewicz 	char				name[5];
14000a2430fSAndrzej Pietrasiewicz 
14100a2430fSAndrzej Pietrasiewicz 	unsigned char			in;	/* P: ffs->eps_lock */
14200a2430fSAndrzej Pietrasiewicz 	unsigned char			isoc;	/* P: ffs->eps_lock */
14300a2430fSAndrzej Pietrasiewicz 
14400a2430fSAndrzej Pietrasiewicz 	unsigned char			_pad;
14500a2430fSAndrzej Pietrasiewicz };
14600a2430fSAndrzej Pietrasiewicz 
1479353afbbSMichal Nazarewicz struct ffs_buffer {
1489353afbbSMichal Nazarewicz 	size_t length;
1499353afbbSMichal Nazarewicz 	char *data;
1509353afbbSMichal Nazarewicz 	char storage[];
1519353afbbSMichal Nazarewicz };
1529353afbbSMichal Nazarewicz 
15300a2430fSAndrzej Pietrasiewicz /*  ffs_io_data structure ***************************************************/
15400a2430fSAndrzej Pietrasiewicz 
15500a2430fSAndrzej Pietrasiewicz struct ffs_io_data {
15600a2430fSAndrzej Pietrasiewicz 	bool aio;
15700a2430fSAndrzej Pietrasiewicz 	bool read;
15800a2430fSAndrzej Pietrasiewicz 
15900a2430fSAndrzej Pietrasiewicz 	struct kiocb *kiocb;
160c993c39bSAl Viro 	struct iov_iter data;
161c993c39bSAl Viro 	const void *to_free;
162c993c39bSAl Viro 	char *buf;
16300a2430fSAndrzej Pietrasiewicz 
16400a2430fSAndrzej Pietrasiewicz 	struct mm_struct *mm;
16500a2430fSAndrzej Pietrasiewicz 	struct work_struct work;
16600a2430fSAndrzej Pietrasiewicz 
16700a2430fSAndrzej Pietrasiewicz 	struct usb_ep *ep;
16800a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
1695e33f6fdSRobert Baldyga 
1705e33f6fdSRobert Baldyga 	struct ffs_data *ffs;
17100a2430fSAndrzej Pietrasiewicz };
17200a2430fSAndrzej Pietrasiewicz 
1736d5c1c77SRobert Baldyga struct ffs_desc_helper {
1746d5c1c77SRobert Baldyga 	struct ffs_data *ffs;
1756d5c1c77SRobert Baldyga 	unsigned interfaces_count;
1766d5c1c77SRobert Baldyga 	unsigned eps_count;
1776d5c1c77SRobert Baldyga };
1786d5c1c77SRobert Baldyga 
17900a2430fSAndrzej Pietrasiewicz static int  __must_check ffs_epfiles_create(struct ffs_data *ffs);
18000a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
18100a2430fSAndrzej Pietrasiewicz 
1821bb27cacSAl Viro static struct dentry *
18300a2430fSAndrzej Pietrasiewicz ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
1841bb27cacSAl Viro 		   const struct file_operations *fops);
18500a2430fSAndrzej Pietrasiewicz 
18600a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
18700a2430fSAndrzej Pietrasiewicz 
18800a2430fSAndrzej Pietrasiewicz DEFINE_MUTEX(ffs_lock);
18900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_lock);
19000a2430fSAndrzej Pietrasiewicz 
19100a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name);
19200a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void);
19300a2430fSAndrzej Pietrasiewicz static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
19400a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev);
19500a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name);
19600a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data);
19700a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs);
19800a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs);
19900a2430fSAndrzej Pietrasiewicz 
20000a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
20100a2430fSAndrzej Pietrasiewicz 
20200a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
20300a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
20400a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
20500a2430fSAndrzej Pietrasiewicz 	__attribute__((warn_unused_result, nonnull));
20600a2430fSAndrzej Pietrasiewicz 
20700a2430fSAndrzej Pietrasiewicz 
20800a2430fSAndrzej Pietrasiewicz /* Control file aka ep0 *****************************************************/
20900a2430fSAndrzej Pietrasiewicz 
21000a2430fSAndrzej Pietrasiewicz static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
21100a2430fSAndrzej Pietrasiewicz {
21200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = req->context;
21300a2430fSAndrzej Pietrasiewicz 
21400a2430fSAndrzej Pietrasiewicz 	complete_all(&ffs->ep0req_completion);
21500a2430fSAndrzej Pietrasiewicz }
21600a2430fSAndrzej Pietrasiewicz 
21700a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
21800a2430fSAndrzej Pietrasiewicz {
21900a2430fSAndrzej Pietrasiewicz 	struct usb_request *req = ffs->ep0req;
22000a2430fSAndrzej Pietrasiewicz 	int ret;
22100a2430fSAndrzej Pietrasiewicz 
22200a2430fSAndrzej Pietrasiewicz 	req->zero     = len < le16_to_cpu(ffs->ev.setup.wLength);
22300a2430fSAndrzej Pietrasiewicz 
22400a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
22500a2430fSAndrzej Pietrasiewicz 
22600a2430fSAndrzej Pietrasiewicz 	req->buf      = data;
22700a2430fSAndrzej Pietrasiewicz 	req->length   = len;
22800a2430fSAndrzej Pietrasiewicz 
22900a2430fSAndrzej Pietrasiewicz 	/*
23000a2430fSAndrzej Pietrasiewicz 	 * UDC layer requires to provide a buffer even for ZLP, but should
23100a2430fSAndrzej Pietrasiewicz 	 * not use it at all. Let's provide some poisoned pointer to catch
23200a2430fSAndrzej Pietrasiewicz 	 * possible bug in the driver.
23300a2430fSAndrzej Pietrasiewicz 	 */
23400a2430fSAndrzej Pietrasiewicz 	if (req->buf == NULL)
23500a2430fSAndrzej Pietrasiewicz 		req->buf = (void *)0xDEADBABE;
23600a2430fSAndrzej Pietrasiewicz 
23700a2430fSAndrzej Pietrasiewicz 	reinit_completion(&ffs->ep0req_completion);
23800a2430fSAndrzej Pietrasiewicz 
23900a2430fSAndrzej Pietrasiewicz 	ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
24000a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
24100a2430fSAndrzej Pietrasiewicz 		return ret;
24200a2430fSAndrzej Pietrasiewicz 
24300a2430fSAndrzej Pietrasiewicz 	ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
24400a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret)) {
24500a2430fSAndrzej Pietrasiewicz 		usb_ep_dequeue(ffs->gadget->ep0, req);
24600a2430fSAndrzej Pietrasiewicz 		return -EINTR;
24700a2430fSAndrzej Pietrasiewicz 	}
24800a2430fSAndrzej Pietrasiewicz 
24900a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
25000a2430fSAndrzej Pietrasiewicz 	return req->status ? req->status : req->actual;
25100a2430fSAndrzej Pietrasiewicz }
25200a2430fSAndrzej Pietrasiewicz 
25300a2430fSAndrzej Pietrasiewicz static int __ffs_ep0_stall(struct ffs_data *ffs)
25400a2430fSAndrzej Pietrasiewicz {
25500a2430fSAndrzej Pietrasiewicz 	if (ffs->ev.can_stall) {
25600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("ep0 stall\n");
25700a2430fSAndrzej Pietrasiewicz 		usb_ep_set_halt(ffs->gadget->ep0);
25800a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_NO_SETUP;
25900a2430fSAndrzej Pietrasiewicz 		return -EL2HLT;
26000a2430fSAndrzej Pietrasiewicz 	} else {
26100a2430fSAndrzej Pietrasiewicz 		pr_debug("bogus ep0 stall!\n");
26200a2430fSAndrzej Pietrasiewicz 		return -ESRCH;
26300a2430fSAndrzej Pietrasiewicz 	}
26400a2430fSAndrzej Pietrasiewicz }
26500a2430fSAndrzej Pietrasiewicz 
26600a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
26700a2430fSAndrzej Pietrasiewicz 			     size_t len, loff_t *ptr)
26800a2430fSAndrzej Pietrasiewicz {
26900a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
27000a2430fSAndrzej Pietrasiewicz 	ssize_t ret;
27100a2430fSAndrzej Pietrasiewicz 	char *data;
27200a2430fSAndrzej Pietrasiewicz 
27300a2430fSAndrzej Pietrasiewicz 	ENTER();
27400a2430fSAndrzej Pietrasiewicz 
27500a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
27600a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
27700a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
27800a2430fSAndrzej Pietrasiewicz 
27900a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
28000a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
28100a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
28200a2430fSAndrzej Pietrasiewicz 		return ret;
28300a2430fSAndrzej Pietrasiewicz 
28400a2430fSAndrzej Pietrasiewicz 	/* Check state */
28500a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
28600a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
28700a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
28800a2430fSAndrzej Pietrasiewicz 		/* Copy data */
28900a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 16)) {
29000a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
29100a2430fSAndrzej Pietrasiewicz 			break;
29200a2430fSAndrzej Pietrasiewicz 		}
29300a2430fSAndrzej Pietrasiewicz 
29400a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
29500a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
29600a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
29700a2430fSAndrzej Pietrasiewicz 			break;
29800a2430fSAndrzej Pietrasiewicz 		}
29900a2430fSAndrzej Pietrasiewicz 
30000a2430fSAndrzej Pietrasiewicz 		/* Handle data */
30100a2430fSAndrzej Pietrasiewicz 		if (ffs->state == FFS_READ_DESCRIPTORS) {
30200a2430fSAndrzej Pietrasiewicz 			pr_info("read descriptors\n");
30300a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_descs(ffs, data, len);
30400a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
30500a2430fSAndrzej Pietrasiewicz 				break;
30600a2430fSAndrzej Pietrasiewicz 
30700a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_READ_STRINGS;
30800a2430fSAndrzej Pietrasiewicz 			ret = len;
30900a2430fSAndrzej Pietrasiewicz 		} else {
31000a2430fSAndrzej Pietrasiewicz 			pr_info("read strings\n");
31100a2430fSAndrzej Pietrasiewicz 			ret = __ffs_data_got_strings(ffs, data, len);
31200a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0))
31300a2430fSAndrzej Pietrasiewicz 				break;
31400a2430fSAndrzej Pietrasiewicz 
31500a2430fSAndrzej Pietrasiewicz 			ret = ffs_epfiles_create(ffs);
31600a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret)) {
31700a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
31800a2430fSAndrzej Pietrasiewicz 				break;
31900a2430fSAndrzej Pietrasiewicz 			}
32000a2430fSAndrzej Pietrasiewicz 
32100a2430fSAndrzej Pietrasiewicz 			ffs->state = FFS_ACTIVE;
32200a2430fSAndrzej Pietrasiewicz 			mutex_unlock(&ffs->mutex);
32300a2430fSAndrzej Pietrasiewicz 
32400a2430fSAndrzej Pietrasiewicz 			ret = ffs_ready(ffs);
32500a2430fSAndrzej Pietrasiewicz 			if (unlikely(ret < 0)) {
32600a2430fSAndrzej Pietrasiewicz 				ffs->state = FFS_CLOSING;
32700a2430fSAndrzej Pietrasiewicz 				return ret;
32800a2430fSAndrzej Pietrasiewicz 			}
32900a2430fSAndrzej Pietrasiewicz 
33000a2430fSAndrzej Pietrasiewicz 			return len;
33100a2430fSAndrzej Pietrasiewicz 		}
33200a2430fSAndrzej Pietrasiewicz 		break;
33300a2430fSAndrzej Pietrasiewicz 
33400a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
33500a2430fSAndrzej Pietrasiewicz 		data = NULL;
33600a2430fSAndrzej Pietrasiewicz 		/*
33700a2430fSAndrzej Pietrasiewicz 		 * We're called from user space, we can use _irq
33800a2430fSAndrzej Pietrasiewicz 		 * rather then _irqsave
33900a2430fSAndrzej Pietrasiewicz 		 */
34000a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
34100a2430fSAndrzej Pietrasiewicz 		switch (ffs_setup_state_clear_cancelled(ffs)) {
34200a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
34300a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
34400a2430fSAndrzej Pietrasiewicz 			goto done_spin;
34500a2430fSAndrzej Pietrasiewicz 
34600a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
34700a2430fSAndrzej Pietrasiewicz 			ret = -ESRCH;
34800a2430fSAndrzej Pietrasiewicz 			goto done_spin;
34900a2430fSAndrzej Pietrasiewicz 
35000a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
35100a2430fSAndrzej Pietrasiewicz 			break;
35200a2430fSAndrzej Pietrasiewicz 		}
35300a2430fSAndrzej Pietrasiewicz 
35400a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING */
35500a2430fSAndrzej Pietrasiewicz 		if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
35600a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
35700a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
35800a2430fSAndrzej Pietrasiewicz 			break;
35900a2430fSAndrzej Pietrasiewicz 		}
36000a2430fSAndrzej Pietrasiewicz 
36100a2430fSAndrzej Pietrasiewicz 		/* FFS_SETUP_PENDING and not stall */
36200a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
36300a2430fSAndrzej Pietrasiewicz 
36400a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
36500a2430fSAndrzej Pietrasiewicz 
36600a2430fSAndrzej Pietrasiewicz 		data = ffs_prepare_buffer(buf, len);
36700a2430fSAndrzej Pietrasiewicz 		if (IS_ERR(data)) {
36800a2430fSAndrzej Pietrasiewicz 			ret = PTR_ERR(data);
36900a2430fSAndrzej Pietrasiewicz 			break;
37000a2430fSAndrzej Pietrasiewicz 		}
37100a2430fSAndrzej Pietrasiewicz 
37200a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
37300a2430fSAndrzej Pietrasiewicz 
37400a2430fSAndrzej Pietrasiewicz 		/*
37500a2430fSAndrzej Pietrasiewicz 		 * We are guaranteed to be still in FFS_ACTIVE state
37600a2430fSAndrzej Pietrasiewicz 		 * but the state of setup could have changed from
37700a2430fSAndrzej Pietrasiewicz 		 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
37800a2430fSAndrzej Pietrasiewicz 		 * to check for that.  If that happened we copied data
37900a2430fSAndrzej Pietrasiewicz 		 * from user space in vain but it's unlikely.
38000a2430fSAndrzej Pietrasiewicz 		 *
38100a2430fSAndrzej Pietrasiewicz 		 * For sure we are not in FFS_NO_SETUP since this is
38200a2430fSAndrzej Pietrasiewicz 		 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
38300a2430fSAndrzej Pietrasiewicz 		 * transition can be performed and it's protected by
38400a2430fSAndrzej Pietrasiewicz 		 * mutex.
38500a2430fSAndrzej Pietrasiewicz 		 */
38600a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
38700a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
38800a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
38900a2430fSAndrzej Pietrasiewicz done_spin:
39000a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
39100a2430fSAndrzej Pietrasiewicz 		} else {
39200a2430fSAndrzej Pietrasiewicz 			/* unlocks spinlock */
39300a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_queue_wait(ffs, data, len);
39400a2430fSAndrzej Pietrasiewicz 		}
39500a2430fSAndrzej Pietrasiewicz 		kfree(data);
39600a2430fSAndrzej Pietrasiewicz 		break;
39700a2430fSAndrzej Pietrasiewicz 
39800a2430fSAndrzej Pietrasiewicz 	default:
39900a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
40000a2430fSAndrzej Pietrasiewicz 		break;
40100a2430fSAndrzej Pietrasiewicz 	}
40200a2430fSAndrzej Pietrasiewicz 
40300a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
40400a2430fSAndrzej Pietrasiewicz 	return ret;
40500a2430fSAndrzej Pietrasiewicz }
40600a2430fSAndrzej Pietrasiewicz 
40767913bbdSMichal Nazarewicz /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
40800a2430fSAndrzej Pietrasiewicz static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
40900a2430fSAndrzej Pietrasiewicz 				     size_t n)
41000a2430fSAndrzej Pietrasiewicz {
41100a2430fSAndrzej Pietrasiewicz 	/*
41267913bbdSMichal Nazarewicz 	 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
41367913bbdSMichal Nazarewicz 	 * size of ffs->ev.types array (which is four) so that's how much space
41467913bbdSMichal Nazarewicz 	 * we reserve.
41500a2430fSAndrzej Pietrasiewicz 	 */
41667913bbdSMichal Nazarewicz 	struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
41767913bbdSMichal Nazarewicz 	const size_t size = n * sizeof *events;
41800a2430fSAndrzej Pietrasiewicz 	unsigned i = 0;
41900a2430fSAndrzej Pietrasiewicz 
42067913bbdSMichal Nazarewicz 	memset(events, 0, size);
42100a2430fSAndrzej Pietrasiewicz 
42200a2430fSAndrzej Pietrasiewicz 	do {
42300a2430fSAndrzej Pietrasiewicz 		events[i].type = ffs->ev.types[i];
42400a2430fSAndrzej Pietrasiewicz 		if (events[i].type == FUNCTIONFS_SETUP) {
42500a2430fSAndrzej Pietrasiewicz 			events[i].u.setup = ffs->ev.setup;
42600a2430fSAndrzej Pietrasiewicz 			ffs->setup_state = FFS_SETUP_PENDING;
42700a2430fSAndrzej Pietrasiewicz 		}
42800a2430fSAndrzej Pietrasiewicz 	} while (++i < n);
42900a2430fSAndrzej Pietrasiewicz 
43000a2430fSAndrzej Pietrasiewicz 	ffs->ev.count -= n;
43167913bbdSMichal Nazarewicz 	if (ffs->ev.count)
43200a2430fSAndrzej Pietrasiewicz 		memmove(ffs->ev.types, ffs->ev.types + n,
43300a2430fSAndrzej Pietrasiewicz 			ffs->ev.count * sizeof *ffs->ev.types);
43400a2430fSAndrzej Pietrasiewicz 
43500a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
43600a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
43700a2430fSAndrzej Pietrasiewicz 
4387fe9a937SDaniel Walter 	return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
43900a2430fSAndrzej Pietrasiewicz }
44000a2430fSAndrzej Pietrasiewicz 
44100a2430fSAndrzej Pietrasiewicz static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
44200a2430fSAndrzej Pietrasiewicz 			    size_t len, loff_t *ptr)
44300a2430fSAndrzej Pietrasiewicz {
44400a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
44500a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
44600a2430fSAndrzej Pietrasiewicz 	size_t n;
44700a2430fSAndrzej Pietrasiewicz 	int ret;
44800a2430fSAndrzej Pietrasiewicz 
44900a2430fSAndrzej Pietrasiewicz 	ENTER();
45000a2430fSAndrzej Pietrasiewicz 
45100a2430fSAndrzej Pietrasiewicz 	/* Fast check if setup was canceled */
45200a2430fSAndrzej Pietrasiewicz 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
45300a2430fSAndrzej Pietrasiewicz 		return -EIDRM;
45400a2430fSAndrzej Pietrasiewicz 
45500a2430fSAndrzej Pietrasiewicz 	/* Acquire mutex */
45600a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
45700a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
45800a2430fSAndrzej Pietrasiewicz 		return ret;
45900a2430fSAndrzej Pietrasiewicz 
46000a2430fSAndrzej Pietrasiewicz 	/* Check state */
46100a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE) {
46200a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
46300a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
46400a2430fSAndrzej Pietrasiewicz 	}
46500a2430fSAndrzej Pietrasiewicz 
46600a2430fSAndrzej Pietrasiewicz 	/*
46700a2430fSAndrzej Pietrasiewicz 	 * We're called from user space, we can use _irq rather then
46800a2430fSAndrzej Pietrasiewicz 	 * _irqsave
46900a2430fSAndrzej Pietrasiewicz 	 */
47000a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&ffs->ev.waitq.lock);
47100a2430fSAndrzej Pietrasiewicz 
47200a2430fSAndrzej Pietrasiewicz 	switch (ffs_setup_state_clear_cancelled(ffs)) {
47300a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_CANCELLED:
47400a2430fSAndrzej Pietrasiewicz 		ret = -EIDRM;
47500a2430fSAndrzej Pietrasiewicz 		break;
47600a2430fSAndrzej Pietrasiewicz 
47700a2430fSAndrzej Pietrasiewicz 	case FFS_NO_SETUP:
47800a2430fSAndrzej Pietrasiewicz 		n = len / sizeof(struct usb_functionfs_event);
47900a2430fSAndrzej Pietrasiewicz 		if (unlikely(!n)) {
48000a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
48100a2430fSAndrzej Pietrasiewicz 			break;
48200a2430fSAndrzej Pietrasiewicz 		}
48300a2430fSAndrzej Pietrasiewicz 
48400a2430fSAndrzej Pietrasiewicz 		if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
48500a2430fSAndrzej Pietrasiewicz 			ret = -EAGAIN;
48600a2430fSAndrzej Pietrasiewicz 			break;
48700a2430fSAndrzej Pietrasiewicz 		}
48800a2430fSAndrzej Pietrasiewicz 
48900a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
49000a2430fSAndrzej Pietrasiewicz 							ffs->ev.count)) {
49100a2430fSAndrzej Pietrasiewicz 			ret = -EINTR;
49200a2430fSAndrzej Pietrasiewicz 			break;
49300a2430fSAndrzej Pietrasiewicz 		}
49400a2430fSAndrzej Pietrasiewicz 
49500a2430fSAndrzej Pietrasiewicz 		return __ffs_ep0_read_events(ffs, buf,
49600a2430fSAndrzej Pietrasiewicz 					     min(n, (size_t)ffs->ev.count));
49700a2430fSAndrzej Pietrasiewicz 
49800a2430fSAndrzej Pietrasiewicz 	case FFS_SETUP_PENDING:
49900a2430fSAndrzej Pietrasiewicz 		if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
50000a2430fSAndrzej Pietrasiewicz 			spin_unlock_irq(&ffs->ev.waitq.lock);
50100a2430fSAndrzej Pietrasiewicz 			ret = __ffs_ep0_stall(ffs);
50200a2430fSAndrzej Pietrasiewicz 			goto done_mutex;
50300a2430fSAndrzej Pietrasiewicz 		}
50400a2430fSAndrzej Pietrasiewicz 
50500a2430fSAndrzej Pietrasiewicz 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
50600a2430fSAndrzej Pietrasiewicz 
50700a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&ffs->ev.waitq.lock);
50800a2430fSAndrzej Pietrasiewicz 
50900a2430fSAndrzej Pietrasiewicz 		if (likely(len)) {
51000a2430fSAndrzej Pietrasiewicz 			data = kmalloc(len, GFP_KERNEL);
51100a2430fSAndrzej Pietrasiewicz 			if (unlikely(!data)) {
51200a2430fSAndrzej Pietrasiewicz 				ret = -ENOMEM;
51300a2430fSAndrzej Pietrasiewicz 				goto done_mutex;
51400a2430fSAndrzej Pietrasiewicz 			}
51500a2430fSAndrzej Pietrasiewicz 		}
51600a2430fSAndrzej Pietrasiewicz 
51700a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&ffs->ev.waitq.lock);
51800a2430fSAndrzej Pietrasiewicz 
51900a2430fSAndrzej Pietrasiewicz 		/* See ffs_ep0_write() */
52000a2430fSAndrzej Pietrasiewicz 		if (ffs_setup_state_clear_cancelled(ffs) ==
52100a2430fSAndrzej Pietrasiewicz 		    FFS_SETUP_CANCELLED) {
52200a2430fSAndrzej Pietrasiewicz 			ret = -EIDRM;
52300a2430fSAndrzej Pietrasiewicz 			break;
52400a2430fSAndrzej Pietrasiewicz 		}
52500a2430fSAndrzej Pietrasiewicz 
52600a2430fSAndrzej Pietrasiewicz 		/* unlocks spinlock */
52700a2430fSAndrzej Pietrasiewicz 		ret = __ffs_ep0_queue_wait(ffs, data, len);
5287fe9a937SDaniel Walter 		if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
52900a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
53000a2430fSAndrzej Pietrasiewicz 		goto done_mutex;
53100a2430fSAndrzej Pietrasiewicz 
53200a2430fSAndrzej Pietrasiewicz 	default:
53300a2430fSAndrzej Pietrasiewicz 		ret = -EBADFD;
53400a2430fSAndrzej Pietrasiewicz 		break;
53500a2430fSAndrzej Pietrasiewicz 	}
53600a2430fSAndrzej Pietrasiewicz 
53700a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&ffs->ev.waitq.lock);
53800a2430fSAndrzej Pietrasiewicz done_mutex:
53900a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
54000a2430fSAndrzej Pietrasiewicz 	kfree(data);
54100a2430fSAndrzej Pietrasiewicz 	return ret;
54200a2430fSAndrzej Pietrasiewicz }
54300a2430fSAndrzej Pietrasiewicz 
54400a2430fSAndrzej Pietrasiewicz static int ffs_ep0_open(struct inode *inode, struct file *file)
54500a2430fSAndrzej Pietrasiewicz {
54600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = inode->i_private;
54700a2430fSAndrzej Pietrasiewicz 
54800a2430fSAndrzej Pietrasiewicz 	ENTER();
54900a2430fSAndrzej Pietrasiewicz 
55000a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs->state == FFS_CLOSING))
55100a2430fSAndrzej Pietrasiewicz 		return -EBUSY;
55200a2430fSAndrzej Pietrasiewicz 
55300a2430fSAndrzej Pietrasiewicz 	file->private_data = ffs;
55400a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(ffs);
55500a2430fSAndrzej Pietrasiewicz 
55600a2430fSAndrzej Pietrasiewicz 	return 0;
55700a2430fSAndrzej Pietrasiewicz }
55800a2430fSAndrzej Pietrasiewicz 
55900a2430fSAndrzej Pietrasiewicz static int ffs_ep0_release(struct inode *inode, struct file *file)
56000a2430fSAndrzej Pietrasiewicz {
56100a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
56200a2430fSAndrzej Pietrasiewicz 
56300a2430fSAndrzej Pietrasiewicz 	ENTER();
56400a2430fSAndrzej Pietrasiewicz 
56500a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(ffs);
56600a2430fSAndrzej Pietrasiewicz 
56700a2430fSAndrzej Pietrasiewicz 	return 0;
56800a2430fSAndrzej Pietrasiewicz }
56900a2430fSAndrzej Pietrasiewicz 
57000a2430fSAndrzej Pietrasiewicz static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
57100a2430fSAndrzej Pietrasiewicz {
57200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
57300a2430fSAndrzej Pietrasiewicz 	struct usb_gadget *gadget = ffs->gadget;
57400a2430fSAndrzej Pietrasiewicz 	long ret;
57500a2430fSAndrzej Pietrasiewicz 
57600a2430fSAndrzej Pietrasiewicz 	ENTER();
57700a2430fSAndrzej Pietrasiewicz 
57800a2430fSAndrzej Pietrasiewicz 	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
57900a2430fSAndrzej Pietrasiewicz 		struct ffs_function *func = ffs->func;
58000a2430fSAndrzej Pietrasiewicz 		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
58100a2430fSAndrzej Pietrasiewicz 	} else if (gadget && gadget->ops->ioctl) {
58200a2430fSAndrzej Pietrasiewicz 		ret = gadget->ops->ioctl(gadget, code, value);
58300a2430fSAndrzej Pietrasiewicz 	} else {
58400a2430fSAndrzej Pietrasiewicz 		ret = -ENOTTY;
58500a2430fSAndrzej Pietrasiewicz 	}
58600a2430fSAndrzej Pietrasiewicz 
58700a2430fSAndrzej Pietrasiewicz 	return ret;
58800a2430fSAndrzej Pietrasiewicz }
58900a2430fSAndrzej Pietrasiewicz 
59000a2430fSAndrzej Pietrasiewicz static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
59100a2430fSAndrzej Pietrasiewicz {
59200a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = file->private_data;
59300a2430fSAndrzej Pietrasiewicz 	unsigned int mask = POLLWRNORM;
59400a2430fSAndrzej Pietrasiewicz 	int ret;
59500a2430fSAndrzej Pietrasiewicz 
59600a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &ffs->ev.waitq, wait);
59700a2430fSAndrzej Pietrasiewicz 
59800a2430fSAndrzej Pietrasiewicz 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
59900a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
60000a2430fSAndrzej Pietrasiewicz 		return mask;
60100a2430fSAndrzej Pietrasiewicz 
60200a2430fSAndrzej Pietrasiewicz 	switch (ffs->state) {
60300a2430fSAndrzej Pietrasiewicz 	case FFS_READ_DESCRIPTORS:
60400a2430fSAndrzej Pietrasiewicz 	case FFS_READ_STRINGS:
60500a2430fSAndrzej Pietrasiewicz 		mask |= POLLOUT;
60600a2430fSAndrzej Pietrasiewicz 		break;
60700a2430fSAndrzej Pietrasiewicz 
60800a2430fSAndrzej Pietrasiewicz 	case FFS_ACTIVE:
60900a2430fSAndrzej Pietrasiewicz 		switch (ffs->setup_state) {
61000a2430fSAndrzej Pietrasiewicz 		case FFS_NO_SETUP:
61100a2430fSAndrzej Pietrasiewicz 			if (ffs->ev.count)
61200a2430fSAndrzej Pietrasiewicz 				mask |= POLLIN;
61300a2430fSAndrzej Pietrasiewicz 			break;
61400a2430fSAndrzej Pietrasiewicz 
61500a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_PENDING:
61600a2430fSAndrzej Pietrasiewicz 		case FFS_SETUP_CANCELLED:
61700a2430fSAndrzej Pietrasiewicz 			mask |= (POLLIN | POLLOUT);
61800a2430fSAndrzej Pietrasiewicz 			break;
61900a2430fSAndrzej Pietrasiewicz 		}
62000a2430fSAndrzej Pietrasiewicz 	case FFS_CLOSING:
62100a2430fSAndrzej Pietrasiewicz 		break;
62218d6b32fSRobert Baldyga 	case FFS_DEACTIVATED:
62318d6b32fSRobert Baldyga 		break;
62400a2430fSAndrzej Pietrasiewicz 	}
62500a2430fSAndrzej Pietrasiewicz 
62600a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&ffs->mutex);
62700a2430fSAndrzej Pietrasiewicz 
62800a2430fSAndrzej Pietrasiewicz 	return mask;
62900a2430fSAndrzej Pietrasiewicz }
63000a2430fSAndrzej Pietrasiewicz 
63100a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_ep0_operations = {
63200a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
63300a2430fSAndrzej Pietrasiewicz 
63400a2430fSAndrzej Pietrasiewicz 	.open =		ffs_ep0_open,
63500a2430fSAndrzej Pietrasiewicz 	.write =	ffs_ep0_write,
63600a2430fSAndrzej Pietrasiewicz 	.read =		ffs_ep0_read,
63700a2430fSAndrzej Pietrasiewicz 	.release =	ffs_ep0_release,
63800a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_ep0_ioctl,
63900a2430fSAndrzej Pietrasiewicz 	.poll =		ffs_ep0_poll,
64000a2430fSAndrzej Pietrasiewicz };
64100a2430fSAndrzej Pietrasiewicz 
64200a2430fSAndrzej Pietrasiewicz 
64300a2430fSAndrzej Pietrasiewicz /* "Normal" endpoints operations ********************************************/
64400a2430fSAndrzej Pietrasiewicz 
64500a2430fSAndrzej Pietrasiewicz static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
64600a2430fSAndrzej Pietrasiewicz {
64700a2430fSAndrzej Pietrasiewicz 	ENTER();
64800a2430fSAndrzej Pietrasiewicz 	if (likely(req->context)) {
64900a2430fSAndrzej Pietrasiewicz 		struct ffs_ep *ep = _ep->driver_data;
65000a2430fSAndrzej Pietrasiewicz 		ep->status = req->status ? req->status : req->actual;
65100a2430fSAndrzej Pietrasiewicz 		complete(req->context);
65200a2430fSAndrzej Pietrasiewicz 	}
65300a2430fSAndrzej Pietrasiewicz }
65400a2430fSAndrzej Pietrasiewicz 
655c662a31bSMichal Nazarewicz static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
656c662a31bSMichal Nazarewicz {
657c662a31bSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
658c662a31bSMichal Nazarewicz 	if (likely(ret == data_len))
659c662a31bSMichal Nazarewicz 		return ret;
660c662a31bSMichal Nazarewicz 
661c662a31bSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
662c662a31bSMichal Nazarewicz 		return -EFAULT;
663c662a31bSMichal Nazarewicz 
664c662a31bSMichal Nazarewicz 	/*
665c662a31bSMichal Nazarewicz 	 * Dear user space developer!
666c662a31bSMichal Nazarewicz 	 *
667c662a31bSMichal Nazarewicz 	 * TL;DR: To stop getting below error message in your kernel log, change
668c662a31bSMichal Nazarewicz 	 * user space code using functionfs to align read buffers to a max
669c662a31bSMichal Nazarewicz 	 * packet size.
670c662a31bSMichal Nazarewicz 	 *
671c662a31bSMichal Nazarewicz 	 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
672c662a31bSMichal Nazarewicz 	 * packet size.  When unaligned buffer is passed to functionfs, it
673c662a31bSMichal Nazarewicz 	 * internally uses a larger, aligned buffer so that such UDCs are happy.
674c662a31bSMichal Nazarewicz 	 *
675c662a31bSMichal Nazarewicz 	 * Unfortunately, this means that host may send more data than was
676c662a31bSMichal Nazarewicz 	 * requested in read(2) system call.  f_fs doesn’t know what to do with
677c662a31bSMichal Nazarewicz 	 * that excess data so it simply drops it.
678c662a31bSMichal Nazarewicz 	 *
679c662a31bSMichal Nazarewicz 	 * Was the buffer aligned in the first place, no such problem would
680c662a31bSMichal Nazarewicz 	 * happen.
681c662a31bSMichal Nazarewicz 	 *
6829353afbbSMichal Nazarewicz 	 * Data may be dropped only in AIO reads.  Synchronous reads are handled
6839353afbbSMichal Nazarewicz 	 * by splitting a request into multiple parts.  This splitting may still
6849353afbbSMichal Nazarewicz 	 * be a problem though so it’s likely best to align the buffer
6859353afbbSMichal Nazarewicz 	 * regardless of it being AIO or not..
6869353afbbSMichal Nazarewicz 	 *
687c662a31bSMichal Nazarewicz 	 * This only affects OUT endpoints, i.e. reading data with a read(2),
688c662a31bSMichal Nazarewicz 	 * aio_read(2) etc. system calls.  Writing data to an IN endpoint is not
689c662a31bSMichal Nazarewicz 	 * affected.
690c662a31bSMichal Nazarewicz 	 */
691c662a31bSMichal Nazarewicz 	pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
692c662a31bSMichal Nazarewicz 	       "Align read buffer size to max packet size to avoid the problem.\n",
693c662a31bSMichal Nazarewicz 	       data_len, ret);
694c662a31bSMichal Nazarewicz 
695c662a31bSMichal Nazarewicz 	return ret;
696c662a31bSMichal Nazarewicz }
697c662a31bSMichal Nazarewicz 
69800a2430fSAndrzej Pietrasiewicz static void ffs_user_copy_worker(struct work_struct *work)
69900a2430fSAndrzej Pietrasiewicz {
70000a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
70100a2430fSAndrzej Pietrasiewicz 						   work);
70200a2430fSAndrzej Pietrasiewicz 	int ret = io_data->req->status ? io_data->req->status :
70300a2430fSAndrzej Pietrasiewicz 					 io_data->req->actual;
70438740a5bSLars-Peter Clausen 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
70500a2430fSAndrzej Pietrasiewicz 
70600a2430fSAndrzej Pietrasiewicz 	if (io_data->read && ret > 0) {
70700a2430fSAndrzej Pietrasiewicz 		use_mm(io_data->mm);
708c662a31bSMichal Nazarewicz 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
70900a2430fSAndrzej Pietrasiewicz 		unuse_mm(io_data->mm);
71000a2430fSAndrzej Pietrasiewicz 	}
71100a2430fSAndrzej Pietrasiewicz 
71204b2fa9fSChristoph Hellwig 	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
71300a2430fSAndrzej Pietrasiewicz 
71438740a5bSLars-Peter Clausen 	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
7155e33f6fdSRobert Baldyga 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);
7165e33f6fdSRobert Baldyga 
71700a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(io_data->ep, io_data->req);
71800a2430fSAndrzej Pietrasiewicz 
71900a2430fSAndrzej Pietrasiewicz 	if (io_data->read)
720c993c39bSAl Viro 		kfree(io_data->to_free);
72100a2430fSAndrzej Pietrasiewicz 	kfree(io_data->buf);
72200a2430fSAndrzej Pietrasiewicz 	kfree(io_data);
72300a2430fSAndrzej Pietrasiewicz }
72400a2430fSAndrzej Pietrasiewicz 
72500a2430fSAndrzej Pietrasiewicz static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
72600a2430fSAndrzej Pietrasiewicz 					 struct usb_request *req)
72700a2430fSAndrzej Pietrasiewicz {
72800a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = req->context;
72900a2430fSAndrzej Pietrasiewicz 
73000a2430fSAndrzej Pietrasiewicz 	ENTER();
73100a2430fSAndrzej Pietrasiewicz 
73200a2430fSAndrzej Pietrasiewicz 	INIT_WORK(&io_data->work, ffs_user_copy_worker);
73300a2430fSAndrzej Pietrasiewicz 	schedule_work(&io_data->work);
73400a2430fSAndrzej Pietrasiewicz }
73500a2430fSAndrzej Pietrasiewicz 
7369353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
7379353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
7389353afbbSMichal Nazarewicz 					  struct iov_iter *iter)
7399353afbbSMichal Nazarewicz {
7409353afbbSMichal Nazarewicz 	struct ffs_buffer *buf = epfile->read_buffer;
7419353afbbSMichal Nazarewicz 	ssize_t ret;
7429353afbbSMichal Nazarewicz 	if (!buf)
7439353afbbSMichal Nazarewicz 		return 0;
7449353afbbSMichal Nazarewicz 
7459353afbbSMichal Nazarewicz 	ret = copy_to_iter(buf->data, buf->length, iter);
7469353afbbSMichal Nazarewicz 	if (buf->length == ret) {
7479353afbbSMichal Nazarewicz 		kfree(buf);
7489353afbbSMichal Nazarewicz 		epfile->read_buffer = NULL;
7499353afbbSMichal Nazarewicz 	} else if (unlikely(iov_iter_count(iter))) {
7509353afbbSMichal Nazarewicz 		ret = -EFAULT;
7519353afbbSMichal Nazarewicz 	} else {
7529353afbbSMichal Nazarewicz 		buf->length -= ret;
7539353afbbSMichal Nazarewicz 		buf->data += ret;
7549353afbbSMichal Nazarewicz 	}
7559353afbbSMichal Nazarewicz 	return ret;
7569353afbbSMichal Nazarewicz }
7579353afbbSMichal Nazarewicz 
7589353afbbSMichal Nazarewicz /* Assumes epfile->mutex is held. */
7599353afbbSMichal Nazarewicz static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
7609353afbbSMichal Nazarewicz 				      void *data, int data_len,
7619353afbbSMichal Nazarewicz 				      struct iov_iter *iter)
7629353afbbSMichal Nazarewicz {
7639353afbbSMichal Nazarewicz 	struct ffs_buffer *buf;
7649353afbbSMichal Nazarewicz 
7659353afbbSMichal Nazarewicz 	ssize_t ret = copy_to_iter(data, data_len, iter);
7669353afbbSMichal Nazarewicz 	if (likely(data_len == ret))
7679353afbbSMichal Nazarewicz 		return ret;
7689353afbbSMichal Nazarewicz 
7699353afbbSMichal Nazarewicz 	if (unlikely(iov_iter_count(iter)))
7709353afbbSMichal Nazarewicz 		return -EFAULT;
7719353afbbSMichal Nazarewicz 
7729353afbbSMichal Nazarewicz 	/* See ffs_copy_to_iter for more context. */
7739353afbbSMichal Nazarewicz 	pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
7749353afbbSMichal Nazarewicz 		data_len, ret);
7759353afbbSMichal Nazarewicz 
7769353afbbSMichal Nazarewicz 	data_len -= ret;
7779353afbbSMichal Nazarewicz 	buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
77844963d64SDan Carpenter 	if (!buf)
77944963d64SDan Carpenter 		return -ENOMEM;
7809353afbbSMichal Nazarewicz 	buf->length = data_len;
7819353afbbSMichal Nazarewicz 	buf->data = buf->storage;
7829353afbbSMichal Nazarewicz 	memcpy(buf->storage, data + ret, data_len);
7839353afbbSMichal Nazarewicz 	epfile->read_buffer = buf;
7849353afbbSMichal Nazarewicz 
7859353afbbSMichal Nazarewicz 	return ret;
7869353afbbSMichal Nazarewicz }
7879353afbbSMichal Nazarewicz 
78800a2430fSAndrzej Pietrasiewicz static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
78900a2430fSAndrzej Pietrasiewicz {
79000a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
791ae76e134SMichal Nazarewicz 	struct usb_request *req;
79200a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep;
79300a2430fSAndrzej Pietrasiewicz 	char *data = NULL;
794c0d31b3cSDavid Cohen 	ssize_t ret, data_len = -EINVAL;
79500a2430fSAndrzej Pietrasiewicz 	int halt;
79600a2430fSAndrzej Pietrasiewicz 
79700a2430fSAndrzej Pietrasiewicz 	/* Are we still active? */
798b3591f67SMichal Nazarewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
799b3591f67SMichal Nazarewicz 		return -ENODEV;
80000a2430fSAndrzej Pietrasiewicz 
80100a2430fSAndrzej Pietrasiewicz 	/* Wait for endpoint to be enabled */
80200a2430fSAndrzej Pietrasiewicz 	ep = epfile->ep;
80300a2430fSAndrzej Pietrasiewicz 	if (!ep) {
804b3591f67SMichal Nazarewicz 		if (file->f_flags & O_NONBLOCK)
805b3591f67SMichal Nazarewicz 			return -EAGAIN;
80600a2430fSAndrzej Pietrasiewicz 
80700a2430fSAndrzej Pietrasiewicz 		ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
808b3591f67SMichal Nazarewicz 		if (ret)
809b3591f67SMichal Nazarewicz 			return -EINTR;
81000a2430fSAndrzej Pietrasiewicz 	}
81100a2430fSAndrzej Pietrasiewicz 
81200a2430fSAndrzej Pietrasiewicz 	/* Do we halt? */
81300a2430fSAndrzej Pietrasiewicz 	halt = (!io_data->read == !epfile->in);
814b3591f67SMichal Nazarewicz 	if (halt && epfile->isoc)
815b3591f67SMichal Nazarewicz 		return -EINVAL;
81600a2430fSAndrzej Pietrasiewicz 
8179353afbbSMichal Nazarewicz 	/* We will be using request and read_buffer */
8189353afbbSMichal Nazarewicz 	ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
8199353afbbSMichal Nazarewicz 	if (unlikely(ret))
8209353afbbSMichal Nazarewicz 		goto error;
8219353afbbSMichal Nazarewicz 
82200a2430fSAndrzej Pietrasiewicz 	/* Allocate & copy */
82300a2430fSAndrzej Pietrasiewicz 	if (!halt) {
8249353afbbSMichal Nazarewicz 		struct usb_gadget *gadget;
8259353afbbSMichal Nazarewicz 
8269353afbbSMichal Nazarewicz 		/*
8279353afbbSMichal Nazarewicz 		 * Do we have buffered data from previous partial read?  Check
8289353afbbSMichal Nazarewicz 		 * that for synchronous case only because we do not have
8299353afbbSMichal Nazarewicz 		 * facility to ‘wake up’ a pending asynchronous read and push
8309353afbbSMichal Nazarewicz 		 * buffered data to it which we would need to make things behave
8319353afbbSMichal Nazarewicz 		 * consistently.
8329353afbbSMichal Nazarewicz 		 */
8339353afbbSMichal Nazarewicz 		if (!io_data->aio && io_data->read) {
8349353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
8359353afbbSMichal Nazarewicz 			if (ret)
8369353afbbSMichal Nazarewicz 				goto error_mutex;
8379353afbbSMichal Nazarewicz 		}
8389353afbbSMichal Nazarewicz 
83900a2430fSAndrzej Pietrasiewicz 		/*
84000a2430fSAndrzej Pietrasiewicz 		 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
841ae76e134SMichal Nazarewicz 		 * before the waiting completes, so do not assign to 'gadget'
842ae76e134SMichal Nazarewicz 		 * earlier
84300a2430fSAndrzej Pietrasiewicz 		 */
8449353afbbSMichal Nazarewicz 		gadget = epfile->ffs->gadget;
84500a2430fSAndrzej Pietrasiewicz 
84600a2430fSAndrzej Pietrasiewicz 		spin_lock_irq(&epfile->ffs->eps_lock);
84700a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
84800a2430fSAndrzej Pietrasiewicz 		if (epfile->ep != ep) {
8499353afbbSMichal Nazarewicz 			ret = -ESHUTDOWN;
8509353afbbSMichal Nazarewicz 			goto error_lock;
85100a2430fSAndrzej Pietrasiewicz 		}
852c993c39bSAl Viro 		data_len = iov_iter_count(&io_data->data);
85300a2430fSAndrzej Pietrasiewicz 		/*
85400a2430fSAndrzej Pietrasiewicz 		 * Controller may require buffer size to be aligned to
85500a2430fSAndrzej Pietrasiewicz 		 * maxpacketsize of an out endpoint.
85600a2430fSAndrzej Pietrasiewicz 		 */
857c993c39bSAl Viro 		if (io_data->read)
858c993c39bSAl Viro 			data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
85900a2430fSAndrzej Pietrasiewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
86000a2430fSAndrzej Pietrasiewicz 
86100a2430fSAndrzej Pietrasiewicz 		data = kmalloc(data_len, GFP_KERNEL);
8629353afbbSMichal Nazarewicz 		if (unlikely(!data)) {
8639353afbbSMichal Nazarewicz 			ret = -ENOMEM;
8649353afbbSMichal Nazarewicz 			goto error_mutex;
8659353afbbSMichal Nazarewicz 		}
8669353afbbSMichal Nazarewicz 		if (!io_data->read &&
8679353afbbSMichal Nazarewicz 		    copy_from_iter(data, data_len, &io_data->data) != data_len) {
86800a2430fSAndrzej Pietrasiewicz 			ret = -EFAULT;
8699353afbbSMichal Nazarewicz 			goto error_mutex;
87000a2430fSAndrzej Pietrasiewicz 		}
87100a2430fSAndrzej Pietrasiewicz 	}
87200a2430fSAndrzej Pietrasiewicz 
87300a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
87400a2430fSAndrzej Pietrasiewicz 
87500a2430fSAndrzej Pietrasiewicz 	if (epfile->ep != ep) {
87600a2430fSAndrzej Pietrasiewicz 		/* In the meantime, endpoint got disabled or changed. */
87700a2430fSAndrzej Pietrasiewicz 		ret = -ESHUTDOWN;
87800a2430fSAndrzej Pietrasiewicz 	} else if (halt) {
87900a2430fSAndrzej Pietrasiewicz 		/* Halt */
88000a2430fSAndrzej Pietrasiewicz 		if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
88100a2430fSAndrzej Pietrasiewicz 			usb_ep_set_halt(ep->ep);
88200a2430fSAndrzej Pietrasiewicz 		ret = -EBADMSG;
883ae76e134SMichal Nazarewicz 	} else if (unlikely(data_len == -EINVAL)) {
884c0d31b3cSDavid Cohen 		/*
885c0d31b3cSDavid Cohen 		 * Sanity Check: even though data_len can't be used
886c0d31b3cSDavid Cohen 		 * uninitialized at the time I write this comment, some
887c0d31b3cSDavid Cohen 		 * compilers complain about this situation.
888c0d31b3cSDavid Cohen 		 * In order to keep the code clean from warnings, data_len is
889c0d31b3cSDavid Cohen 		 * being initialized to -EINVAL during its declaration, which
890c0d31b3cSDavid Cohen 		 * means we can't rely on compiler anymore to warn no future
891c0d31b3cSDavid Cohen 		 * changes won't result in data_len being used uninitialized.
892c0d31b3cSDavid Cohen 		 * For such reason, we're adding this redundant sanity check
893c0d31b3cSDavid Cohen 		 * here.
894c0d31b3cSDavid Cohen 		 */
895c0d31b3cSDavid Cohen 		WARN(1, "%s: data_len == -EINVAL\n", __func__);
896c0d31b3cSDavid Cohen 		ret = -EINVAL;
897ae76e134SMichal Nazarewicz 	} else if (!io_data->aio) {
898ae76e134SMichal Nazarewicz 		DECLARE_COMPLETION_ONSTACK(done);
899ef150884SDu, Changbin 		bool interrupted = false;
900ae76e134SMichal Nazarewicz 
901ae76e134SMichal Nazarewicz 		req = ep->req;
902ae76e134SMichal Nazarewicz 		req->buf      = data;
903ae76e134SMichal Nazarewicz 		req->length   = data_len;
904ae76e134SMichal Nazarewicz 
905ae76e134SMichal Nazarewicz 		req->context  = &done;
906ae76e134SMichal Nazarewicz 		req->complete = ffs_epfile_io_complete;
907ae76e134SMichal Nazarewicz 
908ae76e134SMichal Nazarewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
909ae76e134SMichal Nazarewicz 		if (unlikely(ret < 0))
910c0d31b3cSDavid Cohen 			goto error_lock;
911ae76e134SMichal Nazarewicz 
912ae76e134SMichal Nazarewicz 		spin_unlock_irq(&epfile->ffs->eps_lock);
913ae76e134SMichal Nazarewicz 
914ae76e134SMichal Nazarewicz 		if (unlikely(wait_for_completion_interruptible(&done))) {
915ef150884SDu, Changbin 			/*
916ef150884SDu, Changbin 			 * To avoid race condition with ffs_epfile_io_complete,
917ef150884SDu, Changbin 			 * dequeue the request first then check
918ef150884SDu, Changbin 			 * status. usb_ep_dequeue API should guarantee no race
919ef150884SDu, Changbin 			 * condition with req->complete callback.
920ef150884SDu, Changbin 			 */
921ae76e134SMichal Nazarewicz 			usb_ep_dequeue(ep->ep, req);
922ef150884SDu, Changbin 			interrupted = ep->status < 0;
923c0d31b3cSDavid Cohen 		}
924c0d31b3cSDavid Cohen 
925c662a31bSMichal Nazarewicz 		if (interrupted)
926c662a31bSMichal Nazarewicz 			ret = -EINTR;
927c662a31bSMichal Nazarewicz 		else if (io_data->read && ep->status > 0)
9289353afbbSMichal Nazarewicz 			ret = __ffs_epfile_read_data(epfile, data, ep->status,
929c662a31bSMichal Nazarewicz 						     &io_data->data);
930c662a31bSMichal Nazarewicz 		else
931c662a31bSMichal Nazarewicz 			ret = ep->status;
932ae76e134SMichal Nazarewicz 		goto error_mutex;
933ae76e134SMichal Nazarewicz 	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
9343163c79eSMichal Nazarewicz 		ret = -ENOMEM;
935ae76e134SMichal Nazarewicz 	} else {
93600a2430fSAndrzej Pietrasiewicz 		req->buf      = data;
937c0d31b3cSDavid Cohen 		req->length   = data_len;
93800a2430fSAndrzej Pietrasiewicz 
93900a2430fSAndrzej Pietrasiewicz 		io_data->buf = data;
94000a2430fSAndrzej Pietrasiewicz 		io_data->ep = ep->ep;
94100a2430fSAndrzej Pietrasiewicz 		io_data->req = req;
9425e33f6fdSRobert Baldyga 		io_data->ffs = epfile->ffs;
94300a2430fSAndrzej Pietrasiewicz 
94400a2430fSAndrzej Pietrasiewicz 		req->context  = io_data;
94500a2430fSAndrzej Pietrasiewicz 		req->complete = ffs_epfile_async_io_complete;
94600a2430fSAndrzej Pietrasiewicz 
94700a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
94800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret)) {
94900a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, req);
95000a2430fSAndrzej Pietrasiewicz 			goto error_lock;
95100a2430fSAndrzej Pietrasiewicz 		}
952ae76e134SMichal Nazarewicz 
95300a2430fSAndrzej Pietrasiewicz 		ret = -EIOCBQUEUED;
95400a2430fSAndrzej Pietrasiewicz 		/*
955ae76e134SMichal Nazarewicz 		 * Do not kfree the buffer in this function.  It will be freed
956ae76e134SMichal Nazarewicz 		 * by ffs_user_copy_worker.
95700a2430fSAndrzej Pietrasiewicz 		 */
958ae76e134SMichal Nazarewicz 		data = NULL;
95900a2430fSAndrzej Pietrasiewicz 	}
96000a2430fSAndrzej Pietrasiewicz 
96100a2430fSAndrzej Pietrasiewicz error_lock:
96200a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
963ae76e134SMichal Nazarewicz error_mutex:
96400a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&epfile->mutex);
96500a2430fSAndrzej Pietrasiewicz error:
96600a2430fSAndrzej Pietrasiewicz 	kfree(data);
96700a2430fSAndrzej Pietrasiewicz 	return ret;
96800a2430fSAndrzej Pietrasiewicz }
96900a2430fSAndrzej Pietrasiewicz 
97000a2430fSAndrzej Pietrasiewicz static int
97100a2430fSAndrzej Pietrasiewicz ffs_epfile_open(struct inode *inode, struct file *file)
97200a2430fSAndrzej Pietrasiewicz {
97300a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
97400a2430fSAndrzej Pietrasiewicz 
97500a2430fSAndrzej Pietrasiewicz 	ENTER();
97600a2430fSAndrzej Pietrasiewicz 
97700a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
97800a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
97900a2430fSAndrzej Pietrasiewicz 
98000a2430fSAndrzej Pietrasiewicz 	file->private_data = epfile;
98100a2430fSAndrzej Pietrasiewicz 	ffs_data_opened(epfile->ffs);
98200a2430fSAndrzej Pietrasiewicz 
98300a2430fSAndrzej Pietrasiewicz 	return 0;
98400a2430fSAndrzej Pietrasiewicz }
98500a2430fSAndrzej Pietrasiewicz 
98600a2430fSAndrzej Pietrasiewicz static int ffs_aio_cancel(struct kiocb *kiocb)
98700a2430fSAndrzej Pietrasiewicz {
98800a2430fSAndrzej Pietrasiewicz 	struct ffs_io_data *io_data = kiocb->private;
98900a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
99000a2430fSAndrzej Pietrasiewicz 	int value;
99100a2430fSAndrzej Pietrasiewicz 
99200a2430fSAndrzej Pietrasiewicz 	ENTER();
99300a2430fSAndrzej Pietrasiewicz 
99400a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
99500a2430fSAndrzej Pietrasiewicz 
99600a2430fSAndrzej Pietrasiewicz 	if (likely(io_data && io_data->ep && io_data->req))
99700a2430fSAndrzej Pietrasiewicz 		value = usb_ep_dequeue(io_data->ep, io_data->req);
99800a2430fSAndrzej Pietrasiewicz 	else
99900a2430fSAndrzej Pietrasiewicz 		value = -EINVAL;
100000a2430fSAndrzej Pietrasiewicz 
100100a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
100200a2430fSAndrzej Pietrasiewicz 
100300a2430fSAndrzej Pietrasiewicz 	return value;
100400a2430fSAndrzej Pietrasiewicz }
100500a2430fSAndrzej Pietrasiewicz 
100670e60d91SAl Viro static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
100700a2430fSAndrzej Pietrasiewicz {
100870e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1009de2080d4SAl Viro 	ssize_t res;
101000a2430fSAndrzej Pietrasiewicz 
101100a2430fSAndrzej Pietrasiewicz 	ENTER();
101200a2430fSAndrzej Pietrasiewicz 
101370e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
101470e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
101570e60d91SAl Viro 		if (unlikely(!p))
101600a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
101770e60d91SAl Viro 		p->aio = true;
101870e60d91SAl Viro 	} else {
101970e60d91SAl Viro 		p->aio = false;
102070e60d91SAl Viro 	}
102100a2430fSAndrzej Pietrasiewicz 
102270e60d91SAl Viro 	p->read = false;
102370e60d91SAl Viro 	p->kiocb = kiocb;
102470e60d91SAl Viro 	p->data = *from;
102570e60d91SAl Viro 	p->mm = current->mm;
102600a2430fSAndrzej Pietrasiewicz 
102770e60d91SAl Viro 	kiocb->private = p;
102800a2430fSAndrzej Pietrasiewicz 
10294088acf1SRui Miguel Silva 	if (p->aio)
103000a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
103100a2430fSAndrzej Pietrasiewicz 
103270e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
103370e60d91SAl Viro 	if (res == -EIOCBQUEUED)
103470e60d91SAl Viro 		return res;
103570e60d91SAl Viro 	if (p->aio)
103670e60d91SAl Viro 		kfree(p);
103770e60d91SAl Viro 	else
103870e60d91SAl Viro 		*from = p->data;
1039de2080d4SAl Viro 	return res;
104000a2430fSAndrzej Pietrasiewicz }
104100a2430fSAndrzej Pietrasiewicz 
104270e60d91SAl Viro static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
104300a2430fSAndrzej Pietrasiewicz {
104470e60d91SAl Viro 	struct ffs_io_data io_data, *p = &io_data;
1045de2080d4SAl Viro 	ssize_t res;
104600a2430fSAndrzej Pietrasiewicz 
104700a2430fSAndrzej Pietrasiewicz 	ENTER();
104800a2430fSAndrzej Pietrasiewicz 
104970e60d91SAl Viro 	if (!is_sync_kiocb(kiocb)) {
105070e60d91SAl Viro 		p = kmalloc(sizeof(io_data), GFP_KERNEL);
105170e60d91SAl Viro 		if (unlikely(!p))
105200a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
105370e60d91SAl Viro 		p->aio = true;
105470e60d91SAl Viro 	} else {
105570e60d91SAl Viro 		p->aio = false;
105600a2430fSAndrzej Pietrasiewicz 	}
105700a2430fSAndrzej Pietrasiewicz 
105870e60d91SAl Viro 	p->read = true;
105970e60d91SAl Viro 	p->kiocb = kiocb;
106070e60d91SAl Viro 	if (p->aio) {
106170e60d91SAl Viro 		p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
106270e60d91SAl Viro 		if (!p->to_free) {
106370e60d91SAl Viro 			kfree(p);
106470e60d91SAl Viro 			return -ENOMEM;
106570e60d91SAl Viro 		}
106670e60d91SAl Viro 	} else {
106770e60d91SAl Viro 		p->data = *to;
106870e60d91SAl Viro 		p->to_free = NULL;
106970e60d91SAl Viro 	}
107070e60d91SAl Viro 	p->mm = current->mm;
107100a2430fSAndrzej Pietrasiewicz 
107270e60d91SAl Viro 	kiocb->private = p;
107300a2430fSAndrzej Pietrasiewicz 
10744088acf1SRui Miguel Silva 	if (p->aio)
107500a2430fSAndrzej Pietrasiewicz 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
107600a2430fSAndrzej Pietrasiewicz 
107770e60d91SAl Viro 	res = ffs_epfile_io(kiocb->ki_filp, p);
107870e60d91SAl Viro 	if (res == -EIOCBQUEUED)
107970e60d91SAl Viro 		return res;
108070e60d91SAl Viro 
108170e60d91SAl Viro 	if (p->aio) {
108270e60d91SAl Viro 		kfree(p->to_free);
108370e60d91SAl Viro 		kfree(p);
108470e60d91SAl Viro 	} else {
108570e60d91SAl Viro 		*to = p->data;
1086de2080d4SAl Viro 	}
1087de2080d4SAl Viro 	return res;
108800a2430fSAndrzej Pietrasiewicz }
108900a2430fSAndrzej Pietrasiewicz 
109000a2430fSAndrzej Pietrasiewicz static int
109100a2430fSAndrzej Pietrasiewicz ffs_epfile_release(struct inode *inode, struct file *file)
109200a2430fSAndrzej Pietrasiewicz {
109300a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = inode->i_private;
109400a2430fSAndrzej Pietrasiewicz 
109500a2430fSAndrzej Pietrasiewicz 	ENTER();
109600a2430fSAndrzej Pietrasiewicz 
10979353afbbSMichal Nazarewicz 	kfree(epfile->read_buffer);
10989353afbbSMichal Nazarewicz 	epfile->read_buffer = NULL;
109900a2430fSAndrzej Pietrasiewicz 	ffs_data_closed(epfile->ffs);
110000a2430fSAndrzej Pietrasiewicz 
110100a2430fSAndrzej Pietrasiewicz 	return 0;
110200a2430fSAndrzej Pietrasiewicz }
110300a2430fSAndrzej Pietrasiewicz 
110400a2430fSAndrzej Pietrasiewicz static long ffs_epfile_ioctl(struct file *file, unsigned code,
110500a2430fSAndrzej Pietrasiewicz 			     unsigned long value)
110600a2430fSAndrzej Pietrasiewicz {
110700a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = file->private_data;
110800a2430fSAndrzej Pietrasiewicz 	int ret;
110900a2430fSAndrzej Pietrasiewicz 
111000a2430fSAndrzej Pietrasiewicz 	ENTER();
111100a2430fSAndrzej Pietrasiewicz 
111200a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
111300a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
111400a2430fSAndrzej Pietrasiewicz 
111500a2430fSAndrzej Pietrasiewicz 	spin_lock_irq(&epfile->ffs->eps_lock);
111600a2430fSAndrzej Pietrasiewicz 	if (likely(epfile->ep)) {
111700a2430fSAndrzej Pietrasiewicz 		switch (code) {
111800a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_FIFO_STATUS:
111900a2430fSAndrzej Pietrasiewicz 			ret = usb_ep_fifo_status(epfile->ep->ep);
112000a2430fSAndrzej Pietrasiewicz 			break;
112100a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_FIFO_FLUSH:
112200a2430fSAndrzej Pietrasiewicz 			usb_ep_fifo_flush(epfile->ep->ep);
112300a2430fSAndrzej Pietrasiewicz 			ret = 0;
112400a2430fSAndrzej Pietrasiewicz 			break;
112500a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_CLEAR_HALT:
112600a2430fSAndrzej Pietrasiewicz 			ret = usb_ep_clear_halt(epfile->ep->ep);
112700a2430fSAndrzej Pietrasiewicz 			break;
112800a2430fSAndrzej Pietrasiewicz 		case FUNCTIONFS_ENDPOINT_REVMAP:
112900a2430fSAndrzej Pietrasiewicz 			ret = epfile->ep->num;
113000a2430fSAndrzej Pietrasiewicz 			break;
1131c559a353SRobert Baldyga 		case FUNCTIONFS_ENDPOINT_DESC:
1132c559a353SRobert Baldyga 		{
1133c559a353SRobert Baldyga 			int desc_idx;
1134c559a353SRobert Baldyga 			struct usb_endpoint_descriptor *desc;
1135c559a353SRobert Baldyga 
1136c559a353SRobert Baldyga 			switch (epfile->ffs->gadget->speed) {
1137c559a353SRobert Baldyga 			case USB_SPEED_SUPER:
1138c559a353SRobert Baldyga 				desc_idx = 2;
1139c559a353SRobert Baldyga 				break;
1140c559a353SRobert Baldyga 			case USB_SPEED_HIGH:
1141c559a353SRobert Baldyga 				desc_idx = 1;
1142c559a353SRobert Baldyga 				break;
1143c559a353SRobert Baldyga 			default:
1144c559a353SRobert Baldyga 				desc_idx = 0;
1145c559a353SRobert Baldyga 			}
1146c559a353SRobert Baldyga 			desc = epfile->ep->descs[desc_idx];
1147c559a353SRobert Baldyga 
1148c559a353SRobert Baldyga 			spin_unlock_irq(&epfile->ffs->eps_lock);
1149c559a353SRobert Baldyga 			ret = copy_to_user((void *)value, desc, sizeof(*desc));
1150c559a353SRobert Baldyga 			if (ret)
1151c559a353SRobert Baldyga 				ret = -EFAULT;
1152c559a353SRobert Baldyga 			return ret;
1153c559a353SRobert Baldyga 		}
115400a2430fSAndrzej Pietrasiewicz 		default:
115500a2430fSAndrzej Pietrasiewicz 			ret = -ENOTTY;
115600a2430fSAndrzej Pietrasiewicz 		}
115700a2430fSAndrzej Pietrasiewicz 	} else {
115800a2430fSAndrzej Pietrasiewicz 		ret = -ENODEV;
115900a2430fSAndrzej Pietrasiewicz 	}
116000a2430fSAndrzej Pietrasiewicz 	spin_unlock_irq(&epfile->ffs->eps_lock);
116100a2430fSAndrzej Pietrasiewicz 
116200a2430fSAndrzej Pietrasiewicz 	return ret;
116300a2430fSAndrzej Pietrasiewicz }
116400a2430fSAndrzej Pietrasiewicz 
116500a2430fSAndrzej Pietrasiewicz static const struct file_operations ffs_epfile_operations = {
116600a2430fSAndrzej Pietrasiewicz 	.llseek =	no_llseek,
116700a2430fSAndrzej Pietrasiewicz 
116800a2430fSAndrzej Pietrasiewicz 	.open =		ffs_epfile_open,
116970e60d91SAl Viro 	.write_iter =	ffs_epfile_write_iter,
117070e60d91SAl Viro 	.read_iter =	ffs_epfile_read_iter,
117100a2430fSAndrzej Pietrasiewicz 	.release =	ffs_epfile_release,
117200a2430fSAndrzej Pietrasiewicz 	.unlocked_ioctl =	ffs_epfile_ioctl,
117300a2430fSAndrzej Pietrasiewicz };
117400a2430fSAndrzej Pietrasiewicz 
117500a2430fSAndrzej Pietrasiewicz 
117600a2430fSAndrzej Pietrasiewicz /* File system and super block operations ***********************************/
117700a2430fSAndrzej Pietrasiewicz 
117800a2430fSAndrzej Pietrasiewicz /*
117900a2430fSAndrzej Pietrasiewicz  * Mounting the file system creates a controller file, used first for
118000a2430fSAndrzej Pietrasiewicz  * function configuration then later for event monitoring.
118100a2430fSAndrzej Pietrasiewicz  */
118200a2430fSAndrzej Pietrasiewicz 
118300a2430fSAndrzej Pietrasiewicz static struct inode *__must_check
118400a2430fSAndrzej Pietrasiewicz ffs_sb_make_inode(struct super_block *sb, void *data,
118500a2430fSAndrzej Pietrasiewicz 		  const struct file_operations *fops,
118600a2430fSAndrzej Pietrasiewicz 		  const struct inode_operations *iops,
118700a2430fSAndrzej Pietrasiewicz 		  struct ffs_file_perms *perms)
118800a2430fSAndrzej Pietrasiewicz {
118900a2430fSAndrzej Pietrasiewicz 	struct inode *inode;
119000a2430fSAndrzej Pietrasiewicz 
119100a2430fSAndrzej Pietrasiewicz 	ENTER();
119200a2430fSAndrzej Pietrasiewicz 
119300a2430fSAndrzej Pietrasiewicz 	inode = new_inode(sb);
119400a2430fSAndrzej Pietrasiewicz 
119500a2430fSAndrzej Pietrasiewicz 	if (likely(inode)) {
1196078cd827SDeepa Dinamani 		struct timespec ts = current_time(inode);
119700a2430fSAndrzej Pietrasiewicz 
119800a2430fSAndrzej Pietrasiewicz 		inode->i_ino	 = get_next_ino();
119900a2430fSAndrzej Pietrasiewicz 		inode->i_mode    = perms->mode;
120000a2430fSAndrzej Pietrasiewicz 		inode->i_uid     = perms->uid;
120100a2430fSAndrzej Pietrasiewicz 		inode->i_gid     = perms->gid;
1202078cd827SDeepa Dinamani 		inode->i_atime   = ts;
1203078cd827SDeepa Dinamani 		inode->i_mtime   = ts;
1204078cd827SDeepa Dinamani 		inode->i_ctime   = ts;
120500a2430fSAndrzej Pietrasiewicz 		inode->i_private = data;
120600a2430fSAndrzej Pietrasiewicz 		if (fops)
120700a2430fSAndrzej Pietrasiewicz 			inode->i_fop = fops;
120800a2430fSAndrzej Pietrasiewicz 		if (iops)
120900a2430fSAndrzej Pietrasiewicz 			inode->i_op  = iops;
121000a2430fSAndrzej Pietrasiewicz 	}
121100a2430fSAndrzej Pietrasiewicz 
121200a2430fSAndrzej Pietrasiewicz 	return inode;
121300a2430fSAndrzej Pietrasiewicz }
121400a2430fSAndrzej Pietrasiewicz 
121500a2430fSAndrzej Pietrasiewicz /* Create "regular" file */
12161bb27cacSAl Viro static struct dentry *ffs_sb_create_file(struct super_block *sb,
121700a2430fSAndrzej Pietrasiewicz 					const char *name, void *data,
12181bb27cacSAl Viro 					const struct file_operations *fops)
121900a2430fSAndrzej Pietrasiewicz {
122000a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = sb->s_fs_info;
122100a2430fSAndrzej Pietrasiewicz 	struct dentry	*dentry;
122200a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
122300a2430fSAndrzej Pietrasiewicz 
122400a2430fSAndrzej Pietrasiewicz 	ENTER();
122500a2430fSAndrzej Pietrasiewicz 
122600a2430fSAndrzej Pietrasiewicz 	dentry = d_alloc_name(sb->s_root, name);
122700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!dentry))
122800a2430fSAndrzej Pietrasiewicz 		return NULL;
122900a2430fSAndrzej Pietrasiewicz 
123000a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
123100a2430fSAndrzej Pietrasiewicz 	if (unlikely(!inode)) {
123200a2430fSAndrzej Pietrasiewicz 		dput(dentry);
123300a2430fSAndrzej Pietrasiewicz 		return NULL;
123400a2430fSAndrzej Pietrasiewicz 	}
123500a2430fSAndrzej Pietrasiewicz 
123600a2430fSAndrzej Pietrasiewicz 	d_add(dentry, inode);
12371bb27cacSAl Viro 	return dentry;
123800a2430fSAndrzej Pietrasiewicz }
123900a2430fSAndrzej Pietrasiewicz 
124000a2430fSAndrzej Pietrasiewicz /* Super block */
124100a2430fSAndrzej Pietrasiewicz static const struct super_operations ffs_sb_operations = {
124200a2430fSAndrzej Pietrasiewicz 	.statfs =	simple_statfs,
124300a2430fSAndrzej Pietrasiewicz 	.drop_inode =	generic_delete_inode,
124400a2430fSAndrzej Pietrasiewicz };
124500a2430fSAndrzej Pietrasiewicz 
124600a2430fSAndrzej Pietrasiewicz struct ffs_sb_fill_data {
124700a2430fSAndrzej Pietrasiewicz 	struct ffs_file_perms perms;
124800a2430fSAndrzej Pietrasiewicz 	umode_t root_mode;
124900a2430fSAndrzej Pietrasiewicz 	const char *dev_name;
125018d6b32fSRobert Baldyga 	bool no_disconnect;
125100a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs_data;
125200a2430fSAndrzej Pietrasiewicz };
125300a2430fSAndrzej Pietrasiewicz 
125400a2430fSAndrzej Pietrasiewicz static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
125500a2430fSAndrzej Pietrasiewicz {
125600a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data *data = _data;
125700a2430fSAndrzej Pietrasiewicz 	struct inode	*inode;
125800a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs = data->ffs_data;
125900a2430fSAndrzej Pietrasiewicz 
126000a2430fSAndrzej Pietrasiewicz 	ENTER();
126100a2430fSAndrzej Pietrasiewicz 
126200a2430fSAndrzej Pietrasiewicz 	ffs->sb              = sb;
126300a2430fSAndrzej Pietrasiewicz 	data->ffs_data       = NULL;
126400a2430fSAndrzej Pietrasiewicz 	sb->s_fs_info        = ffs;
126509cbfeafSKirill A. Shutemov 	sb->s_blocksize      = PAGE_SIZE;
126609cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
126700a2430fSAndrzej Pietrasiewicz 	sb->s_magic          = FUNCTIONFS_MAGIC;
126800a2430fSAndrzej Pietrasiewicz 	sb->s_op             = &ffs_sb_operations;
126900a2430fSAndrzej Pietrasiewicz 	sb->s_time_gran      = 1;
127000a2430fSAndrzej Pietrasiewicz 
127100a2430fSAndrzej Pietrasiewicz 	/* Root inode */
127200a2430fSAndrzej Pietrasiewicz 	data->perms.mode = data->root_mode;
127300a2430fSAndrzej Pietrasiewicz 	inode = ffs_sb_make_inode(sb, NULL,
127400a2430fSAndrzej Pietrasiewicz 				  &simple_dir_operations,
127500a2430fSAndrzej Pietrasiewicz 				  &simple_dir_inode_operations,
127600a2430fSAndrzej Pietrasiewicz 				  &data->perms);
127700a2430fSAndrzej Pietrasiewicz 	sb->s_root = d_make_root(inode);
127800a2430fSAndrzej Pietrasiewicz 	if (unlikely(!sb->s_root))
127900a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
128000a2430fSAndrzej Pietrasiewicz 
128100a2430fSAndrzej Pietrasiewicz 	/* EP0 file */
128200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
12831bb27cacSAl Viro 					 &ffs_ep0_operations)))
128400a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
128500a2430fSAndrzej Pietrasiewicz 
128600a2430fSAndrzej Pietrasiewicz 	return 0;
128700a2430fSAndrzej Pietrasiewicz }
128800a2430fSAndrzej Pietrasiewicz 
128900a2430fSAndrzej Pietrasiewicz static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
129000a2430fSAndrzej Pietrasiewicz {
129100a2430fSAndrzej Pietrasiewicz 	ENTER();
129200a2430fSAndrzej Pietrasiewicz 
129300a2430fSAndrzej Pietrasiewicz 	if (!opts || !*opts)
129400a2430fSAndrzej Pietrasiewicz 		return 0;
129500a2430fSAndrzej Pietrasiewicz 
129600a2430fSAndrzej Pietrasiewicz 	for (;;) {
129700a2430fSAndrzej Pietrasiewicz 		unsigned long value;
129800a2430fSAndrzej Pietrasiewicz 		char *eq, *comma;
129900a2430fSAndrzej Pietrasiewicz 
130000a2430fSAndrzej Pietrasiewicz 		/* Option limit */
130100a2430fSAndrzej Pietrasiewicz 		comma = strchr(opts, ',');
130200a2430fSAndrzej Pietrasiewicz 		if (comma)
130300a2430fSAndrzej Pietrasiewicz 			*comma = 0;
130400a2430fSAndrzej Pietrasiewicz 
130500a2430fSAndrzej Pietrasiewicz 		/* Value limit */
130600a2430fSAndrzej Pietrasiewicz 		eq = strchr(opts, '=');
130700a2430fSAndrzej Pietrasiewicz 		if (unlikely(!eq)) {
130800a2430fSAndrzej Pietrasiewicz 			pr_err("'=' missing in %s\n", opts);
130900a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
131000a2430fSAndrzej Pietrasiewicz 		}
131100a2430fSAndrzej Pietrasiewicz 		*eq = 0;
131200a2430fSAndrzej Pietrasiewicz 
131300a2430fSAndrzej Pietrasiewicz 		/* Parse value */
131400a2430fSAndrzej Pietrasiewicz 		if (kstrtoul(eq + 1, 0, &value)) {
131500a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid value: %s\n", opts, eq + 1);
131600a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
131700a2430fSAndrzej Pietrasiewicz 		}
131800a2430fSAndrzej Pietrasiewicz 
131900a2430fSAndrzej Pietrasiewicz 		/* Interpret option */
132000a2430fSAndrzej Pietrasiewicz 		switch (eq - opts) {
132118d6b32fSRobert Baldyga 		case 13:
132218d6b32fSRobert Baldyga 			if (!memcmp(opts, "no_disconnect", 13))
132318d6b32fSRobert Baldyga 				data->no_disconnect = !!value;
132418d6b32fSRobert Baldyga 			else
132518d6b32fSRobert Baldyga 				goto invalid;
132618d6b32fSRobert Baldyga 			break;
132700a2430fSAndrzej Pietrasiewicz 		case 5:
132800a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "rmode", 5))
132900a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
133000a2430fSAndrzej Pietrasiewicz 			else if (!memcmp(opts, "fmode", 5))
133100a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
133200a2430fSAndrzej Pietrasiewicz 			else
133300a2430fSAndrzej Pietrasiewicz 				goto invalid;
133400a2430fSAndrzej Pietrasiewicz 			break;
133500a2430fSAndrzej Pietrasiewicz 
133600a2430fSAndrzej Pietrasiewicz 		case 4:
133700a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "mode", 4)) {
133800a2430fSAndrzej Pietrasiewicz 				data->root_mode  = (value & 0555) | S_IFDIR;
133900a2430fSAndrzej Pietrasiewicz 				data->perms.mode = (value & 0666) | S_IFREG;
134000a2430fSAndrzej Pietrasiewicz 			} else {
134100a2430fSAndrzej Pietrasiewicz 				goto invalid;
134200a2430fSAndrzej Pietrasiewicz 			}
134300a2430fSAndrzej Pietrasiewicz 			break;
134400a2430fSAndrzej Pietrasiewicz 
134500a2430fSAndrzej Pietrasiewicz 		case 3:
134600a2430fSAndrzej Pietrasiewicz 			if (!memcmp(opts, "uid", 3)) {
134700a2430fSAndrzej Pietrasiewicz 				data->perms.uid = make_kuid(current_user_ns(), value);
134800a2430fSAndrzej Pietrasiewicz 				if (!uid_valid(data->perms.uid)) {
134900a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
135000a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
135100a2430fSAndrzej Pietrasiewicz 				}
135200a2430fSAndrzej Pietrasiewicz 			} else if (!memcmp(opts, "gid", 3)) {
135300a2430fSAndrzej Pietrasiewicz 				data->perms.gid = make_kgid(current_user_ns(), value);
135400a2430fSAndrzej Pietrasiewicz 				if (!gid_valid(data->perms.gid)) {
135500a2430fSAndrzej Pietrasiewicz 					pr_err("%s: unmapped value: %lu\n", opts, value);
135600a2430fSAndrzej Pietrasiewicz 					return -EINVAL;
135700a2430fSAndrzej Pietrasiewicz 				}
135800a2430fSAndrzej Pietrasiewicz 			} else {
135900a2430fSAndrzej Pietrasiewicz 				goto invalid;
136000a2430fSAndrzej Pietrasiewicz 			}
136100a2430fSAndrzej Pietrasiewicz 			break;
136200a2430fSAndrzej Pietrasiewicz 
136300a2430fSAndrzej Pietrasiewicz 		default:
136400a2430fSAndrzej Pietrasiewicz invalid:
136500a2430fSAndrzej Pietrasiewicz 			pr_err("%s: invalid option\n", opts);
136600a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
136700a2430fSAndrzej Pietrasiewicz 		}
136800a2430fSAndrzej Pietrasiewicz 
136900a2430fSAndrzej Pietrasiewicz 		/* Next iteration */
137000a2430fSAndrzej Pietrasiewicz 		if (!comma)
137100a2430fSAndrzej Pietrasiewicz 			break;
137200a2430fSAndrzej Pietrasiewicz 		opts = comma + 1;
137300a2430fSAndrzej Pietrasiewicz 	}
137400a2430fSAndrzej Pietrasiewicz 
137500a2430fSAndrzej Pietrasiewicz 	return 0;
137600a2430fSAndrzej Pietrasiewicz }
137700a2430fSAndrzej Pietrasiewicz 
137800a2430fSAndrzej Pietrasiewicz /* "mount -t functionfs dev_name /dev/function" ends up here */
137900a2430fSAndrzej Pietrasiewicz 
138000a2430fSAndrzej Pietrasiewicz static struct dentry *
138100a2430fSAndrzej Pietrasiewicz ffs_fs_mount(struct file_system_type *t, int flags,
138200a2430fSAndrzej Pietrasiewicz 	      const char *dev_name, void *opts)
138300a2430fSAndrzej Pietrasiewicz {
138400a2430fSAndrzej Pietrasiewicz 	struct ffs_sb_fill_data data = {
138500a2430fSAndrzej Pietrasiewicz 		.perms = {
138600a2430fSAndrzej Pietrasiewicz 			.mode = S_IFREG | 0600,
138700a2430fSAndrzej Pietrasiewicz 			.uid = GLOBAL_ROOT_UID,
138800a2430fSAndrzej Pietrasiewicz 			.gid = GLOBAL_ROOT_GID,
138900a2430fSAndrzej Pietrasiewicz 		},
139000a2430fSAndrzej Pietrasiewicz 		.root_mode = S_IFDIR | 0500,
139118d6b32fSRobert Baldyga 		.no_disconnect = false,
139200a2430fSAndrzej Pietrasiewicz 	};
139300a2430fSAndrzej Pietrasiewicz 	struct dentry *rv;
139400a2430fSAndrzej Pietrasiewicz 	int ret;
139500a2430fSAndrzej Pietrasiewicz 	void *ffs_dev;
139600a2430fSAndrzej Pietrasiewicz 	struct ffs_data	*ffs;
139700a2430fSAndrzej Pietrasiewicz 
139800a2430fSAndrzej Pietrasiewicz 	ENTER();
139900a2430fSAndrzej Pietrasiewicz 
140000a2430fSAndrzej Pietrasiewicz 	ret = ffs_fs_parse_opts(&data, opts);
140100a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
140200a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
140300a2430fSAndrzej Pietrasiewicz 
140400a2430fSAndrzej Pietrasiewicz 	ffs = ffs_data_new();
140500a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
140600a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
140700a2430fSAndrzej Pietrasiewicz 	ffs->file_perms = data.perms;
140818d6b32fSRobert Baldyga 	ffs->no_disconnect = data.no_disconnect;
140900a2430fSAndrzej Pietrasiewicz 
141000a2430fSAndrzej Pietrasiewicz 	ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
141100a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->dev_name)) {
141200a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
141300a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
141400a2430fSAndrzej Pietrasiewicz 	}
141500a2430fSAndrzej Pietrasiewicz 
141600a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_acquire_dev(dev_name);
141700a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_dev)) {
141800a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
141900a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(ffs_dev);
142000a2430fSAndrzej Pietrasiewicz 	}
142100a2430fSAndrzej Pietrasiewicz 	ffs->private_data = ffs_dev;
142200a2430fSAndrzej Pietrasiewicz 	data.ffs_data = ffs;
142300a2430fSAndrzej Pietrasiewicz 
142400a2430fSAndrzej Pietrasiewicz 	rv = mount_nodev(t, flags, &data, ffs_sb_fill);
142500a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(rv) && data.ffs_data) {
142600a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(data.ffs_data);
142700a2430fSAndrzej Pietrasiewicz 		ffs_data_put(data.ffs_data);
142800a2430fSAndrzej Pietrasiewicz 	}
142900a2430fSAndrzej Pietrasiewicz 	return rv;
143000a2430fSAndrzej Pietrasiewicz }
143100a2430fSAndrzej Pietrasiewicz 
143200a2430fSAndrzej Pietrasiewicz static void
143300a2430fSAndrzej Pietrasiewicz ffs_fs_kill_sb(struct super_block *sb)
143400a2430fSAndrzej Pietrasiewicz {
143500a2430fSAndrzej Pietrasiewicz 	ENTER();
143600a2430fSAndrzej Pietrasiewicz 
143700a2430fSAndrzej Pietrasiewicz 	kill_litter_super(sb);
143800a2430fSAndrzej Pietrasiewicz 	if (sb->s_fs_info) {
143900a2430fSAndrzej Pietrasiewicz 		ffs_release_dev(sb->s_fs_info);
144018d6b32fSRobert Baldyga 		ffs_data_closed(sb->s_fs_info);
144100a2430fSAndrzej Pietrasiewicz 		ffs_data_put(sb->s_fs_info);
144200a2430fSAndrzej Pietrasiewicz 	}
144300a2430fSAndrzej Pietrasiewicz }
144400a2430fSAndrzej Pietrasiewicz 
144500a2430fSAndrzej Pietrasiewicz static struct file_system_type ffs_fs_type = {
144600a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
144700a2430fSAndrzej Pietrasiewicz 	.name		= "functionfs",
144800a2430fSAndrzej Pietrasiewicz 	.mount		= ffs_fs_mount,
144900a2430fSAndrzej Pietrasiewicz 	.kill_sb	= ffs_fs_kill_sb,
145000a2430fSAndrzej Pietrasiewicz };
145100a2430fSAndrzej Pietrasiewicz MODULE_ALIAS_FS("functionfs");
145200a2430fSAndrzej Pietrasiewicz 
145300a2430fSAndrzej Pietrasiewicz 
145400a2430fSAndrzej Pietrasiewicz /* Driver's main init/cleanup functions *************************************/
145500a2430fSAndrzej Pietrasiewicz 
145600a2430fSAndrzej Pietrasiewicz static int functionfs_init(void)
145700a2430fSAndrzej Pietrasiewicz {
145800a2430fSAndrzej Pietrasiewicz 	int ret;
145900a2430fSAndrzej Pietrasiewicz 
146000a2430fSAndrzej Pietrasiewicz 	ENTER();
146100a2430fSAndrzej Pietrasiewicz 
146200a2430fSAndrzej Pietrasiewicz 	ret = register_filesystem(&ffs_fs_type);
146300a2430fSAndrzej Pietrasiewicz 	if (likely(!ret))
146400a2430fSAndrzej Pietrasiewicz 		pr_info("file system registered\n");
146500a2430fSAndrzej Pietrasiewicz 	else
146600a2430fSAndrzej Pietrasiewicz 		pr_err("failed registering file system (%d)\n", ret);
146700a2430fSAndrzej Pietrasiewicz 
146800a2430fSAndrzej Pietrasiewicz 	return ret;
146900a2430fSAndrzej Pietrasiewicz }
147000a2430fSAndrzej Pietrasiewicz 
147100a2430fSAndrzej Pietrasiewicz static void functionfs_cleanup(void)
147200a2430fSAndrzej Pietrasiewicz {
147300a2430fSAndrzej Pietrasiewicz 	ENTER();
147400a2430fSAndrzej Pietrasiewicz 
147500a2430fSAndrzej Pietrasiewicz 	pr_info("unloading\n");
147600a2430fSAndrzej Pietrasiewicz 	unregister_filesystem(&ffs_fs_type);
147700a2430fSAndrzej Pietrasiewicz }
147800a2430fSAndrzej Pietrasiewicz 
147900a2430fSAndrzej Pietrasiewicz 
148000a2430fSAndrzej Pietrasiewicz /* ffs_data and ffs_function construction and destruction code **************/
148100a2430fSAndrzej Pietrasiewicz 
148200a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs);
148300a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs);
148400a2430fSAndrzej Pietrasiewicz 
148500a2430fSAndrzej Pietrasiewicz static void ffs_data_get(struct ffs_data *ffs)
148600a2430fSAndrzej Pietrasiewicz {
148700a2430fSAndrzej Pietrasiewicz 	ENTER();
148800a2430fSAndrzej Pietrasiewicz 
148900a2430fSAndrzej Pietrasiewicz 	atomic_inc(&ffs->ref);
149000a2430fSAndrzej Pietrasiewicz }
149100a2430fSAndrzej Pietrasiewicz 
149200a2430fSAndrzej Pietrasiewicz static void ffs_data_opened(struct ffs_data *ffs)
149300a2430fSAndrzej Pietrasiewicz {
149400a2430fSAndrzej Pietrasiewicz 	ENTER();
149500a2430fSAndrzej Pietrasiewicz 
149600a2430fSAndrzej Pietrasiewicz 	atomic_inc(&ffs->ref);
149718d6b32fSRobert Baldyga 	if (atomic_add_return(1, &ffs->opened) == 1 &&
149818d6b32fSRobert Baldyga 			ffs->state == FFS_DEACTIVATED) {
149918d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
150018d6b32fSRobert Baldyga 		ffs_data_reset(ffs);
150118d6b32fSRobert Baldyga 	}
150200a2430fSAndrzej Pietrasiewicz }
150300a2430fSAndrzej Pietrasiewicz 
150400a2430fSAndrzej Pietrasiewicz static void ffs_data_put(struct ffs_data *ffs)
150500a2430fSAndrzej Pietrasiewicz {
150600a2430fSAndrzej Pietrasiewicz 	ENTER();
150700a2430fSAndrzej Pietrasiewicz 
150800a2430fSAndrzej Pietrasiewicz 	if (unlikely(atomic_dec_and_test(&ffs->ref))) {
150900a2430fSAndrzej Pietrasiewicz 		pr_info("%s(): freeing\n", __func__);
151000a2430fSAndrzej Pietrasiewicz 		ffs_data_clear(ffs);
151100a2430fSAndrzej Pietrasiewicz 		BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
151200a2430fSAndrzej Pietrasiewicz 		       waitqueue_active(&ffs->ep0req_completion.wait));
151300a2430fSAndrzej Pietrasiewicz 		kfree(ffs->dev_name);
151400a2430fSAndrzej Pietrasiewicz 		kfree(ffs);
151500a2430fSAndrzej Pietrasiewicz 	}
151600a2430fSAndrzej Pietrasiewicz }
151700a2430fSAndrzej Pietrasiewicz 
151800a2430fSAndrzej Pietrasiewicz static void ffs_data_closed(struct ffs_data *ffs)
151900a2430fSAndrzej Pietrasiewicz {
152000a2430fSAndrzej Pietrasiewicz 	ENTER();
152100a2430fSAndrzej Pietrasiewicz 
152200a2430fSAndrzej Pietrasiewicz 	if (atomic_dec_and_test(&ffs->opened)) {
152318d6b32fSRobert Baldyga 		if (ffs->no_disconnect) {
152418d6b32fSRobert Baldyga 			ffs->state = FFS_DEACTIVATED;
152518d6b32fSRobert Baldyga 			if (ffs->epfiles) {
152618d6b32fSRobert Baldyga 				ffs_epfiles_destroy(ffs->epfiles,
152718d6b32fSRobert Baldyga 						   ffs->eps_count);
152818d6b32fSRobert Baldyga 				ffs->epfiles = NULL;
152918d6b32fSRobert Baldyga 			}
153018d6b32fSRobert Baldyga 			if (ffs->setup_state == FFS_SETUP_PENDING)
153118d6b32fSRobert Baldyga 				__ffs_ep0_stall(ffs);
153218d6b32fSRobert Baldyga 		} else {
153318d6b32fSRobert Baldyga 			ffs->state = FFS_CLOSING;
153418d6b32fSRobert Baldyga 			ffs_data_reset(ffs);
153518d6b32fSRobert Baldyga 		}
153618d6b32fSRobert Baldyga 	}
153718d6b32fSRobert Baldyga 	if (atomic_read(&ffs->opened) < 0) {
153800a2430fSAndrzej Pietrasiewicz 		ffs->state = FFS_CLOSING;
153900a2430fSAndrzej Pietrasiewicz 		ffs_data_reset(ffs);
154000a2430fSAndrzej Pietrasiewicz 	}
154100a2430fSAndrzej Pietrasiewicz 
154200a2430fSAndrzej Pietrasiewicz 	ffs_data_put(ffs);
154300a2430fSAndrzej Pietrasiewicz }
154400a2430fSAndrzej Pietrasiewicz 
154500a2430fSAndrzej Pietrasiewicz static struct ffs_data *ffs_data_new(void)
154600a2430fSAndrzej Pietrasiewicz {
154700a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
154800a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs))
154900a2430fSAndrzej Pietrasiewicz 		return NULL;
155000a2430fSAndrzej Pietrasiewicz 
155100a2430fSAndrzej Pietrasiewicz 	ENTER();
155200a2430fSAndrzej Pietrasiewicz 
155300a2430fSAndrzej Pietrasiewicz 	atomic_set(&ffs->ref, 1);
155400a2430fSAndrzej Pietrasiewicz 	atomic_set(&ffs->opened, 0);
155500a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
155600a2430fSAndrzej Pietrasiewicz 	mutex_init(&ffs->mutex);
155700a2430fSAndrzej Pietrasiewicz 	spin_lock_init(&ffs->eps_lock);
155800a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&ffs->ev.waitq);
155900a2430fSAndrzej Pietrasiewicz 	init_completion(&ffs->ep0req_completion);
156000a2430fSAndrzej Pietrasiewicz 
156100a2430fSAndrzej Pietrasiewicz 	/* XXX REVISIT need to update it in some places, or do we? */
156200a2430fSAndrzej Pietrasiewicz 	ffs->ev.can_stall = 1;
156300a2430fSAndrzej Pietrasiewicz 
156400a2430fSAndrzej Pietrasiewicz 	return ffs;
156500a2430fSAndrzej Pietrasiewicz }
156600a2430fSAndrzej Pietrasiewicz 
156700a2430fSAndrzej Pietrasiewicz static void ffs_data_clear(struct ffs_data *ffs)
156800a2430fSAndrzej Pietrasiewicz {
156900a2430fSAndrzej Pietrasiewicz 	ENTER();
157000a2430fSAndrzej Pietrasiewicz 
157100a2430fSAndrzej Pietrasiewicz 	ffs_closed(ffs);
157200a2430fSAndrzej Pietrasiewicz 
157300a2430fSAndrzej Pietrasiewicz 	BUG_ON(ffs->gadget);
157400a2430fSAndrzej Pietrasiewicz 
157500a2430fSAndrzej Pietrasiewicz 	if (ffs->epfiles)
157600a2430fSAndrzej Pietrasiewicz 		ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
157700a2430fSAndrzej Pietrasiewicz 
15785e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
15795e33f6fdSRobert Baldyga 		eventfd_ctx_put(ffs->ffs_eventfd);
15805e33f6fdSRobert Baldyga 
158100a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_descs_data);
158200a2430fSAndrzej Pietrasiewicz 	kfree(ffs->raw_strings);
158300a2430fSAndrzej Pietrasiewicz 	kfree(ffs->stringtabs);
158400a2430fSAndrzej Pietrasiewicz }
158500a2430fSAndrzej Pietrasiewicz 
158600a2430fSAndrzej Pietrasiewicz static void ffs_data_reset(struct ffs_data *ffs)
158700a2430fSAndrzej Pietrasiewicz {
158800a2430fSAndrzej Pietrasiewicz 	ENTER();
158900a2430fSAndrzej Pietrasiewicz 
159000a2430fSAndrzej Pietrasiewicz 	ffs_data_clear(ffs);
159100a2430fSAndrzej Pietrasiewicz 
159200a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = NULL;
159300a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data = NULL;
159400a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs = NULL;
159500a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = NULL;
159600a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = NULL;
159700a2430fSAndrzej Pietrasiewicz 
159800a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length = 0;
159900a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count = 0;
160000a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count = 0;
160100a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count = 0;
160200a2430fSAndrzej Pietrasiewicz 
160300a2430fSAndrzej Pietrasiewicz 	ffs->strings_count = 0;
160400a2430fSAndrzej Pietrasiewicz 	ffs->interfaces_count = 0;
160500a2430fSAndrzej Pietrasiewicz 	ffs->eps_count = 0;
160600a2430fSAndrzej Pietrasiewicz 
160700a2430fSAndrzej Pietrasiewicz 	ffs->ev.count = 0;
160800a2430fSAndrzej Pietrasiewicz 
160900a2430fSAndrzej Pietrasiewicz 	ffs->state = FFS_READ_DESCRIPTORS;
161000a2430fSAndrzej Pietrasiewicz 	ffs->setup_state = FFS_NO_SETUP;
161100a2430fSAndrzej Pietrasiewicz 	ffs->flags = 0;
161200a2430fSAndrzej Pietrasiewicz }
161300a2430fSAndrzej Pietrasiewicz 
161400a2430fSAndrzej Pietrasiewicz 
161500a2430fSAndrzej Pietrasiewicz static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
161600a2430fSAndrzej Pietrasiewicz {
161700a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **lang;
161800a2430fSAndrzej Pietrasiewicz 	int first_id;
161900a2430fSAndrzej Pietrasiewicz 
162000a2430fSAndrzej Pietrasiewicz 	ENTER();
162100a2430fSAndrzej Pietrasiewicz 
162200a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs->state != FFS_ACTIVE
162300a2430fSAndrzej Pietrasiewicz 		 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
162400a2430fSAndrzej Pietrasiewicz 		return -EBADFD;
162500a2430fSAndrzej Pietrasiewicz 
162600a2430fSAndrzej Pietrasiewicz 	first_id = usb_string_ids_n(cdev, ffs->strings_count);
162700a2430fSAndrzej Pietrasiewicz 	if (unlikely(first_id < 0))
162800a2430fSAndrzej Pietrasiewicz 		return first_id;
162900a2430fSAndrzej Pietrasiewicz 
163000a2430fSAndrzej Pietrasiewicz 	ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
163100a2430fSAndrzej Pietrasiewicz 	if (unlikely(!ffs->ep0req))
163200a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
163300a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->complete = ffs_ep0_complete;
163400a2430fSAndrzej Pietrasiewicz 	ffs->ep0req->context = ffs;
163500a2430fSAndrzej Pietrasiewicz 
163600a2430fSAndrzej Pietrasiewicz 	lang = ffs->stringtabs;
163761fe2d75SGreg Kroah-Hartman 	if (lang) {
163861fe2d75SGreg Kroah-Hartman 		for (; *lang; ++lang) {
163900a2430fSAndrzej Pietrasiewicz 			struct usb_string *str = (*lang)->strings;
164000a2430fSAndrzej Pietrasiewicz 			int id = first_id;
164100a2430fSAndrzej Pietrasiewicz 			for (; str->s; ++id, ++str)
164200a2430fSAndrzej Pietrasiewicz 				str->id = id;
164300a2430fSAndrzej Pietrasiewicz 		}
164461fe2d75SGreg Kroah-Hartman 	}
164500a2430fSAndrzej Pietrasiewicz 
164600a2430fSAndrzej Pietrasiewicz 	ffs->gadget = cdev->gadget;
164700a2430fSAndrzej Pietrasiewicz 	ffs_data_get(ffs);
164800a2430fSAndrzej Pietrasiewicz 	return 0;
164900a2430fSAndrzej Pietrasiewicz }
165000a2430fSAndrzej Pietrasiewicz 
165100a2430fSAndrzej Pietrasiewicz static void functionfs_unbind(struct ffs_data *ffs)
165200a2430fSAndrzej Pietrasiewicz {
165300a2430fSAndrzej Pietrasiewicz 	ENTER();
165400a2430fSAndrzej Pietrasiewicz 
165500a2430fSAndrzej Pietrasiewicz 	if (!WARN_ON(!ffs->gadget)) {
165600a2430fSAndrzej Pietrasiewicz 		usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
165700a2430fSAndrzej Pietrasiewicz 		ffs->ep0req = NULL;
165800a2430fSAndrzej Pietrasiewicz 		ffs->gadget = NULL;
165900a2430fSAndrzej Pietrasiewicz 		clear_bit(FFS_FL_BOUND, &ffs->flags);
166000a2430fSAndrzej Pietrasiewicz 		ffs_data_put(ffs);
166100a2430fSAndrzej Pietrasiewicz 	}
166200a2430fSAndrzej Pietrasiewicz }
166300a2430fSAndrzej Pietrasiewicz 
166400a2430fSAndrzej Pietrasiewicz static int ffs_epfiles_create(struct ffs_data *ffs)
166500a2430fSAndrzej Pietrasiewicz {
166600a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile, *epfiles;
166700a2430fSAndrzej Pietrasiewicz 	unsigned i, count;
166800a2430fSAndrzej Pietrasiewicz 
166900a2430fSAndrzej Pietrasiewicz 	ENTER();
167000a2430fSAndrzej Pietrasiewicz 
167100a2430fSAndrzej Pietrasiewicz 	count = ffs->eps_count;
167200a2430fSAndrzej Pietrasiewicz 	epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
167300a2430fSAndrzej Pietrasiewicz 	if (!epfiles)
167400a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
167500a2430fSAndrzej Pietrasiewicz 
167600a2430fSAndrzej Pietrasiewicz 	epfile = epfiles;
167700a2430fSAndrzej Pietrasiewicz 	for (i = 1; i <= count; ++i, ++epfile) {
167800a2430fSAndrzej Pietrasiewicz 		epfile->ffs = ffs;
167900a2430fSAndrzej Pietrasiewicz 		mutex_init(&epfile->mutex);
168000a2430fSAndrzej Pietrasiewicz 		init_waitqueue_head(&epfile->wait);
16811b0bf88fSRobert Baldyga 		if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1682acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
16831b0bf88fSRobert Baldyga 		else
1684acba23feSMario Schuknecht 			sprintf(epfile->name, "ep%u", i);
1685acba23feSMario Schuknecht 		epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
16861bb27cacSAl Viro 						 epfile,
16871bb27cacSAl Viro 						 &ffs_epfile_operations);
16881bb27cacSAl Viro 		if (unlikely(!epfile->dentry)) {
168900a2430fSAndrzej Pietrasiewicz 			ffs_epfiles_destroy(epfiles, i - 1);
169000a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
169100a2430fSAndrzej Pietrasiewicz 		}
169200a2430fSAndrzej Pietrasiewicz 	}
169300a2430fSAndrzej Pietrasiewicz 
169400a2430fSAndrzej Pietrasiewicz 	ffs->epfiles = epfiles;
169500a2430fSAndrzej Pietrasiewicz 	return 0;
169600a2430fSAndrzej Pietrasiewicz }
169700a2430fSAndrzej Pietrasiewicz 
169800a2430fSAndrzej Pietrasiewicz static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
169900a2430fSAndrzej Pietrasiewicz {
170000a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = epfiles;
170100a2430fSAndrzej Pietrasiewicz 
170200a2430fSAndrzej Pietrasiewicz 	ENTER();
170300a2430fSAndrzej Pietrasiewicz 
170400a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++epfile) {
170500a2430fSAndrzej Pietrasiewicz 		BUG_ON(mutex_is_locked(&epfile->mutex) ||
170600a2430fSAndrzej Pietrasiewicz 		       waitqueue_active(&epfile->wait));
170700a2430fSAndrzej Pietrasiewicz 		if (epfile->dentry) {
170800a2430fSAndrzej Pietrasiewicz 			d_delete(epfile->dentry);
170900a2430fSAndrzej Pietrasiewicz 			dput(epfile->dentry);
171000a2430fSAndrzej Pietrasiewicz 			epfile->dentry = NULL;
171100a2430fSAndrzej Pietrasiewicz 		}
171200a2430fSAndrzej Pietrasiewicz 	}
171300a2430fSAndrzej Pietrasiewicz 
171400a2430fSAndrzej Pietrasiewicz 	kfree(epfiles);
171500a2430fSAndrzej Pietrasiewicz }
171600a2430fSAndrzej Pietrasiewicz 
171700a2430fSAndrzej Pietrasiewicz static void ffs_func_eps_disable(struct ffs_function *func)
171800a2430fSAndrzej Pietrasiewicz {
171900a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
172000a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = func->ffs->epfiles;
172100a2430fSAndrzej Pietrasiewicz 	unsigned count            = func->ffs->eps_count;
172200a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
172300a2430fSAndrzej Pietrasiewicz 
172400a2430fSAndrzej Pietrasiewicz 	do {
17259353afbbSMichal Nazarewicz 		if (epfile)
17269353afbbSMichal Nazarewicz 			mutex_lock(&epfile->mutex);
17279353afbbSMichal Nazarewicz 		spin_lock_irqsave(&func->ffs->eps_lock, flags);
172800a2430fSAndrzej Pietrasiewicz 		/* pending requests get nuked */
172900a2430fSAndrzej Pietrasiewicz 		if (likely(ep->ep))
173000a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(ep->ep);
173100a2430fSAndrzej Pietrasiewicz 		++ep;
17329353afbbSMichal Nazarewicz 		spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
173318d6b32fSRobert Baldyga 
173418d6b32fSRobert Baldyga 		if (epfile) {
173518d6b32fSRobert Baldyga 			epfile->ep = NULL;
17369353afbbSMichal Nazarewicz 			kfree(epfile->read_buffer);
17379353afbbSMichal Nazarewicz 			epfile->read_buffer = NULL;
17389353afbbSMichal Nazarewicz 			mutex_unlock(&epfile->mutex);
173900a2430fSAndrzej Pietrasiewicz 			++epfile;
174018d6b32fSRobert Baldyga 		}
174100a2430fSAndrzej Pietrasiewicz 	} while (--count);
174200a2430fSAndrzej Pietrasiewicz }
174300a2430fSAndrzej Pietrasiewicz 
174400a2430fSAndrzej Pietrasiewicz static int ffs_func_eps_enable(struct ffs_function *func)
174500a2430fSAndrzej Pietrasiewicz {
174600a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs      = func->ffs;
174700a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep         = func->eps;
174800a2430fSAndrzej Pietrasiewicz 	struct ffs_epfile *epfile = ffs->epfiles;
174900a2430fSAndrzej Pietrasiewicz 	unsigned count            = ffs->eps_count;
175000a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
175100a2430fSAndrzej Pietrasiewicz 	int ret = 0;
175200a2430fSAndrzej Pietrasiewicz 
175300a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
175400a2430fSAndrzej Pietrasiewicz 	do {
175500a2430fSAndrzej Pietrasiewicz 		struct usb_endpoint_descriptor *ds;
175600a2430fSAndrzej Pietrasiewicz 		int desc_idx;
175700a2430fSAndrzej Pietrasiewicz 
175800a2430fSAndrzej Pietrasiewicz 		if (ffs->gadget->speed == USB_SPEED_SUPER)
175900a2430fSAndrzej Pietrasiewicz 			desc_idx = 2;
176000a2430fSAndrzej Pietrasiewicz 		else if (ffs->gadget->speed == USB_SPEED_HIGH)
176100a2430fSAndrzej Pietrasiewicz 			desc_idx = 1;
176200a2430fSAndrzej Pietrasiewicz 		else
176300a2430fSAndrzej Pietrasiewicz 			desc_idx = 0;
176400a2430fSAndrzej Pietrasiewicz 
176500a2430fSAndrzej Pietrasiewicz 		/* fall-back to lower speed if desc missing for current speed */
176600a2430fSAndrzej Pietrasiewicz 		do {
176700a2430fSAndrzej Pietrasiewicz 			ds = ep->descs[desc_idx];
176800a2430fSAndrzej Pietrasiewicz 		} while (!ds && --desc_idx >= 0);
176900a2430fSAndrzej Pietrasiewicz 
177000a2430fSAndrzej Pietrasiewicz 		if (!ds) {
177100a2430fSAndrzej Pietrasiewicz 			ret = -EINVAL;
177200a2430fSAndrzej Pietrasiewicz 			break;
177300a2430fSAndrzej Pietrasiewicz 		}
177400a2430fSAndrzej Pietrasiewicz 
177500a2430fSAndrzej Pietrasiewicz 		ep->ep->driver_data = ep;
177600a2430fSAndrzej Pietrasiewicz 		ep->ep->desc = ds;
177700a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_enable(ep->ep);
177800a2430fSAndrzej Pietrasiewicz 		if (likely(!ret)) {
177900a2430fSAndrzej Pietrasiewicz 			epfile->ep = ep;
178000a2430fSAndrzej Pietrasiewicz 			epfile->in = usb_endpoint_dir_in(ds);
178100a2430fSAndrzej Pietrasiewicz 			epfile->isoc = usb_endpoint_xfer_isoc(ds);
178200a2430fSAndrzej Pietrasiewicz 		} else {
178300a2430fSAndrzej Pietrasiewicz 			break;
178400a2430fSAndrzej Pietrasiewicz 		}
178500a2430fSAndrzej Pietrasiewicz 
178600a2430fSAndrzej Pietrasiewicz 		wake_up(&epfile->wait);
178700a2430fSAndrzej Pietrasiewicz 
178800a2430fSAndrzej Pietrasiewicz 		++ep;
178900a2430fSAndrzej Pietrasiewicz 		++epfile;
179000a2430fSAndrzej Pietrasiewicz 	} while (--count);
179100a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
179200a2430fSAndrzej Pietrasiewicz 
179300a2430fSAndrzej Pietrasiewicz 	return ret;
179400a2430fSAndrzej Pietrasiewicz }
179500a2430fSAndrzej Pietrasiewicz 
179600a2430fSAndrzej Pietrasiewicz 
179700a2430fSAndrzej Pietrasiewicz /* Parsing and building descriptors and strings *****************************/
179800a2430fSAndrzej Pietrasiewicz 
179900a2430fSAndrzej Pietrasiewicz /*
180000a2430fSAndrzej Pietrasiewicz  * This validates if data pointed by data is a valid USB descriptor as
180100a2430fSAndrzej Pietrasiewicz  * well as record how many interfaces, endpoints and strings are
180200a2430fSAndrzej Pietrasiewicz  * required by given configuration.  Returns address after the
180300a2430fSAndrzej Pietrasiewicz  * descriptor or NULL if data is invalid.
180400a2430fSAndrzej Pietrasiewicz  */
180500a2430fSAndrzej Pietrasiewicz 
180600a2430fSAndrzej Pietrasiewicz enum ffs_entity_type {
180700a2430fSAndrzej Pietrasiewicz 	FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
180800a2430fSAndrzej Pietrasiewicz };
180900a2430fSAndrzej Pietrasiewicz 
181000a2430fSAndrzej Pietrasiewicz enum ffs_os_desc_type {
181100a2430fSAndrzej Pietrasiewicz 	FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
181200a2430fSAndrzej Pietrasiewicz };
181300a2430fSAndrzej Pietrasiewicz 
181400a2430fSAndrzej Pietrasiewicz typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
181500a2430fSAndrzej Pietrasiewicz 				   u8 *valuep,
181600a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
181700a2430fSAndrzej Pietrasiewicz 				   void *priv);
181800a2430fSAndrzej Pietrasiewicz 
181900a2430fSAndrzej Pietrasiewicz typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
182000a2430fSAndrzej Pietrasiewicz 				    struct usb_os_desc_header *h, void *data,
182100a2430fSAndrzej Pietrasiewicz 				    unsigned len, void *priv);
182200a2430fSAndrzej Pietrasiewicz 
182300a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_desc(char *data, unsigned len,
182400a2430fSAndrzej Pietrasiewicz 					   ffs_entity_callback entity,
182500a2430fSAndrzej Pietrasiewicz 					   void *priv)
182600a2430fSAndrzej Pietrasiewicz {
182700a2430fSAndrzej Pietrasiewicz 	struct usb_descriptor_header *_ds = (void *)data;
182800a2430fSAndrzej Pietrasiewicz 	u8 length;
182900a2430fSAndrzej Pietrasiewicz 	int ret;
183000a2430fSAndrzej Pietrasiewicz 
183100a2430fSAndrzej Pietrasiewicz 	ENTER();
183200a2430fSAndrzej Pietrasiewicz 
183300a2430fSAndrzej Pietrasiewicz 	/* At least two bytes are required: length and type */
183400a2430fSAndrzej Pietrasiewicz 	if (len < 2) {
183500a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor too short\n");
183600a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
183700a2430fSAndrzej Pietrasiewicz 	}
183800a2430fSAndrzej Pietrasiewicz 
183900a2430fSAndrzej Pietrasiewicz 	/* If we have at least as many bytes as the descriptor takes? */
184000a2430fSAndrzej Pietrasiewicz 	length = _ds->bLength;
184100a2430fSAndrzej Pietrasiewicz 	if (len < length) {
184200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor longer then available data\n");
184300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
184400a2430fSAndrzej Pietrasiewicz 	}
184500a2430fSAndrzej Pietrasiewicz 
184600a2430fSAndrzej Pietrasiewicz #define __entity_check_INTERFACE(val)  1
184700a2430fSAndrzej Pietrasiewicz #define __entity_check_STRING(val)     (val)
184800a2430fSAndrzej Pietrasiewicz #define __entity_check_ENDPOINT(val)   ((val) & USB_ENDPOINT_NUMBER_MASK)
184900a2430fSAndrzej Pietrasiewicz #define __entity(type, val) do {					\
185000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("entity " #type "(%02x)\n", (val));		\
185100a2430fSAndrzej Pietrasiewicz 		if (unlikely(!__entity_check_ ##type(val))) {		\
185200a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid entity's value\n");		\
185300a2430fSAndrzej Pietrasiewicz 			return -EINVAL;					\
185400a2430fSAndrzej Pietrasiewicz 		}							\
185500a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_ ##type, &val, _ds, priv);		\
185600a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {				\
185700a2430fSAndrzej Pietrasiewicz 			pr_debug("entity " #type "(%02x); ret = %d\n",	\
185800a2430fSAndrzej Pietrasiewicz 				 (val), ret);				\
185900a2430fSAndrzej Pietrasiewicz 			return ret;					\
186000a2430fSAndrzej Pietrasiewicz 		}							\
186100a2430fSAndrzej Pietrasiewicz 	} while (0)
186200a2430fSAndrzej Pietrasiewicz 
186300a2430fSAndrzej Pietrasiewicz 	/* Parse descriptor depending on type. */
186400a2430fSAndrzej Pietrasiewicz 	switch (_ds->bDescriptorType) {
186500a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE:
186600a2430fSAndrzej Pietrasiewicz 	case USB_DT_CONFIG:
186700a2430fSAndrzej Pietrasiewicz 	case USB_DT_STRING:
186800a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEVICE_QUALIFIER:
186900a2430fSAndrzej Pietrasiewicz 		/* function can't have any of those */
187000a2430fSAndrzej Pietrasiewicz 		pr_vdebug("descriptor reserved for gadget: %d\n",
187100a2430fSAndrzej Pietrasiewicz 		      _ds->bDescriptorType);
187200a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
187300a2430fSAndrzej Pietrasiewicz 
187400a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE: {
187500a2430fSAndrzej Pietrasiewicz 		struct usb_interface_descriptor *ds = (void *)_ds;
187600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface descriptor\n");
187700a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
187800a2430fSAndrzej Pietrasiewicz 			goto inv_length;
187900a2430fSAndrzej Pietrasiewicz 
188000a2430fSAndrzej Pietrasiewicz 		__entity(INTERFACE, ds->bInterfaceNumber);
188100a2430fSAndrzej Pietrasiewicz 		if (ds->iInterface)
188200a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iInterface);
188300a2430fSAndrzej Pietrasiewicz 	}
188400a2430fSAndrzej Pietrasiewicz 		break;
188500a2430fSAndrzej Pietrasiewicz 
188600a2430fSAndrzej Pietrasiewicz 	case USB_DT_ENDPOINT: {
188700a2430fSAndrzej Pietrasiewicz 		struct usb_endpoint_descriptor *ds = (void *)_ds;
188800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("endpoint descriptor\n");
188900a2430fSAndrzej Pietrasiewicz 		if (length != USB_DT_ENDPOINT_SIZE &&
189000a2430fSAndrzej Pietrasiewicz 		    length != USB_DT_ENDPOINT_AUDIO_SIZE)
189100a2430fSAndrzej Pietrasiewicz 			goto inv_length;
189200a2430fSAndrzej Pietrasiewicz 		__entity(ENDPOINT, ds->bEndpointAddress);
189300a2430fSAndrzej Pietrasiewicz 	}
189400a2430fSAndrzej Pietrasiewicz 		break;
189500a2430fSAndrzej Pietrasiewicz 
189600a2430fSAndrzej Pietrasiewicz 	case HID_DT_HID:
189700a2430fSAndrzej Pietrasiewicz 		pr_vdebug("hid descriptor\n");
189800a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct hid_descriptor))
189900a2430fSAndrzej Pietrasiewicz 			goto inv_length;
190000a2430fSAndrzej Pietrasiewicz 		break;
190100a2430fSAndrzej Pietrasiewicz 
190200a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTG:
190300a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_otg_descriptor))
190400a2430fSAndrzej Pietrasiewicz 			goto inv_length;
190500a2430fSAndrzej Pietrasiewicz 		break;
190600a2430fSAndrzej Pietrasiewicz 
190700a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_ASSOCIATION: {
190800a2430fSAndrzej Pietrasiewicz 		struct usb_interface_assoc_descriptor *ds = (void *)_ds;
190900a2430fSAndrzej Pietrasiewicz 		pr_vdebug("interface association descriptor\n");
191000a2430fSAndrzej Pietrasiewicz 		if (length != sizeof *ds)
191100a2430fSAndrzej Pietrasiewicz 			goto inv_length;
191200a2430fSAndrzej Pietrasiewicz 		if (ds->iFunction)
191300a2430fSAndrzej Pietrasiewicz 			__entity(STRING, ds->iFunction);
191400a2430fSAndrzej Pietrasiewicz 	}
191500a2430fSAndrzej Pietrasiewicz 		break;
191600a2430fSAndrzej Pietrasiewicz 
191700a2430fSAndrzej Pietrasiewicz 	case USB_DT_SS_ENDPOINT_COMP:
191800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("EP SS companion descriptor\n");
191900a2430fSAndrzej Pietrasiewicz 		if (length != sizeof(struct usb_ss_ep_comp_descriptor))
192000a2430fSAndrzej Pietrasiewicz 			goto inv_length;
192100a2430fSAndrzej Pietrasiewicz 		break;
192200a2430fSAndrzej Pietrasiewicz 
192300a2430fSAndrzej Pietrasiewicz 	case USB_DT_OTHER_SPEED_CONFIG:
192400a2430fSAndrzej Pietrasiewicz 	case USB_DT_INTERFACE_POWER:
192500a2430fSAndrzej Pietrasiewicz 	case USB_DT_DEBUG:
192600a2430fSAndrzej Pietrasiewicz 	case USB_DT_SECURITY:
192700a2430fSAndrzej Pietrasiewicz 	case USB_DT_CS_RADIO_CONTROL:
192800a2430fSAndrzej Pietrasiewicz 		/* TODO */
192900a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
193000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
193100a2430fSAndrzej Pietrasiewicz 
193200a2430fSAndrzej Pietrasiewicz 	default:
193300a2430fSAndrzej Pietrasiewicz 		/* We should never be here */
193400a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
193500a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
193600a2430fSAndrzej Pietrasiewicz 
193700a2430fSAndrzej Pietrasiewicz inv_length:
193800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("invalid length: %d (descriptor %d)\n",
193900a2430fSAndrzej Pietrasiewicz 			  _ds->bLength, _ds->bDescriptorType);
194000a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
194100a2430fSAndrzej Pietrasiewicz 	}
194200a2430fSAndrzej Pietrasiewicz 
194300a2430fSAndrzej Pietrasiewicz #undef __entity
194400a2430fSAndrzej Pietrasiewicz #undef __entity_check_DESCRIPTOR
194500a2430fSAndrzej Pietrasiewicz #undef __entity_check_INTERFACE
194600a2430fSAndrzej Pietrasiewicz #undef __entity_check_STRING
194700a2430fSAndrzej Pietrasiewicz #undef __entity_check_ENDPOINT
194800a2430fSAndrzej Pietrasiewicz 
194900a2430fSAndrzej Pietrasiewicz 	return length;
195000a2430fSAndrzej Pietrasiewicz }
195100a2430fSAndrzej Pietrasiewicz 
195200a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
195300a2430fSAndrzej Pietrasiewicz 				     ffs_entity_callback entity, void *priv)
195400a2430fSAndrzej Pietrasiewicz {
195500a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
195600a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
195700a2430fSAndrzej Pietrasiewicz 
195800a2430fSAndrzej Pietrasiewicz 	ENTER();
195900a2430fSAndrzej Pietrasiewicz 
196000a2430fSAndrzej Pietrasiewicz 	for (;;) {
196100a2430fSAndrzej Pietrasiewicz 		int ret;
196200a2430fSAndrzej Pietrasiewicz 
196300a2430fSAndrzej Pietrasiewicz 		if (num == count)
196400a2430fSAndrzej Pietrasiewicz 			data = NULL;
196500a2430fSAndrzej Pietrasiewicz 
196600a2430fSAndrzej Pietrasiewicz 		/* Record "descriptor" entity */
196700a2430fSAndrzej Pietrasiewicz 		ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
196800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
196900a2430fSAndrzej Pietrasiewicz 			pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
197000a2430fSAndrzej Pietrasiewicz 				 num, ret);
197100a2430fSAndrzej Pietrasiewicz 			return ret;
197200a2430fSAndrzej Pietrasiewicz 		}
197300a2430fSAndrzej Pietrasiewicz 
197400a2430fSAndrzej Pietrasiewicz 		if (!data)
197500a2430fSAndrzej Pietrasiewicz 			return _len - len;
197600a2430fSAndrzej Pietrasiewicz 
197700a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_single_desc(data, len, entity, priv);
197800a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
197900a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
198000a2430fSAndrzej Pietrasiewicz 			return ret;
198100a2430fSAndrzej Pietrasiewicz 		}
198200a2430fSAndrzej Pietrasiewicz 
198300a2430fSAndrzej Pietrasiewicz 		len -= ret;
198400a2430fSAndrzej Pietrasiewicz 		data += ret;
198500a2430fSAndrzej Pietrasiewicz 		++num;
198600a2430fSAndrzej Pietrasiewicz 	}
198700a2430fSAndrzej Pietrasiewicz }
198800a2430fSAndrzej Pietrasiewicz 
198900a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_entity(enum ffs_entity_type type,
199000a2430fSAndrzej Pietrasiewicz 				u8 *valuep, struct usb_descriptor_header *desc,
199100a2430fSAndrzej Pietrasiewicz 				void *priv)
199200a2430fSAndrzej Pietrasiewicz {
19936d5c1c77SRobert Baldyga 	struct ffs_desc_helper *helper = priv;
19946d5c1c77SRobert Baldyga 	struct usb_endpoint_descriptor *d;
199500a2430fSAndrzej Pietrasiewicz 
199600a2430fSAndrzej Pietrasiewicz 	ENTER();
199700a2430fSAndrzej Pietrasiewicz 
199800a2430fSAndrzej Pietrasiewicz 	switch (type) {
199900a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
200000a2430fSAndrzej Pietrasiewicz 		break;
200100a2430fSAndrzej Pietrasiewicz 
200200a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
200300a2430fSAndrzej Pietrasiewicz 		/*
200400a2430fSAndrzej Pietrasiewicz 		 * Interfaces are indexed from zero so if we
200500a2430fSAndrzej Pietrasiewicz 		 * encountered interface "n" then there are at least
200600a2430fSAndrzej Pietrasiewicz 		 * "n+1" interfaces.
200700a2430fSAndrzej Pietrasiewicz 		 */
20086d5c1c77SRobert Baldyga 		if (*valuep >= helper->interfaces_count)
20096d5c1c77SRobert Baldyga 			helper->interfaces_count = *valuep + 1;
201000a2430fSAndrzej Pietrasiewicz 		break;
201100a2430fSAndrzej Pietrasiewicz 
201200a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
201300a2430fSAndrzej Pietrasiewicz 		/*
201400a2430fSAndrzej Pietrasiewicz 		 * Strings are indexed from 1 (0 is magic ;) reserved
201500a2430fSAndrzej Pietrasiewicz 		 * for languages list or some such)
201600a2430fSAndrzej Pietrasiewicz 		 */
20176d5c1c77SRobert Baldyga 		if (*valuep > helper->ffs->strings_count)
20186d5c1c77SRobert Baldyga 			helper->ffs->strings_count = *valuep;
201900a2430fSAndrzej Pietrasiewicz 		break;
202000a2430fSAndrzej Pietrasiewicz 
202100a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
20226d5c1c77SRobert Baldyga 		d = (void *)desc;
20236d5c1c77SRobert Baldyga 		helper->eps_count++;
20246d5c1c77SRobert Baldyga 		if (helper->eps_count >= 15)
20256d5c1c77SRobert Baldyga 			return -EINVAL;
20266d5c1c77SRobert Baldyga 		/* Check if descriptors for any speed were already parsed */
20276d5c1c77SRobert Baldyga 		if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
20286d5c1c77SRobert Baldyga 			helper->ffs->eps_addrmap[helper->eps_count] =
20296d5c1c77SRobert Baldyga 				d->bEndpointAddress;
20306d5c1c77SRobert Baldyga 		else if (helper->ffs->eps_addrmap[helper->eps_count] !=
20316d5c1c77SRobert Baldyga 				d->bEndpointAddress)
20326d5c1c77SRobert Baldyga 			return -EINVAL;
203300a2430fSAndrzej Pietrasiewicz 		break;
203400a2430fSAndrzej Pietrasiewicz 	}
203500a2430fSAndrzej Pietrasiewicz 
203600a2430fSAndrzej Pietrasiewicz 	return 0;
203700a2430fSAndrzej Pietrasiewicz }
203800a2430fSAndrzej Pietrasiewicz 
203900a2430fSAndrzej Pietrasiewicz static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
204000a2430fSAndrzej Pietrasiewicz 				   struct usb_os_desc_header *desc)
204100a2430fSAndrzej Pietrasiewicz {
204200a2430fSAndrzej Pietrasiewicz 	u16 bcd_version = le16_to_cpu(desc->bcdVersion);
204300a2430fSAndrzej Pietrasiewicz 	u16 w_index = le16_to_cpu(desc->wIndex);
204400a2430fSAndrzej Pietrasiewicz 
204500a2430fSAndrzej Pietrasiewicz 	if (bcd_version != 1) {
204600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptors version: %d",
204700a2430fSAndrzej Pietrasiewicz 			  bcd_version);
204800a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
204900a2430fSAndrzej Pietrasiewicz 	}
205000a2430fSAndrzej Pietrasiewicz 	switch (w_index) {
205100a2430fSAndrzej Pietrasiewicz 	case 0x4:
205200a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_COMPAT;
205300a2430fSAndrzej Pietrasiewicz 		break;
205400a2430fSAndrzej Pietrasiewicz 	case 0x5:
205500a2430fSAndrzej Pietrasiewicz 		*next_type = FFS_OS_DESC_EXT_PROP;
205600a2430fSAndrzej Pietrasiewicz 		break;
205700a2430fSAndrzej Pietrasiewicz 	default:
205800a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unsupported os descriptor type: %d", w_index);
205900a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
206000a2430fSAndrzej Pietrasiewicz 	}
206100a2430fSAndrzej Pietrasiewicz 
206200a2430fSAndrzej Pietrasiewicz 	return sizeof(*desc);
206300a2430fSAndrzej Pietrasiewicz }
206400a2430fSAndrzej Pietrasiewicz 
206500a2430fSAndrzej Pietrasiewicz /*
206600a2430fSAndrzej Pietrasiewicz  * Process all extended compatibility/extended property descriptors
206700a2430fSAndrzej Pietrasiewicz  * of a feature descriptor
206800a2430fSAndrzej Pietrasiewicz  */
206900a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
207000a2430fSAndrzej Pietrasiewicz 					      enum ffs_os_desc_type type,
207100a2430fSAndrzej Pietrasiewicz 					      u16 feature_count,
207200a2430fSAndrzej Pietrasiewicz 					      ffs_os_desc_callback entity,
207300a2430fSAndrzej Pietrasiewicz 					      void *priv,
207400a2430fSAndrzej Pietrasiewicz 					      struct usb_os_desc_header *h)
207500a2430fSAndrzej Pietrasiewicz {
207600a2430fSAndrzej Pietrasiewicz 	int ret;
207700a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
207800a2430fSAndrzej Pietrasiewicz 
207900a2430fSAndrzej Pietrasiewicz 	ENTER();
208000a2430fSAndrzej Pietrasiewicz 
208100a2430fSAndrzej Pietrasiewicz 	/* loop over all ext compat/ext prop descriptors */
208200a2430fSAndrzej Pietrasiewicz 	while (feature_count--) {
208300a2430fSAndrzej Pietrasiewicz 		ret = entity(type, h, data, len, priv);
208400a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
208500a2430fSAndrzej Pietrasiewicz 			pr_debug("bad OS descriptor, type: %d\n", type);
208600a2430fSAndrzej Pietrasiewicz 			return ret;
208700a2430fSAndrzej Pietrasiewicz 		}
208800a2430fSAndrzej Pietrasiewicz 		data += ret;
208900a2430fSAndrzej Pietrasiewicz 		len -= ret;
209000a2430fSAndrzej Pietrasiewicz 	}
209100a2430fSAndrzej Pietrasiewicz 	return _len - len;
209200a2430fSAndrzej Pietrasiewicz }
209300a2430fSAndrzej Pietrasiewicz 
209400a2430fSAndrzej Pietrasiewicz /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
209500a2430fSAndrzej Pietrasiewicz static int __must_check ffs_do_os_descs(unsigned count,
209600a2430fSAndrzej Pietrasiewicz 					char *data, unsigned len,
209700a2430fSAndrzej Pietrasiewicz 					ffs_os_desc_callback entity, void *priv)
209800a2430fSAndrzej Pietrasiewicz {
209900a2430fSAndrzej Pietrasiewicz 	const unsigned _len = len;
210000a2430fSAndrzej Pietrasiewicz 	unsigned long num = 0;
210100a2430fSAndrzej Pietrasiewicz 
210200a2430fSAndrzej Pietrasiewicz 	ENTER();
210300a2430fSAndrzej Pietrasiewicz 
210400a2430fSAndrzej Pietrasiewicz 	for (num = 0; num < count; ++num) {
210500a2430fSAndrzej Pietrasiewicz 		int ret;
210600a2430fSAndrzej Pietrasiewicz 		enum ffs_os_desc_type type;
210700a2430fSAndrzej Pietrasiewicz 		u16 feature_count;
210800a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_header *desc = (void *)data;
210900a2430fSAndrzej Pietrasiewicz 
211000a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*desc))
211100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
211200a2430fSAndrzej Pietrasiewicz 
211300a2430fSAndrzej Pietrasiewicz 		/*
211400a2430fSAndrzej Pietrasiewicz 		 * Record "descriptor" entity.
211500a2430fSAndrzej Pietrasiewicz 		 * Process dwLength, bcdVersion, wIndex, get b/wCount.
211600a2430fSAndrzej Pietrasiewicz 		 * Move the data pointer to the beginning of extended
211700a2430fSAndrzej Pietrasiewicz 		 * compatibilities proper or extended properties proper
211800a2430fSAndrzej Pietrasiewicz 		 * portions of the data
211900a2430fSAndrzej Pietrasiewicz 		 */
212000a2430fSAndrzej Pietrasiewicz 		if (le32_to_cpu(desc->dwLength) > len)
212100a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
212200a2430fSAndrzej Pietrasiewicz 
212300a2430fSAndrzej Pietrasiewicz 		ret = __ffs_do_os_desc_header(&type, desc);
212400a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
212500a2430fSAndrzej Pietrasiewicz 			pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
212600a2430fSAndrzej Pietrasiewicz 				 num, ret);
212700a2430fSAndrzej Pietrasiewicz 			return ret;
212800a2430fSAndrzej Pietrasiewicz 		}
212900a2430fSAndrzej Pietrasiewicz 		/*
213000a2430fSAndrzej Pietrasiewicz 		 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
213100a2430fSAndrzej Pietrasiewicz 		 */
213200a2430fSAndrzej Pietrasiewicz 		feature_count = le16_to_cpu(desc->wCount);
213300a2430fSAndrzej Pietrasiewicz 		if (type == FFS_OS_DESC_EXT_COMPAT &&
213400a2430fSAndrzej Pietrasiewicz 		    (feature_count > 255 || desc->Reserved))
213500a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
213600a2430fSAndrzej Pietrasiewicz 		len -= ret;
213700a2430fSAndrzej Pietrasiewicz 		data += ret;
213800a2430fSAndrzej Pietrasiewicz 
213900a2430fSAndrzej Pietrasiewicz 		/*
214000a2430fSAndrzej Pietrasiewicz 		 * Process all function/property descriptors
214100a2430fSAndrzej Pietrasiewicz 		 * of this Feature Descriptor
214200a2430fSAndrzej Pietrasiewicz 		 */
214300a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_single_os_desc(data, len, type,
214400a2430fSAndrzej Pietrasiewicz 					    feature_count, entity, priv, desc);
214500a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0)) {
214600a2430fSAndrzej Pietrasiewicz 			pr_debug("%s returns %d\n", __func__, ret);
214700a2430fSAndrzej Pietrasiewicz 			return ret;
214800a2430fSAndrzej Pietrasiewicz 		}
214900a2430fSAndrzej Pietrasiewicz 
215000a2430fSAndrzej Pietrasiewicz 		len -= ret;
215100a2430fSAndrzej Pietrasiewicz 		data += ret;
215200a2430fSAndrzej Pietrasiewicz 	}
215300a2430fSAndrzej Pietrasiewicz 	return _len - len;
215400a2430fSAndrzej Pietrasiewicz }
215500a2430fSAndrzej Pietrasiewicz 
215600a2430fSAndrzej Pietrasiewicz /**
215700a2430fSAndrzej Pietrasiewicz  * Validate contents of the buffer from userspace related to OS descriptors.
215800a2430fSAndrzej Pietrasiewicz  */
215900a2430fSAndrzej Pietrasiewicz static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
216000a2430fSAndrzej Pietrasiewicz 				 struct usb_os_desc_header *h, void *data,
216100a2430fSAndrzej Pietrasiewicz 				 unsigned len, void *priv)
216200a2430fSAndrzej Pietrasiewicz {
216300a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = priv;
216400a2430fSAndrzej Pietrasiewicz 	u8 length;
216500a2430fSAndrzej Pietrasiewicz 
216600a2430fSAndrzej Pietrasiewicz 	ENTER();
216700a2430fSAndrzej Pietrasiewicz 
216800a2430fSAndrzej Pietrasiewicz 	switch (type) {
216900a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
217000a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *d = data;
217100a2430fSAndrzej Pietrasiewicz 		int i;
217200a2430fSAndrzej Pietrasiewicz 
217300a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) ||
217400a2430fSAndrzej Pietrasiewicz 		    d->bFirstInterfaceNumber >= ffs->interfaces_count ||
217553642399SJim Lin 		    !d->Reserved1)
217600a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
217700a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
217800a2430fSAndrzej Pietrasiewicz 			if (d->Reserved2[i])
217900a2430fSAndrzej Pietrasiewicz 				return -EINVAL;
218000a2430fSAndrzej Pietrasiewicz 
218100a2430fSAndrzej Pietrasiewicz 		length = sizeof(struct usb_ext_compat_desc);
218200a2430fSAndrzej Pietrasiewicz 	}
218300a2430fSAndrzej Pietrasiewicz 		break;
218400a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
218500a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *d = data;
218600a2430fSAndrzej Pietrasiewicz 		u32 type, pdl;
218700a2430fSAndrzej Pietrasiewicz 		u16 pnl;
218800a2430fSAndrzej Pietrasiewicz 
218900a2430fSAndrzej Pietrasiewicz 		if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
219000a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
219100a2430fSAndrzej Pietrasiewicz 		length = le32_to_cpu(d->dwSize);
219200a2430fSAndrzej Pietrasiewicz 		type = le32_to_cpu(d->dwPropertyDataType);
219300a2430fSAndrzej Pietrasiewicz 		if (type < USB_EXT_PROP_UNICODE ||
219400a2430fSAndrzej Pietrasiewicz 		    type > USB_EXT_PROP_UNICODE_MULTI) {
219500a2430fSAndrzej Pietrasiewicz 			pr_vdebug("unsupported os descriptor property type: %d",
219600a2430fSAndrzej Pietrasiewicz 				  type);
219700a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
219800a2430fSAndrzej Pietrasiewicz 		}
219900a2430fSAndrzej Pietrasiewicz 		pnl = le16_to_cpu(d->wPropertyNameLength);
220000a2430fSAndrzej Pietrasiewicz 		pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
220100a2430fSAndrzej Pietrasiewicz 		if (length != 14 + pnl + pdl) {
220200a2430fSAndrzej Pietrasiewicz 			pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
220300a2430fSAndrzej Pietrasiewicz 				  length, pnl, pdl, type);
220400a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
220500a2430fSAndrzej Pietrasiewicz 		}
220600a2430fSAndrzej Pietrasiewicz 		++ffs->ms_os_descs_ext_prop_count;
220700a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
220800a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
220900a2430fSAndrzej Pietrasiewicz 		ffs->ms_os_descs_ext_prop_data_len += pdl;
221000a2430fSAndrzej Pietrasiewicz 	}
221100a2430fSAndrzej Pietrasiewicz 		break;
221200a2430fSAndrzej Pietrasiewicz 	default:
221300a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
221400a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
221500a2430fSAndrzej Pietrasiewicz 	}
221600a2430fSAndrzej Pietrasiewicz 	return length;
221700a2430fSAndrzej Pietrasiewicz }
221800a2430fSAndrzej Pietrasiewicz 
221900a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_descs(struct ffs_data *ffs,
222000a2430fSAndrzej Pietrasiewicz 				char *const _data, size_t len)
222100a2430fSAndrzej Pietrasiewicz {
222200a2430fSAndrzej Pietrasiewicz 	char *data = _data, *raw_descs;
222300a2430fSAndrzej Pietrasiewicz 	unsigned os_descs_count = 0, counts[3], flags;
222400a2430fSAndrzej Pietrasiewicz 	int ret = -EINVAL, i;
22256d5c1c77SRobert Baldyga 	struct ffs_desc_helper helper;
222600a2430fSAndrzej Pietrasiewicz 
222700a2430fSAndrzej Pietrasiewicz 	ENTER();
222800a2430fSAndrzej Pietrasiewicz 
222900a2430fSAndrzej Pietrasiewicz 	if (get_unaligned_le32(data + 4) != len)
223000a2430fSAndrzej Pietrasiewicz 		goto error;
223100a2430fSAndrzej Pietrasiewicz 
223200a2430fSAndrzej Pietrasiewicz 	switch (get_unaligned_le32(data)) {
223300a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC:
223400a2430fSAndrzej Pietrasiewicz 		flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
223500a2430fSAndrzej Pietrasiewicz 		data += 8;
223600a2430fSAndrzej Pietrasiewicz 		len  -= 8;
223700a2430fSAndrzej Pietrasiewicz 		break;
223800a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
223900a2430fSAndrzej Pietrasiewicz 		flags = get_unaligned_le32(data + 8);
22401b0bf88fSRobert Baldyga 		ffs->user_flags = flags;
224100a2430fSAndrzej Pietrasiewicz 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
224200a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_HS_DESC |
224300a2430fSAndrzej Pietrasiewicz 			      FUNCTIONFS_HAS_SS_DESC |
22441b0bf88fSRobert Baldyga 			      FUNCTIONFS_HAS_MS_OS_DESC |
22455e33f6fdSRobert Baldyga 			      FUNCTIONFS_VIRTUAL_ADDR |
22465e33f6fdSRobert Baldyga 			      FUNCTIONFS_EVENTFD)) {
224700a2430fSAndrzej Pietrasiewicz 			ret = -ENOSYS;
224800a2430fSAndrzej Pietrasiewicz 			goto error;
224900a2430fSAndrzej Pietrasiewicz 		}
225000a2430fSAndrzej Pietrasiewicz 		data += 12;
225100a2430fSAndrzej Pietrasiewicz 		len  -= 12;
225200a2430fSAndrzej Pietrasiewicz 		break;
225300a2430fSAndrzej Pietrasiewicz 	default:
225400a2430fSAndrzej Pietrasiewicz 		goto error;
225500a2430fSAndrzej Pietrasiewicz 	}
225600a2430fSAndrzej Pietrasiewicz 
22575e33f6fdSRobert Baldyga 	if (flags & FUNCTIONFS_EVENTFD) {
22585e33f6fdSRobert Baldyga 		if (len < 4)
22595e33f6fdSRobert Baldyga 			goto error;
22605e33f6fdSRobert Baldyga 		ffs->ffs_eventfd =
22615e33f6fdSRobert Baldyga 			eventfd_ctx_fdget((int)get_unaligned_le32(data));
22625e33f6fdSRobert Baldyga 		if (IS_ERR(ffs->ffs_eventfd)) {
22635e33f6fdSRobert Baldyga 			ret = PTR_ERR(ffs->ffs_eventfd);
22645e33f6fdSRobert Baldyga 			ffs->ffs_eventfd = NULL;
22655e33f6fdSRobert Baldyga 			goto error;
22665e33f6fdSRobert Baldyga 		}
22675e33f6fdSRobert Baldyga 		data += 4;
22685e33f6fdSRobert Baldyga 		len  -= 4;
22695e33f6fdSRobert Baldyga 	}
22705e33f6fdSRobert Baldyga 
227100a2430fSAndrzej Pietrasiewicz 	/* Read fs_count, hs_count and ss_count (if present) */
227200a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
227300a2430fSAndrzej Pietrasiewicz 		if (!(flags & (1 << i))) {
227400a2430fSAndrzej Pietrasiewicz 			counts[i] = 0;
227500a2430fSAndrzej Pietrasiewicz 		} else if (len < 4) {
227600a2430fSAndrzej Pietrasiewicz 			goto error;
227700a2430fSAndrzej Pietrasiewicz 		} else {
227800a2430fSAndrzej Pietrasiewicz 			counts[i] = get_unaligned_le32(data);
227900a2430fSAndrzej Pietrasiewicz 			data += 4;
228000a2430fSAndrzej Pietrasiewicz 			len  -= 4;
228100a2430fSAndrzej Pietrasiewicz 		}
228200a2430fSAndrzej Pietrasiewicz 	}
228300a2430fSAndrzej Pietrasiewicz 	if (flags & (1 << i)) {
228400a2430fSAndrzej Pietrasiewicz 		os_descs_count = get_unaligned_le32(data);
228500a2430fSAndrzej Pietrasiewicz 		data += 4;
228600a2430fSAndrzej Pietrasiewicz 		len -= 4;
228700a2430fSAndrzej Pietrasiewicz 	};
228800a2430fSAndrzej Pietrasiewicz 
228900a2430fSAndrzej Pietrasiewicz 	/* Read descriptors */
229000a2430fSAndrzej Pietrasiewicz 	raw_descs = data;
22916d5c1c77SRobert Baldyga 	helper.ffs = ffs;
229200a2430fSAndrzej Pietrasiewicz 	for (i = 0; i < 3; ++i) {
229300a2430fSAndrzej Pietrasiewicz 		if (!counts[i])
229400a2430fSAndrzej Pietrasiewicz 			continue;
22956d5c1c77SRobert Baldyga 		helper.interfaces_count = 0;
22966d5c1c77SRobert Baldyga 		helper.eps_count = 0;
229700a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_descs(counts[i], data, len,
22986d5c1c77SRobert Baldyga 				   __ffs_data_do_entity, &helper);
229900a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
230000a2430fSAndrzej Pietrasiewicz 			goto error;
23016d5c1c77SRobert Baldyga 		if (!ffs->eps_count && !ffs->interfaces_count) {
23026d5c1c77SRobert Baldyga 			ffs->eps_count = helper.eps_count;
23036d5c1c77SRobert Baldyga 			ffs->interfaces_count = helper.interfaces_count;
23046d5c1c77SRobert Baldyga 		} else {
23056d5c1c77SRobert Baldyga 			if (ffs->eps_count != helper.eps_count) {
23066d5c1c77SRobert Baldyga 				ret = -EINVAL;
23076d5c1c77SRobert Baldyga 				goto error;
23086d5c1c77SRobert Baldyga 			}
23096d5c1c77SRobert Baldyga 			if (ffs->interfaces_count != helper.interfaces_count) {
23106d5c1c77SRobert Baldyga 				ret = -EINVAL;
23116d5c1c77SRobert Baldyga 				goto error;
23126d5c1c77SRobert Baldyga 			}
23136d5c1c77SRobert Baldyga 		}
231400a2430fSAndrzej Pietrasiewicz 		data += ret;
231500a2430fSAndrzej Pietrasiewicz 		len  -= ret;
231600a2430fSAndrzej Pietrasiewicz 	}
231700a2430fSAndrzej Pietrasiewicz 	if (os_descs_count) {
231800a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(os_descs_count, data, len,
231900a2430fSAndrzej Pietrasiewicz 				      __ffs_data_do_os_desc, ffs);
232000a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
232100a2430fSAndrzej Pietrasiewicz 			goto error;
232200a2430fSAndrzej Pietrasiewicz 		data += ret;
232300a2430fSAndrzej Pietrasiewicz 		len -= ret;
232400a2430fSAndrzej Pietrasiewicz 	}
232500a2430fSAndrzej Pietrasiewicz 
232600a2430fSAndrzej Pietrasiewicz 	if (raw_descs == data || len) {
232700a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
232800a2430fSAndrzej Pietrasiewicz 		goto error;
232900a2430fSAndrzej Pietrasiewicz 	}
233000a2430fSAndrzej Pietrasiewicz 
233100a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_data	= _data;
233200a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs		= raw_descs;
233300a2430fSAndrzej Pietrasiewicz 	ffs->raw_descs_length	= data - raw_descs;
233400a2430fSAndrzej Pietrasiewicz 	ffs->fs_descs_count	= counts[0];
233500a2430fSAndrzej Pietrasiewicz 	ffs->hs_descs_count	= counts[1];
233600a2430fSAndrzej Pietrasiewicz 	ffs->ss_descs_count	= counts[2];
233700a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_count	= os_descs_count;
233800a2430fSAndrzej Pietrasiewicz 
233900a2430fSAndrzej Pietrasiewicz 	return 0;
234000a2430fSAndrzej Pietrasiewicz 
234100a2430fSAndrzej Pietrasiewicz error:
234200a2430fSAndrzej Pietrasiewicz 	kfree(_data);
234300a2430fSAndrzej Pietrasiewicz 	return ret;
234400a2430fSAndrzej Pietrasiewicz }
234500a2430fSAndrzej Pietrasiewicz 
234600a2430fSAndrzej Pietrasiewicz static int __ffs_data_got_strings(struct ffs_data *ffs,
234700a2430fSAndrzej Pietrasiewicz 				  char *const _data, size_t len)
234800a2430fSAndrzej Pietrasiewicz {
234900a2430fSAndrzej Pietrasiewicz 	u32 str_count, needed_count, lang_count;
235000a2430fSAndrzej Pietrasiewicz 	struct usb_gadget_strings **stringtabs, *t;
235100a2430fSAndrzej Pietrasiewicz 	const char *data = _data;
2352872ce511SMichal Nazarewicz 	struct usb_string *s;
235300a2430fSAndrzej Pietrasiewicz 
235400a2430fSAndrzej Pietrasiewicz 	ENTER();
235500a2430fSAndrzej Pietrasiewicz 
235600a2430fSAndrzej Pietrasiewicz 	if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
235700a2430fSAndrzej Pietrasiewicz 		     get_unaligned_le32(data + 4) != len))
235800a2430fSAndrzej Pietrasiewicz 		goto error;
235900a2430fSAndrzej Pietrasiewicz 	str_count  = get_unaligned_le32(data + 8);
236000a2430fSAndrzej Pietrasiewicz 	lang_count = get_unaligned_le32(data + 12);
236100a2430fSAndrzej Pietrasiewicz 
236200a2430fSAndrzej Pietrasiewicz 	/* if one is zero the other must be zero */
236300a2430fSAndrzej Pietrasiewicz 	if (unlikely(!str_count != !lang_count))
236400a2430fSAndrzej Pietrasiewicz 		goto error;
236500a2430fSAndrzej Pietrasiewicz 
236600a2430fSAndrzej Pietrasiewicz 	/* Do we have at least as many strings as descriptors need? */
236700a2430fSAndrzej Pietrasiewicz 	needed_count = ffs->strings_count;
236800a2430fSAndrzej Pietrasiewicz 	if (unlikely(str_count < needed_count))
236900a2430fSAndrzej Pietrasiewicz 		goto error;
237000a2430fSAndrzej Pietrasiewicz 
237100a2430fSAndrzej Pietrasiewicz 	/*
237200a2430fSAndrzej Pietrasiewicz 	 * If we don't need any strings just return and free all
237300a2430fSAndrzej Pietrasiewicz 	 * memory.
237400a2430fSAndrzej Pietrasiewicz 	 */
237500a2430fSAndrzej Pietrasiewicz 	if (!needed_count) {
237600a2430fSAndrzej Pietrasiewicz 		kfree(_data);
237700a2430fSAndrzej Pietrasiewicz 		return 0;
237800a2430fSAndrzej Pietrasiewicz 	}
237900a2430fSAndrzej Pietrasiewicz 
238000a2430fSAndrzej Pietrasiewicz 	/* Allocate everything in one chunk so there's less maintenance. */
238100a2430fSAndrzej Pietrasiewicz 	{
238200a2430fSAndrzej Pietrasiewicz 		unsigned i = 0;
238300a2430fSAndrzej Pietrasiewicz 		vla_group(d);
238400a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings *, stringtabs,
238500a2430fSAndrzej Pietrasiewicz 			lang_count + 1);
238600a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
238700a2430fSAndrzej Pietrasiewicz 		vla_item(d, struct usb_string, strings,
238800a2430fSAndrzej Pietrasiewicz 			lang_count*(needed_count+1));
238900a2430fSAndrzej Pietrasiewicz 
239000a2430fSAndrzej Pietrasiewicz 		char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
239100a2430fSAndrzej Pietrasiewicz 
239200a2430fSAndrzej Pietrasiewicz 		if (unlikely(!vlabuf)) {
239300a2430fSAndrzej Pietrasiewicz 			kfree(_data);
239400a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
239500a2430fSAndrzej Pietrasiewicz 		}
239600a2430fSAndrzej Pietrasiewicz 
239700a2430fSAndrzej Pietrasiewicz 		/* Initialize the VLA pointers */
239800a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
239900a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
240000a2430fSAndrzej Pietrasiewicz 		i = lang_count;
240100a2430fSAndrzej Pietrasiewicz 		do {
240200a2430fSAndrzej Pietrasiewicz 			*stringtabs++ = t++;
240300a2430fSAndrzej Pietrasiewicz 		} while (--i);
240400a2430fSAndrzej Pietrasiewicz 		*stringtabs = NULL;
240500a2430fSAndrzej Pietrasiewicz 
240600a2430fSAndrzej Pietrasiewicz 		/* stringtabs = vlabuf = d_stringtabs for later kfree */
240700a2430fSAndrzej Pietrasiewicz 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
240800a2430fSAndrzej Pietrasiewicz 		t = vla_ptr(vlabuf, d, stringtab);
240900a2430fSAndrzej Pietrasiewicz 		s = vla_ptr(vlabuf, d, strings);
241000a2430fSAndrzej Pietrasiewicz 	}
241100a2430fSAndrzej Pietrasiewicz 
241200a2430fSAndrzej Pietrasiewicz 	/* For each language */
241300a2430fSAndrzej Pietrasiewicz 	data += 16;
241400a2430fSAndrzej Pietrasiewicz 	len -= 16;
241500a2430fSAndrzej Pietrasiewicz 
241600a2430fSAndrzej Pietrasiewicz 	do { /* lang_count > 0 so we can use do-while */
241700a2430fSAndrzej Pietrasiewicz 		unsigned needed = needed_count;
241800a2430fSAndrzej Pietrasiewicz 
241900a2430fSAndrzej Pietrasiewicz 		if (unlikely(len < 3))
242000a2430fSAndrzej Pietrasiewicz 			goto error_free;
242100a2430fSAndrzej Pietrasiewicz 		t->language = get_unaligned_le16(data);
242200a2430fSAndrzej Pietrasiewicz 		t->strings  = s;
242300a2430fSAndrzej Pietrasiewicz 		++t;
242400a2430fSAndrzej Pietrasiewicz 
242500a2430fSAndrzej Pietrasiewicz 		data += 2;
242600a2430fSAndrzej Pietrasiewicz 		len -= 2;
242700a2430fSAndrzej Pietrasiewicz 
242800a2430fSAndrzej Pietrasiewicz 		/* For each string */
242900a2430fSAndrzej Pietrasiewicz 		do { /* str_count > 0 so we can use do-while */
243000a2430fSAndrzej Pietrasiewicz 			size_t length = strnlen(data, len);
243100a2430fSAndrzej Pietrasiewicz 
243200a2430fSAndrzej Pietrasiewicz 			if (unlikely(length == len))
243300a2430fSAndrzej Pietrasiewicz 				goto error_free;
243400a2430fSAndrzej Pietrasiewicz 
243500a2430fSAndrzej Pietrasiewicz 			/*
243600a2430fSAndrzej Pietrasiewicz 			 * User may provide more strings then we need,
243700a2430fSAndrzej Pietrasiewicz 			 * if that's the case we simply ignore the
243800a2430fSAndrzej Pietrasiewicz 			 * rest
243900a2430fSAndrzej Pietrasiewicz 			 */
244000a2430fSAndrzej Pietrasiewicz 			if (likely(needed)) {
244100a2430fSAndrzej Pietrasiewicz 				/*
244200a2430fSAndrzej Pietrasiewicz 				 * s->id will be set while adding
244300a2430fSAndrzej Pietrasiewicz 				 * function to configuration so for
244400a2430fSAndrzej Pietrasiewicz 				 * now just leave garbage here.
244500a2430fSAndrzej Pietrasiewicz 				 */
244600a2430fSAndrzej Pietrasiewicz 				s->s = data;
244700a2430fSAndrzej Pietrasiewicz 				--needed;
244800a2430fSAndrzej Pietrasiewicz 				++s;
244900a2430fSAndrzej Pietrasiewicz 			}
245000a2430fSAndrzej Pietrasiewicz 
245100a2430fSAndrzej Pietrasiewicz 			data += length + 1;
245200a2430fSAndrzej Pietrasiewicz 			len -= length + 1;
245300a2430fSAndrzej Pietrasiewicz 		} while (--str_count);
245400a2430fSAndrzej Pietrasiewicz 
245500a2430fSAndrzej Pietrasiewicz 		s->id = 0;   /* terminator */
245600a2430fSAndrzej Pietrasiewicz 		s->s = NULL;
245700a2430fSAndrzej Pietrasiewicz 		++s;
245800a2430fSAndrzej Pietrasiewicz 
245900a2430fSAndrzej Pietrasiewicz 	} while (--lang_count);
246000a2430fSAndrzej Pietrasiewicz 
246100a2430fSAndrzej Pietrasiewicz 	/* Some garbage left? */
246200a2430fSAndrzej Pietrasiewicz 	if (unlikely(len))
246300a2430fSAndrzej Pietrasiewicz 		goto error_free;
246400a2430fSAndrzej Pietrasiewicz 
246500a2430fSAndrzej Pietrasiewicz 	/* Done! */
246600a2430fSAndrzej Pietrasiewicz 	ffs->stringtabs = stringtabs;
246700a2430fSAndrzej Pietrasiewicz 	ffs->raw_strings = _data;
246800a2430fSAndrzej Pietrasiewicz 
246900a2430fSAndrzej Pietrasiewicz 	return 0;
247000a2430fSAndrzej Pietrasiewicz 
247100a2430fSAndrzej Pietrasiewicz error_free:
247200a2430fSAndrzej Pietrasiewicz 	kfree(stringtabs);
247300a2430fSAndrzej Pietrasiewicz error:
247400a2430fSAndrzej Pietrasiewicz 	kfree(_data);
247500a2430fSAndrzej Pietrasiewicz 	return -EINVAL;
247600a2430fSAndrzej Pietrasiewicz }
247700a2430fSAndrzej Pietrasiewicz 
247800a2430fSAndrzej Pietrasiewicz 
247900a2430fSAndrzej Pietrasiewicz /* Events handling and management *******************************************/
248000a2430fSAndrzej Pietrasiewicz 
248100a2430fSAndrzej Pietrasiewicz static void __ffs_event_add(struct ffs_data *ffs,
248200a2430fSAndrzej Pietrasiewicz 			    enum usb_functionfs_event_type type)
248300a2430fSAndrzej Pietrasiewicz {
248400a2430fSAndrzej Pietrasiewicz 	enum usb_functionfs_event_type rem_type1, rem_type2 = type;
248500a2430fSAndrzej Pietrasiewicz 	int neg = 0;
248600a2430fSAndrzej Pietrasiewicz 
248700a2430fSAndrzej Pietrasiewicz 	/*
248800a2430fSAndrzej Pietrasiewicz 	 * Abort any unhandled setup
248900a2430fSAndrzej Pietrasiewicz 	 *
249000a2430fSAndrzej Pietrasiewicz 	 * We do not need to worry about some cmpxchg() changing value
249100a2430fSAndrzej Pietrasiewicz 	 * of ffs->setup_state without holding the lock because when
249200a2430fSAndrzej Pietrasiewicz 	 * state is FFS_SETUP_PENDING cmpxchg() in several places in
249300a2430fSAndrzej Pietrasiewicz 	 * the source does nothing.
249400a2430fSAndrzej Pietrasiewicz 	 */
249500a2430fSAndrzej Pietrasiewicz 	if (ffs->setup_state == FFS_SETUP_PENDING)
249600a2430fSAndrzej Pietrasiewicz 		ffs->setup_state = FFS_SETUP_CANCELLED;
249700a2430fSAndrzej Pietrasiewicz 
249867913bbdSMichal Nazarewicz 	/*
249967913bbdSMichal Nazarewicz 	 * Logic of this function guarantees that there are at most four pending
250067913bbdSMichal Nazarewicz 	 * evens on ffs->ev.types queue.  This is important because the queue
250167913bbdSMichal Nazarewicz 	 * has space for four elements only and __ffs_ep0_read_events function
250267913bbdSMichal Nazarewicz 	 * depends on that limit as well.  If more event types are added, those
250367913bbdSMichal Nazarewicz 	 * limits have to be revisited or guaranteed to still hold.
250467913bbdSMichal Nazarewicz 	 */
250500a2430fSAndrzej Pietrasiewicz 	switch (type) {
250600a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_RESUME:
250700a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_SUSPEND;
250800a2430fSAndrzej Pietrasiewicz 		/* FALL THROUGH */
250900a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SUSPEND:
251000a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_SETUP:
251100a2430fSAndrzej Pietrasiewicz 		rem_type1 = type;
251200a2430fSAndrzej Pietrasiewicz 		/* Discard all similar events */
251300a2430fSAndrzej Pietrasiewicz 		break;
251400a2430fSAndrzej Pietrasiewicz 
251500a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_BIND:
251600a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_UNBIND:
251700a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_DISABLE:
251800a2430fSAndrzej Pietrasiewicz 	case FUNCTIONFS_ENABLE:
251900a2430fSAndrzej Pietrasiewicz 		/* Discard everything other then power management. */
252000a2430fSAndrzej Pietrasiewicz 		rem_type1 = FUNCTIONFS_SUSPEND;
252100a2430fSAndrzej Pietrasiewicz 		rem_type2 = FUNCTIONFS_RESUME;
252200a2430fSAndrzej Pietrasiewicz 		neg = 1;
252300a2430fSAndrzej Pietrasiewicz 		break;
252400a2430fSAndrzej Pietrasiewicz 
252500a2430fSAndrzej Pietrasiewicz 	default:
2526fe00bcbfSMichal Nazarewicz 		WARN(1, "%d: unknown event, this should not happen\n", type);
2527fe00bcbfSMichal Nazarewicz 		return;
252800a2430fSAndrzej Pietrasiewicz 	}
252900a2430fSAndrzej Pietrasiewicz 
253000a2430fSAndrzej Pietrasiewicz 	{
253100a2430fSAndrzej Pietrasiewicz 		u8 *ev  = ffs->ev.types, *out = ev;
253200a2430fSAndrzej Pietrasiewicz 		unsigned n = ffs->ev.count;
253300a2430fSAndrzej Pietrasiewicz 		for (; n; --n, ++ev)
253400a2430fSAndrzej Pietrasiewicz 			if ((*ev == rem_type1 || *ev == rem_type2) == neg)
253500a2430fSAndrzej Pietrasiewicz 				*out++ = *ev;
253600a2430fSAndrzej Pietrasiewicz 			else
253700a2430fSAndrzej Pietrasiewicz 				pr_vdebug("purging event %d\n", *ev);
253800a2430fSAndrzej Pietrasiewicz 		ffs->ev.count = out - ffs->ev.types;
253900a2430fSAndrzej Pietrasiewicz 	}
254000a2430fSAndrzej Pietrasiewicz 
254100a2430fSAndrzej Pietrasiewicz 	pr_vdebug("adding event %d\n", type);
254200a2430fSAndrzej Pietrasiewicz 	ffs->ev.types[ffs->ev.count++] = type;
254300a2430fSAndrzej Pietrasiewicz 	wake_up_locked(&ffs->ev.waitq);
25445e33f6fdSRobert Baldyga 	if (ffs->ffs_eventfd)
25455e33f6fdSRobert Baldyga 		eventfd_signal(ffs->ffs_eventfd, 1);
254600a2430fSAndrzej Pietrasiewicz }
254700a2430fSAndrzej Pietrasiewicz 
254800a2430fSAndrzej Pietrasiewicz static void ffs_event_add(struct ffs_data *ffs,
254900a2430fSAndrzej Pietrasiewicz 			  enum usb_functionfs_event_type type)
255000a2430fSAndrzej Pietrasiewicz {
255100a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
255200a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
255300a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, type);
255400a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
255500a2430fSAndrzej Pietrasiewicz }
255600a2430fSAndrzej Pietrasiewicz 
255700a2430fSAndrzej Pietrasiewicz /* Bind/unbind USB function hooks *******************************************/
255800a2430fSAndrzej Pietrasiewicz 
25596d5c1c77SRobert Baldyga static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
25606d5c1c77SRobert Baldyga {
25616d5c1c77SRobert Baldyga 	int i;
25626d5c1c77SRobert Baldyga 
25636d5c1c77SRobert Baldyga 	for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
25646d5c1c77SRobert Baldyga 		if (ffs->eps_addrmap[i] == endpoint_address)
25656d5c1c77SRobert Baldyga 			return i;
25666d5c1c77SRobert Baldyga 	return -ENOENT;
25676d5c1c77SRobert Baldyga }
25686d5c1c77SRobert Baldyga 
256900a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
257000a2430fSAndrzej Pietrasiewicz 				    struct usb_descriptor_header *desc,
257100a2430fSAndrzej Pietrasiewicz 				    void *priv)
257200a2430fSAndrzej Pietrasiewicz {
257300a2430fSAndrzej Pietrasiewicz 	struct usb_endpoint_descriptor *ds = (void *)desc;
257400a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
257500a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ffs_ep;
257685b06f5eSDan Carpenter 	unsigned ep_desc_id;
257785b06f5eSDan Carpenter 	int idx;
257800a2430fSAndrzej Pietrasiewicz 	static const char *speed_names[] = { "full", "high", "super" };
257900a2430fSAndrzej Pietrasiewicz 
258000a2430fSAndrzej Pietrasiewicz 	if (type != FFS_DESCRIPTOR)
258100a2430fSAndrzej Pietrasiewicz 		return 0;
258200a2430fSAndrzej Pietrasiewicz 
258300a2430fSAndrzej Pietrasiewicz 	/*
258400a2430fSAndrzej Pietrasiewicz 	 * If ss_descriptors is not NULL, we are reading super speed
258500a2430fSAndrzej Pietrasiewicz 	 * descriptors; if hs_descriptors is not NULL, we are reading high
258600a2430fSAndrzej Pietrasiewicz 	 * speed descriptors; otherwise, we are reading full speed
258700a2430fSAndrzej Pietrasiewicz 	 * descriptors.
258800a2430fSAndrzej Pietrasiewicz 	 */
258900a2430fSAndrzej Pietrasiewicz 	if (func->function.ss_descriptors) {
259000a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 2;
259100a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors[(long)valuep] = desc;
259200a2430fSAndrzej Pietrasiewicz 	} else if (func->function.hs_descriptors) {
259300a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 1;
259400a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors[(long)valuep] = desc;
259500a2430fSAndrzej Pietrasiewicz 	} else {
259600a2430fSAndrzej Pietrasiewicz 		ep_desc_id = 0;
259700a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors[(long)valuep]    = desc;
259800a2430fSAndrzej Pietrasiewicz 	}
259900a2430fSAndrzej Pietrasiewicz 
260000a2430fSAndrzej Pietrasiewicz 	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
260100a2430fSAndrzej Pietrasiewicz 		return 0;
260200a2430fSAndrzej Pietrasiewicz 
26036d5c1c77SRobert Baldyga 	idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
26046d5c1c77SRobert Baldyga 	if (idx < 0)
26056d5c1c77SRobert Baldyga 		return idx;
26066d5c1c77SRobert Baldyga 
260700a2430fSAndrzej Pietrasiewicz 	ffs_ep = func->eps + idx;
260800a2430fSAndrzej Pietrasiewicz 
260900a2430fSAndrzej Pietrasiewicz 	if (unlikely(ffs_ep->descs[ep_desc_id])) {
261000a2430fSAndrzej Pietrasiewicz 		pr_err("two %sspeed descriptors for EP %d\n",
261100a2430fSAndrzej Pietrasiewicz 			  speed_names[ep_desc_id],
261200a2430fSAndrzej Pietrasiewicz 			  ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
261300a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
261400a2430fSAndrzej Pietrasiewicz 	}
261500a2430fSAndrzej Pietrasiewicz 	ffs_ep->descs[ep_desc_id] = ds;
261600a2430fSAndrzej Pietrasiewicz 
261700a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Original  ep desc", ds, ds->bLength);
261800a2430fSAndrzej Pietrasiewicz 	if (ffs_ep->ep) {
261900a2430fSAndrzej Pietrasiewicz 		ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
262000a2430fSAndrzej Pietrasiewicz 		if (!ds->wMaxPacketSize)
262100a2430fSAndrzej Pietrasiewicz 			ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
262200a2430fSAndrzej Pietrasiewicz 	} else {
262300a2430fSAndrzej Pietrasiewicz 		struct usb_request *req;
262400a2430fSAndrzej Pietrasiewicz 		struct usb_ep *ep;
26251b0bf88fSRobert Baldyga 		u8 bEndpointAddress;
262600a2430fSAndrzej Pietrasiewicz 
26271b0bf88fSRobert Baldyga 		/*
26281b0bf88fSRobert Baldyga 		 * We back up bEndpointAddress because autoconfig overwrites
26291b0bf88fSRobert Baldyga 		 * it with physical endpoint address.
26301b0bf88fSRobert Baldyga 		 */
26311b0bf88fSRobert Baldyga 		bEndpointAddress = ds->bEndpointAddress;
263200a2430fSAndrzej Pietrasiewicz 		pr_vdebug("autoconfig\n");
263300a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(func->gadget, ds);
263400a2430fSAndrzej Pietrasiewicz 		if (unlikely(!ep))
263500a2430fSAndrzej Pietrasiewicz 			return -ENOTSUPP;
263600a2430fSAndrzej Pietrasiewicz 		ep->driver_data = func->eps + idx;
263700a2430fSAndrzej Pietrasiewicz 
263800a2430fSAndrzej Pietrasiewicz 		req = usb_ep_alloc_request(ep, GFP_KERNEL);
263900a2430fSAndrzej Pietrasiewicz 		if (unlikely(!req))
264000a2430fSAndrzej Pietrasiewicz 			return -ENOMEM;
264100a2430fSAndrzej Pietrasiewicz 
264200a2430fSAndrzej Pietrasiewicz 		ffs_ep->ep  = ep;
264300a2430fSAndrzej Pietrasiewicz 		ffs_ep->req = req;
264400a2430fSAndrzej Pietrasiewicz 		func->eps_revmap[ds->bEndpointAddress &
264500a2430fSAndrzej Pietrasiewicz 				 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
26461b0bf88fSRobert Baldyga 		/*
26471b0bf88fSRobert Baldyga 		 * If we use virtual address mapping, we restore
26481b0bf88fSRobert Baldyga 		 * original bEndpointAddress value.
26491b0bf88fSRobert Baldyga 		 */
26501b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
26511b0bf88fSRobert Baldyga 			ds->bEndpointAddress = bEndpointAddress;
265200a2430fSAndrzej Pietrasiewicz 	}
265300a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
265400a2430fSAndrzej Pietrasiewicz 
265500a2430fSAndrzej Pietrasiewicz 	return 0;
265600a2430fSAndrzej Pietrasiewicz }
265700a2430fSAndrzej Pietrasiewicz 
265800a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
265900a2430fSAndrzej Pietrasiewicz 				   struct usb_descriptor_header *desc,
266000a2430fSAndrzej Pietrasiewicz 				   void *priv)
266100a2430fSAndrzej Pietrasiewicz {
266200a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
266300a2430fSAndrzej Pietrasiewicz 	unsigned idx;
266400a2430fSAndrzej Pietrasiewicz 	u8 newValue;
266500a2430fSAndrzej Pietrasiewicz 
266600a2430fSAndrzej Pietrasiewicz 	switch (type) {
266700a2430fSAndrzej Pietrasiewicz 	default:
266800a2430fSAndrzej Pietrasiewicz 	case FFS_DESCRIPTOR:
266900a2430fSAndrzej Pietrasiewicz 		/* Handled in previous pass by __ffs_func_bind_do_descs() */
267000a2430fSAndrzej Pietrasiewicz 		return 0;
267100a2430fSAndrzej Pietrasiewicz 
267200a2430fSAndrzej Pietrasiewicz 	case FFS_INTERFACE:
267300a2430fSAndrzej Pietrasiewicz 		idx = *valuep;
267400a2430fSAndrzej Pietrasiewicz 		if (func->interfaces_nums[idx] < 0) {
267500a2430fSAndrzej Pietrasiewicz 			int id = usb_interface_id(func->conf, &func->function);
267600a2430fSAndrzej Pietrasiewicz 			if (unlikely(id < 0))
267700a2430fSAndrzej Pietrasiewicz 				return id;
267800a2430fSAndrzej Pietrasiewicz 			func->interfaces_nums[idx] = id;
267900a2430fSAndrzej Pietrasiewicz 		}
268000a2430fSAndrzej Pietrasiewicz 		newValue = func->interfaces_nums[idx];
268100a2430fSAndrzej Pietrasiewicz 		break;
268200a2430fSAndrzej Pietrasiewicz 
268300a2430fSAndrzej Pietrasiewicz 	case FFS_STRING:
268400a2430fSAndrzej Pietrasiewicz 		/* String' IDs are allocated when fsf_data is bound to cdev */
268500a2430fSAndrzej Pietrasiewicz 		newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
268600a2430fSAndrzej Pietrasiewicz 		break;
268700a2430fSAndrzej Pietrasiewicz 
268800a2430fSAndrzej Pietrasiewicz 	case FFS_ENDPOINT:
268900a2430fSAndrzej Pietrasiewicz 		/*
269000a2430fSAndrzej Pietrasiewicz 		 * USB_DT_ENDPOINT are handled in
269100a2430fSAndrzej Pietrasiewicz 		 * __ffs_func_bind_do_descs().
269200a2430fSAndrzej Pietrasiewicz 		 */
269300a2430fSAndrzej Pietrasiewicz 		if (desc->bDescriptorType == USB_DT_ENDPOINT)
269400a2430fSAndrzej Pietrasiewicz 			return 0;
269500a2430fSAndrzej Pietrasiewicz 
269600a2430fSAndrzej Pietrasiewicz 		idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
269700a2430fSAndrzej Pietrasiewicz 		if (unlikely(!func->eps[idx].ep))
269800a2430fSAndrzej Pietrasiewicz 			return -EINVAL;
269900a2430fSAndrzej Pietrasiewicz 
270000a2430fSAndrzej Pietrasiewicz 		{
270100a2430fSAndrzej Pietrasiewicz 			struct usb_endpoint_descriptor **descs;
270200a2430fSAndrzej Pietrasiewicz 			descs = func->eps[idx].descs;
270300a2430fSAndrzej Pietrasiewicz 			newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
270400a2430fSAndrzej Pietrasiewicz 		}
270500a2430fSAndrzej Pietrasiewicz 		break;
270600a2430fSAndrzej Pietrasiewicz 	}
270700a2430fSAndrzej Pietrasiewicz 
270800a2430fSAndrzej Pietrasiewicz 	pr_vdebug("%02x -> %02x\n", *valuep, newValue);
270900a2430fSAndrzej Pietrasiewicz 	*valuep = newValue;
271000a2430fSAndrzej Pietrasiewicz 	return 0;
271100a2430fSAndrzej Pietrasiewicz }
271200a2430fSAndrzej Pietrasiewicz 
271300a2430fSAndrzej Pietrasiewicz static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
271400a2430fSAndrzej Pietrasiewicz 				      struct usb_os_desc_header *h, void *data,
271500a2430fSAndrzej Pietrasiewicz 				      unsigned len, void *priv)
271600a2430fSAndrzej Pietrasiewicz {
271700a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = priv;
271800a2430fSAndrzej Pietrasiewicz 	u8 length = 0;
271900a2430fSAndrzej Pietrasiewicz 
272000a2430fSAndrzej Pietrasiewicz 	switch (type) {
272100a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_COMPAT: {
272200a2430fSAndrzej Pietrasiewicz 		struct usb_ext_compat_desc *desc = data;
272300a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
272400a2430fSAndrzej Pietrasiewicz 
272500a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
272600a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
272700a2430fSAndrzej Pietrasiewicz 		memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
272800a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->CompatibleID) +
272900a2430fSAndrzej Pietrasiewicz 		       ARRAY_SIZE(desc->SubCompatibleID));
273000a2430fSAndrzej Pietrasiewicz 		length = sizeof(*desc);
273100a2430fSAndrzej Pietrasiewicz 	}
273200a2430fSAndrzej Pietrasiewicz 		break;
273300a2430fSAndrzej Pietrasiewicz 	case FFS_OS_DESC_EXT_PROP: {
273400a2430fSAndrzej Pietrasiewicz 		struct usb_ext_prop_desc *desc = data;
273500a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_table *t;
273600a2430fSAndrzej Pietrasiewicz 		struct usb_os_desc_ext_prop *ext_prop;
273700a2430fSAndrzej Pietrasiewicz 		char *ext_prop_name;
273800a2430fSAndrzej Pietrasiewicz 		char *ext_prop_data;
273900a2430fSAndrzej Pietrasiewicz 
274000a2430fSAndrzej Pietrasiewicz 		t = &func->function.os_desc_table[h->interface];
274100a2430fSAndrzej Pietrasiewicz 		t->if_id = func->interfaces_nums[h->interface];
274200a2430fSAndrzej Pietrasiewicz 
274300a2430fSAndrzej Pietrasiewicz 		ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
274400a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
274500a2430fSAndrzej Pietrasiewicz 
274600a2430fSAndrzej Pietrasiewicz 		ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
274700a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
274800a2430fSAndrzej Pietrasiewicz 		ext_prop->data_len = le32_to_cpu(*(u32 *)
274900a2430fSAndrzej Pietrasiewicz 			usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
275000a2430fSAndrzej Pietrasiewicz 		length = ext_prop->name_len + ext_prop->data_len + 14;
275100a2430fSAndrzej Pietrasiewicz 
275200a2430fSAndrzej Pietrasiewicz 		ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
275300a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_name_avail +=
275400a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len;
275500a2430fSAndrzej Pietrasiewicz 
275600a2430fSAndrzej Pietrasiewicz 		ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
275700a2430fSAndrzej Pietrasiewicz 		func->ffs->ms_os_descs_ext_prop_data_avail +=
275800a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len;
275900a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_data,
276000a2430fSAndrzej Pietrasiewicz 		       usb_ext_prop_data_ptr(data, ext_prop->name_len),
276100a2430fSAndrzej Pietrasiewicz 		       ext_prop->data_len);
276200a2430fSAndrzej Pietrasiewicz 		/* unicode data reported to the host as "WCHAR"s */
276300a2430fSAndrzej Pietrasiewicz 		switch (ext_prop->type) {
276400a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE:
276500a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_ENV:
276600a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_LINK:
276700a2430fSAndrzej Pietrasiewicz 		case USB_EXT_PROP_UNICODE_MULTI:
276800a2430fSAndrzej Pietrasiewicz 			ext_prop->data_len *= 2;
276900a2430fSAndrzej Pietrasiewicz 			break;
277000a2430fSAndrzej Pietrasiewicz 		}
277100a2430fSAndrzej Pietrasiewicz 		ext_prop->data = ext_prop_data;
277200a2430fSAndrzej Pietrasiewicz 
277300a2430fSAndrzej Pietrasiewicz 		memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
277400a2430fSAndrzej Pietrasiewicz 		       ext_prop->name_len);
277500a2430fSAndrzej Pietrasiewicz 		/* property name reported to the host as "WCHAR"s */
277600a2430fSAndrzej Pietrasiewicz 		ext_prop->name_len *= 2;
277700a2430fSAndrzej Pietrasiewicz 		ext_prop->name = ext_prop_name;
277800a2430fSAndrzej Pietrasiewicz 
277900a2430fSAndrzej Pietrasiewicz 		t->os_desc->ext_prop_len +=
278000a2430fSAndrzej Pietrasiewicz 			ext_prop->name_len + ext_prop->data_len + 14;
278100a2430fSAndrzej Pietrasiewicz 		++t->os_desc->ext_prop_count;
278200a2430fSAndrzej Pietrasiewicz 		list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
278300a2430fSAndrzej Pietrasiewicz 	}
278400a2430fSAndrzej Pietrasiewicz 		break;
278500a2430fSAndrzej Pietrasiewicz 	default:
278600a2430fSAndrzej Pietrasiewicz 		pr_vdebug("unknown descriptor: %d\n", type);
278700a2430fSAndrzej Pietrasiewicz 	}
278800a2430fSAndrzej Pietrasiewicz 
278900a2430fSAndrzej Pietrasiewicz 	return length;
279000a2430fSAndrzej Pietrasiewicz }
279100a2430fSAndrzej Pietrasiewicz 
279200a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
279300a2430fSAndrzej Pietrasiewicz 						struct usb_configuration *c)
279400a2430fSAndrzej Pietrasiewicz {
279500a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
279600a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts =
279700a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
279800a2430fSAndrzej Pietrasiewicz 	int ret;
279900a2430fSAndrzej Pietrasiewicz 
280000a2430fSAndrzej Pietrasiewicz 	ENTER();
280100a2430fSAndrzej Pietrasiewicz 
280200a2430fSAndrzej Pietrasiewicz 	/*
280300a2430fSAndrzej Pietrasiewicz 	 * Legacy gadget triggers binding in functionfs_ready_callback,
280400a2430fSAndrzej Pietrasiewicz 	 * which already uses locking; taking the same lock here would
280500a2430fSAndrzej Pietrasiewicz 	 * cause a deadlock.
280600a2430fSAndrzej Pietrasiewicz 	 *
280700a2430fSAndrzej Pietrasiewicz 	 * Configfs-enabled gadgets however do need ffs_dev_lock.
280800a2430fSAndrzej Pietrasiewicz 	 */
280900a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
281000a2430fSAndrzej Pietrasiewicz 		ffs_dev_lock();
281100a2430fSAndrzej Pietrasiewicz 	ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
281200a2430fSAndrzej Pietrasiewicz 	func->ffs = ffs_opts->dev->ffs_data;
281300a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->no_configfs)
281400a2430fSAndrzej Pietrasiewicz 		ffs_dev_unlock();
281500a2430fSAndrzej Pietrasiewicz 	if (ret)
281600a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(ret);
281700a2430fSAndrzej Pietrasiewicz 
281800a2430fSAndrzej Pietrasiewicz 	func->conf = c;
281900a2430fSAndrzej Pietrasiewicz 	func->gadget = c->cdev->gadget;
282000a2430fSAndrzej Pietrasiewicz 
282100a2430fSAndrzej Pietrasiewicz 	/*
282200a2430fSAndrzej Pietrasiewicz 	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
282300a2430fSAndrzej Pietrasiewicz 	 * configurations are bound in sequence with list_for_each_entry,
282400a2430fSAndrzej Pietrasiewicz 	 * in each configuration its functions are bound in sequence
282500a2430fSAndrzej Pietrasiewicz 	 * with list_for_each_entry, so we assume no race condition
282600a2430fSAndrzej Pietrasiewicz 	 * with regard to ffs_opts->bound access
282700a2430fSAndrzej Pietrasiewicz 	 */
282800a2430fSAndrzej Pietrasiewicz 	if (!ffs_opts->refcnt) {
282900a2430fSAndrzej Pietrasiewicz 		ret = functionfs_bind(func->ffs, c->cdev);
283000a2430fSAndrzej Pietrasiewicz 		if (ret)
283100a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
283200a2430fSAndrzej Pietrasiewicz 	}
283300a2430fSAndrzej Pietrasiewicz 	ffs_opts->refcnt++;
283400a2430fSAndrzej Pietrasiewicz 	func->function.strings = func->ffs->stringtabs;
283500a2430fSAndrzej Pietrasiewicz 
283600a2430fSAndrzej Pietrasiewicz 	return ffs_opts;
283700a2430fSAndrzej Pietrasiewicz }
283800a2430fSAndrzej Pietrasiewicz 
283900a2430fSAndrzej Pietrasiewicz static int _ffs_func_bind(struct usb_configuration *c,
284000a2430fSAndrzej Pietrasiewicz 			  struct usb_function *f)
284100a2430fSAndrzej Pietrasiewicz {
284200a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
284300a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
284400a2430fSAndrzej Pietrasiewicz 
284500a2430fSAndrzej Pietrasiewicz 	const int full = !!func->ffs->fs_descs_count;
284600a2430fSAndrzej Pietrasiewicz 	const int high = gadget_is_dualspeed(func->gadget) &&
284700a2430fSAndrzej Pietrasiewicz 		func->ffs->hs_descs_count;
284800a2430fSAndrzej Pietrasiewicz 	const int super = gadget_is_superspeed(func->gadget) &&
284900a2430fSAndrzej Pietrasiewicz 		func->ffs->ss_descs_count;
285000a2430fSAndrzej Pietrasiewicz 
285100a2430fSAndrzej Pietrasiewicz 	int fs_len, hs_len, ss_len, ret, i;
28520015f915SDan Carpenter 	struct ffs_ep *eps_ptr;
285300a2430fSAndrzej Pietrasiewicz 
285400a2430fSAndrzej Pietrasiewicz 	/* Make it a single chunk, less management later on */
285500a2430fSAndrzej Pietrasiewicz 	vla_group(d);
285600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
285700a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
285800a2430fSAndrzej Pietrasiewicz 		full ? ffs->fs_descs_count + 1 : 0);
285900a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
286000a2430fSAndrzej Pietrasiewicz 		high ? ffs->hs_descs_count + 1 : 0);
286100a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
286200a2430fSAndrzej Pietrasiewicz 		super ? ffs->ss_descs_count + 1 : 0);
286300a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, short, inums, ffs->interfaces_count);
286400a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
286500a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
286600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char[16], ext_compat,
286700a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
286800a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc, os_desc,
286900a2430fSAndrzej Pietrasiewicz 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
287000a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
287100a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_count);
287200a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_name,
287300a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_name_len);
287400a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, ext_prop_data,
287500a2430fSAndrzej Pietrasiewicz 			 ffs->ms_os_descs_ext_prop_data_len);
287600a2430fSAndrzej Pietrasiewicz 	vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
287700a2430fSAndrzej Pietrasiewicz 	char *vlabuf;
287800a2430fSAndrzej Pietrasiewicz 
287900a2430fSAndrzej Pietrasiewicz 	ENTER();
288000a2430fSAndrzej Pietrasiewicz 
288100a2430fSAndrzej Pietrasiewicz 	/* Has descriptors only for speeds gadget does not support */
288200a2430fSAndrzej Pietrasiewicz 	if (unlikely(!(full | high | super)))
288300a2430fSAndrzej Pietrasiewicz 		return -ENOTSUPP;
288400a2430fSAndrzej Pietrasiewicz 
288500a2430fSAndrzej Pietrasiewicz 	/* Allocate a single chunk, less management later on */
288600a2430fSAndrzej Pietrasiewicz 	vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
288700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!vlabuf))
288800a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
288900a2430fSAndrzej Pietrasiewicz 
289000a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
289100a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_name_avail =
289200a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_name);
289300a2430fSAndrzej Pietrasiewicz 	ffs->ms_os_descs_ext_prop_data_avail =
289400a2430fSAndrzej Pietrasiewicz 		vla_ptr(vlabuf, d, ext_prop_data);
289500a2430fSAndrzej Pietrasiewicz 
289600a2430fSAndrzej Pietrasiewicz 	/* Copy descriptors  */
289700a2430fSAndrzej Pietrasiewicz 	memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
289800a2430fSAndrzej Pietrasiewicz 	       ffs->raw_descs_length);
289900a2430fSAndrzej Pietrasiewicz 
290000a2430fSAndrzej Pietrasiewicz 	memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
29010015f915SDan Carpenter 	eps_ptr = vla_ptr(vlabuf, d, eps);
29020015f915SDan Carpenter 	for (i = 0; i < ffs->eps_count; i++)
29030015f915SDan Carpenter 		eps_ptr[i].num = -1;
290400a2430fSAndrzej Pietrasiewicz 
290500a2430fSAndrzej Pietrasiewicz 	/* Save pointers
290600a2430fSAndrzej Pietrasiewicz 	 * d_eps == vlabuf, func->eps used to kfree vlabuf later
290700a2430fSAndrzej Pietrasiewicz 	*/
290800a2430fSAndrzej Pietrasiewicz 	func->eps             = vla_ptr(vlabuf, d, eps);
290900a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = vla_ptr(vlabuf, d, inums);
291000a2430fSAndrzej Pietrasiewicz 
291100a2430fSAndrzej Pietrasiewicz 	/*
291200a2430fSAndrzej Pietrasiewicz 	 * Go through all the endpoint descriptors and allocate
291300a2430fSAndrzej Pietrasiewicz 	 * endpoints first, so that later we can rewrite the endpoint
291400a2430fSAndrzej Pietrasiewicz 	 * numbers without worrying that it may be described later on.
291500a2430fSAndrzej Pietrasiewicz 	 */
291600a2430fSAndrzej Pietrasiewicz 	if (likely(full)) {
291700a2430fSAndrzej Pietrasiewicz 		func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
291800a2430fSAndrzej Pietrasiewicz 		fs_len = ffs_do_descs(ffs->fs_descs_count,
291900a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs),
292000a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz,
292100a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
292200a2430fSAndrzej Pietrasiewicz 		if (unlikely(fs_len < 0)) {
292300a2430fSAndrzej Pietrasiewicz 			ret = fs_len;
292400a2430fSAndrzej Pietrasiewicz 			goto error;
292500a2430fSAndrzej Pietrasiewicz 		}
292600a2430fSAndrzej Pietrasiewicz 	} else {
292700a2430fSAndrzej Pietrasiewicz 		fs_len = 0;
292800a2430fSAndrzej Pietrasiewicz 	}
292900a2430fSAndrzej Pietrasiewicz 
293000a2430fSAndrzej Pietrasiewicz 	if (likely(high)) {
293100a2430fSAndrzej Pietrasiewicz 		func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
293200a2430fSAndrzej Pietrasiewicz 		hs_len = ffs_do_descs(ffs->hs_descs_count,
293300a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) + fs_len,
293400a2430fSAndrzej Pietrasiewicz 				      d_raw_descs__sz - fs_len,
293500a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_descs, func);
293600a2430fSAndrzej Pietrasiewicz 		if (unlikely(hs_len < 0)) {
293700a2430fSAndrzej Pietrasiewicz 			ret = hs_len;
293800a2430fSAndrzej Pietrasiewicz 			goto error;
293900a2430fSAndrzej Pietrasiewicz 		}
294000a2430fSAndrzej Pietrasiewicz 	} else {
294100a2430fSAndrzej Pietrasiewicz 		hs_len = 0;
294200a2430fSAndrzej Pietrasiewicz 	}
294300a2430fSAndrzej Pietrasiewicz 
294400a2430fSAndrzej Pietrasiewicz 	if (likely(super)) {
294500a2430fSAndrzej Pietrasiewicz 		func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
294600a2430fSAndrzej Pietrasiewicz 		ss_len = ffs_do_descs(ffs->ss_descs_count,
294700a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
294800a2430fSAndrzej Pietrasiewicz 				d_raw_descs__sz - fs_len - hs_len,
294900a2430fSAndrzej Pietrasiewicz 				__ffs_func_bind_do_descs, func);
295000a2430fSAndrzej Pietrasiewicz 		if (unlikely(ss_len < 0)) {
295100a2430fSAndrzej Pietrasiewicz 			ret = ss_len;
295200a2430fSAndrzej Pietrasiewicz 			goto error;
295300a2430fSAndrzej Pietrasiewicz 		}
295400a2430fSAndrzej Pietrasiewicz 	} else {
295500a2430fSAndrzej Pietrasiewicz 		ss_len = 0;
295600a2430fSAndrzej Pietrasiewicz 	}
295700a2430fSAndrzej Pietrasiewicz 
295800a2430fSAndrzej Pietrasiewicz 	/*
295900a2430fSAndrzej Pietrasiewicz 	 * Now handle interface numbers allocation and interface and
296000a2430fSAndrzej Pietrasiewicz 	 * endpoint numbers rewriting.  We can do that in one go
296100a2430fSAndrzej Pietrasiewicz 	 * now.
296200a2430fSAndrzej Pietrasiewicz 	 */
296300a2430fSAndrzej Pietrasiewicz 	ret = ffs_do_descs(ffs->fs_descs_count +
296400a2430fSAndrzej Pietrasiewicz 			   (high ? ffs->hs_descs_count : 0) +
296500a2430fSAndrzej Pietrasiewicz 			   (super ? ffs->ss_descs_count : 0),
296600a2430fSAndrzej Pietrasiewicz 			   vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
296700a2430fSAndrzej Pietrasiewicz 			   __ffs_func_bind_do_nums, func);
296800a2430fSAndrzej Pietrasiewicz 	if (unlikely(ret < 0))
296900a2430fSAndrzej Pietrasiewicz 		goto error;
297000a2430fSAndrzej Pietrasiewicz 
297100a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
2972c6010c8bSJim Lin 	if (c->cdev->use_os_string) {
297300a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < ffs->interfaces_count; ++i) {
297400a2430fSAndrzej Pietrasiewicz 			struct usb_os_desc *desc;
297500a2430fSAndrzej Pietrasiewicz 
297600a2430fSAndrzej Pietrasiewicz 			desc = func->function.os_desc_table[i].os_desc =
297700a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, os_desc) +
297800a2430fSAndrzej Pietrasiewicz 				i * sizeof(struct usb_os_desc);
297900a2430fSAndrzej Pietrasiewicz 			desc->ext_compat_id =
298000a2430fSAndrzej Pietrasiewicz 				vla_ptr(vlabuf, d, ext_compat) + i * 16;
298100a2430fSAndrzej Pietrasiewicz 			INIT_LIST_HEAD(&desc->ext_prop);
298200a2430fSAndrzej Pietrasiewicz 		}
298300a2430fSAndrzej Pietrasiewicz 		ret = ffs_do_os_descs(ffs->ms_os_descs_count,
298400a2430fSAndrzej Pietrasiewicz 				      vla_ptr(vlabuf, d, raw_descs) +
298500a2430fSAndrzej Pietrasiewicz 				      fs_len + hs_len + ss_len,
2986c6010c8bSJim Lin 				      d_raw_descs__sz - fs_len - hs_len -
2987c6010c8bSJim Lin 				      ss_len,
298800a2430fSAndrzej Pietrasiewicz 				      __ffs_func_bind_do_os_desc, func);
298900a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
299000a2430fSAndrzej Pietrasiewicz 			goto error;
2991c6010c8bSJim Lin 	}
299200a2430fSAndrzej Pietrasiewicz 	func->function.os_desc_n =
299300a2430fSAndrzej Pietrasiewicz 		c->cdev->use_os_string ? ffs->interfaces_count : 0;
299400a2430fSAndrzej Pietrasiewicz 
299500a2430fSAndrzej Pietrasiewicz 	/* And we're done */
299600a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_BIND);
299700a2430fSAndrzej Pietrasiewicz 	return 0;
299800a2430fSAndrzej Pietrasiewicz 
299900a2430fSAndrzej Pietrasiewicz error:
300000a2430fSAndrzej Pietrasiewicz 	/* XXX Do we need to release all claimed endpoints here? */
300100a2430fSAndrzej Pietrasiewicz 	return ret;
300200a2430fSAndrzej Pietrasiewicz }
300300a2430fSAndrzej Pietrasiewicz 
300400a2430fSAndrzej Pietrasiewicz static int ffs_func_bind(struct usb_configuration *c,
300500a2430fSAndrzej Pietrasiewicz 			 struct usb_function *f)
300600a2430fSAndrzej Pietrasiewicz {
300700a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
300855d81121SRobert Baldyga 	struct ffs_function *func = ffs_func_from_usb(f);
300955d81121SRobert Baldyga 	int ret;
301000a2430fSAndrzej Pietrasiewicz 
301100a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(ffs_opts))
301200a2430fSAndrzej Pietrasiewicz 		return PTR_ERR(ffs_opts);
301300a2430fSAndrzej Pietrasiewicz 
301455d81121SRobert Baldyga 	ret = _ffs_func_bind(c, f);
301555d81121SRobert Baldyga 	if (ret && !--ffs_opts->refcnt)
301655d81121SRobert Baldyga 		functionfs_unbind(func->ffs);
301755d81121SRobert Baldyga 
301855d81121SRobert Baldyga 	return ret;
301900a2430fSAndrzej Pietrasiewicz }
302000a2430fSAndrzej Pietrasiewicz 
302100a2430fSAndrzej Pietrasiewicz 
302200a2430fSAndrzej Pietrasiewicz /* Other USB function hooks *************************************************/
302300a2430fSAndrzej Pietrasiewicz 
302418d6b32fSRobert Baldyga static void ffs_reset_work(struct work_struct *work)
302518d6b32fSRobert Baldyga {
302618d6b32fSRobert Baldyga 	struct ffs_data *ffs = container_of(work,
302718d6b32fSRobert Baldyga 		struct ffs_data, reset_work);
302818d6b32fSRobert Baldyga 	ffs_data_reset(ffs);
302918d6b32fSRobert Baldyga }
303018d6b32fSRobert Baldyga 
303100a2430fSAndrzej Pietrasiewicz static int ffs_func_set_alt(struct usb_function *f,
303200a2430fSAndrzej Pietrasiewicz 			    unsigned interface, unsigned alt)
303300a2430fSAndrzej Pietrasiewicz {
303400a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
303500a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
303600a2430fSAndrzej Pietrasiewicz 	int ret = 0, intf;
303700a2430fSAndrzej Pietrasiewicz 
303800a2430fSAndrzej Pietrasiewicz 	if (alt != (unsigned)-1) {
303900a2430fSAndrzej Pietrasiewicz 		intf = ffs_func_revmap_intf(func, interface);
304000a2430fSAndrzej Pietrasiewicz 		if (unlikely(intf < 0))
304100a2430fSAndrzej Pietrasiewicz 			return intf;
304200a2430fSAndrzej Pietrasiewicz 	}
304300a2430fSAndrzej Pietrasiewicz 
304400a2430fSAndrzej Pietrasiewicz 	if (ffs->func)
304500a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(ffs->func);
304600a2430fSAndrzej Pietrasiewicz 
304718d6b32fSRobert Baldyga 	if (ffs->state == FFS_DEACTIVATED) {
304818d6b32fSRobert Baldyga 		ffs->state = FFS_CLOSING;
304918d6b32fSRobert Baldyga 		INIT_WORK(&ffs->reset_work, ffs_reset_work);
305018d6b32fSRobert Baldyga 		schedule_work(&ffs->reset_work);
305118d6b32fSRobert Baldyga 		return -ENODEV;
305218d6b32fSRobert Baldyga 	}
305318d6b32fSRobert Baldyga 
305400a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
305500a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
305600a2430fSAndrzej Pietrasiewicz 
305700a2430fSAndrzej Pietrasiewicz 	if (alt == (unsigned)-1) {
305800a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
305900a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_DISABLE);
306000a2430fSAndrzej Pietrasiewicz 		return 0;
306100a2430fSAndrzej Pietrasiewicz 	}
306200a2430fSAndrzej Pietrasiewicz 
306300a2430fSAndrzej Pietrasiewicz 	ffs->func = func;
306400a2430fSAndrzej Pietrasiewicz 	ret = ffs_func_eps_enable(func);
306500a2430fSAndrzej Pietrasiewicz 	if (likely(ret >= 0))
306600a2430fSAndrzej Pietrasiewicz 		ffs_event_add(ffs, FUNCTIONFS_ENABLE);
306700a2430fSAndrzej Pietrasiewicz 	return ret;
306800a2430fSAndrzej Pietrasiewicz }
306900a2430fSAndrzej Pietrasiewicz 
307000a2430fSAndrzej Pietrasiewicz static void ffs_func_disable(struct usb_function *f)
307100a2430fSAndrzej Pietrasiewicz {
307200a2430fSAndrzej Pietrasiewicz 	ffs_func_set_alt(f, 0, (unsigned)-1);
307300a2430fSAndrzej Pietrasiewicz }
307400a2430fSAndrzej Pietrasiewicz 
307500a2430fSAndrzej Pietrasiewicz static int ffs_func_setup(struct usb_function *f,
307600a2430fSAndrzej Pietrasiewicz 			  const struct usb_ctrlrequest *creq)
307700a2430fSAndrzej Pietrasiewicz {
307800a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
307900a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
308000a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
308100a2430fSAndrzej Pietrasiewicz 	int ret;
308200a2430fSAndrzej Pietrasiewicz 
308300a2430fSAndrzej Pietrasiewicz 	ENTER();
308400a2430fSAndrzej Pietrasiewicz 
308500a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
308600a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->bRequest     = %02x\n", creq->bRequest);
308700a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wValue       = %04x\n", le16_to_cpu(creq->wValue));
308800a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wIndex       = %04x\n", le16_to_cpu(creq->wIndex));
308900a2430fSAndrzej Pietrasiewicz 	pr_vdebug("creq->wLength      = %04x\n", le16_to_cpu(creq->wLength));
309000a2430fSAndrzej Pietrasiewicz 
309100a2430fSAndrzej Pietrasiewicz 	/*
309200a2430fSAndrzej Pietrasiewicz 	 * Most requests directed to interface go through here
309300a2430fSAndrzej Pietrasiewicz 	 * (notable exceptions are set/get interface) so we need to
309400a2430fSAndrzej Pietrasiewicz 	 * handle them.  All other either handled by composite or
309500a2430fSAndrzej Pietrasiewicz 	 * passed to usb_configuration->setup() (if one is set).  No
309600a2430fSAndrzej Pietrasiewicz 	 * matter, we will handle requests directed to endpoint here
309700a2430fSAndrzej Pietrasiewicz 	 * as well (as it's straightforward) but what to do with any
309800a2430fSAndrzej Pietrasiewicz 	 * other request?
309900a2430fSAndrzej Pietrasiewicz 	 */
310000a2430fSAndrzej Pietrasiewicz 	if (ffs->state != FFS_ACTIVE)
310100a2430fSAndrzej Pietrasiewicz 		return -ENODEV;
310200a2430fSAndrzej Pietrasiewicz 
310300a2430fSAndrzej Pietrasiewicz 	switch (creq->bRequestType & USB_RECIP_MASK) {
310400a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_INTERFACE:
310500a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
310600a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
310700a2430fSAndrzej Pietrasiewicz 			return ret;
310800a2430fSAndrzej Pietrasiewicz 		break;
310900a2430fSAndrzej Pietrasiewicz 
311000a2430fSAndrzej Pietrasiewicz 	case USB_RECIP_ENDPOINT:
311100a2430fSAndrzej Pietrasiewicz 		ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
311200a2430fSAndrzej Pietrasiewicz 		if (unlikely(ret < 0))
311300a2430fSAndrzej Pietrasiewicz 			return ret;
31141b0bf88fSRobert Baldyga 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
31151b0bf88fSRobert Baldyga 			ret = func->ffs->eps_addrmap[ret];
311600a2430fSAndrzej Pietrasiewicz 		break;
311700a2430fSAndrzej Pietrasiewicz 
311800a2430fSAndrzej Pietrasiewicz 	default:
311900a2430fSAndrzej Pietrasiewicz 		return -EOPNOTSUPP;
312000a2430fSAndrzej Pietrasiewicz 	}
312100a2430fSAndrzej Pietrasiewicz 
312200a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
312300a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup = *creq;
312400a2430fSAndrzej Pietrasiewicz 	ffs->ev.setup.wIndex = cpu_to_le16(ret);
312500a2430fSAndrzej Pietrasiewicz 	__ffs_event_add(ffs, FUNCTIONFS_SETUP);
312600a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
312700a2430fSAndrzej Pietrasiewicz 
312800a2430fSAndrzej Pietrasiewicz 	return 0;
312900a2430fSAndrzej Pietrasiewicz }
313000a2430fSAndrzej Pietrasiewicz 
313100a2430fSAndrzej Pietrasiewicz static void ffs_func_suspend(struct usb_function *f)
313200a2430fSAndrzej Pietrasiewicz {
313300a2430fSAndrzej Pietrasiewicz 	ENTER();
313400a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
313500a2430fSAndrzej Pietrasiewicz }
313600a2430fSAndrzej Pietrasiewicz 
313700a2430fSAndrzej Pietrasiewicz static void ffs_func_resume(struct usb_function *f)
313800a2430fSAndrzej Pietrasiewicz {
313900a2430fSAndrzej Pietrasiewicz 	ENTER();
314000a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
314100a2430fSAndrzej Pietrasiewicz }
314200a2430fSAndrzej Pietrasiewicz 
314300a2430fSAndrzej Pietrasiewicz 
314400a2430fSAndrzej Pietrasiewicz /* Endpoint and interface numbers reverse mapping ***************************/
314500a2430fSAndrzej Pietrasiewicz 
314600a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
314700a2430fSAndrzej Pietrasiewicz {
314800a2430fSAndrzej Pietrasiewicz 	num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
314900a2430fSAndrzej Pietrasiewicz 	return num ? num : -EDOM;
315000a2430fSAndrzej Pietrasiewicz }
315100a2430fSAndrzej Pietrasiewicz 
315200a2430fSAndrzej Pietrasiewicz static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
315300a2430fSAndrzej Pietrasiewicz {
315400a2430fSAndrzej Pietrasiewicz 	short *nums = func->interfaces_nums;
315500a2430fSAndrzej Pietrasiewicz 	unsigned count = func->ffs->interfaces_count;
315600a2430fSAndrzej Pietrasiewicz 
315700a2430fSAndrzej Pietrasiewicz 	for (; count; --count, ++nums) {
315800a2430fSAndrzej Pietrasiewicz 		if (*nums >= 0 && *nums == intf)
315900a2430fSAndrzej Pietrasiewicz 			return nums - func->interfaces_nums;
316000a2430fSAndrzej Pietrasiewicz 	}
316100a2430fSAndrzej Pietrasiewicz 
316200a2430fSAndrzej Pietrasiewicz 	return -EDOM;
316300a2430fSAndrzej Pietrasiewicz }
316400a2430fSAndrzej Pietrasiewicz 
316500a2430fSAndrzej Pietrasiewicz 
316600a2430fSAndrzej Pietrasiewicz /* Devices management *******************************************************/
316700a2430fSAndrzej Pietrasiewicz 
316800a2430fSAndrzej Pietrasiewicz static LIST_HEAD(ffs_devices);
316900a2430fSAndrzej Pietrasiewicz 
317000a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_do_find_dev(const char *name)
317100a2430fSAndrzej Pietrasiewicz {
317200a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
317300a2430fSAndrzej Pietrasiewicz 
317400a2430fSAndrzej Pietrasiewicz 	list_for_each_entry(dev, &ffs_devices, entry) {
317500a2430fSAndrzej Pietrasiewicz 		if (!dev->name || !name)
317600a2430fSAndrzej Pietrasiewicz 			continue;
317700a2430fSAndrzej Pietrasiewicz 		if (strcmp(dev->name, name) == 0)
317800a2430fSAndrzej Pietrasiewicz 			return dev;
317900a2430fSAndrzej Pietrasiewicz 	}
318000a2430fSAndrzej Pietrasiewicz 
318100a2430fSAndrzej Pietrasiewicz 	return NULL;
318200a2430fSAndrzej Pietrasiewicz }
318300a2430fSAndrzej Pietrasiewicz 
318400a2430fSAndrzej Pietrasiewicz /*
318500a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
318600a2430fSAndrzej Pietrasiewicz  */
318700a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_get_single_dev(void)
318800a2430fSAndrzej Pietrasiewicz {
318900a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
319000a2430fSAndrzej Pietrasiewicz 
319100a2430fSAndrzej Pietrasiewicz 	if (list_is_singular(&ffs_devices)) {
319200a2430fSAndrzej Pietrasiewicz 		dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
319300a2430fSAndrzej Pietrasiewicz 		if (dev->single)
319400a2430fSAndrzej Pietrasiewicz 			return dev;
319500a2430fSAndrzej Pietrasiewicz 	}
319600a2430fSAndrzej Pietrasiewicz 
319700a2430fSAndrzej Pietrasiewicz 	return NULL;
319800a2430fSAndrzej Pietrasiewicz }
319900a2430fSAndrzej Pietrasiewicz 
320000a2430fSAndrzej Pietrasiewicz /*
320100a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
320200a2430fSAndrzej Pietrasiewicz  */
320300a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_find_dev(const char *name)
320400a2430fSAndrzej Pietrasiewicz {
320500a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
320600a2430fSAndrzej Pietrasiewicz 
320700a2430fSAndrzej Pietrasiewicz 	dev = _ffs_get_single_dev();
320800a2430fSAndrzej Pietrasiewicz 	if (dev)
320900a2430fSAndrzej Pietrasiewicz 		return dev;
321000a2430fSAndrzej Pietrasiewicz 
321100a2430fSAndrzej Pietrasiewicz 	return _ffs_do_find_dev(name);
321200a2430fSAndrzej Pietrasiewicz }
321300a2430fSAndrzej Pietrasiewicz 
321400a2430fSAndrzej Pietrasiewicz /* Configfs support *********************************************************/
321500a2430fSAndrzej Pietrasiewicz 
321600a2430fSAndrzej Pietrasiewicz static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
321700a2430fSAndrzej Pietrasiewicz {
321800a2430fSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_fs_opts,
321900a2430fSAndrzej Pietrasiewicz 			    func_inst.group);
322000a2430fSAndrzej Pietrasiewicz }
322100a2430fSAndrzej Pietrasiewicz 
322200a2430fSAndrzej Pietrasiewicz static void ffs_attr_release(struct config_item *item)
322300a2430fSAndrzej Pietrasiewicz {
322400a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts = to_ffs_opts(item);
322500a2430fSAndrzej Pietrasiewicz 
322600a2430fSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
322700a2430fSAndrzej Pietrasiewicz }
322800a2430fSAndrzej Pietrasiewicz 
322900a2430fSAndrzej Pietrasiewicz static struct configfs_item_operations ffs_item_ops = {
323000a2430fSAndrzej Pietrasiewicz 	.release	= ffs_attr_release,
323100a2430fSAndrzej Pietrasiewicz };
323200a2430fSAndrzej Pietrasiewicz 
323300a2430fSAndrzej Pietrasiewicz static struct config_item_type ffs_func_type = {
323400a2430fSAndrzej Pietrasiewicz 	.ct_item_ops	= &ffs_item_ops,
323500a2430fSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
323600a2430fSAndrzej Pietrasiewicz };
323700a2430fSAndrzej Pietrasiewicz 
323800a2430fSAndrzej Pietrasiewicz 
323900a2430fSAndrzej Pietrasiewicz /* Function registration interface ******************************************/
324000a2430fSAndrzej Pietrasiewicz 
324100a2430fSAndrzej Pietrasiewicz static void ffs_free_inst(struct usb_function_instance *f)
324200a2430fSAndrzej Pietrasiewicz {
324300a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
324400a2430fSAndrzej Pietrasiewicz 
324500a2430fSAndrzej Pietrasiewicz 	opts = to_f_fs_opts(f);
324600a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
324700a2430fSAndrzej Pietrasiewicz 	_ffs_free_dev(opts->dev);
324800a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
324900a2430fSAndrzej Pietrasiewicz 	kfree(opts);
325000a2430fSAndrzej Pietrasiewicz }
325100a2430fSAndrzej Pietrasiewicz 
325200a2430fSAndrzej Pietrasiewicz #define MAX_INST_NAME_LEN	40
325300a2430fSAndrzej Pietrasiewicz 
325400a2430fSAndrzej Pietrasiewicz static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
325500a2430fSAndrzej Pietrasiewicz {
325600a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
325700a2430fSAndrzej Pietrasiewicz 	char *ptr;
325800a2430fSAndrzej Pietrasiewicz 	const char *tmp;
325900a2430fSAndrzej Pietrasiewicz 	int name_len, ret;
326000a2430fSAndrzej Pietrasiewicz 
326100a2430fSAndrzej Pietrasiewicz 	name_len = strlen(name) + 1;
326200a2430fSAndrzej Pietrasiewicz 	if (name_len > MAX_INST_NAME_LEN)
326300a2430fSAndrzej Pietrasiewicz 		return -ENAMETOOLONG;
326400a2430fSAndrzej Pietrasiewicz 
326500a2430fSAndrzej Pietrasiewicz 	ptr = kstrndup(name, name_len, GFP_KERNEL);
326600a2430fSAndrzej Pietrasiewicz 	if (!ptr)
326700a2430fSAndrzej Pietrasiewicz 		return -ENOMEM;
326800a2430fSAndrzej Pietrasiewicz 
326900a2430fSAndrzej Pietrasiewicz 	opts = to_f_fs_opts(fi);
327000a2430fSAndrzej Pietrasiewicz 	tmp = NULL;
327100a2430fSAndrzej Pietrasiewicz 
327200a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
327300a2430fSAndrzej Pietrasiewicz 
327400a2430fSAndrzej Pietrasiewicz 	tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
327500a2430fSAndrzej Pietrasiewicz 	ret = _ffs_name_dev(opts->dev, ptr);
327600a2430fSAndrzej Pietrasiewicz 	if (ret) {
327700a2430fSAndrzej Pietrasiewicz 		kfree(ptr);
327800a2430fSAndrzej Pietrasiewicz 		ffs_dev_unlock();
327900a2430fSAndrzej Pietrasiewicz 		return ret;
328000a2430fSAndrzej Pietrasiewicz 	}
328100a2430fSAndrzej Pietrasiewicz 	opts->dev->name_allocated = true;
328200a2430fSAndrzej Pietrasiewicz 
328300a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
328400a2430fSAndrzej Pietrasiewicz 
328500a2430fSAndrzej Pietrasiewicz 	kfree(tmp);
328600a2430fSAndrzej Pietrasiewicz 
328700a2430fSAndrzej Pietrasiewicz 	return 0;
328800a2430fSAndrzej Pietrasiewicz }
328900a2430fSAndrzej Pietrasiewicz 
329000a2430fSAndrzej Pietrasiewicz static struct usb_function_instance *ffs_alloc_inst(void)
329100a2430fSAndrzej Pietrasiewicz {
329200a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts;
329300a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
329400a2430fSAndrzej Pietrasiewicz 
329500a2430fSAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
329600a2430fSAndrzej Pietrasiewicz 	if (!opts)
329700a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
329800a2430fSAndrzej Pietrasiewicz 
329900a2430fSAndrzej Pietrasiewicz 	opts->func_inst.set_inst_name = ffs_set_inst_name;
330000a2430fSAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = ffs_free_inst;
330100a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
330200a2430fSAndrzej Pietrasiewicz 	dev = _ffs_alloc_dev();
330300a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
330400a2430fSAndrzej Pietrasiewicz 	if (IS_ERR(dev)) {
330500a2430fSAndrzej Pietrasiewicz 		kfree(opts);
330600a2430fSAndrzej Pietrasiewicz 		return ERR_CAST(dev);
330700a2430fSAndrzej Pietrasiewicz 	}
330800a2430fSAndrzej Pietrasiewicz 	opts->dev = dev;
330900a2430fSAndrzej Pietrasiewicz 	dev->opts = opts;
331000a2430fSAndrzej Pietrasiewicz 
331100a2430fSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "",
331200a2430fSAndrzej Pietrasiewicz 				    &ffs_func_type);
331300a2430fSAndrzej Pietrasiewicz 	return &opts->func_inst;
331400a2430fSAndrzej Pietrasiewicz }
331500a2430fSAndrzej Pietrasiewicz 
331600a2430fSAndrzej Pietrasiewicz static void ffs_free(struct usb_function *f)
331700a2430fSAndrzej Pietrasiewicz {
331800a2430fSAndrzej Pietrasiewicz 	kfree(ffs_func_from_usb(f));
331900a2430fSAndrzej Pietrasiewicz }
332000a2430fSAndrzej Pietrasiewicz 
332100a2430fSAndrzej Pietrasiewicz static void ffs_func_unbind(struct usb_configuration *c,
332200a2430fSAndrzej Pietrasiewicz 			    struct usb_function *f)
332300a2430fSAndrzej Pietrasiewicz {
332400a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func = ffs_func_from_usb(f);
332500a2430fSAndrzej Pietrasiewicz 	struct ffs_data *ffs = func->ffs;
332600a2430fSAndrzej Pietrasiewicz 	struct f_fs_opts *opts =
332700a2430fSAndrzej Pietrasiewicz 		container_of(f->fi, struct f_fs_opts, func_inst);
332800a2430fSAndrzej Pietrasiewicz 	struct ffs_ep *ep = func->eps;
332900a2430fSAndrzej Pietrasiewicz 	unsigned count = ffs->eps_count;
333000a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
333100a2430fSAndrzej Pietrasiewicz 
333200a2430fSAndrzej Pietrasiewicz 	ENTER();
333300a2430fSAndrzej Pietrasiewicz 	if (ffs->func == func) {
333400a2430fSAndrzej Pietrasiewicz 		ffs_func_eps_disable(func);
333500a2430fSAndrzej Pietrasiewicz 		ffs->func = NULL;
333600a2430fSAndrzej Pietrasiewicz 	}
333700a2430fSAndrzej Pietrasiewicz 
333800a2430fSAndrzej Pietrasiewicz 	if (!--opts->refcnt)
333900a2430fSAndrzej Pietrasiewicz 		functionfs_unbind(ffs);
334000a2430fSAndrzej Pietrasiewicz 
334100a2430fSAndrzej Pietrasiewicz 	/* cleanup after autoconfig */
334200a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
334300a2430fSAndrzej Pietrasiewicz 	do {
334400a2430fSAndrzej Pietrasiewicz 		if (ep->ep && ep->req)
334500a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(ep->ep, ep->req);
334600a2430fSAndrzej Pietrasiewicz 		ep->req = NULL;
334700a2430fSAndrzej Pietrasiewicz 		++ep;
334800a2430fSAndrzej Pietrasiewicz 	} while (--count);
334900a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
335000a2430fSAndrzej Pietrasiewicz 	kfree(func->eps);
335100a2430fSAndrzej Pietrasiewicz 	func->eps = NULL;
335200a2430fSAndrzej Pietrasiewicz 	/*
335300a2430fSAndrzej Pietrasiewicz 	 * eps, descriptors and interfaces_nums are allocated in the
335400a2430fSAndrzej Pietrasiewicz 	 * same chunk so only one free is required.
335500a2430fSAndrzej Pietrasiewicz 	 */
335600a2430fSAndrzej Pietrasiewicz 	func->function.fs_descriptors = NULL;
335700a2430fSAndrzej Pietrasiewicz 	func->function.hs_descriptors = NULL;
335800a2430fSAndrzej Pietrasiewicz 	func->function.ss_descriptors = NULL;
335900a2430fSAndrzej Pietrasiewicz 	func->interfaces_nums = NULL;
336000a2430fSAndrzej Pietrasiewicz 
336100a2430fSAndrzej Pietrasiewicz 	ffs_event_add(ffs, FUNCTIONFS_UNBIND);
336200a2430fSAndrzej Pietrasiewicz }
336300a2430fSAndrzej Pietrasiewicz 
336400a2430fSAndrzej Pietrasiewicz static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
336500a2430fSAndrzej Pietrasiewicz {
336600a2430fSAndrzej Pietrasiewicz 	struct ffs_function *func;
336700a2430fSAndrzej Pietrasiewicz 
336800a2430fSAndrzej Pietrasiewicz 	ENTER();
336900a2430fSAndrzej Pietrasiewicz 
337000a2430fSAndrzej Pietrasiewicz 	func = kzalloc(sizeof(*func), GFP_KERNEL);
337100a2430fSAndrzej Pietrasiewicz 	if (unlikely(!func))
337200a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
337300a2430fSAndrzej Pietrasiewicz 
337400a2430fSAndrzej Pietrasiewicz 	func->function.name    = "Function FS Gadget";
337500a2430fSAndrzej Pietrasiewicz 
337600a2430fSAndrzej Pietrasiewicz 	func->function.bind    = ffs_func_bind;
337700a2430fSAndrzej Pietrasiewicz 	func->function.unbind  = ffs_func_unbind;
337800a2430fSAndrzej Pietrasiewicz 	func->function.set_alt = ffs_func_set_alt;
337900a2430fSAndrzej Pietrasiewicz 	func->function.disable = ffs_func_disable;
338000a2430fSAndrzej Pietrasiewicz 	func->function.setup   = ffs_func_setup;
338100a2430fSAndrzej Pietrasiewicz 	func->function.suspend = ffs_func_suspend;
338200a2430fSAndrzej Pietrasiewicz 	func->function.resume  = ffs_func_resume;
338300a2430fSAndrzej Pietrasiewicz 	func->function.free_func = ffs_free;
338400a2430fSAndrzej Pietrasiewicz 
338500a2430fSAndrzej Pietrasiewicz 	return &func->function;
338600a2430fSAndrzej Pietrasiewicz }
338700a2430fSAndrzej Pietrasiewicz 
338800a2430fSAndrzej Pietrasiewicz /*
338900a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
339000a2430fSAndrzej Pietrasiewicz  */
339100a2430fSAndrzej Pietrasiewicz static struct ffs_dev *_ffs_alloc_dev(void)
339200a2430fSAndrzej Pietrasiewicz {
339300a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *dev;
339400a2430fSAndrzej Pietrasiewicz 	int ret;
339500a2430fSAndrzej Pietrasiewicz 
339600a2430fSAndrzej Pietrasiewicz 	if (_ffs_get_single_dev())
339700a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(-EBUSY);
339800a2430fSAndrzej Pietrasiewicz 
339900a2430fSAndrzej Pietrasiewicz 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
340000a2430fSAndrzej Pietrasiewicz 	if (!dev)
340100a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
340200a2430fSAndrzej Pietrasiewicz 
340300a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices)) {
340400a2430fSAndrzej Pietrasiewicz 		ret = functionfs_init();
340500a2430fSAndrzej Pietrasiewicz 		if (ret) {
340600a2430fSAndrzej Pietrasiewicz 			kfree(dev);
340700a2430fSAndrzej Pietrasiewicz 			return ERR_PTR(ret);
340800a2430fSAndrzej Pietrasiewicz 		}
340900a2430fSAndrzej Pietrasiewicz 	}
341000a2430fSAndrzej Pietrasiewicz 
341100a2430fSAndrzej Pietrasiewicz 	list_add(&dev->entry, &ffs_devices);
341200a2430fSAndrzej Pietrasiewicz 
341300a2430fSAndrzej Pietrasiewicz 	return dev;
341400a2430fSAndrzej Pietrasiewicz }
341500a2430fSAndrzej Pietrasiewicz 
341600a2430fSAndrzej Pietrasiewicz /*
341700a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
341800a2430fSAndrzej Pietrasiewicz  * The caller is responsible for "name" being available whenever f_fs needs it
341900a2430fSAndrzej Pietrasiewicz  */
342000a2430fSAndrzej Pietrasiewicz static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
342100a2430fSAndrzej Pietrasiewicz {
342200a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *existing;
342300a2430fSAndrzej Pietrasiewicz 
342400a2430fSAndrzej Pietrasiewicz 	existing = _ffs_do_find_dev(name);
342500a2430fSAndrzej Pietrasiewicz 	if (existing)
342600a2430fSAndrzej Pietrasiewicz 		return -EBUSY;
342700a2430fSAndrzej Pietrasiewicz 
342800a2430fSAndrzej Pietrasiewicz 	dev->name = name;
342900a2430fSAndrzej Pietrasiewicz 
343000a2430fSAndrzej Pietrasiewicz 	return 0;
343100a2430fSAndrzej Pietrasiewicz }
343200a2430fSAndrzej Pietrasiewicz 
343300a2430fSAndrzej Pietrasiewicz /*
343400a2430fSAndrzej Pietrasiewicz  * The caller is responsible for "name" being available whenever f_fs needs it
343500a2430fSAndrzej Pietrasiewicz  */
343600a2430fSAndrzej Pietrasiewicz int ffs_name_dev(struct ffs_dev *dev, const char *name)
343700a2430fSAndrzej Pietrasiewicz {
343800a2430fSAndrzej Pietrasiewicz 	int ret;
343900a2430fSAndrzej Pietrasiewicz 
344000a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
344100a2430fSAndrzej Pietrasiewicz 	ret = _ffs_name_dev(dev, name);
344200a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
344300a2430fSAndrzej Pietrasiewicz 
344400a2430fSAndrzej Pietrasiewicz 	return ret;
344500a2430fSAndrzej Pietrasiewicz }
344600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_name_dev);
344700a2430fSAndrzej Pietrasiewicz 
344800a2430fSAndrzej Pietrasiewicz int ffs_single_dev(struct ffs_dev *dev)
344900a2430fSAndrzej Pietrasiewicz {
345000a2430fSAndrzej Pietrasiewicz 	int ret;
345100a2430fSAndrzej Pietrasiewicz 
345200a2430fSAndrzej Pietrasiewicz 	ret = 0;
345300a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
345400a2430fSAndrzej Pietrasiewicz 
345500a2430fSAndrzej Pietrasiewicz 	if (!list_is_singular(&ffs_devices))
345600a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
345700a2430fSAndrzej Pietrasiewicz 	else
345800a2430fSAndrzej Pietrasiewicz 		dev->single = true;
345900a2430fSAndrzej Pietrasiewicz 
346000a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
346100a2430fSAndrzej Pietrasiewicz 	return ret;
346200a2430fSAndrzej Pietrasiewicz }
346300a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(ffs_single_dev);
346400a2430fSAndrzej Pietrasiewicz 
346500a2430fSAndrzej Pietrasiewicz /*
346600a2430fSAndrzej Pietrasiewicz  * ffs_lock must be taken by the caller of this function
346700a2430fSAndrzej Pietrasiewicz  */
346800a2430fSAndrzej Pietrasiewicz static void _ffs_free_dev(struct ffs_dev *dev)
346900a2430fSAndrzej Pietrasiewicz {
347000a2430fSAndrzej Pietrasiewicz 	list_del(&dev->entry);
347100a2430fSAndrzej Pietrasiewicz 	if (dev->name_allocated)
347200a2430fSAndrzej Pietrasiewicz 		kfree(dev->name);
347300a2430fSAndrzej Pietrasiewicz 	kfree(dev);
347400a2430fSAndrzej Pietrasiewicz 	if (list_empty(&ffs_devices))
347500a2430fSAndrzej Pietrasiewicz 		functionfs_cleanup();
347600a2430fSAndrzej Pietrasiewicz }
347700a2430fSAndrzej Pietrasiewicz 
347800a2430fSAndrzej Pietrasiewicz static void *ffs_acquire_dev(const char *dev_name)
347900a2430fSAndrzej Pietrasiewicz {
348000a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
348100a2430fSAndrzej Pietrasiewicz 
348200a2430fSAndrzej Pietrasiewicz 	ENTER();
348300a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
348400a2430fSAndrzej Pietrasiewicz 
348500a2430fSAndrzej Pietrasiewicz 	ffs_dev = _ffs_find_dev(dev_name);
348600a2430fSAndrzej Pietrasiewicz 	if (!ffs_dev)
348700a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
348800a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->mounted)
348900a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-EBUSY);
349000a2430fSAndrzej Pietrasiewicz 	else if (ffs_dev->ffs_acquire_dev_callback &&
349100a2430fSAndrzej Pietrasiewicz 	    ffs_dev->ffs_acquire_dev_callback(ffs_dev))
349200a2430fSAndrzej Pietrasiewicz 		ffs_dev = ERR_PTR(-ENOENT);
349300a2430fSAndrzej Pietrasiewicz 	else
349400a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = true;
349500a2430fSAndrzej Pietrasiewicz 
349600a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
349700a2430fSAndrzej Pietrasiewicz 	return ffs_dev;
349800a2430fSAndrzej Pietrasiewicz }
349900a2430fSAndrzej Pietrasiewicz 
350000a2430fSAndrzej Pietrasiewicz static void ffs_release_dev(struct ffs_data *ffs_data)
350100a2430fSAndrzej Pietrasiewicz {
350200a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_dev;
350300a2430fSAndrzej Pietrasiewicz 
350400a2430fSAndrzej Pietrasiewicz 	ENTER();
350500a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
350600a2430fSAndrzej Pietrasiewicz 
350700a2430fSAndrzej Pietrasiewicz 	ffs_dev = ffs_data->private_data;
350800a2430fSAndrzej Pietrasiewicz 	if (ffs_dev) {
350900a2430fSAndrzej Pietrasiewicz 		ffs_dev->mounted = false;
351000a2430fSAndrzej Pietrasiewicz 
351100a2430fSAndrzej Pietrasiewicz 		if (ffs_dev->ffs_release_dev_callback)
351200a2430fSAndrzej Pietrasiewicz 			ffs_dev->ffs_release_dev_callback(ffs_dev);
351300a2430fSAndrzej Pietrasiewicz 	}
351400a2430fSAndrzej Pietrasiewicz 
351500a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
351600a2430fSAndrzej Pietrasiewicz }
351700a2430fSAndrzej Pietrasiewicz 
351800a2430fSAndrzej Pietrasiewicz static int ffs_ready(struct ffs_data *ffs)
351900a2430fSAndrzej Pietrasiewicz {
352000a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
352100a2430fSAndrzej Pietrasiewicz 	int ret = 0;
352200a2430fSAndrzej Pietrasiewicz 
352300a2430fSAndrzej Pietrasiewicz 	ENTER();
352400a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
352500a2430fSAndrzej Pietrasiewicz 
352600a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
352700a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj) {
352800a2430fSAndrzej Pietrasiewicz 		ret = -EINVAL;
352900a2430fSAndrzej Pietrasiewicz 		goto done;
353000a2430fSAndrzej Pietrasiewicz 	}
353100a2430fSAndrzej Pietrasiewicz 	if (WARN_ON(ffs_obj->desc_ready)) {
353200a2430fSAndrzej Pietrasiewicz 		ret = -EBUSY;
353300a2430fSAndrzej Pietrasiewicz 		goto done;
353400a2430fSAndrzej Pietrasiewicz 	}
353500a2430fSAndrzej Pietrasiewicz 
353600a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = true;
353700a2430fSAndrzej Pietrasiewicz 	ffs_obj->ffs_data = ffs;
353800a2430fSAndrzej Pietrasiewicz 
353949a79d8bSKrzysztof Opasiak 	if (ffs_obj->ffs_ready_callback) {
354000a2430fSAndrzej Pietrasiewicz 		ret = ffs_obj->ffs_ready_callback(ffs);
354149a79d8bSKrzysztof Opasiak 		if (ret)
354249a79d8bSKrzysztof Opasiak 			goto done;
354349a79d8bSKrzysztof Opasiak 	}
354400a2430fSAndrzej Pietrasiewicz 
354549a79d8bSKrzysztof Opasiak 	set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
354600a2430fSAndrzej Pietrasiewicz done:
354700a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
354800a2430fSAndrzej Pietrasiewicz 	return ret;
354900a2430fSAndrzej Pietrasiewicz }
355000a2430fSAndrzej Pietrasiewicz 
355100a2430fSAndrzej Pietrasiewicz static void ffs_closed(struct ffs_data *ffs)
355200a2430fSAndrzej Pietrasiewicz {
355300a2430fSAndrzej Pietrasiewicz 	struct ffs_dev *ffs_obj;
3554f14e9ad1SRui Miguel Silva 	struct f_fs_opts *opts;
355500a2430fSAndrzej Pietrasiewicz 
355600a2430fSAndrzej Pietrasiewicz 	ENTER();
355700a2430fSAndrzej Pietrasiewicz 	ffs_dev_lock();
355800a2430fSAndrzej Pietrasiewicz 
355900a2430fSAndrzej Pietrasiewicz 	ffs_obj = ffs->private_data;
356000a2430fSAndrzej Pietrasiewicz 	if (!ffs_obj)
356100a2430fSAndrzej Pietrasiewicz 		goto done;
356200a2430fSAndrzej Pietrasiewicz 
356300a2430fSAndrzej Pietrasiewicz 	ffs_obj->desc_ready = false;
356400a2430fSAndrzej Pietrasiewicz 
356549a79d8bSKrzysztof Opasiak 	if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
356649a79d8bSKrzysztof Opasiak 	    ffs_obj->ffs_closed_callback)
356700a2430fSAndrzej Pietrasiewicz 		ffs_obj->ffs_closed_callback(ffs);
356800a2430fSAndrzej Pietrasiewicz 
3569f14e9ad1SRui Miguel Silva 	if (ffs_obj->opts)
3570f14e9ad1SRui Miguel Silva 		opts = ffs_obj->opts;
3571f14e9ad1SRui Miguel Silva 	else
3572f14e9ad1SRui Miguel Silva 		goto done;
3573f14e9ad1SRui Miguel Silva 
3574f14e9ad1SRui Miguel Silva 	if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3575f14e9ad1SRui Miguel Silva 	    || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
357600a2430fSAndrzej Pietrasiewicz 		goto done;
357700a2430fSAndrzej Pietrasiewicz 
357800a2430fSAndrzej Pietrasiewicz 	unregister_gadget_item(ffs_obj->opts->
357900a2430fSAndrzej Pietrasiewicz 			       func_inst.group.cg_item.ci_parent->ci_parent);
358000a2430fSAndrzej Pietrasiewicz done:
358100a2430fSAndrzej Pietrasiewicz 	ffs_dev_unlock();
358200a2430fSAndrzej Pietrasiewicz }
358300a2430fSAndrzej Pietrasiewicz 
358400a2430fSAndrzej Pietrasiewicz /* Misc helper functions ****************************************************/
358500a2430fSAndrzej Pietrasiewicz 
358600a2430fSAndrzej Pietrasiewicz static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
358700a2430fSAndrzej Pietrasiewicz {
358800a2430fSAndrzej Pietrasiewicz 	return nonblock
358900a2430fSAndrzej Pietrasiewicz 		? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
359000a2430fSAndrzej Pietrasiewicz 		: mutex_lock_interruptible(mutex);
359100a2430fSAndrzej Pietrasiewicz }
359200a2430fSAndrzej Pietrasiewicz 
359300a2430fSAndrzej Pietrasiewicz static char *ffs_prepare_buffer(const char __user *buf, size_t len)
359400a2430fSAndrzej Pietrasiewicz {
359500a2430fSAndrzej Pietrasiewicz 	char *data;
359600a2430fSAndrzej Pietrasiewicz 
359700a2430fSAndrzej Pietrasiewicz 	if (unlikely(!len))
359800a2430fSAndrzej Pietrasiewicz 		return NULL;
359900a2430fSAndrzej Pietrasiewicz 
360000a2430fSAndrzej Pietrasiewicz 	data = kmalloc(len, GFP_KERNEL);
360100a2430fSAndrzej Pietrasiewicz 	if (unlikely(!data))
360200a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
360300a2430fSAndrzej Pietrasiewicz 
36047fe9a937SDaniel Walter 	if (unlikely(copy_from_user(data, buf, len))) {
360500a2430fSAndrzej Pietrasiewicz 		kfree(data);
360600a2430fSAndrzej Pietrasiewicz 		return ERR_PTR(-EFAULT);
360700a2430fSAndrzej Pietrasiewicz 	}
360800a2430fSAndrzej Pietrasiewicz 
360900a2430fSAndrzej Pietrasiewicz 	pr_vdebug("Buffer from user space:\n");
361000a2430fSAndrzej Pietrasiewicz 	ffs_dump_mem("", data, len);
361100a2430fSAndrzej Pietrasiewicz 
361200a2430fSAndrzej Pietrasiewicz 	return data;
361300a2430fSAndrzej Pietrasiewicz }
361400a2430fSAndrzej Pietrasiewicz 
361500a2430fSAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
361600a2430fSAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
361700a2430fSAndrzej Pietrasiewicz MODULE_AUTHOR("Michal Nazarewicz");
3618