Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcf070ec authored by Linux Build Service Account's avatar Linux Build Service Account Committed by Gerrit - the friendly Code Review server
Browse files

Merge changes I3052bdee,I15011d79 into msm-4.14

* changes:
  USB: f_fs: Set ffs->func to NULL after disabling endpoint in set_alt()
  usb: gadget: f_fs: Add support for ipc logging
parents b818a91a 0f662386
Loading
Loading
Loading
Loading
+371 −3
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <linux/module.h>
#include <linux/sched/signal.h>
#include <linux/uio.h>
#include <linux/ipc_logging.h>
#include <asm/unaligned.h>

#include <linux/usb/composite.h>
@@ -42,6 +43,15 @@

#define FUNCTIONFS_MAGIC	0xa647361 /* Chosen by a honest dice roll ;) */

#define NUM_PAGES	10 /* # of pages for ipc logging */

static void *ffs_ipc_log;
#define ffs_log(fmt, ...) do { \
	ipc_log_string(ffs_ipc_log, "%s: " fmt,  __func__, \
			##__VA_ARGS__); \
	pr_debug(fmt, ##__VA_ARGS__); \
} while (0)

/* Reference counter handling */
static void ffs_data_get(struct ffs_data *ffs);
static void ffs_data_put(struct ffs_data *ffs);
@@ -278,6 +288,9 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)

	spin_unlock_irq(&ffs->ev.waitq.lock);

	ffs_log("enter: state %d setup_state %d flags %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	req->buf      = data;
	req->length   = len;

@@ -302,11 +315,18 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
	}

	ffs->setup_state = FFS_NO_SETUP;

	ffs_log("exit: state %d setup_state %d flags %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	return req->status ? req->status : req->actual;
}

static int __ffs_ep0_stall(struct ffs_data *ffs)
{
	ffs_log("state %d setup_state %d flags %lu can_stall %d", ffs->state,
		ffs->setup_state, ffs->flags, ffs->ev.can_stall);

	if (ffs->ev.can_stall) {
		pr_vdebug("ep0 stall\n");
		usb_ep_set_halt(ffs->gadget->ep0);
@@ -327,6 +347,9 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,

	ENTER();

	ffs_log("enter:len %zu state %d setup_state %d flags %lu", len,
		ffs->state, ffs->setup_state, ffs->flags);

	/* Fast check if setup was canceled */
	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
		return -EIDRM;
@@ -455,6 +478,9 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
		break;
	}

	ffs_log("exit:ret %zu state %d setup_state %d flags %lu", ret,
		ffs->state, ffs->setup_state, ffs->flags);

	mutex_unlock(&ffs->mutex);
	return ret;
}
@@ -488,6 +514,10 @@ static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
			ffs->ev.count * sizeof *ffs->ev.types);

	spin_unlock_irq(&ffs->ev.waitq.lock);

	ffs_log("state %d setup_state %d flags %lu #evt %zu", ffs->state,
		ffs->setup_state, ffs->flags, n);

	mutex_unlock(&ffs->mutex);

	return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
@@ -503,6 +533,9 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,

	ENTER();

	ffs_log("enter:len %zu state %d setup_state %d flags %lu", len,
		ffs->state, ffs->setup_state, ffs->flags);

	/* Fast check if setup was canceled */
	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
		return -EIDRM;
@@ -591,8 +624,12 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,

	spin_unlock_irq(&ffs->ev.waitq.lock);
done_mutex:
	ffs_log("exit:ret %d state %d setup_state %d flags %lu", ret,
		ffs->state, ffs->setup_state, ffs->flags);

	mutex_unlock(&ffs->mutex);
	kfree(data);

	return ret;
}

@@ -602,6 +639,9 @@ static int ffs_ep0_open(struct inode *inode, struct file *file)

	ENTER();

	ffs_log("state %d setup_state %d flags %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	if (unlikely(ffs->state == FFS_CLOSING))
		return -EBUSY;

@@ -617,6 +657,9 @@ static int ffs_ep0_release(struct inode *inode, struct file *file)

	ENTER();

	ffs_log("state %d setup_state %d flags %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	ffs_data_closed(ffs);

	return 0;
@@ -630,6 +673,9 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)

	ENTER();

	ffs_log("state %d setup_state %d flags %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
		struct ffs_function *func = ffs->func;
		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
@@ -648,6 +694,9 @@ static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
	unsigned int mask = POLLWRNORM;
	int ret;

	ffs_log("enter:state %d setup_state %d flags %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	poll_wait(file, &ffs->ev.waitq, wait);

	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
@@ -678,6 +727,8 @@ static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
		break;
	}

	ffs_log("exit: mask %u", mask);

	mutex_unlock(&ffs->mutex);

	return mask;
@@ -703,6 +754,7 @@ static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
	if (likely(req->context)) {
		struct ffs_ep *ep = _ep->driver_data;
		ep->status = req->status ? req->status : req->actual;
		ffs_log("ep status %d for req %pK", ep->status, req);
		complete(req->context);
	}
}
@@ -758,6 +810,8 @@ static void ffs_user_copy_worker(struct work_struct *work)
					 io_data->req->actual;
	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;

	ffs_log("enter: ret %d", ret);

	if (io_data->read && ret > 0) {
		use_mm(io_data->mm);
		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
@@ -775,6 +829,8 @@ static void ffs_user_copy_worker(struct work_struct *work)
		kfree(io_data->to_free);
	kfree(io_data->buf);
	kfree(io_data);

	ffs_log("exit");
}

static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
@@ -785,8 +841,12 @@ static void ffs_epfile_async_io_complete(struct usb_ep *_ep,

	ENTER();

	ffs_log("enter");

	INIT_WORK(&io_data->work, ffs_user_copy_worker);
	queue_work(ffs->io_completion_wq, &io_data->work);

	ffs_log("exit");
}

static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
@@ -880,6 +940,8 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
	ssize_t ret, data_len = -EINVAL;
	int halt;

	ffs_log("enter: epfile name %s", epfile->name);

	/* Are we still active? */
	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
		return -ENODEV;
@@ -1050,6 +1112,9 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
	mutex_unlock(&epfile->mutex);
error:
	kfree(data);

	ffs_log("exit: ret %zu", ret);

	return ret;
}

@@ -1060,12 +1125,18 @@ ffs_epfile_open(struct inode *inode, struct file *file)

	ENTER();

	ffs_log("enter:state %d setup_state %d flag %lu", epfile->ffs->state,
		epfile->ffs->setup_state, epfile->ffs->flags);

	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
		return -ENODEV;

	file->private_data = epfile;
	ffs_data_opened(epfile->ffs);

	ffs_log("exit:state %d setup_state %d flag %lu", epfile->ffs->state,
		epfile->ffs->setup_state, epfile->ffs->flags);

	return 0;
}

@@ -1077,6 +1148,9 @@ static int ffs_aio_cancel(struct kiocb *kiocb)

	ENTER();

	ffs_log("enter:state %d setup_state %d flag %lu", epfile->ffs->state,
		epfile->ffs->setup_state, epfile->ffs->flags);

	spin_lock_irq(&epfile->ffs->eps_lock);

	if (likely(io_data && io_data->ep && io_data->req))
@@ -1086,6 +1160,8 @@ static int ffs_aio_cancel(struct kiocb *kiocb)

	spin_unlock_irq(&epfile->ffs->eps_lock);

	ffs_log("exit: value %d", value);

	return value;
}

@@ -1096,6 +1172,8 @@ static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)

	ENTER();

	ffs_log("enter");

	if (!is_sync_kiocb(kiocb)) {
		p = kmalloc(sizeof(io_data), GFP_KERNEL);
		if (unlikely(!p))
@@ -1122,6 +1200,9 @@ static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
		kfree(p);
	else
		*from = p->data;

	ffs_log("exit");

	return res;
}

@@ -1132,6 +1213,8 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)

	ENTER();

	ffs_log("enter");

	if (!is_sync_kiocb(kiocb)) {
		p = kmalloc(sizeof(io_data), GFP_KERNEL);
		if (unlikely(!p))
@@ -1170,6 +1253,9 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
	} else {
		*to = p->data;
	}

	ffs_log("enter");

	return res;
}

@@ -1181,8 +1267,13 @@ ffs_epfile_release(struct inode *inode, struct file *file)
	ENTER();

	__ffs_epfile_read_buffer_free(epfile);
	ffs_log("enter:state %d setup_state %d flag %lu", epfile->ffs->state,
		epfile->ffs->setup_state, epfile->ffs->flags);

	ffs_data_closed(epfile->ffs);

	ffs_log("exit");

	return 0;
}

@@ -1195,6 +1286,9 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,

	ENTER();

	ffs_log("enter:state %d setup_state %d flag %lu", epfile->ffs->state,
		epfile->ffs->setup_state, epfile->ffs->flags);

	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
		return -ENODEV;

@@ -1260,6 +1354,8 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,
	}
	spin_unlock_irq(&epfile->ffs->eps_lock);

	ffs_log("exit:ret %d", ret);

	return ret;
}

@@ -1291,6 +1387,8 @@ ffs_sb_make_inode(struct super_block *sb, void *data,

	ENTER();

	ffs_log("enter");

	inode = new_inode(sb);

	if (likely(inode)) {
@@ -1310,6 +1408,8 @@ ffs_sb_make_inode(struct super_block *sb, void *data,
			inode->i_op  = iops;
	}

	ffs_log("exit");

	return inode;
}

@@ -1324,6 +1424,8 @@ static struct dentry *ffs_sb_create_file(struct super_block *sb,

	ENTER();

	ffs_log("enter");

	dentry = d_alloc_name(sb->s_root, name);
	if (unlikely(!dentry))
		return NULL;
@@ -1335,6 +1437,9 @@ static struct dentry *ffs_sb_create_file(struct super_block *sb,
	}

	d_add(dentry, inode);

	ffs_log("exit");

	return dentry;
}

@@ -1360,6 +1465,8 @@ static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)

	ENTER();

	ffs_log("enter");

	ffs->sb              = sb;
	data->ffs_data       = NULL;
	sb->s_fs_info        = ffs;
@@ -1384,6 +1491,8 @@ static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
					 &ffs_ep0_operations)))
		return -ENOMEM;

	ffs_log("exit");

	return 0;
}

@@ -1391,6 +1500,8 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
{
	ENTER();

	ffs_log("enter");

	if (!opts || !*opts)
		return 0;

@@ -1473,6 +1584,8 @@ static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
		opts = comma + 1;
	}

	ffs_log("exit");

	return 0;
}

@@ -1498,6 +1611,8 @@ ffs_fs_mount(struct file_system_type *t, int flags,

	ENTER();

	ffs_log("enter");

	ret = ffs_fs_parse_opts(&data, opts);
	if (unlikely(ret < 0))
		return ERR_PTR(ret);
@@ -1527,6 +1642,9 @@ ffs_fs_mount(struct file_system_type *t, int flags,
		ffs_release_dev(data.ffs_data);
		ffs_data_put(data.ffs_data);
	}

	ffs_log("exit");

	return rv;
}

@@ -1535,12 +1653,16 @@ ffs_fs_kill_sb(struct super_block *sb)
{
	ENTER();

	ffs_log("enter");

	kill_litter_super(sb);
	if (sb->s_fs_info) {
		ffs_release_dev(sb->s_fs_info);
		ffs_data_closed(sb->s_fs_info);
		ffs_data_put(sb->s_fs_info);
	}

	ffs_log("exit");
}

static struct file_system_type ffs_fs_type = {
@@ -1566,6 +1688,10 @@ static int functionfs_init(void)
	else
		pr_err("failed registering file system (%d)\n", ret);

	ffs_ipc_log = ipc_log_context_create(NUM_PAGES, "f_fs", 0);
	if (IS_ERR_OR_NULL(ffs_ipc_log))
		ffs_ipc_log =  NULL;

	return ret;
}

@@ -1575,6 +1701,11 @@ static void functionfs_cleanup(void)

	pr_info("unloading\n");
	unregister_filesystem(&ffs_fs_type);

	if (ffs_ipc_log) {
		ipc_log_context_destroy(ffs_ipc_log);
		ffs_ipc_log = NULL;
	}
}


@@ -1587,25 +1718,37 @@ static void ffs_data_get(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter");

	refcount_inc(&ffs->ref);

	ffs_log("exit");
}

static void ffs_data_opened(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	refcount_inc(&ffs->ref);
	if (atomic_add_return(1, &ffs->opened) == 1 &&
			ffs->state == FFS_DEACTIVATED) {
		ffs->state = FFS_CLOSING;
		ffs_data_reset(ffs);
	}

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);
}

static void ffs_data_put(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter");

	if (unlikely(refcount_dec_and_test(&ffs->ref))) {
		pr_info("%s(): freeing\n", __func__);
		ffs_data_clear(ffs);
@@ -1616,12 +1759,17 @@ static void ffs_data_put(struct ffs_data *ffs)
		kfree(ffs->dev_name);
		kfree(ffs);
	}

	ffs_log("exit");
}

static void ffs_data_closed(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu opened %d", ffs->state,
		ffs->setup_state, ffs->flags, atomic_read(&ffs->opened));

	if (atomic_dec_and_test(&ffs->opened)) {
		if (ffs->no_disconnect) {
			ffs->state = FFS_DEACTIVATED;
@@ -1642,6 +1790,9 @@ static void ffs_data_closed(struct ffs_data *ffs)
		ffs_data_reset(ffs);
	}

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	ffs_data_put(ffs);
}

@@ -1653,6 +1804,8 @@ static struct ffs_data *ffs_data_new(const char *dev_name)

	ENTER();

	ffs_log("enter");

	ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name);
	if (!ffs->io_completion_wq) {
		kfree(ffs);
@@ -1671,6 +1824,8 @@ static struct ffs_data *ffs_data_new(const char *dev_name)
	/* XXX REVISIT need to update it in some places, or do we? */
	ffs->ev.can_stall = 1;

	ffs_log("exit");

	return ffs;
}

@@ -1678,6 +1833,11 @@ static void ffs_data_clear(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	pr_debug("%s: ffs->gadget= %pK, ffs->flags= %lu\n",
				__func__, ffs->gadget, ffs->flags);
	ffs_closed(ffs);

	BUG_ON(ffs->gadget);
@@ -1691,12 +1851,18 @@ static void ffs_data_clear(struct ffs_data *ffs)
	kfree(ffs->raw_descs_data);
	kfree(ffs->raw_strings);
	kfree(ffs->stringtabs);

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);
}

static void ffs_data_reset(struct ffs_data *ffs)
{
	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	ffs_data_clear(ffs);

	ffs->epfiles = NULL;
@@ -1719,6 +1885,9 @@ static void ffs_data_reset(struct ffs_data *ffs)
	ffs->state = FFS_READ_DESCRIPTORS;
	ffs->setup_state = FFS_NO_SETUP;
	ffs->flags = 0;

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);
}


@@ -1729,6 +1898,9 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)

	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	if (WARN_ON(ffs->state != FFS_ACTIVE
		 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
		return -EBADFD;
@@ -1754,6 +1926,10 @@ static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
	}

	ffs->gadget = cdev->gadget;

	ffs_log("exit: state %d setup_state %d flag %lu gadget %pK\n",
			ffs->state, ffs->setup_state, ffs->flags, ffs->gadget);

	ffs_data_get(ffs);
	return 0;
}
@@ -1767,6 +1943,8 @@ static void functionfs_unbind(struct ffs_data *ffs)
		ffs->ep0req = NULL;
		ffs->gadget = NULL;
		clear_bit(FFS_FL_BOUND, &ffs->flags);
		ffs_log("state %d setup_state %d flag %lu gadget %pK\n",
			ffs->state, ffs->setup_state, ffs->flags, ffs->gadget);
		ffs_data_put(ffs);
	}
}
@@ -1778,6 +1956,9 @@ static int ffs_epfiles_create(struct ffs_data *ffs)

	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	count = ffs->eps_count;
	epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
	if (!epfiles)
@@ -1801,6 +1982,10 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
	}

	ffs->epfiles = epfiles;

	ffs_log("exit: eps_count %u state %d setup_state %d flag %lu",
		count, ffs->state, ffs->setup_state, ffs->flags);

	return 0;
}

@@ -1810,6 +1995,8 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)

	ENTER();

	ffs_log("enter: count %u", count);

	for (; count; --count, ++epfile) {
		BUG_ON(mutex_is_locked(&epfile->mutex));
		if (epfile->dentry) {
@@ -1820,6 +2007,8 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
	}

	kfree(epfiles);

	ffs_log("exit");
}

static void ffs_func_eps_disable(struct ffs_function *func)
@@ -1829,6 +2018,9 @@ static void ffs_func_eps_disable(struct ffs_function *func)
	unsigned count            = func->ffs->eps_count;
	unsigned long flags;

	ffs_log("enter: state %d setup_state %d flag %lu", func->ffs->state,
		func->ffs->setup_state, func->ffs->flags);

	spin_lock_irqsave(&func->ffs->eps_lock, flags);
	while (count--) {
		/* pending requests get nuked */
@@ -1843,6 +2035,8 @@ static void ffs_func_eps_disable(struct ffs_function *func)
		}
	}
	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);

	ffs_log("exit");
}

static int ffs_func_eps_enable(struct ffs_function *func)
@@ -1854,6 +2048,9 @@ static int ffs_func_eps_enable(struct ffs_function *func)
	unsigned long flags;
	int ret = 0;

	ffs_log("enter: state %d setup_state %d flag %lu", func->ffs->state,
		func->ffs->setup_state, func->ffs->flags);

	spin_lock_irqsave(&func->ffs->eps_lock, flags);
	while(count--) {
		ep->ep->driver_data = ep;
@@ -1870,6 +2067,7 @@ static int ffs_func_eps_enable(struct ffs_function *func)
			epfile->ep = ep;
			epfile->in = usb_endpoint_dir_in(ep->ep->desc);
			epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc);
			ffs_log("usb_ep_enable %s", ep->ep->name);
		} else {
			break;
		}
@@ -1881,6 +2079,8 @@ static int ffs_func_eps_enable(struct ffs_function *func)
	wake_up_interruptible(&ffs->wait);
	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);

	ffs_log("exit: ret %d", ret);

	return ret;
}

@@ -1921,6 +2121,8 @@ static int __must_check ffs_do_single_desc(char *data, unsigned len,

	ENTER();

	ffs_log("enter: len %u", len);

	/* At least two bytes are required: length and type */
	if (len < 2) {
		pr_vdebug("descriptor too short\n");
@@ -2037,6 +2239,8 @@ static int __must_check ffs_do_single_desc(char *data, unsigned len,
#undef __entity_check_STRING
#undef __entity_check_ENDPOINT

	ffs_log("exit: desc type %d length %d", _ds->bDescriptorType, length);

	return length;
}

@@ -2048,6 +2252,8 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,

	ENTER();

	ffs_log("enter: len %u", len);

	for (;;) {
		int ret;

@@ -2075,6 +2281,8 @@ static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
		data += ret;
		++num;
	}

	ffs_log("exit: len %u", len);
}

static int __ffs_data_do_entity(enum ffs_entity_type type,
@@ -2086,6 +2294,8 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,

	ENTER();

	ffs_log("enter: type %u", type);

	switch (type) {
	case FFS_DESCRIPTOR:
		break;
@@ -2124,6 +2334,8 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
		break;
	}

	ffs_log("exit");

	return 0;
}

@@ -2133,6 +2345,8 @@ static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
	u16 bcd_version = le16_to_cpu(desc->bcdVersion);
	u16 w_index = le16_to_cpu(desc->wIndex);

	ffs_log("enter");

	if (bcd_version != 1) {
		pr_vdebug("unsupported os descriptors version: %d",
			  bcd_version);
@@ -2150,6 +2364,8 @@ static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
		return -EINVAL;
	}

	ffs_log("exit: size of desc %zu", sizeof(*desc));

	return sizeof(*desc);
}

@@ -2169,6 +2385,8 @@ static int __must_check ffs_do_single_os_desc(char *data, unsigned len,

	ENTER();

	ffs_log("enter: len %u os desc type %d", len, type);

	/* loop over all ext compat/ext prop descriptors */
	while (feature_count--) {
		ret = entity(type, h, data, len, priv);
@@ -2179,6 +2397,9 @@ static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
		data += ret;
		len -= ret;
	}

	ffs_log("exit");

	return _len - len;
}

@@ -2192,6 +2413,8 @@ static int __must_check ffs_do_os_descs(unsigned count,

	ENTER();

	ffs_log("enter: len %u", len);

	for (num = 0; num < count; ++num) {
		int ret;
		enum ffs_os_desc_type type;
@@ -2241,6 +2464,9 @@ static int __must_check ffs_do_os_descs(unsigned count,
		len -= ret;
		data += ret;
	}

	ffs_log("exit");

	return _len - len;
}

@@ -2256,6 +2482,8 @@ static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,

	ENTER();

	ffs_log("enter: len %u", len);

	switch (type) {
	case FFS_OS_DESC_EXT_COMPAT: {
		struct usb_ext_compat_desc *d = data;
@@ -2320,6 +2548,9 @@ static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
		pr_vdebug("unknown descriptor: %d\n", type);
		return -EINVAL;
	}

	ffs_log("exit");

	return length;
}

@@ -2333,6 +2564,8 @@ static int __ffs_data_got_descs(struct ffs_data *ffs,

	ENTER();

	ffs_log("enter: len %zu", len);

	if (get_unaligned_le32(data + 4) != len)
		goto error;

@@ -2448,10 +2681,13 @@ static int __ffs_data_got_descs(struct ffs_data *ffs,
	ffs->ss_descs_count	= counts[2];
	ffs->ms_os_descs_count	= os_descs_count;

	ffs_log("exit");

	return 0;

error:
	kfree(_data);
	ffs_log("exit: ret %d", ret);
	return ret;
}

@@ -2465,6 +2701,8 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,

	ENTER();

	ffs_log("enter: len %zu", len);

	if (unlikely(len < 16 ||
		     get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
		     get_unaligned_le32(data + 4) != len))
@@ -2579,12 +2817,14 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,
	ffs->stringtabs = stringtabs;
	ffs->raw_strings = _data;

	ffs_log("exit");
	return 0;

error_free:
	kfree(stringtabs);
error:
	kfree(_data);
	ffs_log("exit: -EINVAL");
	return -EINVAL;
}

@@ -2597,6 +2837,9 @@ static void __ffs_event_add(struct ffs_data *ffs,
	enum usb_functionfs_event_type rem_type1, rem_type2 = type;
	int neg = 0;

	ffs_log("enter: type %d state %d setup_state %d flag %lu", type,
		ffs->state, ffs->setup_state, ffs->flags);

	/*
	 * Abort any unhandled setup
	 *
@@ -2656,6 +2899,9 @@ static void __ffs_event_add(struct ffs_data *ffs,
	wake_up_locked(&ffs->ev.waitq);
	if (ffs->ffs_eventfd)
		eventfd_signal(ffs->ffs_eventfd, 1);

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);
}

static void ffs_event_add(struct ffs_data *ffs,
@@ -2690,6 +2936,8 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
	int idx;
	static const char *speed_names[] = { "full", "high", "super" };

	ffs_log("enter");

	if (type != FFS_DESCRIPTOR)
		return 0;

@@ -2765,6 +3013,8 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
	}
	ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);

	ffs_log("exit");

	return 0;
}

@@ -2776,6 +3026,8 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
	unsigned idx;
	u8 newValue;

	ffs_log("enter: type %d", type);

	switch (type) {
	default:
	case FFS_DESCRIPTOR:
@@ -2820,6 +3072,9 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,

	pr_vdebug("%02x -> %02x\n", *valuep, newValue);
	*valuep = newValue;

	ffs_log("exit: newValue %d", newValue);

	return 0;
}

@@ -2830,6 +3085,8 @@ static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
	struct ffs_function *func = priv;
	u8 length = 0;

	ffs_log("enter: type %d", type);

	switch (type) {
	case FFS_OS_DESC_EXT_COMPAT: {
		struct usb_ext_compat_desc *desc = data;
@@ -2899,6 +3156,8 @@ static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
		pr_vdebug("unknown descriptor: %d\n", type);
	}

	ffs_log("exit");

	return length;
}

@@ -2912,6 +3171,8 @@ static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,

	ENTER();

	ffs_log("enter");

	/*
	 * Legacy gadget triggers binding in functionfs_ready_callback,
	 * which already uses locking; taking the same lock here would
@@ -2946,6 +3207,8 @@ static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
	ffs_opts->refcnt++;
	func->function.strings = func->ffs->stringtabs;

	ffs_log("exit");

	return ffs_opts;
}

@@ -2989,6 +3252,9 @@ static int _ffs_func_bind(struct usb_configuration *c,

	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	/* Has descriptors only for speeds gadget does not support */
	if (unlikely(!(full | high | super)))
		return -ENOTSUPP;
@@ -3105,10 +3371,15 @@ static int _ffs_func_bind(struct usb_configuration *c,

	/* And we're done */
	ffs_event_add(ffs, FUNCTIONFS_BIND);

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	return 0;

error:
	/* XXX Do we need to release all claimed endpoints here? */
	ffs_log("exit: ret %d", ret);
	return ret;
}

@@ -3119,6 +3390,8 @@ static int ffs_func_bind(struct usb_configuration *c,
	struct ffs_function *func = ffs_func_from_usb(f);
	int ret;

	ffs_log("enter");

	if (IS_ERR(ffs_opts))
		return PTR_ERR(ffs_opts);

@@ -3126,6 +3399,8 @@ static int ffs_func_bind(struct usb_configuration *c,
	if (ret && !--ffs_opts->refcnt)
		functionfs_unbind(func->ffs);

	ffs_log("exit: ret %d", ret);

	return ret;
}

@@ -3136,7 +3411,12 @@ static void ffs_reset_work(struct work_struct *work)
{
	struct ffs_data *ffs = container_of(work,
		struct ffs_data, reset_work);

	ffs_log("enter");

	ffs_data_reset(ffs);

	ffs_log("exit");
}

static int ffs_func_set_alt(struct usb_function *f,
@@ -3146,14 +3426,18 @@ static int ffs_func_set_alt(struct usb_function *f,
	struct ffs_data *ffs = func->ffs;
	int ret = 0, intf;

	ffs_log("enter");

	if (alt != (unsigned)-1) {
		intf = ffs_func_revmap_intf(func, interface);
		if (unlikely(intf < 0))
			return intf;
	}

	if (ffs->func)
	if (ffs->func) {
		ffs_func_eps_disable(ffs->func);
		ffs->func = NULL;
	}

	if (ffs->state == FFS_DEACTIVATED) {
		ffs->state = FFS_CLOSING;
@@ -3175,12 +3459,17 @@ static int ffs_func_set_alt(struct usb_function *f,
	ret = ffs_func_eps_enable(func);
	if (likely(ret >= 0))
		ffs_event_add(ffs, FUNCTIONFS_ENABLE);

	ffs_log("exit: ret %d", ret);

	return ret;
}

static void ffs_func_disable(struct usb_function *f)
{
	ffs_log("enter");
	ffs_func_set_alt(f, 0, (unsigned)-1);
	ffs_log("exit");
}

static int ffs_func_setup(struct usb_function *f,
@@ -3193,6 +3482,8 @@ static int ffs_func_setup(struct usb_function *f,

	ENTER();

	ffs_log("enter");

	pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
	pr_vdebug("creq->bRequest     = %02x\n", creq->bRequest);
	pr_vdebug("creq->wValue       = %04x\n", le16_to_cpu(creq->wValue));
@@ -3240,6 +3531,8 @@ static int ffs_func_setup(struct usb_function *f,
	__ffs_event_add(ffs, FUNCTIONFS_SETUP);
	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);

	ffs_log("exit");

	return 0;
}

@@ -3268,13 +3561,23 @@ static bool ffs_func_req_match(struct usb_function *f,
static void ffs_func_suspend(struct usb_function *f)
{
	ENTER();

	ffs_log("enter");

	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);

	ffs_log("exit");
}

static void ffs_func_resume(struct usb_function *f)
{
	ENTER();

	ffs_log("enter");

	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);

	ffs_log("exit");
}


@@ -3291,11 +3594,15 @@ static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
	short *nums = func->interfaces_nums;
	unsigned count = func->ffs->interfaces_count;

	ffs_log("enter");

	for (; count; --count, ++nums) {
		if (*nums >= 0 && *nums == intf)
			return nums - func->interfaces_nums;
	}

	ffs_log("exit");

	return -EDOM;
}

@@ -3308,6 +3615,8 @@ static struct ffs_dev *_ffs_do_find_dev(const char *name)
{
	struct ffs_dev *dev;

	ffs_log("enter");

	if (!name)
		return NULL;

@@ -3316,6 +3625,8 @@ static struct ffs_dev *_ffs_do_find_dev(const char *name)
			return dev;
	}

	ffs_log("exit");

	return NULL;
}

@@ -3326,12 +3637,16 @@ static struct ffs_dev *_ffs_get_single_dev(void)
{
	struct ffs_dev *dev;

	ffs_log("enter");

	if (list_is_singular(&ffs_devices)) {
		dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
		if (dev->single)
			return dev;
	}

	ffs_log("exit");

	return NULL;
}

@@ -3342,11 +3657,17 @@ static struct ffs_dev *_ffs_find_dev(const char *name)
{
	struct ffs_dev *dev;

	ffs_log("enter");

	dev = _ffs_get_single_dev();
	if (dev)
		return dev;

	return _ffs_do_find_dev(name);
	dev = _ffs_do_find_dev(name);

	ffs_log("exit");

	return dev;
}

/* Configfs support *********************************************************/
@@ -3437,6 +3758,10 @@ static void ffs_func_unbind(struct usb_configuration *c,
	unsigned long flags;

	ENTER();

	ffs_log("enter: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);

	if (ffs->func == func) {
		ffs_func_eps_disable(func);
		ffs->func = NULL;
@@ -3466,6 +3791,9 @@ static void ffs_func_unbind(struct usb_configuration *c,
	func->interfaces_nums = NULL;

	ffs_event_add(ffs, FUNCTIONFS_UNBIND);

	ffs_log("exit: state %d setup_state %d flag %lu", ffs->state,
		ffs->setup_state, ffs->flags);
}

static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
@@ -3526,6 +3854,8 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name)
	struct ffs_dev *existing;
	int ret = 0;

	ffs_log("enter");

	ffs_dev_lock();

	existing = _ffs_do_find_dev(name);
@@ -3536,6 +3866,8 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name)

	ffs_dev_unlock();

	ffs_log("exit");

	return ret;
}
EXPORT_SYMBOL_GPL(ffs_name_dev);
@@ -3544,6 +3876,8 @@ int ffs_single_dev(struct ffs_dev *dev)
{
	int ret;

	ffs_log("enter");

	ret = 0;
	ffs_dev_lock();

@@ -3553,6 +3887,9 @@ int ffs_single_dev(struct ffs_dev *dev)
		dev->single = true;

	ffs_dev_unlock();

	ffs_log("exit");

	return ret;
}
EXPORT_SYMBOL_GPL(ffs_single_dev);
@@ -3562,6 +3899,9 @@ EXPORT_SYMBOL_GPL(ffs_single_dev);
 */
static void _ffs_free_dev(struct ffs_dev *dev)
{

	ffs_log("enter");

	list_del(&dev->entry);

	/* Clear the private_data pointer to stop incorrect dev access */
@@ -3571,6 +3911,8 @@ static void _ffs_free_dev(struct ffs_dev *dev)
	kfree(dev);
	if (list_empty(&ffs_devices))
		functionfs_cleanup();

	ffs_log("exit");
}

static void *ffs_acquire_dev(const char *dev_name)
@@ -3578,6 +3920,9 @@ static void *ffs_acquire_dev(const char *dev_name)
	struct ffs_dev *ffs_dev;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_dev = _ffs_find_dev(dev_name);
@@ -3592,6 +3937,9 @@ static void *ffs_acquire_dev(const char *dev_name)
		ffs_dev->mounted = true;

	ffs_dev_unlock();

	ffs_log("exit");

	return ffs_dev;
}

@@ -3600,6 +3948,9 @@ static void ffs_release_dev(struct ffs_data *ffs_data)
	struct ffs_dev *ffs_dev;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_dev = ffs_data->private_data;
@@ -3611,6 +3962,8 @@ static void ffs_release_dev(struct ffs_data *ffs_data)
	}

	ffs_dev_unlock();

	ffs_log("exit");
}

static int ffs_ready(struct ffs_data *ffs)
@@ -3619,6 +3972,9 @@ static int ffs_ready(struct ffs_data *ffs)
	int ret = 0;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_obj = ffs->private_data;
@@ -3643,6 +3999,9 @@ static int ffs_ready(struct ffs_data *ffs)
	set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
done:
	ffs_dev_unlock();

	ffs_log("exit");

	return ret;
}

@@ -3653,6 +4012,9 @@ static void ffs_closed(struct ffs_data *ffs)
	struct config_item *ci;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_obj = ffs->private_data;
@@ -3678,11 +4040,17 @@ static void ffs_closed(struct ffs_data *ffs)
	ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
	ffs_dev_unlock();

	if (test_bit(FFS_FL_BOUND, &ffs->flags))
	if (test_bit(FFS_FL_BOUND, &ffs->flags)) {
		unregister_gadget_item(ci);
		ffs_log("unreg gadget done");
	}

	ffs_log("exit");
	return;
done:
	ffs_dev_unlock();

	ffs_log("exit error");
}

/* Misc helper functions ****************************************************/