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 void ffs_func_suspend(struct usb_function *);
102 static void ffs_func_resume(struct usb_function *);
105 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
106 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
109 /* The endpoints structures *************************************************/
112 struct usb_ep *ep; /* P: ffs->eps_lock */
113 struct usb_request *req; /* P: epfile->mutex */
115 /* [0]: full speed, [1]: high speed, [2]: super speed */
116 struct usb_endpoint_descriptor *descs[3];
120 int status; /* P: epfile->mutex */
124 /* Protects ep->ep and ep->req. */
126 wait_queue_head_t wait;
128 struct ffs_data *ffs;
129 struct ffs_ep *ep; /* P: ffs->eps_lock */
131 struct dentry *dentry;
134 * Buffer for holding data from partial reads which may happen since
135 * we’re rounding user read requests to a multiple of a max packet size.
137 struct ffs_buffer *read_buffer; /* P: epfile->mutex */
141 unsigned char in; /* P: ffs->eps_lock */
142 unsigned char isoc; /* P: ffs->eps_lock */
153 /* ffs_io_data structure ***************************************************/
160 struct iov_iter data;
164 struct mm_struct *mm;
165 struct work_struct work;
168 struct usb_request *req;
170 struct ffs_data *ffs;
173 struct ffs_desc_helper {
174 struct ffs_data *ffs;
175 unsigned interfaces_count;
179 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
180 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
182 static struct dentry *
183 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
184 const struct file_operations *fops);
186 /* Devices management *******************************************************/
188 DEFINE_MUTEX(ffs_lock);
189 EXPORT_SYMBOL_GPL(ffs_lock);
191 static struct ffs_dev *_ffs_find_dev(const char *name);
192 static struct ffs_dev *_ffs_alloc_dev(void);
193 static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
194 static void _ffs_free_dev(struct ffs_dev *dev);
195 static void *ffs_acquire_dev(const char *dev_name);
196 static void ffs_release_dev(struct ffs_data *ffs_data);
197 static int ffs_ready(struct ffs_data *ffs);
198 static void ffs_closed(struct ffs_data *ffs);
200 /* Misc helper functions ****************************************************/
202 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
203 __attribute__((warn_unused_result, nonnull));
204 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
205 __attribute__((warn_unused_result, nonnull));
208 /* Control file aka ep0 *****************************************************/
210 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
212 struct ffs_data *ffs = req->context;
214 complete_all(&ffs->ep0req_completion);
217 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
219 struct usb_request *req = ffs->ep0req;
222 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
224 spin_unlock_irq(&ffs->ev.waitq.lock);
230 * UDC layer requires to provide a buffer even for ZLP, but should
231 * not use it at all. Let's provide some poisoned pointer to catch
232 * possible bug in the driver.
234 if (req->buf == NULL)
235 req->buf = (void *)0xDEADBABE;
237 reinit_completion(&ffs->ep0req_completion);
239 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
240 if (unlikely(ret < 0))
243 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
245 usb_ep_dequeue(ffs->gadget->ep0, req);
249 ffs->setup_state = FFS_NO_SETUP;
250 return req->status ? req->status : req->actual;
253 static int __ffs_ep0_stall(struct ffs_data *ffs)
255 if (ffs->ev.can_stall) {
256 pr_vdebug("ep0 stall\n");
257 usb_ep_set_halt(ffs->gadget->ep0);
258 ffs->setup_state = FFS_NO_SETUP;
261 pr_debug("bogus ep0 stall!\n");
266 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
267 size_t len, loff_t *ptr)
269 struct ffs_data *ffs = file->private_data;
275 /* Fast check if setup was canceled */
276 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
280 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
281 if (unlikely(ret < 0))
285 switch (ffs->state) {
286 case FFS_READ_DESCRIPTORS:
287 case FFS_READ_STRINGS:
289 if (unlikely(len < 16)) {
294 data = ffs_prepare_buffer(buf, len);
301 if (ffs->state == FFS_READ_DESCRIPTORS) {
302 pr_info("read descriptors\n");
303 ret = __ffs_data_got_descs(ffs, data, len);
304 if (unlikely(ret < 0))
307 ffs->state = FFS_READ_STRINGS;
310 pr_info("read strings\n");
311 ret = __ffs_data_got_strings(ffs, data, len);
312 if (unlikely(ret < 0))
315 ret = ffs_epfiles_create(ffs);
317 ffs->state = FFS_CLOSING;
321 ffs->state = FFS_ACTIVE;
322 mutex_unlock(&ffs->mutex);
324 ret = ffs_ready(ffs);
325 if (unlikely(ret < 0)) {
326 ffs->state = FFS_CLOSING;
337 * We're called from user space, we can use _irq
338 * rather then _irqsave
340 spin_lock_irq(&ffs->ev.waitq.lock);
341 switch (ffs_setup_state_clear_cancelled(ffs)) {
342 case FFS_SETUP_CANCELLED:
350 case FFS_SETUP_PENDING:
354 /* FFS_SETUP_PENDING */
355 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
356 spin_unlock_irq(&ffs->ev.waitq.lock);
357 ret = __ffs_ep0_stall(ffs);
361 /* FFS_SETUP_PENDING and not stall */
362 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
364 spin_unlock_irq(&ffs->ev.waitq.lock);
366 data = ffs_prepare_buffer(buf, len);
372 spin_lock_irq(&ffs->ev.waitq.lock);
375 * We are guaranteed to be still in FFS_ACTIVE state
376 * but the state of setup could have changed from
377 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
378 * to check for that. If that happened we copied data
379 * from user space in vain but it's unlikely.
381 * For sure we are not in FFS_NO_SETUP since this is
382 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
383 * transition can be performed and it's protected by
386 if (ffs_setup_state_clear_cancelled(ffs) ==
387 FFS_SETUP_CANCELLED) {
390 spin_unlock_irq(&ffs->ev.waitq.lock);
392 /* unlocks spinlock */
393 ret = __ffs_ep0_queue_wait(ffs, data, len);
403 mutex_unlock(&ffs->mutex);
407 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
408 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
412 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
413 * size of ffs->ev.types array (which is four) so that's how much space
416 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
417 const size_t size = n * sizeof *events;
420 memset(events, 0, size);
423 events[i].type = ffs->ev.types[i];
424 if (events[i].type == FUNCTIONFS_SETUP) {
425 events[i].u.setup = ffs->ev.setup;
426 ffs->setup_state = FFS_SETUP_PENDING;
432 memmove(ffs->ev.types, ffs->ev.types + n,
433 ffs->ev.count * sizeof *ffs->ev.types);
435 spin_unlock_irq(&ffs->ev.waitq.lock);
436 mutex_unlock(&ffs->mutex);
438 return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
441 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
442 size_t len, loff_t *ptr)
444 struct ffs_data *ffs = file->private_data;
451 /* Fast check if setup was canceled */
452 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
456 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
457 if (unlikely(ret < 0))
461 if (ffs->state != FFS_ACTIVE) {
467 * We're called from user space, we can use _irq rather then
470 spin_lock_irq(&ffs->ev.waitq.lock);
472 switch (ffs_setup_state_clear_cancelled(ffs)) {
473 case FFS_SETUP_CANCELLED:
478 n = len / sizeof(struct usb_functionfs_event);
484 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
489 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
495 return __ffs_ep0_read_events(ffs, buf,
496 min(n, (size_t)ffs->ev.count));
498 case FFS_SETUP_PENDING:
499 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
500 spin_unlock_irq(&ffs->ev.waitq.lock);
501 ret = __ffs_ep0_stall(ffs);
505 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
507 spin_unlock_irq(&ffs->ev.waitq.lock);
510 data = kmalloc(len, GFP_KERNEL);
511 if (unlikely(!data)) {
517 spin_lock_irq(&ffs->ev.waitq.lock);
519 /* See ffs_ep0_write() */
520 if (ffs_setup_state_clear_cancelled(ffs) ==
521 FFS_SETUP_CANCELLED) {
526 /* unlocks spinlock */
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
528 if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
537 spin_unlock_irq(&ffs->ev.waitq.lock);
539 mutex_unlock(&ffs->mutex);
544 static int ffs_ep0_open(struct inode *inode, struct file *file)
546 struct ffs_data *ffs = inode->i_private;
550 if (unlikely(ffs->state == FFS_CLOSING))
553 file->private_data = ffs;
554 ffs_data_opened(ffs);
559 static int ffs_ep0_release(struct inode *inode, struct file *file)
561 struct ffs_data *ffs = file->private_data;
565 ffs_data_closed(ffs);
570 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
572 struct ffs_data *ffs = file->private_data;
573 struct usb_gadget *gadget = ffs->gadget;
578 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
579 struct ffs_function *func = ffs->func;
580 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
581 } else if (gadget && gadget->ops->ioctl) {
582 ret = gadget->ops->ioctl(gadget, code, value);
590 static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
592 struct ffs_data *ffs = file->private_data;
593 unsigned int mask = POLLWRNORM;
596 poll_wait(file, &ffs->ev.waitq, wait);
598 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
599 if (unlikely(ret < 0))
602 switch (ffs->state) {
603 case FFS_READ_DESCRIPTORS:
604 case FFS_READ_STRINGS:
609 switch (ffs->setup_state) {
615 case FFS_SETUP_PENDING:
616 case FFS_SETUP_CANCELLED:
617 mask |= (POLLIN | POLLOUT);
622 case FFS_DEACTIVATED:
626 mutex_unlock(&ffs->mutex);
631 static const struct file_operations ffs_ep0_operations = {
634 .open = ffs_ep0_open,
635 .write = ffs_ep0_write,
636 .read = ffs_ep0_read,
637 .release = ffs_ep0_release,
638 .unlocked_ioctl = ffs_ep0_ioctl,
639 .poll = ffs_ep0_poll,
643 /* "Normal" endpoints operations ********************************************/
645 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
648 if (likely(req->context)) {
649 struct ffs_ep *ep = _ep->driver_data;
650 ep->status = req->status ? req->status : req->actual;
651 complete(req->context);
655 static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
657 ssize_t ret = copy_to_iter(data, data_len, iter);
658 if (likely(ret == data_len))
661 if (unlikely(iov_iter_count(iter)))
665 * Dear user space developer!
667 * TL;DR: To stop getting below error message in your kernel log, change
668 * user space code using functionfs to align read buffers to a max
671 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
672 * packet size. When unaligned buffer is passed to functionfs, it
673 * internally uses a larger, aligned buffer so that such UDCs are happy.
675 * Unfortunately, this means that host may send more data than was
676 * requested in read(2) system call. f_fs doesn’t know what to do with
677 * that excess data so it simply drops it.
679 * Was the buffer aligned in the first place, no such problem would
682 * Data may be dropped only in AIO reads. Synchronous reads are handled
683 * by splitting a request into multiple parts. This splitting may still
684 * be a problem though so it’s likely best to align the buffer
685 * regardless of it being AIO or not..
687 * This only affects OUT endpoints, i.e. reading data with a read(2),
688 * aio_read(2) etc. system calls. Writing data to an IN endpoint is not
691 pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
692 "Align read buffer size to max packet size to avoid the problem.\n",
698 static void ffs_user_copy_worker(struct work_struct *work)
700 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
702 int ret = io_data->req->status ? io_data->req->status :
703 io_data->req->actual;
704 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
706 if (io_data->read && ret > 0) {
708 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
709 unuse_mm(io_data->mm);
712 io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
714 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
715 eventfd_signal(io_data->ffs->ffs_eventfd, 1);
717 usb_ep_free_request(io_data->ep, io_data->req);
720 kfree(io_data->to_free);
725 static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
726 struct usb_request *req)
728 struct ffs_io_data *io_data = req->context;
732 INIT_WORK(&io_data->work, ffs_user_copy_worker);
733 schedule_work(&io_data->work);
736 /* Assumes epfile->mutex is held. */
737 static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
738 struct iov_iter *iter)
740 struct ffs_buffer *buf = epfile->read_buffer;
745 ret = copy_to_iter(buf->data, buf->length, iter);
746 if (buf->length == ret) {
748 epfile->read_buffer = NULL;
749 } else if (unlikely(iov_iter_count(iter))) {
758 /* Assumes epfile->mutex is held. */
759 static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
760 void *data, int data_len,
761 struct iov_iter *iter)
763 struct ffs_buffer *buf;
765 ssize_t ret = copy_to_iter(data, data_len, iter);
766 if (likely(data_len == ret))
769 if (unlikely(iov_iter_count(iter)))
772 /* See ffs_copy_to_iter for more context. */
773 pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
777 buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
780 buf->length = data_len;
781 buf->data = buf->storage;
782 memcpy(buf->storage, data + ret, data_len);
783 epfile->read_buffer = buf;
788 static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
790 struct ffs_epfile *epfile = file->private_data;
791 struct usb_request *req;
794 ssize_t ret, data_len = -EINVAL;
797 /* Are we still active? */
798 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
801 /* Wait for endpoint to be enabled */
804 if (file->f_flags & O_NONBLOCK)
807 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
813 halt = (!io_data->read == !epfile->in);
814 if (halt && epfile->isoc)
817 /* We will be using request and read_buffer */
818 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
822 /* Allocate & copy */
824 struct usb_gadget *gadget;
827 * Do we have buffered data from previous partial read? Check
828 * that for synchronous case only because we do not have
829 * facility to ‘wake up’ a pending asynchronous read and push
830 * buffered data to it which we would need to make things behave
833 if (!io_data->aio && io_data->read) {
834 ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
840 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
841 * before the waiting completes, so do not assign to 'gadget'
844 gadget = epfile->ffs->gadget;
846 spin_lock_irq(&epfile->ffs->eps_lock);
847 /* In the meantime, endpoint got disabled or changed. */
848 if (epfile->ep != ep) {
852 data_len = iov_iter_count(&io_data->data);
854 * Controller may require buffer size to be aligned to
855 * maxpacketsize of an out endpoint.
858 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
859 spin_unlock_irq(&epfile->ffs->eps_lock);
861 data = kmalloc(data_len, GFP_KERNEL);
862 if (unlikely(!data)) {
866 if (!io_data->read &&
867 copy_from_iter(data, data_len, &io_data->data) != data_len) {
873 spin_lock_irq(&epfile->ffs->eps_lock);
875 if (epfile->ep != ep) {
876 /* In the meantime, endpoint got disabled or changed. */
880 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
881 usb_ep_set_halt(ep->ep);
883 } else if (unlikely(data_len == -EINVAL)) {
885 * Sanity Check: even though data_len can't be used
886 * uninitialized at the time I write this comment, some
887 * compilers complain about this situation.
888 * In order to keep the code clean from warnings, data_len is
889 * being initialized to -EINVAL during its declaration, which
890 * means we can't rely on compiler anymore to warn no future
891 * changes won't result in data_len being used uninitialized.
892 * For such reason, we're adding this redundant sanity check
895 WARN(1, "%s: data_len == -EINVAL\n", __func__);
897 } else if (!io_data->aio) {
898 DECLARE_COMPLETION_ONSTACK(done);
899 bool interrupted = false;
903 req->length = data_len;
905 req->context = &done;
906 req->complete = ffs_epfile_io_complete;
908 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
909 if (unlikely(ret < 0))
912 spin_unlock_irq(&epfile->ffs->eps_lock);
914 if (unlikely(wait_for_completion_interruptible(&done))) {
916 * To avoid race condition with ffs_epfile_io_complete,
917 * dequeue the request first then check
918 * status. usb_ep_dequeue API should guarantee no race
919 * condition with req->complete callback.
921 usb_ep_dequeue(ep->ep, req);
922 interrupted = ep->status < 0;
927 else if (io_data->read && ep->status > 0)
928 ret = __ffs_epfile_read_data(epfile, data, ep->status,
933 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
937 req->length = data_len;
940 io_data->ep = ep->ep;
942 io_data->ffs = epfile->ffs;
944 req->context = io_data;
945 req->complete = ffs_epfile_async_io_complete;
947 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
949 usb_ep_free_request(ep->ep, req);
955 * Do not kfree the buffer in this function. It will be freed
956 * by ffs_user_copy_worker.
962 spin_unlock_irq(&epfile->ffs->eps_lock);
964 mutex_unlock(&epfile->mutex);
971 ffs_epfile_open(struct inode *inode, struct file *file)
973 struct ffs_epfile *epfile = inode->i_private;
977 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
980 file->private_data = epfile;
981 ffs_data_opened(epfile->ffs);
986 static int ffs_aio_cancel(struct kiocb *kiocb)
988 struct ffs_io_data *io_data = kiocb->private;
989 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
994 spin_lock_irq(&epfile->ffs->eps_lock);
996 if (likely(io_data && io_data->ep && io_data->req))
997 value = usb_ep_dequeue(io_data->ep, io_data->req);
1001 spin_unlock_irq(&epfile->ffs->eps_lock);
1006 static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
1008 struct ffs_io_data io_data, *p = &io_data;
1013 if (!is_sync_kiocb(kiocb)) {
1014 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1025 p->mm = current->mm;
1030 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1032 res = ffs_epfile_io(kiocb->ki_filp, p);
1033 if (res == -EIOCBQUEUED)
1042 static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
1044 struct ffs_io_data io_data, *p = &io_data;
1049 if (!is_sync_kiocb(kiocb)) {
1050 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1061 p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
1070 p->mm = current->mm;
1075 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1077 res = ffs_epfile_io(kiocb->ki_filp, p);
1078 if (res == -EIOCBQUEUED)
1091 ffs_epfile_release(struct inode *inode, struct file *file)
1093 struct ffs_epfile *epfile = inode->i_private;
1097 kfree(epfile->read_buffer);
1098 epfile->read_buffer = NULL;
1099 ffs_data_closed(epfile->ffs);
1104 static long ffs_epfile_ioctl(struct file *file, unsigned code,
1105 unsigned long value)
1107 struct ffs_epfile *epfile = file->private_data;
1112 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1115 spin_lock_irq(&epfile->ffs->eps_lock);
1116 if (likely(epfile->ep)) {
1118 case FUNCTIONFS_FIFO_STATUS:
1119 ret = usb_ep_fifo_status(epfile->ep->ep);
1121 case FUNCTIONFS_FIFO_FLUSH:
1122 usb_ep_fifo_flush(epfile->ep->ep);
1125 case FUNCTIONFS_CLEAR_HALT:
1126 ret = usb_ep_clear_halt(epfile->ep->ep);
1128 case FUNCTIONFS_ENDPOINT_REVMAP:
1129 ret = epfile->ep->num;
1131 case FUNCTIONFS_ENDPOINT_DESC:
1134 struct usb_endpoint_descriptor *desc;
1136 switch (epfile->ffs->gadget->speed) {
1137 case USB_SPEED_SUPER:
1140 case USB_SPEED_HIGH:
1146 desc = epfile->ep->descs[desc_idx];
1148 spin_unlock_irq(&epfile->ffs->eps_lock);
1149 ret = copy_to_user((void *)value, desc, sizeof(*desc));
1160 spin_unlock_irq(&epfile->ffs->eps_lock);
1165 static const struct file_operations ffs_epfile_operations = {
1166 .llseek = no_llseek,
1168 .open = ffs_epfile_open,
1169 .write_iter = ffs_epfile_write_iter,
1170 .read_iter = ffs_epfile_read_iter,
1171 .release = ffs_epfile_release,
1172 .unlocked_ioctl = ffs_epfile_ioctl,
1176 /* File system and super block operations ***********************************/
1179 * Mounting the file system creates a controller file, used first for
1180 * function configuration then later for event monitoring.
1183 static struct inode *__must_check
1184 ffs_sb_make_inode(struct super_block *sb, void *data,
1185 const struct file_operations *fops,
1186 const struct inode_operations *iops,
1187 struct ffs_file_perms *perms)
1189 struct inode *inode;
1193 inode = new_inode(sb);
1195 if (likely(inode)) {
1196 struct timespec current_time = CURRENT_TIME;
1198 inode->i_ino = get_next_ino();
1199 inode->i_mode = perms->mode;
1200 inode->i_uid = perms->uid;
1201 inode->i_gid = perms->gid;
1202 inode->i_atime = current_time;
1203 inode->i_mtime = current_time;
1204 inode->i_ctime = current_time;
1205 inode->i_private = data;
1207 inode->i_fop = fops;
1215 /* Create "regular" file */
1216 static struct dentry *ffs_sb_create_file(struct super_block *sb,
1217 const char *name, void *data,
1218 const struct file_operations *fops)
1220 struct ffs_data *ffs = sb->s_fs_info;
1221 struct dentry *dentry;
1222 struct inode *inode;
1226 dentry = d_alloc_name(sb->s_root, name);
1227 if (unlikely(!dentry))
1230 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1231 if (unlikely(!inode)) {
1236 d_add(dentry, inode);
1241 static const struct super_operations ffs_sb_operations = {
1242 .statfs = simple_statfs,
1243 .drop_inode = generic_delete_inode,
1246 struct ffs_sb_fill_data {
1247 struct ffs_file_perms perms;
1249 const char *dev_name;
1251 struct ffs_data *ffs_data;
1254 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1256 struct ffs_sb_fill_data *data = _data;
1257 struct inode *inode;
1258 struct ffs_data *ffs = data->ffs_data;
1263 data->ffs_data = NULL;
1264 sb->s_fs_info = ffs;
1265 sb->s_blocksize = PAGE_SIZE;
1266 sb->s_blocksize_bits = PAGE_SHIFT;
1267 sb->s_magic = FUNCTIONFS_MAGIC;
1268 sb->s_op = &ffs_sb_operations;
1269 sb->s_time_gran = 1;
1272 data->perms.mode = data->root_mode;
1273 inode = ffs_sb_make_inode(sb, NULL,
1274 &simple_dir_operations,
1275 &simple_dir_inode_operations,
1277 sb->s_root = d_make_root(inode);
1278 if (unlikely(!sb->s_root))
1282 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1283 &ffs_ep0_operations)))
1289 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1293 if (!opts || !*opts)
1297 unsigned long value;
1301 comma = strchr(opts, ',');
1306 eq = strchr(opts, '=');
1307 if (unlikely(!eq)) {
1308 pr_err("'=' missing in %s\n", opts);
1314 if (kstrtoul(eq + 1, 0, &value)) {
1315 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1319 /* Interpret option */
1320 switch (eq - opts) {
1322 if (!memcmp(opts, "no_disconnect", 13))
1323 data->no_disconnect = !!value;
1328 if (!memcmp(opts, "rmode", 5))
1329 data->root_mode = (value & 0555) | S_IFDIR;
1330 else if (!memcmp(opts, "fmode", 5))
1331 data->perms.mode = (value & 0666) | S_IFREG;
1337 if (!memcmp(opts, "mode", 4)) {
1338 data->root_mode = (value & 0555) | S_IFDIR;
1339 data->perms.mode = (value & 0666) | S_IFREG;
1346 if (!memcmp(opts, "uid", 3)) {
1347 data->perms.uid = make_kuid(current_user_ns(), value);
1348 if (!uid_valid(data->perms.uid)) {
1349 pr_err("%s: unmapped value: %lu\n", opts, value);
1352 } else if (!memcmp(opts, "gid", 3)) {
1353 data->perms.gid = make_kgid(current_user_ns(), value);
1354 if (!gid_valid(data->perms.gid)) {
1355 pr_err("%s: unmapped value: %lu\n", opts, value);
1365 pr_err("%s: invalid option\n", opts);
1369 /* Next iteration */
1378 /* "mount -t functionfs dev_name /dev/function" ends up here */
1380 static struct dentry *
1381 ffs_fs_mount(struct file_system_type *t, int flags,
1382 const char *dev_name, void *opts)
1384 struct ffs_sb_fill_data data = {
1386 .mode = S_IFREG | 0600,
1387 .uid = GLOBAL_ROOT_UID,
1388 .gid = GLOBAL_ROOT_GID,
1390 .root_mode = S_IFDIR | 0500,
1391 .no_disconnect = false,
1396 struct ffs_data *ffs;
1400 ret = ffs_fs_parse_opts(&data, opts);
1401 if (unlikely(ret < 0))
1402 return ERR_PTR(ret);
1404 ffs = ffs_data_new();
1406 return ERR_PTR(-ENOMEM);
1407 ffs->file_perms = data.perms;
1408 ffs->no_disconnect = data.no_disconnect;
1410 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1411 if (unlikely(!ffs->dev_name)) {
1413 return ERR_PTR(-ENOMEM);
1416 ffs_dev = ffs_acquire_dev(dev_name);
1417 if (IS_ERR(ffs_dev)) {
1419 return ERR_CAST(ffs_dev);
1421 ffs->private_data = ffs_dev;
1422 data.ffs_data = ffs;
1424 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1425 if (IS_ERR(rv) && data.ffs_data) {
1426 ffs_release_dev(data.ffs_data);
1427 ffs_data_put(data.ffs_data);
1433 ffs_fs_kill_sb(struct super_block *sb)
1437 kill_litter_super(sb);
1438 if (sb->s_fs_info) {
1439 ffs_release_dev(sb->s_fs_info);
1440 ffs_data_closed(sb->s_fs_info);
1441 ffs_data_put(sb->s_fs_info);
1445 static struct file_system_type ffs_fs_type = {
1446 .owner = THIS_MODULE,
1447 .name = "functionfs",
1448 .mount = ffs_fs_mount,
1449 .kill_sb = ffs_fs_kill_sb,
1451 MODULE_ALIAS_FS("functionfs");
1454 /* Driver's main init/cleanup functions *************************************/
1456 static int functionfs_init(void)
1462 ret = register_filesystem(&ffs_fs_type);
1464 pr_info("file system registered\n");
1466 pr_err("failed registering file system (%d)\n", ret);
1471 static void functionfs_cleanup(void)
1475 pr_info("unloading\n");
1476 unregister_filesystem(&ffs_fs_type);
1480 /* ffs_data and ffs_function construction and destruction code **************/
1482 static void ffs_data_clear(struct ffs_data *ffs);
1483 static void ffs_data_reset(struct ffs_data *ffs);
1485 static void ffs_data_get(struct ffs_data *ffs)
1489 atomic_inc(&ffs->ref);
1492 static void ffs_data_opened(struct ffs_data *ffs)
1496 atomic_inc(&ffs->ref);
1497 if (atomic_add_return(1, &ffs->opened) == 1 &&
1498 ffs->state == FFS_DEACTIVATED) {
1499 ffs->state = FFS_CLOSING;
1500 ffs_data_reset(ffs);
1504 static void ffs_data_put(struct ffs_data *ffs)
1508 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1509 pr_info("%s(): freeing\n", __func__);
1510 ffs_data_clear(ffs);
1511 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1512 waitqueue_active(&ffs->ep0req_completion.wait));
1513 kfree(ffs->dev_name);
1518 static void ffs_data_closed(struct ffs_data *ffs)
1522 if (atomic_dec_and_test(&ffs->opened)) {
1523 if (ffs->no_disconnect) {
1524 ffs->state = FFS_DEACTIVATED;
1526 ffs_epfiles_destroy(ffs->epfiles,
1528 ffs->epfiles = NULL;
1530 if (ffs->setup_state == FFS_SETUP_PENDING)
1531 __ffs_ep0_stall(ffs);
1533 ffs->state = FFS_CLOSING;
1534 ffs_data_reset(ffs);
1537 if (atomic_read(&ffs->opened) < 0) {
1538 ffs->state = FFS_CLOSING;
1539 ffs_data_reset(ffs);
1545 static struct ffs_data *ffs_data_new(void)
1547 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1553 atomic_set(&ffs->ref, 1);
1554 atomic_set(&ffs->opened, 0);
1555 ffs->state = FFS_READ_DESCRIPTORS;
1556 mutex_init(&ffs->mutex);
1557 spin_lock_init(&ffs->eps_lock);
1558 init_waitqueue_head(&ffs->ev.waitq);
1559 init_completion(&ffs->ep0req_completion);
1561 /* XXX REVISIT need to update it in some places, or do we? */
1562 ffs->ev.can_stall = 1;
1567 static void ffs_data_clear(struct ffs_data *ffs)
1573 BUG_ON(ffs->gadget);
1576 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1578 if (ffs->ffs_eventfd)
1579 eventfd_ctx_put(ffs->ffs_eventfd);
1581 kfree(ffs->raw_descs_data);
1582 kfree(ffs->raw_strings);
1583 kfree(ffs->stringtabs);
1586 static void ffs_data_reset(struct ffs_data *ffs)
1590 ffs_data_clear(ffs);
1592 ffs->epfiles = NULL;
1593 ffs->raw_descs_data = NULL;
1594 ffs->raw_descs = NULL;
1595 ffs->raw_strings = NULL;
1596 ffs->stringtabs = NULL;
1598 ffs->raw_descs_length = 0;
1599 ffs->fs_descs_count = 0;
1600 ffs->hs_descs_count = 0;
1601 ffs->ss_descs_count = 0;
1603 ffs->strings_count = 0;
1604 ffs->interfaces_count = 0;
1609 ffs->state = FFS_READ_DESCRIPTORS;
1610 ffs->setup_state = FFS_NO_SETUP;
1615 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1617 struct usb_gadget_strings **lang;
1622 if (WARN_ON(ffs->state != FFS_ACTIVE
1623 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1626 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1627 if (unlikely(first_id < 0))
1630 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1631 if (unlikely(!ffs->ep0req))
1633 ffs->ep0req->complete = ffs_ep0_complete;
1634 ffs->ep0req->context = ffs;
1636 lang = ffs->stringtabs;
1638 for (; *lang; ++lang) {
1639 struct usb_string *str = (*lang)->strings;
1641 for (; str->s; ++id, ++str)
1646 ffs->gadget = cdev->gadget;
1651 static void functionfs_unbind(struct ffs_data *ffs)
1655 if (!WARN_ON(!ffs->gadget)) {
1656 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1659 clear_bit(FFS_FL_BOUND, &ffs->flags);
1664 static int ffs_epfiles_create(struct ffs_data *ffs)
1666 struct ffs_epfile *epfile, *epfiles;
1671 count = ffs->eps_count;
1672 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1677 for (i = 1; i <= count; ++i, ++epfile) {
1679 mutex_init(&epfile->mutex);
1680 init_waitqueue_head(&epfile->wait);
1681 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1682 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
1684 sprintf(epfile->name, "ep%u", i);
1685 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
1687 &ffs_epfile_operations);
1688 if (unlikely(!epfile->dentry)) {
1689 ffs_epfiles_destroy(epfiles, i - 1);
1694 ffs->epfiles = epfiles;
1698 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1700 struct ffs_epfile *epfile = epfiles;
1704 for (; count; --count, ++epfile) {
1705 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1706 waitqueue_active(&epfile->wait));
1707 if (epfile->dentry) {
1708 d_delete(epfile->dentry);
1709 dput(epfile->dentry);
1710 epfile->dentry = NULL;
1717 static void ffs_func_eps_disable(struct ffs_function *func)
1719 struct ffs_ep *ep = func->eps;
1720 struct ffs_epfile *epfile = func->ffs->epfiles;
1721 unsigned count = func->ffs->eps_count;
1722 unsigned long flags;
1726 mutex_lock(&epfile->mutex);
1727 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1728 /* pending requests get nuked */
1730 usb_ep_disable(ep->ep);
1732 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1736 kfree(epfile->read_buffer);
1737 epfile->read_buffer = NULL;
1738 mutex_unlock(&epfile->mutex);
1744 static int ffs_func_eps_enable(struct ffs_function *func)
1746 struct ffs_data *ffs = func->ffs;
1747 struct ffs_ep *ep = func->eps;
1748 struct ffs_epfile *epfile = ffs->epfiles;
1749 unsigned count = ffs->eps_count;
1750 unsigned long flags;
1753 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1755 struct usb_endpoint_descriptor *ds;
1758 if (ffs->gadget->speed == USB_SPEED_SUPER)
1760 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1765 /* fall-back to lower speed if desc missing for current speed */
1767 ds = ep->descs[desc_idx];
1768 } while (!ds && --desc_idx >= 0);
1775 ep->ep->driver_data = ep;
1777 ret = usb_ep_enable(ep->ep);
1780 epfile->in = usb_endpoint_dir_in(ds);
1781 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1786 wake_up(&epfile->wait);
1791 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1797 /* Parsing and building descriptors and strings *****************************/
1800 * This validates if data pointed by data is a valid USB descriptor as
1801 * well as record how many interfaces, endpoints and strings are
1802 * required by given configuration. Returns address after the
1803 * descriptor or NULL if data is invalid.
1806 enum ffs_entity_type {
1807 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1810 enum ffs_os_desc_type {
1811 FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
1814 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1816 struct usb_descriptor_header *desc,
1819 typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
1820 struct usb_os_desc_header *h, void *data,
1821 unsigned len, void *priv);
1823 static int __must_check ffs_do_single_desc(char *data, unsigned len,
1824 ffs_entity_callback entity,
1827 struct usb_descriptor_header *_ds = (void *)data;
1833 /* At least two bytes are required: length and type */
1835 pr_vdebug("descriptor too short\n");
1839 /* If we have at least as many bytes as the descriptor takes? */
1840 length = _ds->bLength;
1842 pr_vdebug("descriptor longer then available data\n");
1846 #define __entity_check_INTERFACE(val) 1
1847 #define __entity_check_STRING(val) (val)
1848 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1849 #define __entity(type, val) do { \
1850 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1851 if (unlikely(!__entity_check_ ##type(val))) { \
1852 pr_vdebug("invalid entity's value\n"); \
1855 ret = entity(FFS_ ##type, &val, _ds, priv); \
1856 if (unlikely(ret < 0)) { \
1857 pr_debug("entity " #type "(%02x); ret = %d\n", \
1863 /* Parse descriptor depending on type. */
1864 switch (_ds->bDescriptorType) {
1868 case USB_DT_DEVICE_QUALIFIER:
1869 /* function can't have any of those */
1870 pr_vdebug("descriptor reserved for gadget: %d\n",
1871 _ds->bDescriptorType);
1874 case USB_DT_INTERFACE: {
1875 struct usb_interface_descriptor *ds = (void *)_ds;
1876 pr_vdebug("interface descriptor\n");
1877 if (length != sizeof *ds)
1880 __entity(INTERFACE, ds->bInterfaceNumber);
1882 __entity(STRING, ds->iInterface);
1886 case USB_DT_ENDPOINT: {
1887 struct usb_endpoint_descriptor *ds = (void *)_ds;
1888 pr_vdebug("endpoint descriptor\n");
1889 if (length != USB_DT_ENDPOINT_SIZE &&
1890 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1892 __entity(ENDPOINT, ds->bEndpointAddress);
1897 pr_vdebug("hid descriptor\n");
1898 if (length != sizeof(struct hid_descriptor))
1903 if (length != sizeof(struct usb_otg_descriptor))
1907 case USB_DT_INTERFACE_ASSOCIATION: {
1908 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1909 pr_vdebug("interface association descriptor\n");
1910 if (length != sizeof *ds)
1913 __entity(STRING, ds->iFunction);
1917 case USB_DT_SS_ENDPOINT_COMP:
1918 pr_vdebug("EP SS companion descriptor\n");
1919 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1923 case USB_DT_OTHER_SPEED_CONFIG:
1924 case USB_DT_INTERFACE_POWER:
1926 case USB_DT_SECURITY:
1927 case USB_DT_CS_RADIO_CONTROL:
1929 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1933 /* We should never be here */
1934 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1938 pr_vdebug("invalid length: %d (descriptor %d)\n",
1939 _ds->bLength, _ds->bDescriptorType);
1944 #undef __entity_check_DESCRIPTOR
1945 #undef __entity_check_INTERFACE
1946 #undef __entity_check_STRING
1947 #undef __entity_check_ENDPOINT
1952 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1953 ffs_entity_callback entity, void *priv)
1955 const unsigned _len = len;
1956 unsigned long num = 0;
1966 /* Record "descriptor" entity */
1967 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1968 if (unlikely(ret < 0)) {
1969 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1977 ret = ffs_do_single_desc(data, len, entity, priv);
1978 if (unlikely(ret < 0)) {
1979 pr_debug("%s returns %d\n", __func__, ret);
1989 static int __ffs_data_do_entity(enum ffs_entity_type type,
1990 u8 *valuep, struct usb_descriptor_header *desc,
1993 struct ffs_desc_helper *helper = priv;
1994 struct usb_endpoint_descriptor *d;
1999 case FFS_DESCRIPTOR:
2004 * Interfaces are indexed from zero so if we
2005 * encountered interface "n" then there are at least
2008 if (*valuep >= helper->interfaces_count)
2009 helper->interfaces_count = *valuep + 1;
2014 * Strings are indexed from 1 (0 is magic ;) reserved
2015 * for languages list or some such)
2017 if (*valuep > helper->ffs->strings_count)
2018 helper->ffs->strings_count = *valuep;
2023 helper->eps_count++;
2024 if (helper->eps_count >= 15)
2026 /* Check if descriptors for any speed were already parsed */
2027 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
2028 helper->ffs->eps_addrmap[helper->eps_count] =
2029 d->bEndpointAddress;
2030 else if (helper->ffs->eps_addrmap[helper->eps_count] !=
2031 d->bEndpointAddress)
2039 static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
2040 struct usb_os_desc_header *desc)
2042 u16 bcd_version = le16_to_cpu(desc->bcdVersion);
2043 u16 w_index = le16_to_cpu(desc->wIndex);
2045 if (bcd_version != 1) {
2046 pr_vdebug("unsupported os descriptors version: %d",
2052 *next_type = FFS_OS_DESC_EXT_COMPAT;
2055 *next_type = FFS_OS_DESC_EXT_PROP;
2058 pr_vdebug("unsupported os descriptor type: %d", w_index);
2062 return sizeof(*desc);
2066 * Process all extended compatibility/extended property descriptors
2067 * of a feature descriptor
2069 static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
2070 enum ffs_os_desc_type type,
2072 ffs_os_desc_callback entity,
2074 struct usb_os_desc_header *h)
2077 const unsigned _len = len;
2081 /* loop over all ext compat/ext prop descriptors */
2082 while (feature_count--) {
2083 ret = entity(type, h, data, len, priv);
2084 if (unlikely(ret < 0)) {
2085 pr_debug("bad OS descriptor, type: %d\n", type);
2094 /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2095 static int __must_check ffs_do_os_descs(unsigned count,
2096 char *data, unsigned len,
2097 ffs_os_desc_callback entity, void *priv)
2099 const unsigned _len = len;
2100 unsigned long num = 0;
2104 for (num = 0; num < count; ++num) {
2106 enum ffs_os_desc_type type;
2108 struct usb_os_desc_header *desc = (void *)data;
2110 if (len < sizeof(*desc))
2114 * Record "descriptor" entity.
2115 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2116 * Move the data pointer to the beginning of extended
2117 * compatibilities proper or extended properties proper
2118 * portions of the data
2120 if (le32_to_cpu(desc->dwLength) > len)
2123 ret = __ffs_do_os_desc_header(&type, desc);
2124 if (unlikely(ret < 0)) {
2125 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2130 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2132 feature_count = le16_to_cpu(desc->wCount);
2133 if (type == FFS_OS_DESC_EXT_COMPAT &&
2134 (feature_count > 255 || desc->Reserved))
2140 * Process all function/property descriptors
2141 * of this Feature Descriptor
2143 ret = ffs_do_single_os_desc(data, len, type,
2144 feature_count, entity, priv, desc);
2145 if (unlikely(ret < 0)) {
2146 pr_debug("%s returns %d\n", __func__, ret);
2157 * Validate contents of the buffer from userspace related to OS descriptors.
2159 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2160 struct usb_os_desc_header *h, void *data,
2161 unsigned len, void *priv)
2163 struct ffs_data *ffs = priv;
2169 case FFS_OS_DESC_EXT_COMPAT: {
2170 struct usb_ext_compat_desc *d = data;
2173 if (len < sizeof(*d) ||
2174 d->bFirstInterfaceNumber >= ffs->interfaces_count ||
2177 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2178 if (d->Reserved2[i])
2181 length = sizeof(struct usb_ext_compat_desc);
2184 case FFS_OS_DESC_EXT_PROP: {
2185 struct usb_ext_prop_desc *d = data;
2189 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2191 length = le32_to_cpu(d->dwSize);
2192 type = le32_to_cpu(d->dwPropertyDataType);
2193 if (type < USB_EXT_PROP_UNICODE ||
2194 type > USB_EXT_PROP_UNICODE_MULTI) {
2195 pr_vdebug("unsupported os descriptor property type: %d",
2199 pnl = le16_to_cpu(d->wPropertyNameLength);
2200 pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
2201 if (length != 14 + pnl + pdl) {
2202 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2203 length, pnl, pdl, type);
2206 ++ffs->ms_os_descs_ext_prop_count;
2207 /* property name reported to the host as "WCHAR"s */
2208 ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2209 ffs->ms_os_descs_ext_prop_data_len += pdl;
2213 pr_vdebug("unknown descriptor: %d\n", type);
2219 static int __ffs_data_got_descs(struct ffs_data *ffs,
2220 char *const _data, size_t len)
2222 char *data = _data, *raw_descs;
2223 unsigned os_descs_count = 0, counts[3], flags;
2224 int ret = -EINVAL, i;
2225 struct ffs_desc_helper helper;
2229 if (get_unaligned_le32(data + 4) != len)
2232 switch (get_unaligned_le32(data)) {
2233 case FUNCTIONFS_DESCRIPTORS_MAGIC:
2234 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
2238 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2239 flags = get_unaligned_le32(data + 8);
2240 ffs->user_flags = flags;
2241 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2242 FUNCTIONFS_HAS_HS_DESC |
2243 FUNCTIONFS_HAS_SS_DESC |
2244 FUNCTIONFS_HAS_MS_OS_DESC |
2245 FUNCTIONFS_VIRTUAL_ADDR |
2246 FUNCTIONFS_EVENTFD)) {
2257 if (flags & FUNCTIONFS_EVENTFD) {
2261 eventfd_ctx_fdget((int)get_unaligned_le32(data));
2262 if (IS_ERR(ffs->ffs_eventfd)) {
2263 ret = PTR_ERR(ffs->ffs_eventfd);
2264 ffs->ffs_eventfd = NULL;
2271 /* Read fs_count, hs_count and ss_count (if present) */
2272 for (i = 0; i < 3; ++i) {
2273 if (!(flags & (1 << i))) {
2275 } else if (len < 4) {
2278 counts[i] = get_unaligned_le32(data);
2283 if (flags & (1 << i)) {
2284 os_descs_count = get_unaligned_le32(data);
2289 /* Read descriptors */
2292 for (i = 0; i < 3; ++i) {
2295 helper.interfaces_count = 0;
2296 helper.eps_count = 0;
2297 ret = ffs_do_descs(counts[i], data, len,
2298 __ffs_data_do_entity, &helper);
2301 if (!ffs->eps_count && !ffs->interfaces_count) {
2302 ffs->eps_count = helper.eps_count;
2303 ffs->interfaces_count = helper.interfaces_count;
2305 if (ffs->eps_count != helper.eps_count) {
2309 if (ffs->interfaces_count != helper.interfaces_count) {
2317 if (os_descs_count) {
2318 ret = ffs_do_os_descs(os_descs_count, data, len,
2319 __ffs_data_do_os_desc, ffs);
2326 if (raw_descs == data || len) {
2331 ffs->raw_descs_data = _data;
2332 ffs->raw_descs = raw_descs;
2333 ffs->raw_descs_length = data - raw_descs;
2334 ffs->fs_descs_count = counts[0];
2335 ffs->hs_descs_count = counts[1];
2336 ffs->ss_descs_count = counts[2];
2337 ffs->ms_os_descs_count = os_descs_count;
2346 static int __ffs_data_got_strings(struct ffs_data *ffs,
2347 char *const _data, size_t len)
2349 u32 str_count, needed_count, lang_count;
2350 struct usb_gadget_strings **stringtabs, *t;
2351 const char *data = _data;
2352 struct usb_string *s;
2356 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2357 get_unaligned_le32(data + 4) != len))
2359 str_count = get_unaligned_le32(data + 8);
2360 lang_count = get_unaligned_le32(data + 12);
2362 /* if one is zero the other must be zero */
2363 if (unlikely(!str_count != !lang_count))
2366 /* Do we have at least as many strings as descriptors need? */
2367 needed_count = ffs->strings_count;
2368 if (unlikely(str_count < needed_count))
2372 * If we don't need any strings just return and free all
2375 if (!needed_count) {
2380 /* Allocate everything in one chunk so there's less maintenance. */
2384 vla_item(d, struct usb_gadget_strings *, stringtabs,
2386 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2387 vla_item(d, struct usb_string, strings,
2388 lang_count*(needed_count+1));
2390 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2392 if (unlikely(!vlabuf)) {
2397 /* Initialize the VLA pointers */
2398 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2399 t = vla_ptr(vlabuf, d, stringtab);
2402 *stringtabs++ = t++;
2406 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2407 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2408 t = vla_ptr(vlabuf, d, stringtab);
2409 s = vla_ptr(vlabuf, d, strings);
2412 /* For each language */
2416 do { /* lang_count > 0 so we can use do-while */
2417 unsigned needed = needed_count;
2419 if (unlikely(len < 3))
2421 t->language = get_unaligned_le16(data);
2428 /* For each string */
2429 do { /* str_count > 0 so we can use do-while */
2430 size_t length = strnlen(data, len);
2432 if (unlikely(length == len))
2436 * User may provide more strings then we need,
2437 * if that's the case we simply ignore the
2440 if (likely(needed)) {
2442 * s->id will be set while adding
2443 * function to configuration so for
2444 * now just leave garbage here.
2453 } while (--str_count);
2455 s->id = 0; /* terminator */
2459 } while (--lang_count);
2461 /* Some garbage left? */
2466 ffs->stringtabs = stringtabs;
2467 ffs->raw_strings = _data;
2479 /* Events handling and management *******************************************/
2481 static void __ffs_event_add(struct ffs_data *ffs,
2482 enum usb_functionfs_event_type type)
2484 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2488 * Abort any unhandled setup
2490 * We do not need to worry about some cmpxchg() changing value
2491 * of ffs->setup_state without holding the lock because when
2492 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2493 * the source does nothing.
2495 if (ffs->setup_state == FFS_SETUP_PENDING)
2496 ffs->setup_state = FFS_SETUP_CANCELLED;
2499 * Logic of this function guarantees that there are at most four pending
2500 * evens on ffs->ev.types queue. This is important because the queue
2501 * has space for four elements only and __ffs_ep0_read_events function
2502 * depends on that limit as well. If more event types are added, those
2503 * limits have to be revisited or guaranteed to still hold.
2506 case FUNCTIONFS_RESUME:
2507 rem_type2 = FUNCTIONFS_SUSPEND;
2509 case FUNCTIONFS_SUSPEND:
2510 case FUNCTIONFS_SETUP:
2512 /* Discard all similar events */
2515 case FUNCTIONFS_BIND:
2516 case FUNCTIONFS_UNBIND:
2517 case FUNCTIONFS_DISABLE:
2518 case FUNCTIONFS_ENABLE:
2519 /* Discard everything other then power management. */
2520 rem_type1 = FUNCTIONFS_SUSPEND;
2521 rem_type2 = FUNCTIONFS_RESUME;
2526 WARN(1, "%d: unknown event, this should not happen\n", type);
2531 u8 *ev = ffs->ev.types, *out = ev;
2532 unsigned n = ffs->ev.count;
2533 for (; n; --n, ++ev)
2534 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2537 pr_vdebug("purging event %d\n", *ev);
2538 ffs->ev.count = out - ffs->ev.types;
2541 pr_vdebug("adding event %d\n", type);
2542 ffs->ev.types[ffs->ev.count++] = type;
2543 wake_up_locked(&ffs->ev.waitq);
2544 if (ffs->ffs_eventfd)
2545 eventfd_signal(ffs->ffs_eventfd, 1);
2548 static void ffs_event_add(struct ffs_data *ffs,
2549 enum usb_functionfs_event_type type)
2551 unsigned long flags;
2552 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2553 __ffs_event_add(ffs, type);
2554 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2557 /* Bind/unbind USB function hooks *******************************************/
2559 static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2563 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2564 if (ffs->eps_addrmap[i] == endpoint_address)
2569 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2570 struct usb_descriptor_header *desc,
2573 struct usb_endpoint_descriptor *ds = (void *)desc;
2574 struct ffs_function *func = priv;
2575 struct ffs_ep *ffs_ep;
2576 unsigned ep_desc_id;
2578 static const char *speed_names[] = { "full", "high", "super" };
2580 if (type != FFS_DESCRIPTOR)
2584 * If ss_descriptors is not NULL, we are reading super speed
2585 * descriptors; if hs_descriptors is not NULL, we are reading high
2586 * speed descriptors; otherwise, we are reading full speed
2589 if (func->function.ss_descriptors) {
2591 func->function.ss_descriptors[(long)valuep] = desc;
2592 } else if (func->function.hs_descriptors) {
2594 func->function.hs_descriptors[(long)valuep] = desc;
2597 func->function.fs_descriptors[(long)valuep] = desc;
2600 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2603 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2607 ffs_ep = func->eps + idx;
2609 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2610 pr_err("two %sspeed descriptors for EP %d\n",
2611 speed_names[ep_desc_id],
2612 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2615 ffs_ep->descs[ep_desc_id] = ds;
2617 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2619 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2620 if (!ds->wMaxPacketSize)
2621 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2623 struct usb_request *req;
2625 u8 bEndpointAddress;
2628 * We back up bEndpointAddress because autoconfig overwrites
2629 * it with physical endpoint address.
2631 bEndpointAddress = ds->bEndpointAddress;
2632 pr_vdebug("autoconfig\n");
2633 ep = usb_ep_autoconfig(func->gadget, ds);
2636 ep->driver_data = func->eps + idx;
2638 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2644 func->eps_revmap[ds->bEndpointAddress &
2645 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2647 * If we use virtual address mapping, we restore
2648 * original bEndpointAddress value.
2650 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
2651 ds->bEndpointAddress = bEndpointAddress;
2653 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2658 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2659 struct usb_descriptor_header *desc,
2662 struct ffs_function *func = priv;
2668 case FFS_DESCRIPTOR:
2669 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2674 if (func->interfaces_nums[idx] < 0) {
2675 int id = usb_interface_id(func->conf, &func->function);
2676 if (unlikely(id < 0))
2678 func->interfaces_nums[idx] = id;
2680 newValue = func->interfaces_nums[idx];
2684 /* String' IDs are allocated when fsf_data is bound to cdev */
2685 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2690 * USB_DT_ENDPOINT are handled in
2691 * __ffs_func_bind_do_descs().
2693 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2696 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2697 if (unlikely(!func->eps[idx].ep))
2701 struct usb_endpoint_descriptor **descs;
2702 descs = func->eps[idx].descs;
2703 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2708 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2713 static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2714 struct usb_os_desc_header *h, void *data,
2715 unsigned len, void *priv)
2717 struct ffs_function *func = priv;
2721 case FFS_OS_DESC_EXT_COMPAT: {
2722 struct usb_ext_compat_desc *desc = data;
2723 struct usb_os_desc_table *t;
2725 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
2726 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
2727 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
2728 ARRAY_SIZE(desc->CompatibleID) +
2729 ARRAY_SIZE(desc->SubCompatibleID));
2730 length = sizeof(*desc);
2733 case FFS_OS_DESC_EXT_PROP: {
2734 struct usb_ext_prop_desc *desc = data;
2735 struct usb_os_desc_table *t;
2736 struct usb_os_desc_ext_prop *ext_prop;
2737 char *ext_prop_name;
2738 char *ext_prop_data;
2740 t = &func->function.os_desc_table[h->interface];
2741 t->if_id = func->interfaces_nums[h->interface];
2743 ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
2744 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
2746 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
2747 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2748 ext_prop->data_len = le32_to_cpu(*(u32 *)
2749 usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
2750 length = ext_prop->name_len + ext_prop->data_len + 14;
2752 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
2753 func->ffs->ms_os_descs_ext_prop_name_avail +=
2756 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
2757 func->ffs->ms_os_descs_ext_prop_data_avail +=
2759 memcpy(ext_prop_data,
2760 usb_ext_prop_data_ptr(data, ext_prop->name_len),
2761 ext_prop->data_len);
2762 /* unicode data reported to the host as "WCHAR"s */
2763 switch (ext_prop->type) {
2764 case USB_EXT_PROP_UNICODE:
2765 case USB_EXT_PROP_UNICODE_ENV:
2766 case USB_EXT_PROP_UNICODE_LINK:
2767 case USB_EXT_PROP_UNICODE_MULTI:
2768 ext_prop->data_len *= 2;
2771 ext_prop->data = ext_prop_data;
2773 memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
2774 ext_prop->name_len);
2775 /* property name reported to the host as "WCHAR"s */
2776 ext_prop->name_len *= 2;
2777 ext_prop->name = ext_prop_name;
2779 t->os_desc->ext_prop_len +=
2780 ext_prop->name_len + ext_prop->data_len + 14;
2781 ++t->os_desc->ext_prop_count;
2782 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
2786 pr_vdebug("unknown descriptor: %d\n", type);
2792 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2793 struct usb_configuration *c)
2795 struct ffs_function *func = ffs_func_from_usb(f);
2796 struct f_fs_opts *ffs_opts =
2797 container_of(f->fi, struct f_fs_opts, func_inst);
2803 * Legacy gadget triggers binding in functionfs_ready_callback,
2804 * which already uses locking; taking the same lock here would
2807 * Configfs-enabled gadgets however do need ffs_dev_lock.
2809 if (!ffs_opts->no_configfs)
2811 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2812 func->ffs = ffs_opts->dev->ffs_data;
2813 if (!ffs_opts->no_configfs)
2816 return ERR_PTR(ret);
2819 func->gadget = c->cdev->gadget;
2822 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2823 * configurations are bound in sequence with list_for_each_entry,
2824 * in each configuration its functions are bound in sequence
2825 * with list_for_each_entry, so we assume no race condition
2826 * with regard to ffs_opts->bound access
2828 if (!ffs_opts->refcnt) {
2829 ret = functionfs_bind(func->ffs, c->cdev);
2831 return ERR_PTR(ret);
2834 func->function.strings = func->ffs->stringtabs;
2839 static int _ffs_func_bind(struct usb_configuration *c,
2840 struct usb_function *f)
2842 struct ffs_function *func = ffs_func_from_usb(f);
2843 struct ffs_data *ffs = func->ffs;
2845 const int full = !!func->ffs->fs_descs_count;
2846 const int high = gadget_is_dualspeed(func->gadget) &&
2847 func->ffs->hs_descs_count;
2848 const int super = gadget_is_superspeed(func->gadget) &&
2849 func->ffs->ss_descs_count;
2851 int fs_len, hs_len, ss_len, ret, i;
2852 struct ffs_ep *eps_ptr;
2854 /* Make it a single chunk, less management later on */
2856 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2857 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2858 full ? ffs->fs_descs_count + 1 : 0);
2859 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2860 high ? ffs->hs_descs_count + 1 : 0);
2861 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2862 super ? ffs->ss_descs_count + 1 : 0);
2863 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2864 vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
2865 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2866 vla_item_with_sz(d, char[16], ext_compat,
2867 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2868 vla_item_with_sz(d, struct usb_os_desc, os_desc,
2869 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2870 vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
2871 ffs->ms_os_descs_ext_prop_count);
2872 vla_item_with_sz(d, char, ext_prop_name,
2873 ffs->ms_os_descs_ext_prop_name_len);
2874 vla_item_with_sz(d, char, ext_prop_data,
2875 ffs->ms_os_descs_ext_prop_data_len);
2876 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
2881 /* Has descriptors only for speeds gadget does not support */
2882 if (unlikely(!(full | high | super)))
2885 /* Allocate a single chunk, less management later on */
2886 vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
2887 if (unlikely(!vlabuf))
2890 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
2891 ffs->ms_os_descs_ext_prop_name_avail =
2892 vla_ptr(vlabuf, d, ext_prop_name);
2893 ffs->ms_os_descs_ext_prop_data_avail =
2894 vla_ptr(vlabuf, d, ext_prop_data);
2896 /* Copy descriptors */
2897 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2898 ffs->raw_descs_length);
2900 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2901 eps_ptr = vla_ptr(vlabuf, d, eps);
2902 for (i = 0; i < ffs->eps_count; i++)
2903 eps_ptr[i].num = -1;
2906 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2908 func->eps = vla_ptr(vlabuf, d, eps);
2909 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
2912 * Go through all the endpoint descriptors and allocate
2913 * endpoints first, so that later we can rewrite the endpoint
2914 * numbers without worrying that it may be described later on.
2917 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2918 fs_len = ffs_do_descs(ffs->fs_descs_count,
2919 vla_ptr(vlabuf, d, raw_descs),
2921 __ffs_func_bind_do_descs, func);
2922 if (unlikely(fs_len < 0)) {
2931 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2932 hs_len = ffs_do_descs(ffs->hs_descs_count,
2933 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2934 d_raw_descs__sz - fs_len,
2935 __ffs_func_bind_do_descs, func);
2936 if (unlikely(hs_len < 0)) {
2944 if (likely(super)) {
2945 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2946 ss_len = ffs_do_descs(ffs->ss_descs_count,
2947 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2948 d_raw_descs__sz - fs_len - hs_len,
2949 __ffs_func_bind_do_descs, func);
2950 if (unlikely(ss_len < 0)) {
2959 * Now handle interface numbers allocation and interface and
2960 * endpoint numbers rewriting. We can do that in one go
2963 ret = ffs_do_descs(ffs->fs_descs_count +
2964 (high ? ffs->hs_descs_count : 0) +
2965 (super ? ffs->ss_descs_count : 0),
2966 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
2967 __ffs_func_bind_do_nums, func);
2968 if (unlikely(ret < 0))
2971 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
2972 if (c->cdev->use_os_string) {
2973 for (i = 0; i < ffs->interfaces_count; ++i) {
2974 struct usb_os_desc *desc;
2976 desc = func->function.os_desc_table[i].os_desc =
2977 vla_ptr(vlabuf, d, os_desc) +
2978 i * sizeof(struct usb_os_desc);
2979 desc->ext_compat_id =
2980 vla_ptr(vlabuf, d, ext_compat) + i * 16;
2981 INIT_LIST_HEAD(&desc->ext_prop);
2983 ret = ffs_do_os_descs(ffs->ms_os_descs_count,
2984 vla_ptr(vlabuf, d, raw_descs) +
2985 fs_len + hs_len + ss_len,
2986 d_raw_descs__sz - fs_len - hs_len -
2988 __ffs_func_bind_do_os_desc, func);
2989 if (unlikely(ret < 0))
2992 func->function.os_desc_n =
2993 c->cdev->use_os_string ? ffs->interfaces_count : 0;
2995 /* And we're done */
2996 ffs_event_add(ffs, FUNCTIONFS_BIND);
3000 /* XXX Do we need to release all claimed endpoints here? */
3004 static int ffs_func_bind(struct usb_configuration *c,
3005 struct usb_function *f)
3007 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
3008 struct ffs_function *func = ffs_func_from_usb(f);
3011 if (IS_ERR(ffs_opts))
3012 return PTR_ERR(ffs_opts);
3014 ret = _ffs_func_bind(c, f);
3015 if (ret && !--ffs_opts->refcnt)
3016 functionfs_unbind(func->ffs);
3022 /* Other USB function hooks *************************************************/
3024 static void ffs_reset_work(struct work_struct *work)
3026 struct ffs_data *ffs = container_of(work,
3027 struct ffs_data, reset_work);
3028 ffs_data_reset(ffs);
3031 static int ffs_func_set_alt(struct usb_function *f,
3032 unsigned interface, unsigned alt)
3034 struct ffs_function *func = ffs_func_from_usb(f);
3035 struct ffs_data *ffs = func->ffs;
3038 if (alt != (unsigned)-1) {
3039 intf = ffs_func_revmap_intf(func, interface);
3040 if (unlikely(intf < 0))
3045 ffs_func_eps_disable(ffs->func);
3047 if (ffs->state == FFS_DEACTIVATED) {
3048 ffs->state = FFS_CLOSING;
3049 INIT_WORK(&ffs->reset_work, ffs_reset_work);
3050 schedule_work(&ffs->reset_work);
3054 if (ffs->state != FFS_ACTIVE)
3057 if (alt == (unsigned)-1) {
3059 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
3064 ret = ffs_func_eps_enable(func);
3065 if (likely(ret >= 0))
3066 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
3070 static void ffs_func_disable(struct usb_function *f)
3072 ffs_func_set_alt(f, 0, (unsigned)-1);
3075 static int ffs_func_setup(struct usb_function *f,
3076 const struct usb_ctrlrequest *creq)
3078 struct ffs_function *func = ffs_func_from_usb(f);
3079 struct ffs_data *ffs = func->ffs;
3080 unsigned long flags;
3085 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
3086 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
3087 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
3088 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
3089 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
3092 * Most requests directed to interface go through here
3093 * (notable exceptions are set/get interface) so we need to
3094 * handle them. All other either handled by composite or
3095 * passed to usb_configuration->setup() (if one is set). No
3096 * matter, we will handle requests directed to endpoint here
3097 * as well (as it's straightforward) but what to do with any
3100 if (ffs->state != FFS_ACTIVE)
3103 switch (creq->bRequestType & USB_RECIP_MASK) {
3104 case USB_RECIP_INTERFACE:
3105 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
3106 if (unlikely(ret < 0))
3110 case USB_RECIP_ENDPOINT:
3111 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
3112 if (unlikely(ret < 0))
3114 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
3115 ret = func->ffs->eps_addrmap[ret];
3122 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
3123 ffs->ev.setup = *creq;
3124 ffs->ev.setup.wIndex = cpu_to_le16(ret);
3125 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
3126 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
3131 static void ffs_func_suspend(struct usb_function *f)
3134 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
3137 static void ffs_func_resume(struct usb_function *f)
3140 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
3144 /* Endpoint and interface numbers reverse mapping ***************************/
3146 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
3148 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
3149 return num ? num : -EDOM;
3152 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
3154 short *nums = func->interfaces_nums;
3155 unsigned count = func->ffs->interfaces_count;
3157 for (; count; --count, ++nums) {
3158 if (*nums >= 0 && *nums == intf)
3159 return nums - func->interfaces_nums;
3166 /* Devices management *******************************************************/
3168 static LIST_HEAD(ffs_devices);
3170 static struct ffs_dev *_ffs_do_find_dev(const char *name)
3172 struct ffs_dev *dev;
3174 list_for_each_entry(dev, &ffs_devices, entry) {
3175 if (!dev->name || !name)
3177 if (strcmp(dev->name, name) == 0)
3185 * ffs_lock must be taken by the caller of this function
3187 static struct ffs_dev *_ffs_get_single_dev(void)
3189 struct ffs_dev *dev;
3191 if (list_is_singular(&ffs_devices)) {
3192 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
3201 * ffs_lock must be taken by the caller of this function
3203 static struct ffs_dev *_ffs_find_dev(const char *name)
3205 struct ffs_dev *dev;
3207 dev = _ffs_get_single_dev();
3211 return _ffs_do_find_dev(name);
3214 /* Configfs support *********************************************************/
3216 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3218 return container_of(to_config_group(item), struct f_fs_opts,
3222 static void ffs_attr_release(struct config_item *item)
3224 struct f_fs_opts *opts = to_ffs_opts(item);
3226 usb_put_function_instance(&opts->func_inst);
3229 static struct configfs_item_operations ffs_item_ops = {
3230 .release = ffs_attr_release,
3233 static struct config_item_type ffs_func_type = {
3234 .ct_item_ops = &ffs_item_ops,
3235 .ct_owner = THIS_MODULE,
3239 /* Function registration interface ******************************************/
3241 static void ffs_free_inst(struct usb_function_instance *f)
3243 struct f_fs_opts *opts;
3245 opts = to_f_fs_opts(f);
3247 _ffs_free_dev(opts->dev);
3252 #define MAX_INST_NAME_LEN 40
3254 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3256 struct f_fs_opts *opts;
3261 name_len = strlen(name) + 1;
3262 if (name_len > MAX_INST_NAME_LEN)
3263 return -ENAMETOOLONG;
3265 ptr = kstrndup(name, name_len, GFP_KERNEL);
3269 opts = to_f_fs_opts(fi);
3274 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
3275 ret = _ffs_name_dev(opts->dev, ptr);
3281 opts->dev->name_allocated = true;
3290 static struct usb_function_instance *ffs_alloc_inst(void)
3292 struct f_fs_opts *opts;
3293 struct ffs_dev *dev;
3295 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3297 return ERR_PTR(-ENOMEM);
3299 opts->func_inst.set_inst_name = ffs_set_inst_name;
3300 opts->func_inst.free_func_inst = ffs_free_inst;
3302 dev = _ffs_alloc_dev();
3306 return ERR_CAST(dev);
3311 config_group_init_type_name(&opts->func_inst.group, "",
3313 return &opts->func_inst;
3316 static void ffs_free(struct usb_function *f)
3318 kfree(ffs_func_from_usb(f));
3321 static void ffs_func_unbind(struct usb_configuration *c,
3322 struct usb_function *f)
3324 struct ffs_function *func = ffs_func_from_usb(f);
3325 struct ffs_data *ffs = func->ffs;
3326 struct f_fs_opts *opts =
3327 container_of(f->fi, struct f_fs_opts, func_inst);
3328 struct ffs_ep *ep = func->eps;
3329 unsigned count = ffs->eps_count;
3330 unsigned long flags;
3333 if (ffs->func == func) {
3334 ffs_func_eps_disable(func);
3338 if (!--opts->refcnt)
3339 functionfs_unbind(ffs);
3341 /* cleanup after autoconfig */
3342 spin_lock_irqsave(&func->ffs->eps_lock, flags);
3344 if (ep->ep && ep->req)
3345 usb_ep_free_request(ep->ep, ep->req);
3349 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3353 * eps, descriptors and interfaces_nums are allocated in the
3354 * same chunk so only one free is required.
3356 func->function.fs_descriptors = NULL;
3357 func->function.hs_descriptors = NULL;
3358 func->function.ss_descriptors = NULL;
3359 func->interfaces_nums = NULL;
3361 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3364 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3366 struct ffs_function *func;
3370 func = kzalloc(sizeof(*func), GFP_KERNEL);
3371 if (unlikely(!func))
3372 return ERR_PTR(-ENOMEM);
3374 func->function.name = "Function FS Gadget";
3376 func->function.bind = ffs_func_bind;
3377 func->function.unbind = ffs_func_unbind;
3378 func->function.set_alt = ffs_func_set_alt;
3379 func->function.disable = ffs_func_disable;
3380 func->function.setup = ffs_func_setup;
3381 func->function.suspend = ffs_func_suspend;
3382 func->function.resume = ffs_func_resume;
3383 func->function.free_func = ffs_free;
3385 return &func->function;
3389 * ffs_lock must be taken by the caller of this function
3391 static struct ffs_dev *_ffs_alloc_dev(void)
3393 struct ffs_dev *dev;
3396 if (_ffs_get_single_dev())
3397 return ERR_PTR(-EBUSY);
3399 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3401 return ERR_PTR(-ENOMEM);
3403 if (list_empty(&ffs_devices)) {
3404 ret = functionfs_init();
3407 return ERR_PTR(ret);
3411 list_add(&dev->entry, &ffs_devices);
3417 * ffs_lock must be taken by the caller of this function
3418 * The caller is responsible for "name" being available whenever f_fs needs it
3420 static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
3422 struct ffs_dev *existing;
3424 existing = _ffs_do_find_dev(name);
3434 * The caller is responsible for "name" being available whenever f_fs needs it
3436 int ffs_name_dev(struct ffs_dev *dev, const char *name)
3441 ret = _ffs_name_dev(dev, name);
3446 EXPORT_SYMBOL_GPL(ffs_name_dev);
3448 int ffs_single_dev(struct ffs_dev *dev)
3455 if (!list_is_singular(&ffs_devices))
3463 EXPORT_SYMBOL_GPL(ffs_single_dev);
3466 * ffs_lock must be taken by the caller of this function
3468 static void _ffs_free_dev(struct ffs_dev *dev)
3470 list_del(&dev->entry);
3471 if (dev->name_allocated)
3474 if (list_empty(&ffs_devices))
3475 functionfs_cleanup();
3478 static void *ffs_acquire_dev(const char *dev_name)
3480 struct ffs_dev *ffs_dev;
3485 ffs_dev = _ffs_find_dev(dev_name);
3487 ffs_dev = ERR_PTR(-ENOENT);
3488 else if (ffs_dev->mounted)
3489 ffs_dev = ERR_PTR(-EBUSY);
3490 else if (ffs_dev->ffs_acquire_dev_callback &&
3491 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
3492 ffs_dev = ERR_PTR(-ENOENT);
3494 ffs_dev->mounted = true;
3500 static void ffs_release_dev(struct ffs_data *ffs_data)
3502 struct ffs_dev *ffs_dev;
3507 ffs_dev = ffs_data->private_data;
3509 ffs_dev->mounted = false;
3511 if (ffs_dev->ffs_release_dev_callback)
3512 ffs_dev->ffs_release_dev_callback(ffs_dev);
3518 static int ffs_ready(struct ffs_data *ffs)
3520 struct ffs_dev *ffs_obj;
3526 ffs_obj = ffs->private_data;
3531 if (WARN_ON(ffs_obj->desc_ready)) {
3536 ffs_obj->desc_ready = true;
3537 ffs_obj->ffs_data = ffs;
3539 if (ffs_obj->ffs_ready_callback) {
3540 ret = ffs_obj->ffs_ready_callback(ffs);
3545 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
3551 static void ffs_closed(struct ffs_data *ffs)
3553 struct ffs_dev *ffs_obj;
3554 struct f_fs_opts *opts;
3559 ffs_obj = ffs->private_data;
3563 ffs_obj->desc_ready = false;
3565 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
3566 ffs_obj->ffs_closed_callback)
3567 ffs_obj->ffs_closed_callback(ffs);
3570 opts = ffs_obj->opts;
3574 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3575 || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
3578 unregister_gadget_item(ffs_obj->opts->
3579 func_inst.group.cg_item.ci_parent->ci_parent);
3584 /* Misc helper functions ****************************************************/
3586 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3589 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3590 : mutex_lock_interruptible(mutex);
3593 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
3600 data = kmalloc(len, GFP_KERNEL);
3601 if (unlikely(!data))
3602 return ERR_PTR(-ENOMEM);
3604 if (unlikely(copy_from_user(data, buf, len))) {
3606 return ERR_PTR(-EFAULT);
3609 pr_vdebug("Buffer from user space:\n");
3610 ffs_dump_mem("", data, len);
3615 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3616 MODULE_LICENSE("GPL");
3617 MODULE_AUTHOR("Michal Nazarewicz");