2 * f_fs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
7 * Based on inode.c (GadgetFS) which was:
8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
19 /* #define VERBOSE_DEBUG */
21 #include <linux/blkdev.h>
22 #include <linux/pagemap.h>
23 #include <linux/export.h>
24 #include <linux/hid.h>
25 #include <linux/module.h>
26 #include <linux/uio.h>
27 #include <asm/unaligned.h>
29 #include <linux/usb/composite.h>
30 #include <linux/usb/functionfs.h>
32 #include <linux/aio.h>
33 #include <linux/mmu_context.h>
34 #include <linux/poll.h>
35 #include <linux/eventfd.h>
39 #include "u_os_desc.h"
42 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
44 /* Reference counter handling */
45 static void ffs_data_get(struct ffs_data *ffs);
46 static void ffs_data_put(struct ffs_data *ffs);
47 /* Creates new ffs_data object. */
48 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
50 /* Opened counter handling. */
51 static void ffs_data_opened(struct ffs_data *ffs);
52 static void ffs_data_closed(struct ffs_data *ffs);
54 /* Called with ffs->mutex held; take over ownership of data. */
55 static int __must_check
56 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
57 static int __must_check
58 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
61 /* The function structure ***************************************************/
66 struct usb_configuration *conf;
67 struct usb_gadget *gadget;
72 short *interfaces_nums;
74 struct usb_function function;
78 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
80 return container_of(f, struct ffs_function, function);
84 static inline enum ffs_setup_state
85 ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
87 return (enum ffs_setup_state)
88 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
92 static void ffs_func_eps_disable(struct ffs_function *func);
93 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
95 static int ffs_func_bind(struct usb_configuration *,
96 struct usb_function *);
97 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
98 static void ffs_func_disable(struct usb_function *);
99 static int ffs_func_setup(struct usb_function *,
100 const struct usb_ctrlrequest *);
101 static bool ffs_func_req_match(struct usb_function *,
102 const struct usb_ctrlrequest *,
104 static void ffs_func_suspend(struct usb_function *);
105 static void ffs_func_resume(struct usb_function *);
108 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
109 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
112 /* The endpoints structures *************************************************/
115 struct usb_ep *ep; /* P: ffs->eps_lock */
116 struct usb_request *req; /* P: epfile->mutex */
118 /* [0]: full speed, [1]: high speed, [2]: super speed */
119 struct usb_endpoint_descriptor *descs[3];
123 int status; /* P: epfile->mutex */
127 /* Protects ep->ep and ep->req. */
129 wait_queue_head_t wait;
131 struct ffs_data *ffs;
132 struct ffs_ep *ep; /* P: ffs->eps_lock */
134 struct dentry *dentry;
137 * Buffer for holding data from partial reads which may happen since
138 * we’re rounding user read requests to a multiple of a max packet size.
140 struct ffs_buffer *read_buffer; /* P: epfile->mutex */
144 unsigned char in; /* P: ffs->eps_lock */
145 unsigned char isoc; /* P: ffs->eps_lock */
156 /* ffs_io_data structure ***************************************************/
163 struct iov_iter data;
167 struct mm_struct *mm;
168 struct work_struct work;
171 struct usb_request *req;
173 struct ffs_data *ffs;
176 struct ffs_desc_helper {
177 struct ffs_data *ffs;
178 unsigned interfaces_count;
182 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
183 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
185 static struct dentry *
186 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
187 const struct file_operations *fops);
189 /* Devices management *******************************************************/
191 DEFINE_MUTEX(ffs_lock);
192 EXPORT_SYMBOL_GPL(ffs_lock);
194 static struct ffs_dev *_ffs_find_dev(const char *name);
195 static struct ffs_dev *_ffs_alloc_dev(void);
196 static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
197 static void _ffs_free_dev(struct ffs_dev *dev);
198 static void *ffs_acquire_dev(const char *dev_name);
199 static void ffs_release_dev(struct ffs_data *ffs_data);
200 static int ffs_ready(struct ffs_data *ffs);
201 static void ffs_closed(struct ffs_data *ffs);
203 /* Misc helper functions ****************************************************/
205 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
206 __attribute__((warn_unused_result, nonnull));
207 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
208 __attribute__((warn_unused_result, nonnull));
211 /* Control file aka ep0 *****************************************************/
213 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
215 struct ffs_data *ffs = req->context;
217 complete_all(&ffs->ep0req_completion);
220 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
222 struct usb_request *req = ffs->ep0req;
225 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
227 spin_unlock_irq(&ffs->ev.waitq.lock);
233 * UDC layer requires to provide a buffer even for ZLP, but should
234 * not use it at all. Let's provide some poisoned pointer to catch
235 * possible bug in the driver.
237 if (req->buf == NULL)
238 req->buf = (void *)0xDEADBABE;
240 reinit_completion(&ffs->ep0req_completion);
242 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
243 if (unlikely(ret < 0))
246 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
248 usb_ep_dequeue(ffs->gadget->ep0, req);
252 ffs->setup_state = FFS_NO_SETUP;
253 return req->status ? req->status : req->actual;
256 static int __ffs_ep0_stall(struct ffs_data *ffs)
258 if (ffs->ev.can_stall) {
259 pr_vdebug("ep0 stall\n");
260 usb_ep_set_halt(ffs->gadget->ep0);
261 ffs->setup_state = FFS_NO_SETUP;
264 pr_debug("bogus ep0 stall!\n");
269 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
270 size_t len, loff_t *ptr)
272 struct ffs_data *ffs = file->private_data;
278 /* Fast check if setup was canceled */
279 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
283 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
284 if (unlikely(ret < 0))
288 switch (ffs->state) {
289 case FFS_READ_DESCRIPTORS:
290 case FFS_READ_STRINGS:
292 if (unlikely(len < 16)) {
297 data = ffs_prepare_buffer(buf, len);
304 if (ffs->state == FFS_READ_DESCRIPTORS) {
305 pr_info("read descriptors\n");
306 ret = __ffs_data_got_descs(ffs, data, len);
307 if (unlikely(ret < 0))
310 ffs->state = FFS_READ_STRINGS;
313 pr_info("read strings\n");
314 ret = __ffs_data_got_strings(ffs, data, len);
315 if (unlikely(ret < 0))
318 ret = ffs_epfiles_create(ffs);
320 ffs->state = FFS_CLOSING;
324 ffs->state = FFS_ACTIVE;
325 mutex_unlock(&ffs->mutex);
327 ret = ffs_ready(ffs);
328 if (unlikely(ret < 0)) {
329 ffs->state = FFS_CLOSING;
340 * We're called from user space, we can use _irq
341 * rather then _irqsave
343 spin_lock_irq(&ffs->ev.waitq.lock);
344 switch (ffs_setup_state_clear_cancelled(ffs)) {
345 case FFS_SETUP_CANCELLED:
353 case FFS_SETUP_PENDING:
357 /* FFS_SETUP_PENDING */
358 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
359 spin_unlock_irq(&ffs->ev.waitq.lock);
360 ret = __ffs_ep0_stall(ffs);
364 /* FFS_SETUP_PENDING and not stall */
365 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
367 spin_unlock_irq(&ffs->ev.waitq.lock);
369 data = ffs_prepare_buffer(buf, len);
375 spin_lock_irq(&ffs->ev.waitq.lock);
378 * We are guaranteed to be still in FFS_ACTIVE state
379 * but the state of setup could have changed from
380 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
381 * to check for that. If that happened we copied data
382 * from user space in vain but it's unlikely.
384 * For sure we are not in FFS_NO_SETUP since this is
385 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
386 * transition can be performed and it's protected by
389 if (ffs_setup_state_clear_cancelled(ffs) ==
390 FFS_SETUP_CANCELLED) {
393 spin_unlock_irq(&ffs->ev.waitq.lock);
395 /* unlocks spinlock */
396 ret = __ffs_ep0_queue_wait(ffs, data, len);
406 mutex_unlock(&ffs->mutex);
410 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
411 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
415 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
416 * size of ffs->ev.types array (which is four) so that's how much space
419 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
420 const size_t size = n * sizeof *events;
423 memset(events, 0, size);
426 events[i].type = ffs->ev.types[i];
427 if (events[i].type == FUNCTIONFS_SETUP) {
428 events[i].u.setup = ffs->ev.setup;
429 ffs->setup_state = FFS_SETUP_PENDING;
435 memmove(ffs->ev.types, ffs->ev.types + n,
436 ffs->ev.count * sizeof *ffs->ev.types);
438 spin_unlock_irq(&ffs->ev.waitq.lock);
439 mutex_unlock(&ffs->mutex);
441 return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
444 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
445 size_t len, loff_t *ptr)
447 struct ffs_data *ffs = file->private_data;
454 /* Fast check if setup was canceled */
455 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
459 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
460 if (unlikely(ret < 0))
464 if (ffs->state != FFS_ACTIVE) {
470 * We're called from user space, we can use _irq rather then
473 spin_lock_irq(&ffs->ev.waitq.lock);
475 switch (ffs_setup_state_clear_cancelled(ffs)) {
476 case FFS_SETUP_CANCELLED:
481 n = len / sizeof(struct usb_functionfs_event);
487 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
492 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
498 return __ffs_ep0_read_events(ffs, buf,
499 min(n, (size_t)ffs->ev.count));
501 case FFS_SETUP_PENDING:
502 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
503 spin_unlock_irq(&ffs->ev.waitq.lock);
504 ret = __ffs_ep0_stall(ffs);
508 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
510 spin_unlock_irq(&ffs->ev.waitq.lock);
513 data = kmalloc(len, GFP_KERNEL);
514 if (unlikely(!data)) {
520 spin_lock_irq(&ffs->ev.waitq.lock);
522 /* See ffs_ep0_write() */
523 if (ffs_setup_state_clear_cancelled(ffs) ==
524 FFS_SETUP_CANCELLED) {
529 /* unlocks spinlock */
530 ret = __ffs_ep0_queue_wait(ffs, data, len);
531 if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
540 spin_unlock_irq(&ffs->ev.waitq.lock);
542 mutex_unlock(&ffs->mutex);
547 static int ffs_ep0_open(struct inode *inode, struct file *file)
549 struct ffs_data *ffs = inode->i_private;
553 if (unlikely(ffs->state == FFS_CLOSING))
556 file->private_data = ffs;
557 ffs_data_opened(ffs);
562 static int ffs_ep0_release(struct inode *inode, struct file *file)
564 struct ffs_data *ffs = file->private_data;
568 ffs_data_closed(ffs);
573 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
575 struct ffs_data *ffs = file->private_data;
576 struct usb_gadget *gadget = ffs->gadget;
581 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
582 struct ffs_function *func = ffs->func;
583 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
584 } else if (gadget && gadget->ops->ioctl) {
585 ret = gadget->ops->ioctl(gadget, code, value);
593 static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
595 struct ffs_data *ffs = file->private_data;
596 unsigned int mask = POLLWRNORM;
599 poll_wait(file, &ffs->ev.waitq, wait);
601 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
602 if (unlikely(ret < 0))
605 switch (ffs->state) {
606 case FFS_READ_DESCRIPTORS:
607 case FFS_READ_STRINGS:
612 switch (ffs->setup_state) {
618 case FFS_SETUP_PENDING:
619 case FFS_SETUP_CANCELLED:
620 mask |= (POLLIN | POLLOUT);
625 case FFS_DEACTIVATED:
629 mutex_unlock(&ffs->mutex);
634 static const struct file_operations ffs_ep0_operations = {
637 .open = ffs_ep0_open,
638 .write = ffs_ep0_write,
639 .read = ffs_ep0_read,
640 .release = ffs_ep0_release,
641 .unlocked_ioctl = ffs_ep0_ioctl,
642 .poll = ffs_ep0_poll,
646 /* "Normal" endpoints operations ********************************************/
648 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
651 if (likely(req->context)) {
652 struct ffs_ep *ep = _ep->driver_data;
653 ep->status = req->status ? req->status : req->actual;
654 complete(req->context);
658 static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
660 ssize_t ret = copy_to_iter(data, data_len, iter);
661 if (likely(ret == data_len))
664 if (unlikely(iov_iter_count(iter)))
668 * Dear user space developer!
670 * TL;DR: To stop getting below error message in your kernel log, change
671 * user space code using functionfs to align read buffers to a max
674 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
675 * packet size. When unaligned buffer is passed to functionfs, it
676 * internally uses a larger, aligned buffer so that such UDCs are happy.
678 * Unfortunately, this means that host may send more data than was
679 * requested in read(2) system call. f_fs doesn’t know what to do with
680 * that excess data so it simply drops it.
682 * Was the buffer aligned in the first place, no such problem would
685 * Data may be dropped only in AIO reads. Synchronous reads are handled
686 * by splitting a request into multiple parts. This splitting may still
687 * be a problem though so it’s likely best to align the buffer
688 * regardless of it being AIO or not..
690 * This only affects OUT endpoints, i.e. reading data with a read(2),
691 * aio_read(2) etc. system calls. Writing data to an IN endpoint is not
694 pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
695 "Align read buffer size to max packet size to avoid the problem.\n",
701 static void ffs_user_copy_worker(struct work_struct *work)
703 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
705 int ret = io_data->req->status ? io_data->req->status :
706 io_data->req->actual;
707 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
709 if (io_data->read && ret > 0) {
711 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
712 unuse_mm(io_data->mm);
715 io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
717 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
718 eventfd_signal(io_data->ffs->ffs_eventfd, 1);
720 usb_ep_free_request(io_data->ep, io_data->req);
723 kfree(io_data->to_free);
728 static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
729 struct usb_request *req)
731 struct ffs_io_data *io_data = req->context;
735 INIT_WORK(&io_data->work, ffs_user_copy_worker);
736 schedule_work(&io_data->work);
739 /* Assumes epfile->mutex is held. */
740 static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
741 struct iov_iter *iter)
743 struct ffs_buffer *buf = epfile->read_buffer;
748 ret = copy_to_iter(buf->data, buf->length, iter);
749 if (buf->length == ret) {
751 epfile->read_buffer = NULL;
752 } else if (unlikely(iov_iter_count(iter))) {
761 /* Assumes epfile->mutex is held. */
762 static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
763 void *data, int data_len,
764 struct iov_iter *iter)
766 struct ffs_buffer *buf;
768 ssize_t ret = copy_to_iter(data, data_len, iter);
769 if (likely(data_len == ret))
772 if (unlikely(iov_iter_count(iter)))
775 /* See ffs_copy_to_iter for more context. */
776 pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
780 buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
783 buf->length = data_len;
784 buf->data = buf->storage;
785 memcpy(buf->storage, data + ret, data_len);
786 epfile->read_buffer = buf;
791 static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
793 struct ffs_epfile *epfile = file->private_data;
794 struct usb_request *req;
797 ssize_t ret, data_len = -EINVAL;
800 /* Are we still active? */
801 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
804 /* Wait for endpoint to be enabled */
807 if (file->f_flags & O_NONBLOCK)
810 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
816 halt = (!io_data->read == !epfile->in);
817 if (halt && epfile->isoc)
820 /* We will be using request and read_buffer */
821 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
825 /* Allocate & copy */
827 struct usb_gadget *gadget;
830 * Do we have buffered data from previous partial read? Check
831 * that for synchronous case only because we do not have
832 * facility to ‘wake up’ a pending asynchronous read and push
833 * buffered data to it which we would need to make things behave
836 if (!io_data->aio && io_data->read) {
837 ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
843 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
844 * before the waiting completes, so do not assign to 'gadget'
847 gadget = epfile->ffs->gadget;
849 spin_lock_irq(&epfile->ffs->eps_lock);
850 /* In the meantime, endpoint got disabled or changed. */
851 if (epfile->ep != ep) {
855 data_len = iov_iter_count(&io_data->data);
857 * Controller may require buffer size to be aligned to
858 * maxpacketsize of an out endpoint.
861 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
862 spin_unlock_irq(&epfile->ffs->eps_lock);
864 data = kmalloc(data_len, GFP_KERNEL);
865 if (unlikely(!data)) {
869 if (!io_data->read &&
870 copy_from_iter(data, data_len, &io_data->data) != data_len) {
876 spin_lock_irq(&epfile->ffs->eps_lock);
878 if (epfile->ep != ep) {
879 /* In the meantime, endpoint got disabled or changed. */
883 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
884 usb_ep_set_halt(ep->ep);
886 } else if (unlikely(data_len == -EINVAL)) {
888 * Sanity Check: even though data_len can't be used
889 * uninitialized at the time I write this comment, some
890 * compilers complain about this situation.
891 * In order to keep the code clean from warnings, data_len is
892 * being initialized to -EINVAL during its declaration, which
893 * means we can't rely on compiler anymore to warn no future
894 * changes won't result in data_len being used uninitialized.
895 * For such reason, we're adding this redundant sanity check
898 WARN(1, "%s: data_len == -EINVAL\n", __func__);
900 } else if (!io_data->aio) {
901 DECLARE_COMPLETION_ONSTACK(done);
902 bool interrupted = false;
906 req->length = data_len;
908 req->context = &done;
909 req->complete = ffs_epfile_io_complete;
911 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
912 if (unlikely(ret < 0))
915 spin_unlock_irq(&epfile->ffs->eps_lock);
917 if (unlikely(wait_for_completion_interruptible(&done))) {
919 * To avoid race condition with ffs_epfile_io_complete,
920 * dequeue the request first then check
921 * status. usb_ep_dequeue API should guarantee no race
922 * condition with req->complete callback.
924 usb_ep_dequeue(ep->ep, req);
925 interrupted = ep->status < 0;
930 else if (io_data->read && ep->status > 0)
931 ret = __ffs_epfile_read_data(epfile, data, ep->status,
936 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
940 req->length = data_len;
943 io_data->ep = ep->ep;
945 io_data->ffs = epfile->ffs;
947 req->context = io_data;
948 req->complete = ffs_epfile_async_io_complete;
950 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
952 usb_ep_free_request(ep->ep, req);
958 * Do not kfree the buffer in this function. It will be freed
959 * by ffs_user_copy_worker.
965 spin_unlock_irq(&epfile->ffs->eps_lock);
967 mutex_unlock(&epfile->mutex);
974 ffs_epfile_open(struct inode *inode, struct file *file)
976 struct ffs_epfile *epfile = inode->i_private;
980 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
983 file->private_data = epfile;
984 ffs_data_opened(epfile->ffs);
989 static int ffs_aio_cancel(struct kiocb *kiocb)
991 struct ffs_io_data *io_data = kiocb->private;
992 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
997 spin_lock_irq(&epfile->ffs->eps_lock);
999 if (likely(io_data && io_data->ep && io_data->req))
1000 value = usb_ep_dequeue(io_data->ep, io_data->req);
1004 spin_unlock_irq(&epfile->ffs->eps_lock);
1009 static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
1011 struct ffs_io_data io_data, *p = &io_data;
1016 if (!is_sync_kiocb(kiocb)) {
1017 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1028 p->mm = current->mm;
1033 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1035 res = ffs_epfile_io(kiocb->ki_filp, p);
1036 if (res == -EIOCBQUEUED)
1045 static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
1047 struct ffs_io_data io_data, *p = &io_data;
1052 if (!is_sync_kiocb(kiocb)) {
1053 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1064 p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
1073 p->mm = current->mm;
1078 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1080 res = ffs_epfile_io(kiocb->ki_filp, p);
1081 if (res == -EIOCBQUEUED)
1094 ffs_epfile_release(struct inode *inode, struct file *file)
1096 struct ffs_epfile *epfile = inode->i_private;
1100 kfree(epfile->read_buffer);
1101 epfile->read_buffer = NULL;
1102 ffs_data_closed(epfile->ffs);
1107 static long ffs_epfile_ioctl(struct file *file, unsigned code,
1108 unsigned long value)
1110 struct ffs_epfile *epfile = file->private_data;
1115 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1118 spin_lock_irq(&epfile->ffs->eps_lock);
1119 if (likely(epfile->ep)) {
1121 case FUNCTIONFS_FIFO_STATUS:
1122 ret = usb_ep_fifo_status(epfile->ep->ep);
1124 case FUNCTIONFS_FIFO_FLUSH:
1125 usb_ep_fifo_flush(epfile->ep->ep);
1128 case FUNCTIONFS_CLEAR_HALT:
1129 ret = usb_ep_clear_halt(epfile->ep->ep);
1131 case FUNCTIONFS_ENDPOINT_REVMAP:
1132 ret = epfile->ep->num;
1134 case FUNCTIONFS_ENDPOINT_DESC:
1137 struct usb_endpoint_descriptor *desc;
1139 switch (epfile->ffs->gadget->speed) {
1140 case USB_SPEED_SUPER:
1143 case USB_SPEED_HIGH:
1149 desc = epfile->ep->descs[desc_idx];
1151 spin_unlock_irq(&epfile->ffs->eps_lock);
1152 ret = copy_to_user((void *)value, desc, sizeof(*desc));
1163 spin_unlock_irq(&epfile->ffs->eps_lock);
1168 static const struct file_operations ffs_epfile_operations = {
1169 .llseek = no_llseek,
1171 .open = ffs_epfile_open,
1172 .write_iter = ffs_epfile_write_iter,
1173 .read_iter = ffs_epfile_read_iter,
1174 .release = ffs_epfile_release,
1175 .unlocked_ioctl = ffs_epfile_ioctl,
1179 /* File system and super block operations ***********************************/
1182 * Mounting the file system creates a controller file, used first for
1183 * function configuration then later for event monitoring.
1186 static struct inode *__must_check
1187 ffs_sb_make_inode(struct super_block *sb, void *data,
1188 const struct file_operations *fops,
1189 const struct inode_operations *iops,
1190 struct ffs_file_perms *perms)
1192 struct inode *inode;
1196 inode = new_inode(sb);
1198 if (likely(inode)) {
1199 struct timespec current_time = CURRENT_TIME;
1201 inode->i_ino = get_next_ino();
1202 inode->i_mode = perms->mode;
1203 inode->i_uid = perms->uid;
1204 inode->i_gid = perms->gid;
1205 inode->i_atime = current_time;
1206 inode->i_mtime = current_time;
1207 inode->i_ctime = current_time;
1208 inode->i_private = data;
1210 inode->i_fop = fops;
1218 /* Create "regular" file */
1219 static struct dentry *ffs_sb_create_file(struct super_block *sb,
1220 const char *name, void *data,
1221 const struct file_operations *fops)
1223 struct ffs_data *ffs = sb->s_fs_info;
1224 struct dentry *dentry;
1225 struct inode *inode;
1229 dentry = d_alloc_name(sb->s_root, name);
1230 if (unlikely(!dentry))
1233 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1234 if (unlikely(!inode)) {
1239 d_add(dentry, inode);
1244 static const struct super_operations ffs_sb_operations = {
1245 .statfs = simple_statfs,
1246 .drop_inode = generic_delete_inode,
1249 struct ffs_sb_fill_data {
1250 struct ffs_file_perms perms;
1252 const char *dev_name;
1254 struct ffs_data *ffs_data;
1257 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1259 struct ffs_sb_fill_data *data = _data;
1260 struct inode *inode;
1261 struct ffs_data *ffs = data->ffs_data;
1266 data->ffs_data = NULL;
1267 sb->s_fs_info = ffs;
1268 sb->s_blocksize = PAGE_SIZE;
1269 sb->s_blocksize_bits = PAGE_SHIFT;
1270 sb->s_magic = FUNCTIONFS_MAGIC;
1271 sb->s_op = &ffs_sb_operations;
1272 sb->s_time_gran = 1;
1275 data->perms.mode = data->root_mode;
1276 inode = ffs_sb_make_inode(sb, NULL,
1277 &simple_dir_operations,
1278 &simple_dir_inode_operations,
1280 sb->s_root = d_make_root(inode);
1281 if (unlikely(!sb->s_root))
1285 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1286 &ffs_ep0_operations)))
1292 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1296 if (!opts || !*opts)
1300 unsigned long value;
1304 comma = strchr(opts, ',');
1309 eq = strchr(opts, '=');
1310 if (unlikely(!eq)) {
1311 pr_err("'=' missing in %s\n", opts);
1317 if (kstrtoul(eq + 1, 0, &value)) {
1318 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1322 /* Interpret option */
1323 switch (eq - opts) {
1325 if (!memcmp(opts, "no_disconnect", 13))
1326 data->no_disconnect = !!value;
1331 if (!memcmp(opts, "rmode", 5))
1332 data->root_mode = (value & 0555) | S_IFDIR;
1333 else if (!memcmp(opts, "fmode", 5))
1334 data->perms.mode = (value & 0666) | S_IFREG;
1340 if (!memcmp(opts, "mode", 4)) {
1341 data->root_mode = (value & 0555) | S_IFDIR;
1342 data->perms.mode = (value & 0666) | S_IFREG;
1349 if (!memcmp(opts, "uid", 3)) {
1350 data->perms.uid = make_kuid(current_user_ns(), value);
1351 if (!uid_valid(data->perms.uid)) {
1352 pr_err("%s: unmapped value: %lu\n", opts, value);
1355 } else if (!memcmp(opts, "gid", 3)) {
1356 data->perms.gid = make_kgid(current_user_ns(), value);
1357 if (!gid_valid(data->perms.gid)) {
1358 pr_err("%s: unmapped value: %lu\n", opts, value);
1368 pr_err("%s: invalid option\n", opts);
1372 /* Next iteration */
1381 /* "mount -t functionfs dev_name /dev/function" ends up here */
1383 static struct dentry *
1384 ffs_fs_mount(struct file_system_type *t, int flags,
1385 const char *dev_name, void *opts)
1387 struct ffs_sb_fill_data data = {
1389 .mode = S_IFREG | 0600,
1390 .uid = GLOBAL_ROOT_UID,
1391 .gid = GLOBAL_ROOT_GID,
1393 .root_mode = S_IFDIR | 0500,
1394 .no_disconnect = false,
1399 struct ffs_data *ffs;
1403 ret = ffs_fs_parse_opts(&data, opts);
1404 if (unlikely(ret < 0))
1405 return ERR_PTR(ret);
1407 ffs = ffs_data_new();
1409 return ERR_PTR(-ENOMEM);
1410 ffs->file_perms = data.perms;
1411 ffs->no_disconnect = data.no_disconnect;
1413 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1414 if (unlikely(!ffs->dev_name)) {
1416 return ERR_PTR(-ENOMEM);
1419 ffs_dev = ffs_acquire_dev(dev_name);
1420 if (IS_ERR(ffs_dev)) {
1422 return ERR_CAST(ffs_dev);
1424 ffs->private_data = ffs_dev;
1425 data.ffs_data = ffs;
1427 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1428 if (IS_ERR(rv) && data.ffs_data) {
1429 ffs_release_dev(data.ffs_data);
1430 ffs_data_put(data.ffs_data);
1436 ffs_fs_kill_sb(struct super_block *sb)
1440 kill_litter_super(sb);
1441 if (sb->s_fs_info) {
1442 ffs_release_dev(sb->s_fs_info);
1443 ffs_data_closed(sb->s_fs_info);
1444 ffs_data_put(sb->s_fs_info);
1448 static struct file_system_type ffs_fs_type = {
1449 .owner = THIS_MODULE,
1450 .name = "functionfs",
1451 .mount = ffs_fs_mount,
1452 .kill_sb = ffs_fs_kill_sb,
1454 MODULE_ALIAS_FS("functionfs");
1457 /* Driver's main init/cleanup functions *************************************/
1459 static int functionfs_init(void)
1465 ret = register_filesystem(&ffs_fs_type);
1467 pr_info("file system registered\n");
1469 pr_err("failed registering file system (%d)\n", ret);
1474 static void functionfs_cleanup(void)
1478 pr_info("unloading\n");
1479 unregister_filesystem(&ffs_fs_type);
1483 /* ffs_data and ffs_function construction and destruction code **************/
1485 static void ffs_data_clear(struct ffs_data *ffs);
1486 static void ffs_data_reset(struct ffs_data *ffs);
1488 static void ffs_data_get(struct ffs_data *ffs)
1492 atomic_inc(&ffs->ref);
1495 static void ffs_data_opened(struct ffs_data *ffs)
1499 atomic_inc(&ffs->ref);
1500 if (atomic_add_return(1, &ffs->opened) == 1 &&
1501 ffs->state == FFS_DEACTIVATED) {
1502 ffs->state = FFS_CLOSING;
1503 ffs_data_reset(ffs);
1507 static void ffs_data_put(struct ffs_data *ffs)
1511 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1512 pr_info("%s(): freeing\n", __func__);
1513 ffs_data_clear(ffs);
1514 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1515 waitqueue_active(&ffs->ep0req_completion.wait));
1516 kfree(ffs->dev_name);
1521 static void ffs_data_closed(struct ffs_data *ffs)
1525 if (atomic_dec_and_test(&ffs->opened)) {
1526 if (ffs->no_disconnect) {
1527 ffs->state = FFS_DEACTIVATED;
1529 ffs_epfiles_destroy(ffs->epfiles,
1531 ffs->epfiles = NULL;
1533 if (ffs->setup_state == FFS_SETUP_PENDING)
1534 __ffs_ep0_stall(ffs);
1536 ffs->state = FFS_CLOSING;
1537 ffs_data_reset(ffs);
1540 if (atomic_read(&ffs->opened) < 0) {
1541 ffs->state = FFS_CLOSING;
1542 ffs_data_reset(ffs);
1548 static struct ffs_data *ffs_data_new(void)
1550 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1556 atomic_set(&ffs->ref, 1);
1557 atomic_set(&ffs->opened, 0);
1558 ffs->state = FFS_READ_DESCRIPTORS;
1559 mutex_init(&ffs->mutex);
1560 spin_lock_init(&ffs->eps_lock);
1561 init_waitqueue_head(&ffs->ev.waitq);
1562 init_completion(&ffs->ep0req_completion);
1564 /* XXX REVISIT need to update it in some places, or do we? */
1565 ffs->ev.can_stall = 1;
1570 static void ffs_data_clear(struct ffs_data *ffs)
1576 BUG_ON(ffs->gadget);
1579 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1581 if (ffs->ffs_eventfd)
1582 eventfd_ctx_put(ffs->ffs_eventfd);
1584 kfree(ffs->raw_descs_data);
1585 kfree(ffs->raw_strings);
1586 kfree(ffs->stringtabs);
1589 static void ffs_data_reset(struct ffs_data *ffs)
1593 ffs_data_clear(ffs);
1595 ffs->epfiles = NULL;
1596 ffs->raw_descs_data = NULL;
1597 ffs->raw_descs = NULL;
1598 ffs->raw_strings = NULL;
1599 ffs->stringtabs = NULL;
1601 ffs->raw_descs_length = 0;
1602 ffs->fs_descs_count = 0;
1603 ffs->hs_descs_count = 0;
1604 ffs->ss_descs_count = 0;
1606 ffs->strings_count = 0;
1607 ffs->interfaces_count = 0;
1612 ffs->state = FFS_READ_DESCRIPTORS;
1613 ffs->setup_state = FFS_NO_SETUP;
1618 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1620 struct usb_gadget_strings **lang;
1625 if (WARN_ON(ffs->state != FFS_ACTIVE
1626 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1629 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1630 if (unlikely(first_id < 0))
1633 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1634 if (unlikely(!ffs->ep0req))
1636 ffs->ep0req->complete = ffs_ep0_complete;
1637 ffs->ep0req->context = ffs;
1639 lang = ffs->stringtabs;
1641 for (; *lang; ++lang) {
1642 struct usb_string *str = (*lang)->strings;
1644 for (; str->s; ++id, ++str)
1649 ffs->gadget = cdev->gadget;
1654 static void functionfs_unbind(struct ffs_data *ffs)
1658 if (!WARN_ON(!ffs->gadget)) {
1659 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1662 clear_bit(FFS_FL_BOUND, &ffs->flags);
1667 static int ffs_epfiles_create(struct ffs_data *ffs)
1669 struct ffs_epfile *epfile, *epfiles;
1674 count = ffs->eps_count;
1675 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1680 for (i = 1; i <= count; ++i, ++epfile) {
1682 mutex_init(&epfile->mutex);
1683 init_waitqueue_head(&epfile->wait);
1684 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1685 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
1687 sprintf(epfile->name, "ep%u", i);
1688 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
1690 &ffs_epfile_operations);
1691 if (unlikely(!epfile->dentry)) {
1692 ffs_epfiles_destroy(epfiles, i - 1);
1697 ffs->epfiles = epfiles;
1701 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1703 struct ffs_epfile *epfile = epfiles;
1707 for (; count; --count, ++epfile) {
1708 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1709 waitqueue_active(&epfile->wait));
1710 if (epfile->dentry) {
1711 d_delete(epfile->dentry);
1712 dput(epfile->dentry);
1713 epfile->dentry = NULL;
1720 static void ffs_func_eps_disable(struct ffs_function *func)
1722 struct ffs_ep *ep = func->eps;
1723 struct ffs_epfile *epfile = func->ffs->epfiles;
1724 unsigned count = func->ffs->eps_count;
1725 unsigned long flags;
1729 mutex_lock(&epfile->mutex);
1730 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1731 /* pending requests get nuked */
1733 usb_ep_disable(ep->ep);
1735 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1739 kfree(epfile->read_buffer);
1740 epfile->read_buffer = NULL;
1741 mutex_unlock(&epfile->mutex);
1747 static int ffs_func_eps_enable(struct ffs_function *func)
1749 struct ffs_data *ffs = func->ffs;
1750 struct ffs_ep *ep = func->eps;
1751 struct ffs_epfile *epfile = ffs->epfiles;
1752 unsigned count = ffs->eps_count;
1753 unsigned long flags;
1756 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1758 struct usb_endpoint_descriptor *ds;
1761 if (ffs->gadget->speed == USB_SPEED_SUPER)
1763 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1768 /* fall-back to lower speed if desc missing for current speed */
1770 ds = ep->descs[desc_idx];
1771 } while (!ds && --desc_idx >= 0);
1778 ep->ep->driver_data = ep;
1780 ret = usb_ep_enable(ep->ep);
1783 epfile->in = usb_endpoint_dir_in(ds);
1784 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1789 wake_up(&epfile->wait);
1794 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1800 /* Parsing and building descriptors and strings *****************************/
1803 * This validates if data pointed by data is a valid USB descriptor as
1804 * well as record how many interfaces, endpoints and strings are
1805 * required by given configuration. Returns address after the
1806 * descriptor or NULL if data is invalid.
1809 enum ffs_entity_type {
1810 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1813 enum ffs_os_desc_type {
1814 FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
1817 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1819 struct usb_descriptor_header *desc,
1822 typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
1823 struct usb_os_desc_header *h, void *data,
1824 unsigned len, void *priv);
1826 static int __must_check ffs_do_single_desc(char *data, unsigned len,
1827 ffs_entity_callback entity,
1830 struct usb_descriptor_header *_ds = (void *)data;
1836 /* At least two bytes are required: length and type */
1838 pr_vdebug("descriptor too short\n");
1842 /* If we have at least as many bytes as the descriptor takes? */
1843 length = _ds->bLength;
1845 pr_vdebug("descriptor longer then available data\n");
1849 #define __entity_check_INTERFACE(val) 1
1850 #define __entity_check_STRING(val) (val)
1851 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1852 #define __entity(type, val) do { \
1853 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1854 if (unlikely(!__entity_check_ ##type(val))) { \
1855 pr_vdebug("invalid entity's value\n"); \
1858 ret = entity(FFS_ ##type, &val, _ds, priv); \
1859 if (unlikely(ret < 0)) { \
1860 pr_debug("entity " #type "(%02x); ret = %d\n", \
1866 /* Parse descriptor depending on type. */
1867 switch (_ds->bDescriptorType) {
1871 case USB_DT_DEVICE_QUALIFIER:
1872 /* function can't have any of those */
1873 pr_vdebug("descriptor reserved for gadget: %d\n",
1874 _ds->bDescriptorType);
1877 case USB_DT_INTERFACE: {
1878 struct usb_interface_descriptor *ds = (void *)_ds;
1879 pr_vdebug("interface descriptor\n");
1880 if (length != sizeof *ds)
1883 __entity(INTERFACE, ds->bInterfaceNumber);
1885 __entity(STRING, ds->iInterface);
1889 case USB_DT_ENDPOINT: {
1890 struct usb_endpoint_descriptor *ds = (void *)_ds;
1891 pr_vdebug("endpoint descriptor\n");
1892 if (length != USB_DT_ENDPOINT_SIZE &&
1893 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1895 __entity(ENDPOINT, ds->bEndpointAddress);
1900 pr_vdebug("hid descriptor\n");
1901 if (length != sizeof(struct hid_descriptor))
1906 if (length != sizeof(struct usb_otg_descriptor))
1910 case USB_DT_INTERFACE_ASSOCIATION: {
1911 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1912 pr_vdebug("interface association descriptor\n");
1913 if (length != sizeof *ds)
1916 __entity(STRING, ds->iFunction);
1920 case USB_DT_SS_ENDPOINT_COMP:
1921 pr_vdebug("EP SS companion descriptor\n");
1922 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1926 case USB_DT_OTHER_SPEED_CONFIG:
1927 case USB_DT_INTERFACE_POWER:
1929 case USB_DT_SECURITY:
1930 case USB_DT_CS_RADIO_CONTROL:
1932 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1936 /* We should never be here */
1937 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1941 pr_vdebug("invalid length: %d (descriptor %d)\n",
1942 _ds->bLength, _ds->bDescriptorType);
1947 #undef __entity_check_DESCRIPTOR
1948 #undef __entity_check_INTERFACE
1949 #undef __entity_check_STRING
1950 #undef __entity_check_ENDPOINT
1955 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1956 ffs_entity_callback entity, void *priv)
1958 const unsigned _len = len;
1959 unsigned long num = 0;
1969 /* Record "descriptor" entity */
1970 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1971 if (unlikely(ret < 0)) {
1972 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1980 ret = ffs_do_single_desc(data, len, entity, priv);
1981 if (unlikely(ret < 0)) {
1982 pr_debug("%s returns %d\n", __func__, ret);
1992 static int __ffs_data_do_entity(enum ffs_entity_type type,
1993 u8 *valuep, struct usb_descriptor_header *desc,
1996 struct ffs_desc_helper *helper = priv;
1997 struct usb_endpoint_descriptor *d;
2002 case FFS_DESCRIPTOR:
2007 * Interfaces are indexed from zero so if we
2008 * encountered interface "n" then there are at least
2011 if (*valuep >= helper->interfaces_count)
2012 helper->interfaces_count = *valuep + 1;
2017 * Strings are indexed from 1 (0 is magic ;) reserved
2018 * for languages list or some such)
2020 if (*valuep > helper->ffs->strings_count)
2021 helper->ffs->strings_count = *valuep;
2026 helper->eps_count++;
2027 if (helper->eps_count >= 15)
2029 /* Check if descriptors for any speed were already parsed */
2030 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
2031 helper->ffs->eps_addrmap[helper->eps_count] =
2032 d->bEndpointAddress;
2033 else if (helper->ffs->eps_addrmap[helper->eps_count] !=
2034 d->bEndpointAddress)
2042 static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
2043 struct usb_os_desc_header *desc)
2045 u16 bcd_version = le16_to_cpu(desc->bcdVersion);
2046 u16 w_index = le16_to_cpu(desc->wIndex);
2048 if (bcd_version != 1) {
2049 pr_vdebug("unsupported os descriptors version: %d",
2055 *next_type = FFS_OS_DESC_EXT_COMPAT;
2058 *next_type = FFS_OS_DESC_EXT_PROP;
2061 pr_vdebug("unsupported os descriptor type: %d", w_index);
2065 return sizeof(*desc);
2069 * Process all extended compatibility/extended property descriptors
2070 * of a feature descriptor
2072 static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
2073 enum ffs_os_desc_type type,
2075 ffs_os_desc_callback entity,
2077 struct usb_os_desc_header *h)
2080 const unsigned _len = len;
2084 /* loop over all ext compat/ext prop descriptors */
2085 while (feature_count--) {
2086 ret = entity(type, h, data, len, priv);
2087 if (unlikely(ret < 0)) {
2088 pr_debug("bad OS descriptor, type: %d\n", type);
2097 /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2098 static int __must_check ffs_do_os_descs(unsigned count,
2099 char *data, unsigned len,
2100 ffs_os_desc_callback entity, void *priv)
2102 const unsigned _len = len;
2103 unsigned long num = 0;
2107 for (num = 0; num < count; ++num) {
2109 enum ffs_os_desc_type type;
2111 struct usb_os_desc_header *desc = (void *)data;
2113 if (len < sizeof(*desc))
2117 * Record "descriptor" entity.
2118 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2119 * Move the data pointer to the beginning of extended
2120 * compatibilities proper or extended properties proper
2121 * portions of the data
2123 if (le32_to_cpu(desc->dwLength) > len)
2126 ret = __ffs_do_os_desc_header(&type, desc);
2127 if (unlikely(ret < 0)) {
2128 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2133 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2135 feature_count = le16_to_cpu(desc->wCount);
2136 if (type == FFS_OS_DESC_EXT_COMPAT &&
2137 (feature_count > 255 || desc->Reserved))
2143 * Process all function/property descriptors
2144 * of this Feature Descriptor
2146 ret = ffs_do_single_os_desc(data, len, type,
2147 feature_count, entity, priv, desc);
2148 if (unlikely(ret < 0)) {
2149 pr_debug("%s returns %d\n", __func__, ret);
2160 * Validate contents of the buffer from userspace related to OS descriptors.
2162 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2163 struct usb_os_desc_header *h, void *data,
2164 unsigned len, void *priv)
2166 struct ffs_data *ffs = priv;
2172 case FFS_OS_DESC_EXT_COMPAT: {
2173 struct usb_ext_compat_desc *d = data;
2176 if (len < sizeof(*d) ||
2177 d->bFirstInterfaceNumber >= ffs->interfaces_count ||
2180 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2181 if (d->Reserved2[i])
2184 length = sizeof(struct usb_ext_compat_desc);
2187 case FFS_OS_DESC_EXT_PROP: {
2188 struct usb_ext_prop_desc *d = data;
2192 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2194 length = le32_to_cpu(d->dwSize);
2195 type = le32_to_cpu(d->dwPropertyDataType);
2196 if (type < USB_EXT_PROP_UNICODE ||
2197 type > USB_EXT_PROP_UNICODE_MULTI) {
2198 pr_vdebug("unsupported os descriptor property type: %d",
2202 pnl = le16_to_cpu(d->wPropertyNameLength);
2203 pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
2204 if (length != 14 + pnl + pdl) {
2205 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2206 length, pnl, pdl, type);
2209 ++ffs->ms_os_descs_ext_prop_count;
2210 /* property name reported to the host as "WCHAR"s */
2211 ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2212 ffs->ms_os_descs_ext_prop_data_len += pdl;
2216 pr_vdebug("unknown descriptor: %d\n", type);
2222 static int __ffs_data_got_descs(struct ffs_data *ffs,
2223 char *const _data, size_t len)
2225 char *data = _data, *raw_descs;
2226 unsigned os_descs_count = 0, counts[3], flags;
2227 int ret = -EINVAL, i;
2228 struct ffs_desc_helper helper;
2232 if (get_unaligned_le32(data + 4) != len)
2235 switch (get_unaligned_le32(data)) {
2236 case FUNCTIONFS_DESCRIPTORS_MAGIC:
2237 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
2241 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2242 flags = get_unaligned_le32(data + 8);
2243 ffs->user_flags = flags;
2244 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2245 FUNCTIONFS_HAS_HS_DESC |
2246 FUNCTIONFS_HAS_SS_DESC |
2247 FUNCTIONFS_HAS_MS_OS_DESC |
2248 FUNCTIONFS_VIRTUAL_ADDR |
2249 FUNCTIONFS_EVENTFD |
2250 FUNCTIONFS_ALL_CTRL_RECIP |
2251 FUNCTIONFS_CONFIG0_SETUP)) {
2262 if (flags & FUNCTIONFS_EVENTFD) {
2266 eventfd_ctx_fdget((int)get_unaligned_le32(data));
2267 if (IS_ERR(ffs->ffs_eventfd)) {
2268 ret = PTR_ERR(ffs->ffs_eventfd);
2269 ffs->ffs_eventfd = NULL;
2276 /* Read fs_count, hs_count and ss_count (if present) */
2277 for (i = 0; i < 3; ++i) {
2278 if (!(flags & (1 << i))) {
2280 } else if (len < 4) {
2283 counts[i] = get_unaligned_le32(data);
2288 if (flags & (1 << i)) {
2289 os_descs_count = get_unaligned_le32(data);
2294 /* Read descriptors */
2297 for (i = 0; i < 3; ++i) {
2300 helper.interfaces_count = 0;
2301 helper.eps_count = 0;
2302 ret = ffs_do_descs(counts[i], data, len,
2303 __ffs_data_do_entity, &helper);
2306 if (!ffs->eps_count && !ffs->interfaces_count) {
2307 ffs->eps_count = helper.eps_count;
2308 ffs->interfaces_count = helper.interfaces_count;
2310 if (ffs->eps_count != helper.eps_count) {
2314 if (ffs->interfaces_count != helper.interfaces_count) {
2322 if (os_descs_count) {
2323 ret = ffs_do_os_descs(os_descs_count, data, len,
2324 __ffs_data_do_os_desc, ffs);
2331 if (raw_descs == data || len) {
2336 ffs->raw_descs_data = _data;
2337 ffs->raw_descs = raw_descs;
2338 ffs->raw_descs_length = data - raw_descs;
2339 ffs->fs_descs_count = counts[0];
2340 ffs->hs_descs_count = counts[1];
2341 ffs->ss_descs_count = counts[2];
2342 ffs->ms_os_descs_count = os_descs_count;
2351 static int __ffs_data_got_strings(struct ffs_data *ffs,
2352 char *const _data, size_t len)
2354 u32 str_count, needed_count, lang_count;
2355 struct usb_gadget_strings **stringtabs, *t;
2356 const char *data = _data;
2357 struct usb_string *s;
2361 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2362 get_unaligned_le32(data + 4) != len))
2364 str_count = get_unaligned_le32(data + 8);
2365 lang_count = get_unaligned_le32(data + 12);
2367 /* if one is zero the other must be zero */
2368 if (unlikely(!str_count != !lang_count))
2371 /* Do we have at least as many strings as descriptors need? */
2372 needed_count = ffs->strings_count;
2373 if (unlikely(str_count < needed_count))
2377 * If we don't need any strings just return and free all
2380 if (!needed_count) {
2385 /* Allocate everything in one chunk so there's less maintenance. */
2389 vla_item(d, struct usb_gadget_strings *, stringtabs,
2391 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2392 vla_item(d, struct usb_string, strings,
2393 lang_count*(needed_count+1));
2395 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2397 if (unlikely(!vlabuf)) {
2402 /* Initialize the VLA pointers */
2403 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2404 t = vla_ptr(vlabuf, d, stringtab);
2407 *stringtabs++ = t++;
2411 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2412 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2413 t = vla_ptr(vlabuf, d, stringtab);
2414 s = vla_ptr(vlabuf, d, strings);
2417 /* For each language */
2421 do { /* lang_count > 0 so we can use do-while */
2422 unsigned needed = needed_count;
2424 if (unlikely(len < 3))
2426 t->language = get_unaligned_le16(data);
2433 /* For each string */
2434 do { /* str_count > 0 so we can use do-while */
2435 size_t length = strnlen(data, len);
2437 if (unlikely(length == len))
2441 * User may provide more strings then we need,
2442 * if that's the case we simply ignore the
2445 if (likely(needed)) {
2447 * s->id will be set while adding
2448 * function to configuration so for
2449 * now just leave garbage here.
2458 } while (--str_count);
2460 s->id = 0; /* terminator */
2464 } while (--lang_count);
2466 /* Some garbage left? */
2471 ffs->stringtabs = stringtabs;
2472 ffs->raw_strings = _data;
2484 /* Events handling and management *******************************************/
2486 static void __ffs_event_add(struct ffs_data *ffs,
2487 enum usb_functionfs_event_type type)
2489 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2493 * Abort any unhandled setup
2495 * We do not need to worry about some cmpxchg() changing value
2496 * of ffs->setup_state without holding the lock because when
2497 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2498 * the source does nothing.
2500 if (ffs->setup_state == FFS_SETUP_PENDING)
2501 ffs->setup_state = FFS_SETUP_CANCELLED;
2504 * Logic of this function guarantees that there are at most four pending
2505 * evens on ffs->ev.types queue. This is important because the queue
2506 * has space for four elements only and __ffs_ep0_read_events function
2507 * depends on that limit as well. If more event types are added, those
2508 * limits have to be revisited or guaranteed to still hold.
2511 case FUNCTIONFS_RESUME:
2512 rem_type2 = FUNCTIONFS_SUSPEND;
2514 case FUNCTIONFS_SUSPEND:
2515 case FUNCTIONFS_SETUP:
2517 /* Discard all similar events */
2520 case FUNCTIONFS_BIND:
2521 case FUNCTIONFS_UNBIND:
2522 case FUNCTIONFS_DISABLE:
2523 case FUNCTIONFS_ENABLE:
2524 /* Discard everything other then power management. */
2525 rem_type1 = FUNCTIONFS_SUSPEND;
2526 rem_type2 = FUNCTIONFS_RESUME;
2531 WARN(1, "%d: unknown event, this should not happen\n", type);
2536 u8 *ev = ffs->ev.types, *out = ev;
2537 unsigned n = ffs->ev.count;
2538 for (; n; --n, ++ev)
2539 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2542 pr_vdebug("purging event %d\n", *ev);
2543 ffs->ev.count = out - ffs->ev.types;
2546 pr_vdebug("adding event %d\n", type);
2547 ffs->ev.types[ffs->ev.count++] = type;
2548 wake_up_locked(&ffs->ev.waitq);
2549 if (ffs->ffs_eventfd)
2550 eventfd_signal(ffs->ffs_eventfd, 1);
2553 static void ffs_event_add(struct ffs_data *ffs,
2554 enum usb_functionfs_event_type type)
2556 unsigned long flags;
2557 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2558 __ffs_event_add(ffs, type);
2559 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2562 /* Bind/unbind USB function hooks *******************************************/
2564 static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2568 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2569 if (ffs->eps_addrmap[i] == endpoint_address)
2574 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2575 struct usb_descriptor_header *desc,
2578 struct usb_endpoint_descriptor *ds = (void *)desc;
2579 struct ffs_function *func = priv;
2580 struct ffs_ep *ffs_ep;
2581 unsigned ep_desc_id;
2583 static const char *speed_names[] = { "full", "high", "super" };
2585 if (type != FFS_DESCRIPTOR)
2589 * If ss_descriptors is not NULL, we are reading super speed
2590 * descriptors; if hs_descriptors is not NULL, we are reading high
2591 * speed descriptors; otherwise, we are reading full speed
2594 if (func->function.ss_descriptors) {
2596 func->function.ss_descriptors[(long)valuep] = desc;
2597 } else if (func->function.hs_descriptors) {
2599 func->function.hs_descriptors[(long)valuep] = desc;
2602 func->function.fs_descriptors[(long)valuep] = desc;
2605 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2608 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2612 ffs_ep = func->eps + idx;
2614 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2615 pr_err("two %sspeed descriptors for EP %d\n",
2616 speed_names[ep_desc_id],
2617 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2620 ffs_ep->descs[ep_desc_id] = ds;
2622 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2624 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2625 if (!ds->wMaxPacketSize)
2626 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2628 struct usb_request *req;
2630 u8 bEndpointAddress;
2633 * We back up bEndpointAddress because autoconfig overwrites
2634 * it with physical endpoint address.
2636 bEndpointAddress = ds->bEndpointAddress;
2637 pr_vdebug("autoconfig\n");
2638 ep = usb_ep_autoconfig(func->gadget, ds);
2641 ep->driver_data = func->eps + idx;
2643 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2649 func->eps_revmap[ds->bEndpointAddress &
2650 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2652 * If we use virtual address mapping, we restore
2653 * original bEndpointAddress value.
2655 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
2656 ds->bEndpointAddress = bEndpointAddress;
2658 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2663 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2664 struct usb_descriptor_header *desc,
2667 struct ffs_function *func = priv;
2673 case FFS_DESCRIPTOR:
2674 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2679 if (func->interfaces_nums[idx] < 0) {
2680 int id = usb_interface_id(func->conf, &func->function);
2681 if (unlikely(id < 0))
2683 func->interfaces_nums[idx] = id;
2685 newValue = func->interfaces_nums[idx];
2689 /* String' IDs are allocated when fsf_data is bound to cdev */
2690 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2695 * USB_DT_ENDPOINT are handled in
2696 * __ffs_func_bind_do_descs().
2698 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2701 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2702 if (unlikely(!func->eps[idx].ep))
2706 struct usb_endpoint_descriptor **descs;
2707 descs = func->eps[idx].descs;
2708 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2713 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2718 static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2719 struct usb_os_desc_header *h, void *data,
2720 unsigned len, void *priv)
2722 struct ffs_function *func = priv;
2726 case FFS_OS_DESC_EXT_COMPAT: {
2727 struct usb_ext_compat_desc *desc = data;
2728 struct usb_os_desc_table *t;
2730 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
2731 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
2732 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
2733 ARRAY_SIZE(desc->CompatibleID) +
2734 ARRAY_SIZE(desc->SubCompatibleID));
2735 length = sizeof(*desc);
2738 case FFS_OS_DESC_EXT_PROP: {
2739 struct usb_ext_prop_desc *desc = data;
2740 struct usb_os_desc_table *t;
2741 struct usb_os_desc_ext_prop *ext_prop;
2742 char *ext_prop_name;
2743 char *ext_prop_data;
2745 t = &func->function.os_desc_table[h->interface];
2746 t->if_id = func->interfaces_nums[h->interface];
2748 ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
2749 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
2751 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
2752 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2753 ext_prop->data_len = le32_to_cpu(*(u32 *)
2754 usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
2755 length = ext_prop->name_len + ext_prop->data_len + 14;
2757 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
2758 func->ffs->ms_os_descs_ext_prop_name_avail +=
2761 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
2762 func->ffs->ms_os_descs_ext_prop_data_avail +=
2764 memcpy(ext_prop_data,
2765 usb_ext_prop_data_ptr(data, ext_prop->name_len),
2766 ext_prop->data_len);
2767 /* unicode data reported to the host as "WCHAR"s */
2768 switch (ext_prop->type) {
2769 case USB_EXT_PROP_UNICODE:
2770 case USB_EXT_PROP_UNICODE_ENV:
2771 case USB_EXT_PROP_UNICODE_LINK:
2772 case USB_EXT_PROP_UNICODE_MULTI:
2773 ext_prop->data_len *= 2;
2776 ext_prop->data = ext_prop_data;
2778 memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
2779 ext_prop->name_len);
2780 /* property name reported to the host as "WCHAR"s */
2781 ext_prop->name_len *= 2;
2782 ext_prop->name = ext_prop_name;
2784 t->os_desc->ext_prop_len +=
2785 ext_prop->name_len + ext_prop->data_len + 14;
2786 ++t->os_desc->ext_prop_count;
2787 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
2791 pr_vdebug("unknown descriptor: %d\n", type);
2797 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2798 struct usb_configuration *c)
2800 struct ffs_function *func = ffs_func_from_usb(f);
2801 struct f_fs_opts *ffs_opts =
2802 container_of(f->fi, struct f_fs_opts, func_inst);
2808 * Legacy gadget triggers binding in functionfs_ready_callback,
2809 * which already uses locking; taking the same lock here would
2812 * Configfs-enabled gadgets however do need ffs_dev_lock.
2814 if (!ffs_opts->no_configfs)
2816 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2817 func->ffs = ffs_opts->dev->ffs_data;
2818 if (!ffs_opts->no_configfs)
2821 return ERR_PTR(ret);
2824 func->gadget = c->cdev->gadget;
2827 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2828 * configurations are bound in sequence with list_for_each_entry,
2829 * in each configuration its functions are bound in sequence
2830 * with list_for_each_entry, so we assume no race condition
2831 * with regard to ffs_opts->bound access
2833 if (!ffs_opts->refcnt) {
2834 ret = functionfs_bind(func->ffs, c->cdev);
2836 return ERR_PTR(ret);
2839 func->function.strings = func->ffs->stringtabs;
2844 static int _ffs_func_bind(struct usb_configuration *c,
2845 struct usb_function *f)
2847 struct ffs_function *func = ffs_func_from_usb(f);
2848 struct ffs_data *ffs = func->ffs;
2850 const int full = !!func->ffs->fs_descs_count;
2851 const int high = gadget_is_dualspeed(func->gadget) &&
2852 func->ffs->hs_descs_count;
2853 const int super = gadget_is_superspeed(func->gadget) &&
2854 func->ffs->ss_descs_count;
2856 int fs_len, hs_len, ss_len, ret, i;
2857 struct ffs_ep *eps_ptr;
2859 /* Make it a single chunk, less management later on */
2861 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2862 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2863 full ? ffs->fs_descs_count + 1 : 0);
2864 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2865 high ? ffs->hs_descs_count + 1 : 0);
2866 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2867 super ? ffs->ss_descs_count + 1 : 0);
2868 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2869 vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
2870 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2871 vla_item_with_sz(d, char[16], ext_compat,
2872 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2873 vla_item_with_sz(d, struct usb_os_desc, os_desc,
2874 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2875 vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
2876 ffs->ms_os_descs_ext_prop_count);
2877 vla_item_with_sz(d, char, ext_prop_name,
2878 ffs->ms_os_descs_ext_prop_name_len);
2879 vla_item_with_sz(d, char, ext_prop_data,
2880 ffs->ms_os_descs_ext_prop_data_len);
2881 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
2886 /* Has descriptors only for speeds gadget does not support */
2887 if (unlikely(!(full | high | super)))
2890 /* Allocate a single chunk, less management later on */
2891 vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
2892 if (unlikely(!vlabuf))
2895 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
2896 ffs->ms_os_descs_ext_prop_name_avail =
2897 vla_ptr(vlabuf, d, ext_prop_name);
2898 ffs->ms_os_descs_ext_prop_data_avail =
2899 vla_ptr(vlabuf, d, ext_prop_data);
2901 /* Copy descriptors */
2902 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2903 ffs->raw_descs_length);
2905 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2906 eps_ptr = vla_ptr(vlabuf, d, eps);
2907 for (i = 0; i < ffs->eps_count; i++)
2908 eps_ptr[i].num = -1;
2911 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2913 func->eps = vla_ptr(vlabuf, d, eps);
2914 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
2917 * Go through all the endpoint descriptors and allocate
2918 * endpoints first, so that later we can rewrite the endpoint
2919 * numbers without worrying that it may be described later on.
2922 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2923 fs_len = ffs_do_descs(ffs->fs_descs_count,
2924 vla_ptr(vlabuf, d, raw_descs),
2926 __ffs_func_bind_do_descs, func);
2927 if (unlikely(fs_len < 0)) {
2936 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2937 hs_len = ffs_do_descs(ffs->hs_descs_count,
2938 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2939 d_raw_descs__sz - fs_len,
2940 __ffs_func_bind_do_descs, func);
2941 if (unlikely(hs_len < 0)) {
2949 if (likely(super)) {
2950 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2951 ss_len = ffs_do_descs(ffs->ss_descs_count,
2952 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2953 d_raw_descs__sz - fs_len - hs_len,
2954 __ffs_func_bind_do_descs, func);
2955 if (unlikely(ss_len < 0)) {
2964 * Now handle interface numbers allocation and interface and
2965 * endpoint numbers rewriting. We can do that in one go
2968 ret = ffs_do_descs(ffs->fs_descs_count +
2969 (high ? ffs->hs_descs_count : 0) +
2970 (super ? ffs->ss_descs_count : 0),
2971 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
2972 __ffs_func_bind_do_nums, func);
2973 if (unlikely(ret < 0))
2976 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
2977 if (c->cdev->use_os_string) {
2978 for (i = 0; i < ffs->interfaces_count; ++i) {
2979 struct usb_os_desc *desc;
2981 desc = func->function.os_desc_table[i].os_desc =
2982 vla_ptr(vlabuf, d, os_desc) +
2983 i * sizeof(struct usb_os_desc);
2984 desc->ext_compat_id =
2985 vla_ptr(vlabuf, d, ext_compat) + i * 16;
2986 INIT_LIST_HEAD(&desc->ext_prop);
2988 ret = ffs_do_os_descs(ffs->ms_os_descs_count,
2989 vla_ptr(vlabuf, d, raw_descs) +
2990 fs_len + hs_len + ss_len,
2991 d_raw_descs__sz - fs_len - hs_len -
2993 __ffs_func_bind_do_os_desc, func);
2994 if (unlikely(ret < 0))
2997 func->function.os_desc_n =
2998 c->cdev->use_os_string ? ffs->interfaces_count : 0;
3000 /* And we're done */
3001 ffs_event_add(ffs, FUNCTIONFS_BIND);
3005 /* XXX Do we need to release all claimed endpoints here? */
3009 static int ffs_func_bind(struct usb_configuration *c,
3010 struct usb_function *f)
3012 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
3013 struct ffs_function *func = ffs_func_from_usb(f);
3016 if (IS_ERR(ffs_opts))
3017 return PTR_ERR(ffs_opts);
3019 ret = _ffs_func_bind(c, f);
3020 if (ret && !--ffs_opts->refcnt)
3021 functionfs_unbind(func->ffs);
3027 /* Other USB function hooks *************************************************/
3029 static void ffs_reset_work(struct work_struct *work)
3031 struct ffs_data *ffs = container_of(work,
3032 struct ffs_data, reset_work);
3033 ffs_data_reset(ffs);
3036 static int ffs_func_set_alt(struct usb_function *f,
3037 unsigned interface, unsigned alt)
3039 struct ffs_function *func = ffs_func_from_usb(f);
3040 struct ffs_data *ffs = func->ffs;
3043 if (alt != (unsigned)-1) {
3044 intf = ffs_func_revmap_intf(func, interface);
3045 if (unlikely(intf < 0))
3050 ffs_func_eps_disable(ffs->func);
3052 if (ffs->state == FFS_DEACTIVATED) {
3053 ffs->state = FFS_CLOSING;
3054 INIT_WORK(&ffs->reset_work, ffs_reset_work);
3055 schedule_work(&ffs->reset_work);
3059 if (ffs->state != FFS_ACTIVE)
3062 if (alt == (unsigned)-1) {
3064 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
3069 ret = ffs_func_eps_enable(func);
3070 if (likely(ret >= 0))
3071 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
3075 static void ffs_func_disable(struct usb_function *f)
3077 ffs_func_set_alt(f, 0, (unsigned)-1);
3080 static int ffs_func_setup(struct usb_function *f,
3081 const struct usb_ctrlrequest *creq)
3083 struct ffs_function *func = ffs_func_from_usb(f);
3084 struct ffs_data *ffs = func->ffs;
3085 unsigned long flags;
3090 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
3091 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
3092 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
3093 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
3094 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
3097 * Most requests directed to interface go through here
3098 * (notable exceptions are set/get interface) so we need to
3099 * handle them. All other either handled by composite or
3100 * passed to usb_configuration->setup() (if one is set). No
3101 * matter, we will handle requests directed to endpoint here
3102 * as well (as it's straightforward). Other request recipient
3103 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
3106 if (ffs->state != FFS_ACTIVE)
3109 switch (creq->bRequestType & USB_RECIP_MASK) {
3110 case USB_RECIP_INTERFACE:
3111 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
3112 if (unlikely(ret < 0))
3116 case USB_RECIP_ENDPOINT:
3117 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
3118 if (unlikely(ret < 0))
3120 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
3121 ret = func->ffs->eps_addrmap[ret];
3125 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
3126 ret = le16_to_cpu(creq->wIndex);
3131 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
3132 ffs->ev.setup = *creq;
3133 ffs->ev.setup.wIndex = cpu_to_le16(ret);
3134 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
3135 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
3140 static bool ffs_func_req_match(struct usb_function *f,
3141 const struct usb_ctrlrequest *creq,
3144 struct ffs_function *func = ffs_func_from_usb(f);
3146 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
3149 switch (creq->bRequestType & USB_RECIP_MASK) {
3150 case USB_RECIP_INTERFACE:
3151 return ffs_func_revmap_intf(func,
3152 le16_to_cpu(creq->wIndex) >= 0);
3153 case USB_RECIP_ENDPOINT:
3154 return ffs_func_revmap_ep(func,
3155 le16_to_cpu(creq->wIndex) >= 0);
3157 return (bool) (func->ffs->user_flags &
3158 FUNCTIONFS_ALL_CTRL_RECIP);
3162 static void ffs_func_suspend(struct usb_function *f)
3165 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
3168 static void ffs_func_resume(struct usb_function *f)
3171 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
3175 /* Endpoint and interface numbers reverse mapping ***************************/
3177 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
3179 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
3180 return num ? num : -EDOM;
3183 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
3185 short *nums = func->interfaces_nums;
3186 unsigned count = func->ffs->interfaces_count;
3188 for (; count; --count, ++nums) {
3189 if (*nums >= 0 && *nums == intf)
3190 return nums - func->interfaces_nums;
3197 /* Devices management *******************************************************/
3199 static LIST_HEAD(ffs_devices);
3201 static struct ffs_dev *_ffs_do_find_dev(const char *name)
3203 struct ffs_dev *dev;
3205 list_for_each_entry(dev, &ffs_devices, entry) {
3206 if (!dev->name || !name)
3208 if (strcmp(dev->name, name) == 0)
3216 * ffs_lock must be taken by the caller of this function
3218 static struct ffs_dev *_ffs_get_single_dev(void)
3220 struct ffs_dev *dev;
3222 if (list_is_singular(&ffs_devices)) {
3223 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
3232 * ffs_lock must be taken by the caller of this function
3234 static struct ffs_dev *_ffs_find_dev(const char *name)
3236 struct ffs_dev *dev;
3238 dev = _ffs_get_single_dev();
3242 return _ffs_do_find_dev(name);
3245 /* Configfs support *********************************************************/
3247 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3249 return container_of(to_config_group(item), struct f_fs_opts,
3253 static void ffs_attr_release(struct config_item *item)
3255 struct f_fs_opts *opts = to_ffs_opts(item);
3257 usb_put_function_instance(&opts->func_inst);
3260 static struct configfs_item_operations ffs_item_ops = {
3261 .release = ffs_attr_release,
3264 static struct config_item_type ffs_func_type = {
3265 .ct_item_ops = &ffs_item_ops,
3266 .ct_owner = THIS_MODULE,
3270 /* Function registration interface ******************************************/
3272 static void ffs_free_inst(struct usb_function_instance *f)
3274 struct f_fs_opts *opts;
3276 opts = to_f_fs_opts(f);
3278 _ffs_free_dev(opts->dev);
3283 #define MAX_INST_NAME_LEN 40
3285 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3287 struct f_fs_opts *opts;
3292 name_len = strlen(name) + 1;
3293 if (name_len > MAX_INST_NAME_LEN)
3294 return -ENAMETOOLONG;
3296 ptr = kstrndup(name, name_len, GFP_KERNEL);
3300 opts = to_f_fs_opts(fi);
3305 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
3306 ret = _ffs_name_dev(opts->dev, ptr);
3312 opts->dev->name_allocated = true;
3321 static struct usb_function_instance *ffs_alloc_inst(void)
3323 struct f_fs_opts *opts;
3324 struct ffs_dev *dev;
3326 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3328 return ERR_PTR(-ENOMEM);
3330 opts->func_inst.set_inst_name = ffs_set_inst_name;
3331 opts->func_inst.free_func_inst = ffs_free_inst;
3333 dev = _ffs_alloc_dev();
3337 return ERR_CAST(dev);
3342 config_group_init_type_name(&opts->func_inst.group, "",
3344 return &opts->func_inst;
3347 static void ffs_free(struct usb_function *f)
3349 kfree(ffs_func_from_usb(f));
3352 static void ffs_func_unbind(struct usb_configuration *c,
3353 struct usb_function *f)
3355 struct ffs_function *func = ffs_func_from_usb(f);
3356 struct ffs_data *ffs = func->ffs;
3357 struct f_fs_opts *opts =
3358 container_of(f->fi, struct f_fs_opts, func_inst);
3359 struct ffs_ep *ep = func->eps;
3360 unsigned count = ffs->eps_count;
3361 unsigned long flags;
3364 if (ffs->func == func) {
3365 ffs_func_eps_disable(func);
3369 if (!--opts->refcnt)
3370 functionfs_unbind(ffs);
3372 /* cleanup after autoconfig */
3373 spin_lock_irqsave(&func->ffs->eps_lock, flags);
3375 if (ep->ep && ep->req)
3376 usb_ep_free_request(ep->ep, ep->req);
3380 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3384 * eps, descriptors and interfaces_nums are allocated in the
3385 * same chunk so only one free is required.
3387 func->function.fs_descriptors = NULL;
3388 func->function.hs_descriptors = NULL;
3389 func->function.ss_descriptors = NULL;
3390 func->interfaces_nums = NULL;
3392 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3395 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3397 struct ffs_function *func;
3401 func = kzalloc(sizeof(*func), GFP_KERNEL);
3402 if (unlikely(!func))
3403 return ERR_PTR(-ENOMEM);
3405 func->function.name = "Function FS Gadget";
3407 func->function.bind = ffs_func_bind;
3408 func->function.unbind = ffs_func_unbind;
3409 func->function.set_alt = ffs_func_set_alt;
3410 func->function.disable = ffs_func_disable;
3411 func->function.setup = ffs_func_setup;
3412 func->function.req_match = ffs_func_req_match;
3413 func->function.suspend = ffs_func_suspend;
3414 func->function.resume = ffs_func_resume;
3415 func->function.free_func = ffs_free;
3417 return &func->function;
3421 * ffs_lock must be taken by the caller of this function
3423 static struct ffs_dev *_ffs_alloc_dev(void)
3425 struct ffs_dev *dev;
3428 if (_ffs_get_single_dev())
3429 return ERR_PTR(-EBUSY);
3431 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3433 return ERR_PTR(-ENOMEM);
3435 if (list_empty(&ffs_devices)) {
3436 ret = functionfs_init();
3439 return ERR_PTR(ret);
3443 list_add(&dev->entry, &ffs_devices);
3449 * ffs_lock must be taken by the caller of this function
3450 * The caller is responsible for "name" being available whenever f_fs needs it
3452 static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
3454 struct ffs_dev *existing;
3456 existing = _ffs_do_find_dev(name);
3466 * The caller is responsible for "name" being available whenever f_fs needs it
3468 int ffs_name_dev(struct ffs_dev *dev, const char *name)
3473 ret = _ffs_name_dev(dev, name);
3478 EXPORT_SYMBOL_GPL(ffs_name_dev);
3480 int ffs_single_dev(struct ffs_dev *dev)
3487 if (!list_is_singular(&ffs_devices))
3495 EXPORT_SYMBOL_GPL(ffs_single_dev);
3498 * ffs_lock must be taken by the caller of this function
3500 static void _ffs_free_dev(struct ffs_dev *dev)
3502 list_del(&dev->entry);
3503 if (dev->name_allocated)
3506 /* Clear the private_data pointer to stop incorrect dev access */
3508 dev->ffs_data->private_data = NULL;
3511 if (list_empty(&ffs_devices))
3512 functionfs_cleanup();
3515 static void *ffs_acquire_dev(const char *dev_name)
3517 struct ffs_dev *ffs_dev;
3522 ffs_dev = _ffs_find_dev(dev_name);
3524 ffs_dev = ERR_PTR(-ENOENT);
3525 else if (ffs_dev->mounted)
3526 ffs_dev = ERR_PTR(-EBUSY);
3527 else if (ffs_dev->ffs_acquire_dev_callback &&
3528 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
3529 ffs_dev = ERR_PTR(-ENOENT);
3531 ffs_dev->mounted = true;
3537 static void ffs_release_dev(struct ffs_data *ffs_data)
3539 struct ffs_dev *ffs_dev;
3544 ffs_dev = ffs_data->private_data;
3546 ffs_dev->mounted = false;
3548 if (ffs_dev->ffs_release_dev_callback)
3549 ffs_dev->ffs_release_dev_callback(ffs_dev);
3555 static int ffs_ready(struct ffs_data *ffs)
3557 struct ffs_dev *ffs_obj;
3563 ffs_obj = ffs->private_data;
3568 if (WARN_ON(ffs_obj->desc_ready)) {
3573 ffs_obj->desc_ready = true;
3574 ffs_obj->ffs_data = ffs;
3576 if (ffs_obj->ffs_ready_callback) {
3577 ret = ffs_obj->ffs_ready_callback(ffs);
3582 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
3588 static void ffs_closed(struct ffs_data *ffs)
3590 struct ffs_dev *ffs_obj;
3591 struct f_fs_opts *opts;
3596 ffs_obj = ffs->private_data;
3600 ffs_obj->desc_ready = false;
3602 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
3603 ffs_obj->ffs_closed_callback)
3604 ffs_obj->ffs_closed_callback(ffs);
3607 opts = ffs_obj->opts;
3611 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3612 || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
3615 unregister_gadget_item(ffs_obj->opts->
3616 func_inst.group.cg_item.ci_parent->ci_parent);
3621 /* Misc helper functions ****************************************************/
3623 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3626 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3627 : mutex_lock_interruptible(mutex);
3630 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
3637 data = kmalloc(len, GFP_KERNEL);
3638 if (unlikely(!data))
3639 return ERR_PTR(-ENOMEM);
3641 if (unlikely(copy_from_user(data, buf, len))) {
3643 return ERR_PTR(-EFAULT);
3646 pr_vdebug("Buffer from user space:\n");
3647 ffs_dump_mem("", data, len);
3652 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3653 MODULE_LICENSE("GPL");
3654 MODULE_AUTHOR("Michal Nazarewicz");