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

Commit de406b7f authored by Hemant Kumar's avatar Hemant Kumar Committed by Mayank Rana
Browse files

usb: gadget: f_fs: Add support for ipc logging



Log function entry and exit and dump relevant values
into ipc log buffer. This allows to debug various
race conditions and stability issues.

Change-Id: I15011d79fc2f054e64f8bbd1f8f5db8944b46ada
Signed-off-by: default avatarHemant Kumar <hemantk@codeaurora.org>
Signed-off-by: default avatarMayank Rana <mrana@codeaurora.org>
parent be964309
Loading
Loading
Loading
Loading
+362 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/uio.h>
#include <linux/ipc_logging.h>
#include <asm/unaligned.h>

#include <linux/usb/composite.h>
@@ -41,6 +42,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 %p", 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,
@@ -784,8 +840,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);
	schedule_work(&io_data->work);

	ffs_log("exit");
}

static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
@@ -879,6 +939,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;
@@ -1049,6 +1111,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;
}

@@ -1059,12 +1124,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;
}

@@ -1076,6 +1147,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))
@@ -1085,6 +1159,8 @@ static int ffs_aio_cancel(struct kiocb *kiocb)

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

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

	return value;
}

@@ -1095,6 +1171,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))
@@ -1121,6 +1199,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;
}

@@ -1131,6 +1212,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))
@@ -1169,6 +1252,9 @@ static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
	} else {
		*to = p->data;
	}

	ffs_log("enter");

	return res;
}

@@ -1180,8 +1266,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;
}

@@ -1193,6 +1284,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;

@@ -1243,6 +1337,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;
}

@@ -1274,6 +1370,8 @@ ffs_sb_make_inode(struct super_block *sb, void *data,

	ENTER();

	ffs_log("enter");

	inode = new_inode(sb);

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

	ffs_log("exit");

	return inode;
}

@@ -1307,6 +1407,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;
@@ -1318,6 +1420,9 @@ static struct dentry *ffs_sb_create_file(struct super_block *sb,
	}

	d_add(dentry, inode);

	ffs_log("exit");

	return dentry;
}

@@ -1343,6 +1448,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;
@@ -1367,6 +1474,8 @@ static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
					 &ffs_ep0_operations)))
		return -ENOMEM;

	ffs_log("exit");

	return 0;
}

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

	ffs_log("enter");

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

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

	ffs_log("exit");

	return 0;
}

@@ -1481,6 +1594,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);
@@ -1510,6 +1625,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;
}

@@ -1518,12 +1636,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 = {
@@ -1549,6 +1671,8 @@ 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);

	return ret;
}

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

	ffs_log("enter");

	atomic_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));

	atomic_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(atomic_dec_and_test(&ffs->ref))) {
		pr_info("%s(): freeing\n", __func__);
		ffs_data_clear(ffs);
@@ -1597,12 +1733,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;
@@ -1623,6 +1764,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);
}

@@ -1634,6 +1778,8 @@ static struct ffs_data *ffs_data_new(void)

	ENTER();

	ffs_log("enter");

	atomic_set(&ffs->ref, 1);
	atomic_set(&ffs->opened, 0);
	ffs->state = FFS_READ_DESCRIPTORS;
@@ -1645,6 +1791,8 @@ static struct ffs_data *ffs_data_new(void)
	/* XXX REVISIT need to update it in some places, or do we? */
	ffs->ev.can_stall = 1;

	ffs_log("exit");

	return ffs;
}

@@ -1652,6 +1800,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= %p, ffs->flags= %lu\n",
				__func__, ffs->gadget, ffs->flags);
	ffs_closed(ffs);

	BUG_ON(ffs->gadget);
@@ -1665,12 +1818,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;
@@ -1693,6 +1852,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);
}


@@ -1703,6 +1865,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;
@@ -1728,6 +1893,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 %p\n",
			ffs->state, ffs->setup_state, ffs->flags, ffs->gadget);

	ffs_data_get(ffs);
	return 0;
}
@@ -1741,6 +1910,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 %p\n",
			ffs->state, ffs->setup_state, ffs->flags, ffs->gadget);
		ffs_data_put(ffs);
	}
}
@@ -1752,6 +1923,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)
@@ -1776,6 +1950,10 @@ static int ffs_epfiles_create(struct ffs_data *ffs)
	}

	ffs->epfiles = epfiles;

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

	return 0;
}

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

	ENTER();

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

	for (; count; --count, ++epfile) {
		BUG_ON(mutex_is_locked(&epfile->mutex) ||
		       waitqueue_active(&epfile->wait));
@@ -1796,6 +1976,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)
@@ -1805,6 +1987,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);
	do {
		/* pending requests get nuked */
@@ -1819,6 +2004,8 @@ static void ffs_func_eps_disable(struct ffs_function *func)
		}
	} while (--count);
	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);

	ffs_log("exit");
}

static int ffs_func_eps_enable(struct ffs_function *func)
@@ -1830,6 +2017,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);
	do {
		struct usb_endpoint_descriptor *ds;
@@ -1867,6 +2057,7 @@ static int ffs_func_eps_enable(struct ffs_function *func)
			epfile->ep = ep;
			epfile->in = usb_endpoint_dir_in(ds);
			epfile->isoc = usb_endpoint_xfer_isoc(ds);
			ffs_log("usb_ep_enable %s", ep->ep->name);
		} else {
			break;
		}
@@ -1878,6 +2069,8 @@ static int ffs_func_eps_enable(struct ffs_function *func)
	} while (--count);
	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);

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

	return ret;
}

@@ -1918,6 +2111,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");
@@ -2034,6 +2229,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;
}

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

	ENTER();

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

	for (;;) {
		int ret;

@@ -2072,6 +2271,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,
@@ -2083,6 +2284,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;
@@ -2121,6 +2324,8 @@ static int __ffs_data_do_entity(enum ffs_entity_type type,
		break;
	}

	ffs_log("exit");

	return 0;
}

@@ -2130,6 +2335,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);
@@ -2147,6 +2354,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);
}

@@ -2166,6 +2375,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);
@@ -2176,6 +2387,9 @@ static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
		data += ret;
		len -= ret;
	}

	ffs_log("exit");

	return _len - len;
}

@@ -2189,6 +2403,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;
@@ -2238,6 +2454,9 @@ static int __must_check ffs_do_os_descs(unsigned count,
		len -= ret;
		data += ret;
	}

	ffs_log("exit");

	return _len - len;
}

@@ -2253,6 +2472,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;
@@ -2301,6 +2522,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;
}

@@ -2314,6 +2538,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;

@@ -2426,10 +2652,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;
}

@@ -2443,6 +2672,8 @@ static int __ffs_data_got_strings(struct ffs_data *ffs,

	ENTER();

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

	if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
		     get_unaligned_le32(data + 4) != len))
		goto error;
@@ -2556,12 +2787,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;
}

@@ -2574,6 +2807,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
	 *
@@ -2633,6 +2869,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,
@@ -2667,6 +2906,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;

@@ -2742,6 +2983,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;
}

@@ -2753,6 +2996,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:
@@ -2797,6 +3042,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;
}

@@ -2807,6 +3055,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;
@@ -2876,6 +3126,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;
}

@@ -2889,6 +3141,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
@@ -2923,6 +3177,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;
}

@@ -2966,6 +3222,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;
@@ -3082,10 +3341,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;
}

@@ -3096,6 +3360,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);

@@ -3103,6 +3369,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;
}

@@ -3113,7 +3381,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,
@@ -3123,6 +3396,8 @@ 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))
@@ -3152,12 +3427,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,
@@ -3170,6 +3450,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));
@@ -3217,6 +3499,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;
}

@@ -3245,13 +3529,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");
}


@@ -3268,11 +3562,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;
}

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

	ffs_log("enter");

	list_for_each_entry(dev, &ffs_devices, entry) {
		if (!dev->name || !name)
			continue;
@@ -3292,6 +3592,8 @@ static struct ffs_dev *_ffs_do_find_dev(const char *name)
			return dev;
	}

	ffs_log("exit");

	return NULL;
}

@@ -3302,12 +3604,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;
}

@@ -3318,11 +3624,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 *********************************************************/
@@ -3444,6 +3756,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;
@@ -3473,6 +3789,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)
@@ -3536,12 +3855,16 @@ static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
{
	struct ffs_dev *existing;

	ffs_log("enter");

	existing = _ffs_do_find_dev(name);
	if (existing)
		return -EBUSY;

	dev->name = name;

	ffs_log("exit");

	return 0;
}

@@ -3552,10 +3875,14 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name)
{
	int ret;

	ffs_log("enter");

	ffs_dev_lock();
	ret = _ffs_name_dev(dev, name);
	ffs_dev_unlock();

	ffs_log("exit");

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

	ffs_log("enter");

	ret = 0;
	ffs_dev_lock();

@@ -3573,6 +3902,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);
@@ -3582,6 +3914,9 @@ EXPORT_SYMBOL_GPL(ffs_single_dev);
 */
static void _ffs_free_dev(struct ffs_dev *dev)
{

	ffs_log("enter");

	list_del(&dev->entry);
	if (dev->name_allocated)
		kfree(dev->name);
@@ -3593,6 +3928,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)
@@ -3600,6 +3937,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);
@@ -3614,6 +3954,9 @@ static void *ffs_acquire_dev(const char *dev_name)
		ffs_dev->mounted = true;

	ffs_dev_unlock();

	ffs_log("exit");

	return ffs_dev;
}

@@ -3622,6 +3965,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;
@@ -3633,6 +3979,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)
@@ -3641,6 +3989,9 @@ static int ffs_ready(struct ffs_data *ffs)
	int ret = 0;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_obj = ffs->private_data;
@@ -3665,6 +4016,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;
}

@@ -3674,6 +4028,9 @@ static void ffs_closed(struct ffs_data *ffs)
	struct f_fs_opts *opts;

	ENTER();

	ffs_log("enter");

	ffs_dev_lock();

	ffs_obj = ffs->private_data;
@@ -3697,8 +4054,12 @@ static void ffs_closed(struct ffs_data *ffs)

	unregister_gadget_item(ffs_obj->opts->
			       func_inst.group.cg_item.ci_parent->ci_parent);

	ffs_log("unreg gadget done");
done:
	ffs_dev_unlock();

	ffs_log("exit");
}

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