1 // SPDX-License-Identifier: GPL-2.0+
3 * f_fs.c -- user mode file system API for USB composite function controllers
5 * Copyright (C) 2010 Samsung Electronics
6 * Author: Michal Nazarewicz <mina86@mina86.com>
8 * Based on inode.c (GadgetFS) which was:
9 * Copyright (C) 2003-2004 David Brownell
10 * Copyright (C) 2003 Agilent Technologies
15 /* #define VERBOSE_DEBUG */
17 #include <linux/blkdev.h>
18 #include <linux/pagemap.h>
19 #include <linux/export.h>
20 #include <linux/fs_parser.h>
21 #include <linux/hid.h>
23 #include <linux/module.h>
24 #include <linux/scatterlist.h>
25 #include <linux/sched/signal.h>
26 #include <linux/uio.h>
27 #include <linux/vmalloc.h>
28 #include <asm/unaligned.h>
30 #include <linux/usb/ccid.h>
31 #include <linux/usb/composite.h>
32 #include <linux/usb/functionfs.h>
34 #include <linux/aio.h>
35 #include <linux/kthread.h>
36 #include <linux/poll.h>
37 #include <linux/eventfd.h>
41 #include "u_os_desc.h"
44 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
46 /* Reference counter handling */
47 static void ffs_data_get(struct ffs_data *ffs);
48 static void ffs_data_put(struct ffs_data *ffs);
49 /* Creates new ffs_data object. */
50 static struct ffs_data *__must_check ffs_data_new(const char *dev_name)
51 __attribute__((malloc));
53 /* Opened counter handling. */
54 static void ffs_data_opened(struct ffs_data *ffs);
55 static void ffs_data_closed(struct ffs_data *ffs);
57 /* Called with ffs->mutex held; take over ownership of data. */
58 static int __must_check
59 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
60 static int __must_check
61 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
64 /* The function structure ***************************************************/
69 struct usb_configuration *conf;
70 struct usb_gadget *gadget;
75 short *interfaces_nums;
77 struct usb_function function;
81 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
83 return container_of(f, struct ffs_function, function);
87 static inline enum ffs_setup_state
88 ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
90 return (enum ffs_setup_state)
91 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
95 static void ffs_func_eps_disable(struct ffs_function *func);
96 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
98 static int ffs_func_bind(struct usb_configuration *,
99 struct usb_function *);
100 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
101 static void ffs_func_disable(struct usb_function *);
102 static int ffs_func_setup(struct usb_function *,
103 const struct usb_ctrlrequest *);
104 static bool ffs_func_req_match(struct usb_function *,
105 const struct usb_ctrlrequest *,
107 static void ffs_func_suspend(struct usb_function *);
108 static void ffs_func_resume(struct usb_function *);
111 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
112 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
115 /* The endpoints structures *************************************************/
118 struct usb_ep *ep; /* P: ffs->eps_lock */
119 struct usb_request *req; /* P: epfile->mutex */
121 /* [0]: full speed, [1]: high speed, [2]: super speed */
122 struct usb_endpoint_descriptor *descs[3];
128 /* Protects ep->ep and ep->req. */
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 * The pointer is initialised with NULL value and may be set by
141 * __ffs_epfile_read_data function to point to a temporary buffer.
143 * In normal operation, calls to __ffs_epfile_read_buffered will consume
144 * data from said buffer and eventually free it. Importantly, while the
145 * function is using the buffer, it sets the pointer to NULL. This is
146 * all right since __ffs_epfile_read_data and __ffs_epfile_read_buffered
147 * can never run concurrently (they are synchronised by epfile->mutex)
148 * so the latter will not assign a new value to the pointer.
150 * Meanwhile ffs_func_eps_disable frees the buffer (if the pointer is
151 * valid) and sets the pointer to READ_BUFFER_DROP value. This special
152 * value is crux of the synchronisation between ffs_func_eps_disable and
153 * __ffs_epfile_read_data.
155 * Once __ffs_epfile_read_data is about to finish it will try to set the
156 * pointer back to its old value (as described above), but seeing as the
157 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
160 * == State transitions ==
162 * • ptr == NULL: (initial state)
163 * ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP
164 * ◦ __ffs_epfile_read_buffered: nop
165 * ◦ __ffs_epfile_read_data allocates temp buffer: go to ptr == buf
166 * ◦ reading finishes: n/a, not in ‘and reading’ state
168 * ◦ __ffs_epfile_read_buffer_free: nop
169 * ◦ __ffs_epfile_read_buffered: go to ptr == NULL
170 * ◦ __ffs_epfile_read_data allocates temp buffer: free buf, nop
171 * ◦ reading finishes: n/a, not in ‘and reading’ state
173 * ◦ __ffs_epfile_read_buffer_free: free buf, go to ptr == DROP
174 * ◦ __ffs_epfile_read_buffered: go to ptr == NULL and reading
175 * ◦ __ffs_epfile_read_data: n/a, __ffs_epfile_read_buffered
176 * is always called first
177 * ◦ reading finishes: n/a, not in ‘and reading’ state
178 * • ptr == NULL and reading:
179 * ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP and reading
180 * ◦ __ffs_epfile_read_buffered: n/a, mutex is held
181 * ◦ __ffs_epfile_read_data: n/a, mutex is held
182 * ◦ reading finishes and …
183 * … all data read: free buf, go to ptr == NULL
184 * … otherwise: go to ptr == buf and reading
185 * • ptr == DROP and reading:
186 * ◦ __ffs_epfile_read_buffer_free: nop
187 * ◦ __ffs_epfile_read_buffered: n/a, mutex is held
188 * ◦ __ffs_epfile_read_data: n/a, mutex is held
189 * ◦ reading finishes: free buf, go to ptr == DROP
191 struct ffs_buffer *read_buffer;
192 #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
196 unsigned char in; /* P: ffs->eps_lock */
197 unsigned char isoc; /* P: ffs->eps_lock */
208 /* ffs_io_data structure ***************************************************/
215 struct iov_iter data;
219 struct mm_struct *mm;
220 struct work_struct work;
223 struct usb_request *req;
227 struct ffs_data *ffs;
230 struct completion done;
233 struct ffs_desc_helper {
234 struct ffs_data *ffs;
235 unsigned interfaces_count;
239 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
240 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
242 static struct dentry *
243 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
244 const struct file_operations *fops);
246 /* Devices management *******************************************************/
248 DEFINE_MUTEX(ffs_lock);
249 EXPORT_SYMBOL_GPL(ffs_lock);
251 static struct ffs_dev *_ffs_find_dev(const char *name);
252 static struct ffs_dev *_ffs_alloc_dev(void);
253 static void _ffs_free_dev(struct ffs_dev *dev);
254 static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data);
255 static void ffs_release_dev(struct ffs_dev *ffs_dev);
256 static int ffs_ready(struct ffs_data *ffs);
257 static void ffs_closed(struct ffs_data *ffs);
259 /* Misc helper functions ****************************************************/
261 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
262 __attribute__((warn_unused_result, nonnull));
263 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
264 __attribute__((warn_unused_result, nonnull));
267 /* Control file aka ep0 *****************************************************/
269 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
271 struct ffs_data *ffs = req->context;
273 complete(&ffs->ep0req_completion);
276 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
277 __releases(&ffs->ev.waitq.lock)
279 struct usb_request *req = ffs->ep0req;
282 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
284 spin_unlock_irq(&ffs->ev.waitq.lock);
290 * UDC layer requires to provide a buffer even for ZLP, but should
291 * not use it at all. Let's provide some poisoned pointer to catch
292 * possible bug in the driver.
294 if (req->buf == NULL)
295 req->buf = (void *)0xDEADBABE;
297 reinit_completion(&ffs->ep0req_completion);
299 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
303 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
305 usb_ep_dequeue(ffs->gadget->ep0, req);
309 ffs->setup_state = FFS_NO_SETUP;
310 return req->status ? req->status : req->actual;
313 static int __ffs_ep0_stall(struct ffs_data *ffs)
315 if (ffs->ev.can_stall) {
316 pr_vdebug("ep0 stall\n");
317 usb_ep_set_halt(ffs->gadget->ep0);
318 ffs->setup_state = FFS_NO_SETUP;
321 pr_debug("bogus ep0 stall!\n");
326 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
327 size_t len, loff_t *ptr)
329 struct ffs_data *ffs = file->private_data;
335 /* Fast check if setup was canceled */
336 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
340 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
345 switch (ffs->state) {
346 case FFS_READ_DESCRIPTORS:
347 case FFS_READ_STRINGS:
354 data = ffs_prepare_buffer(buf, len);
361 if (ffs->state == FFS_READ_DESCRIPTORS) {
362 pr_info("read descriptors\n");
363 ret = __ffs_data_got_descs(ffs, data, len);
367 ffs->state = FFS_READ_STRINGS;
370 pr_info("read strings\n");
371 ret = __ffs_data_got_strings(ffs, data, len);
375 ret = ffs_epfiles_create(ffs);
377 ffs->state = FFS_CLOSING;
381 ffs->state = FFS_ACTIVE;
382 mutex_unlock(&ffs->mutex);
384 ret = ffs_ready(ffs);
386 ffs->state = FFS_CLOSING;
397 * We're called from user space, we can use _irq
398 * rather then _irqsave
400 spin_lock_irq(&ffs->ev.waitq.lock);
401 switch (ffs_setup_state_clear_cancelled(ffs)) {
402 case FFS_SETUP_CANCELLED:
410 case FFS_SETUP_PENDING:
414 /* FFS_SETUP_PENDING */
415 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
416 spin_unlock_irq(&ffs->ev.waitq.lock);
417 ret = __ffs_ep0_stall(ffs);
421 /* FFS_SETUP_PENDING and not stall */
422 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
424 spin_unlock_irq(&ffs->ev.waitq.lock);
426 data = ffs_prepare_buffer(buf, len);
432 spin_lock_irq(&ffs->ev.waitq.lock);
435 * We are guaranteed to be still in FFS_ACTIVE state
436 * but the state of setup could have changed from
437 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
438 * to check for that. If that happened we copied data
439 * from user space in vain but it's unlikely.
441 * For sure we are not in FFS_NO_SETUP since this is
442 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
443 * transition can be performed and it's protected by
446 if (ffs_setup_state_clear_cancelled(ffs) ==
447 FFS_SETUP_CANCELLED) {
450 spin_unlock_irq(&ffs->ev.waitq.lock);
452 /* unlocks spinlock */
453 ret = __ffs_ep0_queue_wait(ffs, data, len);
463 mutex_unlock(&ffs->mutex);
467 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
468 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
470 __releases(&ffs->ev.waitq.lock)
473 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
474 * size of ffs->ev.types array (which is four) so that's how much space
477 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
478 const size_t size = n * sizeof *events;
481 memset(events, 0, size);
484 events[i].type = ffs->ev.types[i];
485 if (events[i].type == FUNCTIONFS_SETUP) {
486 events[i].u.setup = ffs->ev.setup;
487 ffs->setup_state = FFS_SETUP_PENDING;
493 memmove(ffs->ev.types, ffs->ev.types + n,
494 ffs->ev.count * sizeof *ffs->ev.types);
496 spin_unlock_irq(&ffs->ev.waitq.lock);
497 mutex_unlock(&ffs->mutex);
499 return copy_to_user(buf, events, size) ? -EFAULT : size;
502 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
503 size_t len, loff_t *ptr)
505 struct ffs_data *ffs = file->private_data;
512 /* Fast check if setup was canceled */
513 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
517 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
522 if (ffs->state != FFS_ACTIVE) {
528 * We're called from user space, we can use _irq rather then
531 spin_lock_irq(&ffs->ev.waitq.lock);
533 switch (ffs_setup_state_clear_cancelled(ffs)) {
534 case FFS_SETUP_CANCELLED:
539 n = len / sizeof(struct usb_functionfs_event);
545 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
550 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
556 /* unlocks spinlock */
557 return __ffs_ep0_read_events(ffs, buf,
558 min(n, (size_t)ffs->ev.count));
560 case FFS_SETUP_PENDING:
561 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
562 spin_unlock_irq(&ffs->ev.waitq.lock);
563 ret = __ffs_ep0_stall(ffs);
567 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
569 spin_unlock_irq(&ffs->ev.waitq.lock);
572 data = kmalloc(len, GFP_KERNEL);
579 spin_lock_irq(&ffs->ev.waitq.lock);
581 /* See ffs_ep0_write() */
582 if (ffs_setup_state_clear_cancelled(ffs) ==
583 FFS_SETUP_CANCELLED) {
588 /* unlocks spinlock */
589 ret = __ffs_ep0_queue_wait(ffs, data, len);
590 if ((ret > 0) && (copy_to_user(buf, data, len)))
599 spin_unlock_irq(&ffs->ev.waitq.lock);
601 mutex_unlock(&ffs->mutex);
606 static int ffs_ep0_open(struct inode *inode, struct file *file)
608 struct ffs_data *ffs = inode->i_private;
612 if (ffs->state == FFS_CLOSING)
615 file->private_data = ffs;
616 ffs_data_opened(ffs);
618 return stream_open(inode, file);
621 static int ffs_ep0_release(struct inode *inode, struct file *file)
623 struct ffs_data *ffs = file->private_data;
627 ffs_data_closed(ffs);
632 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
634 struct ffs_data *ffs = file->private_data;
635 struct usb_gadget *gadget = ffs->gadget;
640 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
641 struct ffs_function *func = ffs->func;
642 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
643 } else if (gadget && gadget->ops->ioctl) {
644 ret = gadget->ops->ioctl(gadget, code, value);
652 static __poll_t ffs_ep0_poll(struct file *file, poll_table *wait)
654 struct ffs_data *ffs = file->private_data;
655 __poll_t mask = EPOLLWRNORM;
658 poll_wait(file, &ffs->ev.waitq, wait);
660 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
664 switch (ffs->state) {
665 case FFS_READ_DESCRIPTORS:
666 case FFS_READ_STRINGS:
671 switch (ffs->setup_state) {
677 case FFS_SETUP_PENDING:
678 case FFS_SETUP_CANCELLED:
679 mask |= (EPOLLIN | EPOLLOUT);
686 case FFS_DEACTIVATED:
690 mutex_unlock(&ffs->mutex);
695 static const struct file_operations ffs_ep0_operations = {
698 .open = ffs_ep0_open,
699 .write = ffs_ep0_write,
700 .read = ffs_ep0_read,
701 .release = ffs_ep0_release,
702 .unlocked_ioctl = ffs_ep0_ioctl,
703 .poll = ffs_ep0_poll,
707 /* "Normal" endpoints operations ********************************************/
709 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
711 struct ffs_io_data *io_data = req->context;
715 io_data->status = req->status;
717 io_data->status = req->actual;
719 complete(&io_data->done);
722 static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
724 ssize_t ret = copy_to_iter(data, data_len, iter);
728 if (iov_iter_count(iter))
732 * Dear user space developer!
734 * TL;DR: To stop getting below error message in your kernel log, change
735 * user space code using functionfs to align read buffers to a max
738 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
739 * packet size. When unaligned buffer is passed to functionfs, it
740 * internally uses a larger, aligned buffer so that such UDCs are happy.
742 * Unfortunately, this means that host may send more data than was
743 * requested in read(2) system call. f_fs doesn’t know what to do with
744 * that excess data so it simply drops it.
746 * Was the buffer aligned in the first place, no such problem would
749 * Data may be dropped only in AIO reads. Synchronous reads are handled
750 * by splitting a request into multiple parts. This splitting may still
751 * be a problem though so it’s likely best to align the buffer
752 * regardless of it being AIO or not..
754 * This only affects OUT endpoints, i.e. reading data with a read(2),
755 * aio_read(2) etc. system calls. Writing data to an IN endpoint is not
758 pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
759 "Align read buffer size to max packet size to avoid the problem.\n",
766 * allocate a virtually contiguous buffer and create a scatterlist describing it
767 * @sg_table - pointer to a place to be filled with sg_table contents
768 * @size - required buffer size
770 static void *ffs_build_sg_list(struct sg_table *sgt, size_t sz)
774 unsigned int n_pages;
781 n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
782 pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
788 for (i = 0, ptr = vaddr; i < n_pages; ++i, ptr += PAGE_SIZE)
789 pages[i] = vmalloc_to_page(ptr);
791 if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL)) {
802 static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
806 return ffs_build_sg_list(&io_data->sgt, data_len);
808 return kmalloc(data_len, GFP_KERNEL);
811 static inline void ffs_free_buffer(struct ffs_io_data *io_data)
816 if (io_data->use_sg) {
817 sg_free_table(&io_data->sgt);
824 static void ffs_user_copy_worker(struct work_struct *work)
826 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
828 int ret = io_data->req->status ? io_data->req->status :
829 io_data->req->actual;
830 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
832 if (io_data->read && ret > 0) {
833 kthread_use_mm(io_data->mm);
834 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
835 kthread_unuse_mm(io_data->mm);
838 io_data->kiocb->ki_complete(io_data->kiocb, ret);
840 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
841 eventfd_signal(io_data->ffs->ffs_eventfd, 1);
843 usb_ep_free_request(io_data->ep, io_data->req);
846 kfree(io_data->to_free);
847 ffs_free_buffer(io_data);
851 static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
852 struct usb_request *req)
854 struct ffs_io_data *io_data = req->context;
855 struct ffs_data *ffs = io_data->ffs;
859 INIT_WORK(&io_data->work, ffs_user_copy_worker);
860 queue_work(ffs->io_completion_wq, &io_data->work);
863 static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
866 * See comment in struct ffs_epfile for full read_buffer pointer
867 * synchronisation story.
869 struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP);
870 if (buf && buf != READ_BUFFER_DROP)
874 /* Assumes epfile->mutex is held. */
875 static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
876 struct iov_iter *iter)
879 * Null out epfile->read_buffer so ffs_func_eps_disable does not free
880 * the buffer while we are using it. See comment in struct ffs_epfile
881 * for full read_buffer pointer synchronisation story.
883 struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL);
885 if (!buf || buf == READ_BUFFER_DROP)
888 ret = copy_to_iter(buf->data, buf->length, iter);
889 if (buf->length == ret) {
894 if (iov_iter_count(iter)) {
901 if (cmpxchg(&epfile->read_buffer, NULL, buf))
907 /* Assumes epfile->mutex is held. */
908 static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
909 void *data, int data_len,
910 struct iov_iter *iter)
912 struct ffs_buffer *buf;
914 ssize_t ret = copy_to_iter(data, data_len, iter);
918 if (iov_iter_count(iter))
921 /* See ffs_copy_to_iter for more context. */
922 pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
926 buf = kmalloc(struct_size(buf, storage, data_len), GFP_KERNEL);
929 buf->length = data_len;
930 buf->data = buf->storage;
931 memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len));
934 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
935 * ffs_func_eps_disable has been called in the meanwhile). See comment
936 * in struct ffs_epfile for full read_buffer pointer synchronisation
939 if (cmpxchg(&epfile->read_buffer, NULL, buf))
945 static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
947 struct ffs_epfile *epfile = file->private_data;
948 struct usb_request *req;
951 ssize_t ret, data_len = -EINVAL;
954 /* Are we still active? */
955 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
958 /* Wait for endpoint to be enabled */
961 if (file->f_flags & O_NONBLOCK)
964 ret = wait_event_interruptible(
965 epfile->ffs->wait, (ep = epfile->ep));
971 halt = (!io_data->read == !epfile->in);
972 if (halt && epfile->isoc)
975 /* We will be using request and read_buffer */
976 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
980 /* Allocate & copy */
982 struct usb_gadget *gadget;
985 * Do we have buffered data from previous partial read? Check
986 * that for synchronous case only because we do not have
987 * facility to ‘wake up’ a pending asynchronous read and push
988 * buffered data to it which we would need to make things behave
991 if (!io_data->aio && io_data->read) {
992 ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
998 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
999 * before the waiting completes, so do not assign to 'gadget'
1002 gadget = epfile->ffs->gadget;
1004 spin_lock_irq(&epfile->ffs->eps_lock);
1005 /* In the meantime, endpoint got disabled or changed. */
1006 if (epfile->ep != ep) {
1010 data_len = iov_iter_count(&io_data->data);
1012 * Controller may require buffer size to be aligned to
1013 * maxpacketsize of an out endpoint.
1016 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
1018 io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
1019 spin_unlock_irq(&epfile->ffs->eps_lock);
1021 data = ffs_alloc_buffer(io_data, data_len);
1026 if (!io_data->read &&
1027 !copy_from_iter_full(data, data_len, &io_data->data)) {
1033 spin_lock_irq(&epfile->ffs->eps_lock);
1035 if (epfile->ep != ep) {
1036 /* In the meantime, endpoint got disabled or changed. */
1039 ret = usb_ep_set_halt(ep->ep);
1042 } else if (data_len == -EINVAL) {
1044 * Sanity Check: even though data_len can't be used
1045 * uninitialized at the time I write this comment, some
1046 * compilers complain about this situation.
1047 * In order to keep the code clean from warnings, data_len is
1048 * being initialized to -EINVAL during its declaration, which
1049 * means we can't rely on compiler anymore to warn no future
1050 * changes won't result in data_len being used uninitialized.
1051 * For such reason, we're adding this redundant sanity check
1054 WARN(1, "%s: data_len == -EINVAL\n", __func__);
1056 } else if (!io_data->aio) {
1057 bool interrupted = false;
1060 if (io_data->use_sg) {
1062 req->sg = io_data->sgt.sgl;
1063 req->num_sgs = io_data->sgt.nents;
1068 req->length = data_len;
1070 io_data->buf = data;
1072 init_completion(&io_data->done);
1073 req->context = io_data;
1074 req->complete = ffs_epfile_io_complete;
1076 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
1080 spin_unlock_irq(&epfile->ffs->eps_lock);
1082 if (wait_for_completion_interruptible(&io_data->done)) {
1083 spin_lock_irq(&epfile->ffs->eps_lock);
1084 if (epfile->ep != ep) {
1089 * To avoid race condition with ffs_epfile_io_complete,
1090 * dequeue the request first then check
1091 * status. usb_ep_dequeue API should guarantee no race
1092 * condition with req->complete callback.
1094 usb_ep_dequeue(ep->ep, req);
1095 spin_unlock_irq(&epfile->ffs->eps_lock);
1096 wait_for_completion(&io_data->done);
1097 interrupted = io_data->status < 0;
1102 else if (io_data->read && io_data->status > 0)
1103 ret = __ffs_epfile_read_data(epfile, data, io_data->status,
1106 ret = io_data->status;
1108 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
1111 if (io_data->use_sg) {
1113 req->sg = io_data->sgt.sgl;
1114 req->num_sgs = io_data->sgt.nents;
1119 req->length = data_len;
1121 io_data->buf = data;
1122 io_data->ep = ep->ep;
1124 io_data->ffs = epfile->ffs;
1126 req->context = io_data;
1127 req->complete = ffs_epfile_async_io_complete;
1129 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
1131 io_data->req = NULL;
1132 usb_ep_free_request(ep->ep, req);
1138 * Do not kfree the buffer in this function. It will be freed
1139 * by ffs_user_copy_worker.
1145 spin_unlock_irq(&epfile->ffs->eps_lock);
1147 mutex_unlock(&epfile->mutex);
1149 if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */
1150 ffs_free_buffer(io_data);
1155 ffs_epfile_open(struct inode *inode, struct file *file)
1157 struct ffs_epfile *epfile = inode->i_private;
1161 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1164 file->private_data = epfile;
1165 ffs_data_opened(epfile->ffs);
1167 return stream_open(inode, file);
1170 static int ffs_aio_cancel(struct kiocb *kiocb)
1172 struct ffs_io_data *io_data = kiocb->private;
1173 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
1174 unsigned long flags;
1179 spin_lock_irqsave(&epfile->ffs->eps_lock, flags);
1181 if (io_data && io_data->ep && io_data->req)
1182 value = usb_ep_dequeue(io_data->ep, io_data->req);
1186 spin_unlock_irqrestore(&epfile->ffs->eps_lock, flags);
1191 static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
1193 struct ffs_io_data io_data, *p = &io_data;
1198 if (!is_sync_kiocb(kiocb)) {
1199 p = kzalloc(sizeof(io_data), GFP_KERNEL);
1204 memset(p, 0, sizeof(*p));
1211 p->mm = current->mm;
1216 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1218 res = ffs_epfile_io(kiocb->ki_filp, p);
1219 if (res == -EIOCBQUEUED)
1228 static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
1230 struct ffs_io_data io_data, *p = &io_data;
1235 if (!is_sync_kiocb(kiocb)) {
1236 p = kzalloc(sizeof(io_data), GFP_KERNEL);
1241 memset(p, 0, sizeof(*p));
1248 p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
1257 p->mm = current->mm;
1262 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1264 res = ffs_epfile_io(kiocb->ki_filp, p);
1265 if (res == -EIOCBQUEUED)
1278 ffs_epfile_release(struct inode *inode, struct file *file)
1280 struct ffs_epfile *epfile = inode->i_private;
1284 __ffs_epfile_read_buffer_free(epfile);
1285 ffs_data_closed(epfile->ffs);
1290 static long ffs_epfile_ioctl(struct file *file, unsigned code,
1291 unsigned long value)
1293 struct ffs_epfile *epfile = file->private_data;
1299 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1302 /* Wait for endpoint to be enabled */
1305 if (file->f_flags & O_NONBLOCK)
1308 ret = wait_event_interruptible(
1309 epfile->ffs->wait, (ep = epfile->ep));
1314 spin_lock_irq(&epfile->ffs->eps_lock);
1316 /* In the meantime, endpoint got disabled or changed. */
1317 if (epfile->ep != ep) {
1318 spin_unlock_irq(&epfile->ffs->eps_lock);
1323 case FUNCTIONFS_FIFO_STATUS:
1324 ret = usb_ep_fifo_status(epfile->ep->ep);
1326 case FUNCTIONFS_FIFO_FLUSH:
1327 usb_ep_fifo_flush(epfile->ep->ep);
1330 case FUNCTIONFS_CLEAR_HALT:
1331 ret = usb_ep_clear_halt(epfile->ep->ep);
1333 case FUNCTIONFS_ENDPOINT_REVMAP:
1334 ret = epfile->ep->num;
1336 case FUNCTIONFS_ENDPOINT_DESC:
1339 struct usb_endpoint_descriptor desc1, *desc;
1341 switch (epfile->ffs->gadget->speed) {
1342 case USB_SPEED_SUPER:
1343 case USB_SPEED_SUPER_PLUS:
1346 case USB_SPEED_HIGH:
1353 desc = epfile->ep->descs[desc_idx];
1354 memcpy(&desc1, desc, desc->bLength);
1356 spin_unlock_irq(&epfile->ffs->eps_lock);
1357 ret = copy_to_user((void __user *)value, &desc1, desc1.bLength);
1365 spin_unlock_irq(&epfile->ffs->eps_lock);
1370 static const struct file_operations ffs_epfile_operations = {
1371 .llseek = no_llseek,
1373 .open = ffs_epfile_open,
1374 .write_iter = ffs_epfile_write_iter,
1375 .read_iter = ffs_epfile_read_iter,
1376 .release = ffs_epfile_release,
1377 .unlocked_ioctl = ffs_epfile_ioctl,
1378 .compat_ioctl = compat_ptr_ioctl,
1382 /* File system and super block operations ***********************************/
1385 * Mounting the file system creates a controller file, used first for
1386 * function configuration then later for event monitoring.
1389 static struct inode *__must_check
1390 ffs_sb_make_inode(struct super_block *sb, void *data,
1391 const struct file_operations *fops,
1392 const struct inode_operations *iops,
1393 struct ffs_file_perms *perms)
1395 struct inode *inode;
1399 inode = new_inode(sb);
1402 struct timespec64 ts = current_time(inode);
1404 inode->i_ino = get_next_ino();
1405 inode->i_mode = perms->mode;
1406 inode->i_uid = perms->uid;
1407 inode->i_gid = perms->gid;
1408 inode->i_atime = ts;
1409 inode->i_mtime = ts;
1410 inode->i_ctime = ts;
1411 inode->i_private = data;
1413 inode->i_fop = fops;
1421 /* Create "regular" file */
1422 static struct dentry *ffs_sb_create_file(struct super_block *sb,
1423 const char *name, void *data,
1424 const struct file_operations *fops)
1426 struct ffs_data *ffs = sb->s_fs_info;
1427 struct dentry *dentry;
1428 struct inode *inode;
1432 dentry = d_alloc_name(sb->s_root, name);
1436 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1442 d_add(dentry, inode);
1447 static const struct super_operations ffs_sb_operations = {
1448 .statfs = simple_statfs,
1449 .drop_inode = generic_delete_inode,
1452 struct ffs_sb_fill_data {
1453 struct ffs_file_perms perms;
1455 const char *dev_name;
1457 struct ffs_data *ffs_data;
1460 static int ffs_sb_fill(struct super_block *sb, struct fs_context *fc)
1462 struct ffs_sb_fill_data *data = fc->fs_private;
1463 struct inode *inode;
1464 struct ffs_data *ffs = data->ffs_data;
1469 data->ffs_data = NULL;
1470 sb->s_fs_info = ffs;
1471 sb->s_blocksize = PAGE_SIZE;
1472 sb->s_blocksize_bits = PAGE_SHIFT;
1473 sb->s_magic = FUNCTIONFS_MAGIC;
1474 sb->s_op = &ffs_sb_operations;
1475 sb->s_time_gran = 1;
1478 data->perms.mode = data->root_mode;
1479 inode = ffs_sb_make_inode(sb, NULL,
1480 &simple_dir_operations,
1481 &simple_dir_inode_operations,
1483 sb->s_root = d_make_root(inode);
1488 if (!ffs_sb_create_file(sb, "ep0", ffs, &ffs_ep0_operations))
1503 static const struct fs_parameter_spec ffs_fs_fs_parameters[] = {
1504 fsparam_bool ("no_disconnect", Opt_no_disconnect),
1505 fsparam_u32 ("rmode", Opt_rmode),
1506 fsparam_u32 ("fmode", Opt_fmode),
1507 fsparam_u32 ("mode", Opt_mode),
1508 fsparam_u32 ("uid", Opt_uid),
1509 fsparam_u32 ("gid", Opt_gid),
1513 static int ffs_fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
1515 struct ffs_sb_fill_data *data = fc->fs_private;
1516 struct fs_parse_result result;
1521 opt = fs_parse(fc, ffs_fs_fs_parameters, param, &result);
1526 case Opt_no_disconnect:
1527 data->no_disconnect = result.boolean;
1530 data->root_mode = (result.uint_32 & 0555) | S_IFDIR;
1533 data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1536 data->root_mode = (result.uint_32 & 0555) | S_IFDIR;
1537 data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1541 data->perms.uid = make_kuid(current_user_ns(), result.uint_32);
1542 if (!uid_valid(data->perms.uid))
1543 goto unmapped_value;
1546 data->perms.gid = make_kgid(current_user_ns(), result.uint_32);
1547 if (!gid_valid(data->perms.gid))
1548 goto unmapped_value;
1558 return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32);
1562 * Set up the superblock for a mount.
1564 static int ffs_fs_get_tree(struct fs_context *fc)
1566 struct ffs_sb_fill_data *ctx = fc->fs_private;
1567 struct ffs_data *ffs;
1573 return invalf(fc, "No source specified");
1575 ffs = ffs_data_new(fc->source);
1578 ffs->file_perms = ctx->perms;
1579 ffs->no_disconnect = ctx->no_disconnect;
1581 ffs->dev_name = kstrdup(fc->source, GFP_KERNEL);
1582 if (!ffs->dev_name) {
1587 ret = ffs_acquire_dev(ffs->dev_name, ffs);
1593 ctx->ffs_data = ffs;
1594 return get_tree_nodev(fc, ffs_sb_fill);
1597 static void ffs_fs_free_fc(struct fs_context *fc)
1599 struct ffs_sb_fill_data *ctx = fc->fs_private;
1602 if (ctx->ffs_data) {
1603 ffs_data_put(ctx->ffs_data);
1610 static const struct fs_context_operations ffs_fs_context_ops = {
1611 .free = ffs_fs_free_fc,
1612 .parse_param = ffs_fs_parse_param,
1613 .get_tree = ffs_fs_get_tree,
1616 static int ffs_fs_init_fs_context(struct fs_context *fc)
1618 struct ffs_sb_fill_data *ctx;
1620 ctx = kzalloc(sizeof(struct ffs_sb_fill_data), GFP_KERNEL);
1624 ctx->perms.mode = S_IFREG | 0600;
1625 ctx->perms.uid = GLOBAL_ROOT_UID;
1626 ctx->perms.gid = GLOBAL_ROOT_GID;
1627 ctx->root_mode = S_IFDIR | 0500;
1628 ctx->no_disconnect = false;
1630 fc->fs_private = ctx;
1631 fc->ops = &ffs_fs_context_ops;
1636 ffs_fs_kill_sb(struct super_block *sb)
1640 kill_litter_super(sb);
1642 ffs_data_closed(sb->s_fs_info);
1645 static struct file_system_type ffs_fs_type = {
1646 .owner = THIS_MODULE,
1647 .name = "functionfs",
1648 .init_fs_context = ffs_fs_init_fs_context,
1649 .parameters = ffs_fs_fs_parameters,
1650 .kill_sb = ffs_fs_kill_sb,
1652 MODULE_ALIAS_FS("functionfs");
1655 /* Driver's main init/cleanup functions *************************************/
1657 static int functionfs_init(void)
1663 ret = register_filesystem(&ffs_fs_type);
1665 pr_info("file system registered\n");
1667 pr_err("failed registering file system (%d)\n", ret);
1672 static void functionfs_cleanup(void)
1676 pr_info("unloading\n");
1677 unregister_filesystem(&ffs_fs_type);
1681 /* ffs_data and ffs_function construction and destruction code **************/
1683 static void ffs_data_clear(struct ffs_data *ffs);
1684 static void ffs_data_reset(struct ffs_data *ffs);
1686 static void ffs_data_get(struct ffs_data *ffs)
1690 refcount_inc(&ffs->ref);
1693 static void ffs_data_opened(struct ffs_data *ffs)
1697 refcount_inc(&ffs->ref);
1698 if (atomic_add_return(1, &ffs->opened) == 1 &&
1699 ffs->state == FFS_DEACTIVATED) {
1700 ffs->state = FFS_CLOSING;
1701 ffs_data_reset(ffs);
1705 static void ffs_data_put(struct ffs_data *ffs)
1709 if (refcount_dec_and_test(&ffs->ref)) {
1710 pr_info("%s(): freeing\n", __func__);
1711 ffs_data_clear(ffs);
1712 ffs_release_dev(ffs->private_data);
1713 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1714 swait_active(&ffs->ep0req_completion.wait) ||
1715 waitqueue_active(&ffs->wait));
1716 destroy_workqueue(ffs->io_completion_wq);
1717 kfree(ffs->dev_name);
1722 static void ffs_data_closed(struct ffs_data *ffs)
1724 struct ffs_epfile *epfiles;
1725 unsigned long flags;
1729 if (atomic_dec_and_test(&ffs->opened)) {
1730 if (ffs->no_disconnect) {
1731 ffs->state = FFS_DEACTIVATED;
1732 spin_lock_irqsave(&ffs->eps_lock, flags);
1733 epfiles = ffs->epfiles;
1734 ffs->epfiles = NULL;
1735 spin_unlock_irqrestore(&ffs->eps_lock,
1739 ffs_epfiles_destroy(epfiles,
1742 if (ffs->setup_state == FFS_SETUP_PENDING)
1743 __ffs_ep0_stall(ffs);
1745 ffs->state = FFS_CLOSING;
1746 ffs_data_reset(ffs);
1749 if (atomic_read(&ffs->opened) < 0) {
1750 ffs->state = FFS_CLOSING;
1751 ffs_data_reset(ffs);
1757 static struct ffs_data *ffs_data_new(const char *dev_name)
1759 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1765 ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name);
1766 if (!ffs->io_completion_wq) {
1771 refcount_set(&ffs->ref, 1);
1772 atomic_set(&ffs->opened, 0);
1773 ffs->state = FFS_READ_DESCRIPTORS;
1774 mutex_init(&ffs->mutex);
1775 spin_lock_init(&ffs->eps_lock);
1776 init_waitqueue_head(&ffs->ev.waitq);
1777 init_waitqueue_head(&ffs->wait);
1778 init_completion(&ffs->ep0req_completion);
1780 /* XXX REVISIT need to update it in some places, or do we? */
1781 ffs->ev.can_stall = 1;
1786 static void ffs_data_clear(struct ffs_data *ffs)
1788 struct ffs_epfile *epfiles;
1789 unsigned long flags;
1795 BUG_ON(ffs->gadget);
1797 spin_lock_irqsave(&ffs->eps_lock, flags);
1798 epfiles = ffs->epfiles;
1799 ffs->epfiles = NULL;
1800 spin_unlock_irqrestore(&ffs->eps_lock, flags);
1803 * potential race possible between ffs_func_eps_disable
1804 * & ffs_epfile_release therefore maintaining a local
1805 * copy of epfile will save us from use-after-free.
1808 ffs_epfiles_destroy(epfiles, ffs->eps_count);
1809 ffs->epfiles = NULL;
1812 if (ffs->ffs_eventfd) {
1813 eventfd_ctx_put(ffs->ffs_eventfd);
1814 ffs->ffs_eventfd = NULL;
1817 kfree(ffs->raw_descs_data);
1818 kfree(ffs->raw_strings);
1819 kfree(ffs->stringtabs);
1822 static void ffs_data_reset(struct ffs_data *ffs)
1826 ffs_data_clear(ffs);
1828 ffs->raw_descs_data = NULL;
1829 ffs->raw_descs = NULL;
1830 ffs->raw_strings = NULL;
1831 ffs->stringtabs = NULL;
1833 ffs->raw_descs_length = 0;
1834 ffs->fs_descs_count = 0;
1835 ffs->hs_descs_count = 0;
1836 ffs->ss_descs_count = 0;
1838 ffs->strings_count = 0;
1839 ffs->interfaces_count = 0;
1844 ffs->state = FFS_READ_DESCRIPTORS;
1845 ffs->setup_state = FFS_NO_SETUP;
1848 ffs->ms_os_descs_ext_prop_count = 0;
1849 ffs->ms_os_descs_ext_prop_name_len = 0;
1850 ffs->ms_os_descs_ext_prop_data_len = 0;
1854 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1856 struct usb_gadget_strings **lang;
1861 if (WARN_ON(ffs->state != FFS_ACTIVE
1862 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1865 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1869 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1872 ffs->ep0req->complete = ffs_ep0_complete;
1873 ffs->ep0req->context = ffs;
1875 lang = ffs->stringtabs;
1877 for (; *lang; ++lang) {
1878 struct usb_string *str = (*lang)->strings;
1880 for (; str->s; ++id, ++str)
1885 ffs->gadget = cdev->gadget;
1890 static void functionfs_unbind(struct ffs_data *ffs)
1894 if (!WARN_ON(!ffs->gadget)) {
1895 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1898 clear_bit(FFS_FL_BOUND, &ffs->flags);
1903 static int ffs_epfiles_create(struct ffs_data *ffs)
1905 struct ffs_epfile *epfile, *epfiles;
1910 count = ffs->eps_count;
1911 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1916 for (i = 1; i <= count; ++i, ++epfile) {
1918 mutex_init(&epfile->mutex);
1919 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1920 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
1922 sprintf(epfile->name, "ep%u", i);
1923 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
1925 &ffs_epfile_operations);
1926 if (!epfile->dentry) {
1927 ffs_epfiles_destroy(epfiles, i - 1);
1932 ffs->epfiles = epfiles;
1936 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1938 struct ffs_epfile *epfile = epfiles;
1942 for (; count; --count, ++epfile) {
1943 BUG_ON(mutex_is_locked(&epfile->mutex));
1944 if (epfile->dentry) {
1945 d_delete(epfile->dentry);
1946 dput(epfile->dentry);
1947 epfile->dentry = NULL;
1954 static void ffs_func_eps_disable(struct ffs_function *func)
1957 struct ffs_epfile *epfile;
1958 unsigned short count;
1959 unsigned long flags;
1961 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1962 count = func->ffs->eps_count;
1963 epfile = func->ffs->epfiles;
1966 /* pending requests get nuked */
1968 usb_ep_disable(ep->ep);
1973 __ffs_epfile_read_buffer_free(epfile);
1977 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1980 static int ffs_func_eps_enable(struct ffs_function *func)
1982 struct ffs_data *ffs;
1984 struct ffs_epfile *epfile;
1985 unsigned short count;
1986 unsigned long flags;
1989 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1992 epfile = ffs->epfiles;
1993 count = ffs->eps_count;
1995 ep->ep->driver_data = ep;
1997 ret = config_ep_by_speed(func->gadget, &func->function, ep->ep);
1999 pr_err("%s: config_ep_by_speed(%s) returned %d\n",
2000 __func__, ep->ep->name, ret);
2004 ret = usb_ep_enable(ep->ep);
2007 epfile->in = usb_endpoint_dir_in(ep->ep->desc);
2008 epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc);
2017 wake_up_interruptible(&ffs->wait);
2018 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2024 /* Parsing and building descriptors and strings *****************************/
2027 * This validates if data pointed by data is a valid USB descriptor as
2028 * well as record how many interfaces, endpoints and strings are
2029 * required by given configuration. Returns address after the
2030 * descriptor or NULL if data is invalid.
2033 enum ffs_entity_type {
2034 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
2037 enum ffs_os_desc_type {
2038 FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
2041 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
2043 struct usb_descriptor_header *desc,
2046 typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
2047 struct usb_os_desc_header *h, void *data,
2048 unsigned len, void *priv);
2050 static int __must_check ffs_do_single_desc(char *data, unsigned len,
2051 ffs_entity_callback entity,
2052 void *priv, int *current_class)
2054 struct usb_descriptor_header *_ds = (void *)data;
2060 /* At least two bytes are required: length and type */
2062 pr_vdebug("descriptor too short\n");
2066 /* If we have at least as many bytes as the descriptor takes? */
2067 length = _ds->bLength;
2069 pr_vdebug("descriptor longer then available data\n");
2073 #define __entity_check_INTERFACE(val) 1
2074 #define __entity_check_STRING(val) (val)
2075 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
2076 #define __entity(type, val) do { \
2077 pr_vdebug("entity " #type "(%02x)\n", (val)); \
2078 if (!__entity_check_ ##type(val)) { \
2079 pr_vdebug("invalid entity's value\n"); \
2082 ret = entity(FFS_ ##type, &val, _ds, priv); \
2084 pr_debug("entity " #type "(%02x); ret = %d\n", \
2090 /* Parse descriptor depending on type. */
2091 switch (_ds->bDescriptorType) {
2095 case USB_DT_DEVICE_QUALIFIER:
2096 /* function can't have any of those */
2097 pr_vdebug("descriptor reserved for gadget: %d\n",
2098 _ds->bDescriptorType);
2101 case USB_DT_INTERFACE: {
2102 struct usb_interface_descriptor *ds = (void *)_ds;
2103 pr_vdebug("interface descriptor\n");
2104 if (length != sizeof *ds)
2107 __entity(INTERFACE, ds->bInterfaceNumber);
2109 __entity(STRING, ds->iInterface);
2110 *current_class = ds->bInterfaceClass;
2114 case USB_DT_ENDPOINT: {
2115 struct usb_endpoint_descriptor *ds = (void *)_ds;
2116 pr_vdebug("endpoint descriptor\n");
2117 if (length != USB_DT_ENDPOINT_SIZE &&
2118 length != USB_DT_ENDPOINT_AUDIO_SIZE)
2120 __entity(ENDPOINT, ds->bEndpointAddress);
2124 case USB_TYPE_CLASS | 0x01:
2125 if (*current_class == USB_INTERFACE_CLASS_HID) {
2126 pr_vdebug("hid descriptor\n");
2127 if (length != sizeof(struct hid_descriptor))
2130 } else if (*current_class == USB_INTERFACE_CLASS_CCID) {
2131 pr_vdebug("ccid descriptor\n");
2132 if (length != sizeof(struct ccid_descriptor))
2136 pr_vdebug("unknown descriptor: %d for class %d\n",
2137 _ds->bDescriptorType, *current_class);
2142 if (length != sizeof(struct usb_otg_descriptor))
2146 case USB_DT_INTERFACE_ASSOCIATION: {
2147 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
2148 pr_vdebug("interface association descriptor\n");
2149 if (length != sizeof *ds)
2152 __entity(STRING, ds->iFunction);
2156 case USB_DT_SS_ENDPOINT_COMP:
2157 pr_vdebug("EP SS companion descriptor\n");
2158 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
2162 case USB_DT_OTHER_SPEED_CONFIG:
2163 case USB_DT_INTERFACE_POWER:
2165 case USB_DT_SECURITY:
2166 case USB_DT_CS_RADIO_CONTROL:
2168 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
2172 /* We should never be here */
2173 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
2177 pr_vdebug("invalid length: %d (descriptor %d)\n",
2178 _ds->bLength, _ds->bDescriptorType);
2183 #undef __entity_check_DESCRIPTOR
2184 #undef __entity_check_INTERFACE
2185 #undef __entity_check_STRING
2186 #undef __entity_check_ENDPOINT
2191 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
2192 ffs_entity_callback entity, void *priv)
2194 const unsigned _len = len;
2195 unsigned long num = 0;
2196 int current_class = -1;
2206 /* Record "descriptor" entity */
2207 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
2209 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
2217 ret = ffs_do_single_desc(data, len, entity, priv,
2220 pr_debug("%s returns %d\n", __func__, ret);
2230 static int __ffs_data_do_entity(enum ffs_entity_type type,
2231 u8 *valuep, struct usb_descriptor_header *desc,
2234 struct ffs_desc_helper *helper = priv;
2235 struct usb_endpoint_descriptor *d;
2240 case FFS_DESCRIPTOR:
2245 * Interfaces are indexed from zero so if we
2246 * encountered interface "n" then there are at least
2249 if (*valuep >= helper->interfaces_count)
2250 helper->interfaces_count = *valuep + 1;
2255 * Strings are indexed from 1 (0 is reserved
2256 * for languages list)
2258 if (*valuep > helper->ffs->strings_count)
2259 helper->ffs->strings_count = *valuep;
2264 helper->eps_count++;
2265 if (helper->eps_count >= FFS_MAX_EPS_COUNT)
2267 /* Check if descriptors for any speed were already parsed */
2268 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
2269 helper->ffs->eps_addrmap[helper->eps_count] =
2270 d->bEndpointAddress;
2271 else if (helper->ffs->eps_addrmap[helper->eps_count] !=
2272 d->bEndpointAddress)
2280 static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
2281 struct usb_os_desc_header *desc)
2283 u16 bcd_version = le16_to_cpu(desc->bcdVersion);
2284 u16 w_index = le16_to_cpu(desc->wIndex);
2286 if (bcd_version != 1) {
2287 pr_vdebug("unsupported os descriptors version: %d",
2293 *next_type = FFS_OS_DESC_EXT_COMPAT;
2296 *next_type = FFS_OS_DESC_EXT_PROP;
2299 pr_vdebug("unsupported os descriptor type: %d", w_index);
2303 return sizeof(*desc);
2307 * Process all extended compatibility/extended property descriptors
2308 * of a feature descriptor
2310 static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
2311 enum ffs_os_desc_type type,
2313 ffs_os_desc_callback entity,
2315 struct usb_os_desc_header *h)
2318 const unsigned _len = len;
2322 /* loop over all ext compat/ext prop descriptors */
2323 while (feature_count--) {
2324 ret = entity(type, h, data, len, priv);
2326 pr_debug("bad OS descriptor, type: %d\n", type);
2335 /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2336 static int __must_check ffs_do_os_descs(unsigned count,
2337 char *data, unsigned len,
2338 ffs_os_desc_callback entity, void *priv)
2340 const unsigned _len = len;
2341 unsigned long num = 0;
2345 for (num = 0; num < count; ++num) {
2347 enum ffs_os_desc_type type;
2349 struct usb_os_desc_header *desc = (void *)data;
2351 if (len < sizeof(*desc))
2355 * Record "descriptor" entity.
2356 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2357 * Move the data pointer to the beginning of extended
2358 * compatibilities proper or extended properties proper
2359 * portions of the data
2361 if (le32_to_cpu(desc->dwLength) > len)
2364 ret = __ffs_do_os_desc_header(&type, desc);
2366 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2371 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2373 feature_count = le16_to_cpu(desc->wCount);
2374 if (type == FFS_OS_DESC_EXT_COMPAT &&
2375 (feature_count > 255 || desc->Reserved))
2381 * Process all function/property descriptors
2382 * of this Feature Descriptor
2384 ret = ffs_do_single_os_desc(data, len, type,
2385 feature_count, entity, priv, desc);
2387 pr_debug("%s returns %d\n", __func__, ret);
2398 * Validate contents of the buffer from userspace related to OS descriptors.
2400 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2401 struct usb_os_desc_header *h, void *data,
2402 unsigned len, void *priv)
2404 struct ffs_data *ffs = priv;
2410 case FFS_OS_DESC_EXT_COMPAT: {
2411 struct usb_ext_compat_desc *d = data;
2414 if (len < sizeof(*d) ||
2415 d->bFirstInterfaceNumber >= ffs->interfaces_count)
2417 if (d->Reserved1 != 1) {
2419 * According to the spec, Reserved1 must be set to 1
2420 * but older kernels incorrectly rejected non-zero
2421 * values. We fix it here to avoid returning EINVAL
2422 * in response to values we used to accept.
2424 pr_debug("usb_ext_compat_desc::Reserved1 forced to 1\n");
2427 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2428 if (d->Reserved2[i])
2431 length = sizeof(struct usb_ext_compat_desc);
2434 case FFS_OS_DESC_EXT_PROP: {
2435 struct usb_ext_prop_desc *d = data;
2439 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2441 length = le32_to_cpu(d->dwSize);
2444 type = le32_to_cpu(d->dwPropertyDataType);
2445 if (type < USB_EXT_PROP_UNICODE ||
2446 type > USB_EXT_PROP_UNICODE_MULTI) {
2447 pr_vdebug("unsupported os descriptor property type: %d",
2451 pnl = le16_to_cpu(d->wPropertyNameLength);
2452 if (length < 14 + pnl) {
2453 pr_vdebug("invalid os descriptor length: %d pnl:%d (descriptor %d)\n",
2457 pdl = le32_to_cpu(*(__le32 *)((u8 *)data + 10 + pnl));
2458 if (length != 14 + pnl + pdl) {
2459 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2460 length, pnl, pdl, type);
2463 ++ffs->ms_os_descs_ext_prop_count;
2464 /* property name reported to the host as "WCHAR"s */
2465 ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2466 ffs->ms_os_descs_ext_prop_data_len += pdl;
2470 pr_vdebug("unknown descriptor: %d\n", type);
2476 static int __ffs_data_got_descs(struct ffs_data *ffs,
2477 char *const _data, size_t len)
2479 char *data = _data, *raw_descs;
2480 unsigned os_descs_count = 0, counts[3], flags;
2481 int ret = -EINVAL, i;
2482 struct ffs_desc_helper helper;
2486 if (get_unaligned_le32(data + 4) != len)
2489 switch (get_unaligned_le32(data)) {
2490 case FUNCTIONFS_DESCRIPTORS_MAGIC:
2491 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
2495 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2496 flags = get_unaligned_le32(data + 8);
2497 ffs->user_flags = flags;
2498 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2499 FUNCTIONFS_HAS_HS_DESC |
2500 FUNCTIONFS_HAS_SS_DESC |
2501 FUNCTIONFS_HAS_MS_OS_DESC |
2502 FUNCTIONFS_VIRTUAL_ADDR |
2503 FUNCTIONFS_EVENTFD |
2504 FUNCTIONFS_ALL_CTRL_RECIP |
2505 FUNCTIONFS_CONFIG0_SETUP)) {
2516 if (flags & FUNCTIONFS_EVENTFD) {
2520 eventfd_ctx_fdget((int)get_unaligned_le32(data));
2521 if (IS_ERR(ffs->ffs_eventfd)) {
2522 ret = PTR_ERR(ffs->ffs_eventfd);
2523 ffs->ffs_eventfd = NULL;
2530 /* Read fs_count, hs_count and ss_count (if present) */
2531 for (i = 0; i < 3; ++i) {
2532 if (!(flags & (1 << i))) {
2534 } else if (len < 4) {
2537 counts[i] = get_unaligned_le32(data);
2542 if (flags & (1 << i)) {
2546 os_descs_count = get_unaligned_le32(data);
2551 /* Read descriptors */
2554 for (i = 0; i < 3; ++i) {
2557 helper.interfaces_count = 0;
2558 helper.eps_count = 0;
2559 ret = ffs_do_descs(counts[i], data, len,
2560 __ffs_data_do_entity, &helper);
2563 if (!ffs->eps_count && !ffs->interfaces_count) {
2564 ffs->eps_count = helper.eps_count;
2565 ffs->interfaces_count = helper.interfaces_count;
2567 if (ffs->eps_count != helper.eps_count) {
2571 if (ffs->interfaces_count != helper.interfaces_count) {
2579 if (os_descs_count) {
2580 ret = ffs_do_os_descs(os_descs_count, data, len,
2581 __ffs_data_do_os_desc, ffs);
2588 if (raw_descs == data || len) {
2593 ffs->raw_descs_data = _data;
2594 ffs->raw_descs = raw_descs;
2595 ffs->raw_descs_length = data - raw_descs;
2596 ffs->fs_descs_count = counts[0];
2597 ffs->hs_descs_count = counts[1];
2598 ffs->ss_descs_count = counts[2];
2599 ffs->ms_os_descs_count = os_descs_count;
2608 static int __ffs_data_got_strings(struct ffs_data *ffs,
2609 char *const _data, size_t len)
2611 u32 str_count, needed_count, lang_count;
2612 struct usb_gadget_strings **stringtabs, *t;
2613 const char *data = _data;
2614 struct usb_string *s;
2619 get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2620 get_unaligned_le32(data + 4) != len)
2622 str_count = get_unaligned_le32(data + 8);
2623 lang_count = get_unaligned_le32(data + 12);
2625 /* if one is zero the other must be zero */
2626 if (!str_count != !lang_count)
2629 /* Do we have at least as many strings as descriptors need? */
2630 needed_count = ffs->strings_count;
2631 if (str_count < needed_count)
2635 * If we don't need any strings just return and free all
2638 if (!needed_count) {
2643 /* Allocate everything in one chunk so there's less maintenance. */
2647 vla_item(d, struct usb_gadget_strings *, stringtabs,
2649 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2650 vla_item(d, struct usb_string, strings,
2651 lang_count*(needed_count+1));
2653 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2660 /* Initialize the VLA pointers */
2661 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2662 t = vla_ptr(vlabuf, d, stringtab);
2665 *stringtabs++ = t++;
2669 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2670 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2671 t = vla_ptr(vlabuf, d, stringtab);
2672 s = vla_ptr(vlabuf, d, strings);
2675 /* For each language */
2679 do { /* lang_count > 0 so we can use do-while */
2680 unsigned needed = needed_count;
2681 u32 str_per_lang = str_count;
2685 t->language = get_unaligned_le16(data);
2692 /* For each string */
2693 do { /* str_count > 0 so we can use do-while */
2694 size_t length = strnlen(data, len);
2700 * User may provide more strings then we need,
2701 * if that's the case we simply ignore the
2706 * s->id will be set while adding
2707 * function to configuration so for
2708 * now just leave garbage here.
2717 } while (--str_per_lang);
2719 s->id = 0; /* terminator */
2723 } while (--lang_count);
2725 /* Some garbage left? */
2730 ffs->stringtabs = stringtabs;
2731 ffs->raw_strings = _data;
2743 /* Events handling and management *******************************************/
2745 static void __ffs_event_add(struct ffs_data *ffs,
2746 enum usb_functionfs_event_type type)
2748 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2752 * Abort any unhandled setup
2754 * We do not need to worry about some cmpxchg() changing value
2755 * of ffs->setup_state without holding the lock because when
2756 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2757 * the source does nothing.
2759 if (ffs->setup_state == FFS_SETUP_PENDING)
2760 ffs->setup_state = FFS_SETUP_CANCELLED;
2763 * Logic of this function guarantees that there are at most four pending
2764 * evens on ffs->ev.types queue. This is important because the queue
2765 * has space for four elements only and __ffs_ep0_read_events function
2766 * depends on that limit as well. If more event types are added, those
2767 * limits have to be revisited or guaranteed to still hold.
2770 case FUNCTIONFS_RESUME:
2771 rem_type2 = FUNCTIONFS_SUSPEND;
2773 case FUNCTIONFS_SUSPEND:
2774 case FUNCTIONFS_SETUP:
2776 /* Discard all similar events */
2779 case FUNCTIONFS_BIND:
2780 case FUNCTIONFS_UNBIND:
2781 case FUNCTIONFS_DISABLE:
2782 case FUNCTIONFS_ENABLE:
2783 /* Discard everything other then power management. */
2784 rem_type1 = FUNCTIONFS_SUSPEND;
2785 rem_type2 = FUNCTIONFS_RESUME;
2790 WARN(1, "%d: unknown event, this should not happen\n", type);
2795 u8 *ev = ffs->ev.types, *out = ev;
2796 unsigned n = ffs->ev.count;
2797 for (; n; --n, ++ev)
2798 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2801 pr_vdebug("purging event %d\n", *ev);
2802 ffs->ev.count = out - ffs->ev.types;
2805 pr_vdebug("adding event %d\n", type);
2806 ffs->ev.types[ffs->ev.count++] = type;
2807 wake_up_locked(&ffs->ev.waitq);
2808 if (ffs->ffs_eventfd)
2809 eventfd_signal(ffs->ffs_eventfd, 1);
2812 static void ffs_event_add(struct ffs_data *ffs,
2813 enum usb_functionfs_event_type type)
2815 unsigned long flags;
2816 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2817 __ffs_event_add(ffs, type);
2818 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2821 /* Bind/unbind USB function hooks *******************************************/
2823 static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2827 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2828 if (ffs->eps_addrmap[i] == endpoint_address)
2833 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2834 struct usb_descriptor_header *desc,
2837 struct usb_endpoint_descriptor *ds = (void *)desc;
2838 struct ffs_function *func = priv;
2839 struct ffs_ep *ffs_ep;
2840 unsigned ep_desc_id;
2842 static const char *speed_names[] = { "full", "high", "super" };
2844 if (type != FFS_DESCRIPTOR)
2848 * If ss_descriptors is not NULL, we are reading super speed
2849 * descriptors; if hs_descriptors is not NULL, we are reading high
2850 * speed descriptors; otherwise, we are reading full speed
2853 if (func->function.ss_descriptors) {
2855 func->function.ss_descriptors[(long)valuep] = desc;
2856 } else if (func->function.hs_descriptors) {
2858 func->function.hs_descriptors[(long)valuep] = desc;
2861 func->function.fs_descriptors[(long)valuep] = desc;
2864 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2867 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2871 ffs_ep = func->eps + idx;
2873 if (ffs_ep->descs[ep_desc_id]) {
2874 pr_err("two %sspeed descriptors for EP %d\n",
2875 speed_names[ep_desc_id],
2876 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2879 ffs_ep->descs[ep_desc_id] = ds;
2881 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2883 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2884 if (!ds->wMaxPacketSize)
2885 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2887 struct usb_request *req;
2889 u8 bEndpointAddress;
2893 * We back up bEndpointAddress because autoconfig overwrites
2894 * it with physical endpoint address.
2896 bEndpointAddress = ds->bEndpointAddress;
2898 * We back up wMaxPacketSize because autoconfig treats
2899 * endpoint descriptors as if they were full speed.
2901 wMaxPacketSize = ds->wMaxPacketSize;
2902 pr_vdebug("autoconfig\n");
2903 ep = usb_ep_autoconfig(func->gadget, ds);
2906 ep->driver_data = func->eps + idx;
2908 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2914 func->eps_revmap[ds->bEndpointAddress &
2915 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2917 * If we use virtual address mapping, we restore
2918 * original bEndpointAddress value.
2920 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
2921 ds->bEndpointAddress = bEndpointAddress;
2923 * Restore wMaxPacketSize which was potentially
2924 * overwritten by autoconfig.
2926 ds->wMaxPacketSize = wMaxPacketSize;
2928 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2933 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2934 struct usb_descriptor_header *desc,
2937 struct ffs_function *func = priv;
2943 case FFS_DESCRIPTOR:
2944 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2949 if (func->interfaces_nums[idx] < 0) {
2950 int id = usb_interface_id(func->conf, &func->function);
2953 func->interfaces_nums[idx] = id;
2955 newValue = func->interfaces_nums[idx];
2959 /* String' IDs are allocated when fsf_data is bound to cdev */
2960 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2965 * USB_DT_ENDPOINT are handled in
2966 * __ffs_func_bind_do_descs().
2968 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2971 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2972 if (!func->eps[idx].ep)
2976 struct usb_endpoint_descriptor **descs;
2977 descs = func->eps[idx].descs;
2978 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2983 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2988 static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2989 struct usb_os_desc_header *h, void *data,
2990 unsigned len, void *priv)
2992 struct ffs_function *func = priv;
2996 case FFS_OS_DESC_EXT_COMPAT: {
2997 struct usb_ext_compat_desc *desc = data;
2998 struct usb_os_desc_table *t;
3000 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
3001 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
3002 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
3003 ARRAY_SIZE(desc->CompatibleID) +
3004 ARRAY_SIZE(desc->SubCompatibleID));
3005 length = sizeof(*desc);
3008 case FFS_OS_DESC_EXT_PROP: {
3009 struct usb_ext_prop_desc *desc = data;
3010 struct usb_os_desc_table *t;
3011 struct usb_os_desc_ext_prop *ext_prop;
3012 char *ext_prop_name;
3013 char *ext_prop_data;
3015 t = &func->function.os_desc_table[h->interface];
3016 t->if_id = func->interfaces_nums[h->interface];
3018 ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
3019 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
3021 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
3022 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
3023 ext_prop->data_len = le32_to_cpu(*(__le32 *)
3024 usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
3025 length = ext_prop->name_len + ext_prop->data_len + 14;
3027 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
3028 func->ffs->ms_os_descs_ext_prop_name_avail +=
3031 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
3032 func->ffs->ms_os_descs_ext_prop_data_avail +=
3034 memcpy(ext_prop_data,
3035 usb_ext_prop_data_ptr(data, ext_prop->name_len),
3036 ext_prop->data_len);
3037 /* unicode data reported to the host as "WCHAR"s */
3038 switch (ext_prop->type) {
3039 case USB_EXT_PROP_UNICODE:
3040 case USB_EXT_PROP_UNICODE_ENV:
3041 case USB_EXT_PROP_UNICODE_LINK:
3042 case USB_EXT_PROP_UNICODE_MULTI:
3043 ext_prop->data_len *= 2;
3046 ext_prop->data = ext_prop_data;
3048 memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
3049 ext_prop->name_len);
3050 /* property name reported to the host as "WCHAR"s */
3051 ext_prop->name_len *= 2;
3052 ext_prop->name = ext_prop_name;
3054 t->os_desc->ext_prop_len +=
3055 ext_prop->name_len + ext_prop->data_len + 14;
3056 ++t->os_desc->ext_prop_count;
3057 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
3061 pr_vdebug("unknown descriptor: %d\n", type);
3067 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
3068 struct usb_configuration *c)
3070 struct ffs_function *func = ffs_func_from_usb(f);
3071 struct f_fs_opts *ffs_opts =
3072 container_of(f->fi, struct f_fs_opts, func_inst);
3073 struct ffs_data *ffs_data;
3079 * Legacy gadget triggers binding in functionfs_ready_callback,
3080 * which already uses locking; taking the same lock here would
3083 * Configfs-enabled gadgets however do need ffs_dev_lock.
3085 if (!ffs_opts->no_configfs)
3087 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
3088 ffs_data = ffs_opts->dev->ffs_data;
3089 if (!ffs_opts->no_configfs)
3092 return ERR_PTR(ret);
3094 func->ffs = ffs_data;
3096 func->gadget = c->cdev->gadget;
3099 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
3100 * configurations are bound in sequence with list_for_each_entry,
3101 * in each configuration its functions are bound in sequence
3102 * with list_for_each_entry, so we assume no race condition
3103 * with regard to ffs_opts->bound access
3105 if (!ffs_opts->refcnt) {
3106 ret = functionfs_bind(func->ffs, c->cdev);
3108 return ERR_PTR(ret);
3111 func->function.strings = func->ffs->stringtabs;
3116 static int _ffs_func_bind(struct usb_configuration *c,
3117 struct usb_function *f)
3119 struct ffs_function *func = ffs_func_from_usb(f);
3120 struct ffs_data *ffs = func->ffs;
3122 const int full = !!func->ffs->fs_descs_count;
3123 const int high = !!func->ffs->hs_descs_count;
3124 const int super = !!func->ffs->ss_descs_count;
3126 int fs_len, hs_len, ss_len, ret, i;
3127 struct ffs_ep *eps_ptr;
3129 /* Make it a single chunk, less management later on */
3131 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
3132 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
3133 full ? ffs->fs_descs_count + 1 : 0);
3134 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
3135 high ? ffs->hs_descs_count + 1 : 0);
3136 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
3137 super ? ffs->ss_descs_count + 1 : 0);
3138 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
3139 vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
3140 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3141 vla_item_with_sz(d, char[16], ext_compat,
3142 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3143 vla_item_with_sz(d, struct usb_os_desc, os_desc,
3144 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3145 vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
3146 ffs->ms_os_descs_ext_prop_count);
3147 vla_item_with_sz(d, char, ext_prop_name,
3148 ffs->ms_os_descs_ext_prop_name_len);
3149 vla_item_with_sz(d, char, ext_prop_data,
3150 ffs->ms_os_descs_ext_prop_data_len);
3151 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
3156 /* Has descriptors only for speeds gadget does not support */
3157 if (!(full | high | super))
3160 /* Allocate a single chunk, less management later on */
3161 vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
3165 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
3166 ffs->ms_os_descs_ext_prop_name_avail =
3167 vla_ptr(vlabuf, d, ext_prop_name);
3168 ffs->ms_os_descs_ext_prop_data_avail =
3169 vla_ptr(vlabuf, d, ext_prop_data);
3171 /* Copy descriptors */
3172 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
3173 ffs->raw_descs_length);
3175 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
3176 eps_ptr = vla_ptr(vlabuf, d, eps);
3177 for (i = 0; i < ffs->eps_count; i++)
3178 eps_ptr[i].num = -1;
3181 * d_eps == vlabuf, func->eps used to kfree vlabuf later
3183 func->eps = vla_ptr(vlabuf, d, eps);
3184 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
3187 * Go through all the endpoint descriptors and allocate
3188 * endpoints first, so that later we can rewrite the endpoint
3189 * numbers without worrying that it may be described later on.
3192 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
3193 fs_len = ffs_do_descs(ffs->fs_descs_count,
3194 vla_ptr(vlabuf, d, raw_descs),
3196 __ffs_func_bind_do_descs, func);
3206 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
3207 hs_len = ffs_do_descs(ffs->hs_descs_count,
3208 vla_ptr(vlabuf, d, raw_descs) + fs_len,
3209 d_raw_descs__sz - fs_len,
3210 __ffs_func_bind_do_descs, func);
3220 func->function.ss_descriptors = func->function.ssp_descriptors =
3221 vla_ptr(vlabuf, d, ss_descs);
3222 ss_len = ffs_do_descs(ffs->ss_descs_count,
3223 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
3224 d_raw_descs__sz - fs_len - hs_len,
3225 __ffs_func_bind_do_descs, func);
3235 * Now handle interface numbers allocation and interface and
3236 * endpoint numbers rewriting. We can do that in one go
3239 ret = ffs_do_descs(ffs->fs_descs_count +
3240 (high ? ffs->hs_descs_count : 0) +
3241 (super ? ffs->ss_descs_count : 0),
3242 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
3243 __ffs_func_bind_do_nums, func);
3247 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
3248 if (c->cdev->use_os_string) {
3249 for (i = 0; i < ffs->interfaces_count; ++i) {
3250 struct usb_os_desc *desc;
3252 desc = func->function.os_desc_table[i].os_desc =
3253 vla_ptr(vlabuf, d, os_desc) +
3254 i * sizeof(struct usb_os_desc);
3255 desc->ext_compat_id =
3256 vla_ptr(vlabuf, d, ext_compat) + i * 16;
3257 INIT_LIST_HEAD(&desc->ext_prop);
3259 ret = ffs_do_os_descs(ffs->ms_os_descs_count,
3260 vla_ptr(vlabuf, d, raw_descs) +
3261 fs_len + hs_len + ss_len,
3262 d_raw_descs__sz - fs_len - hs_len -
3264 __ffs_func_bind_do_os_desc, func);
3268 func->function.os_desc_n =
3269 c->cdev->use_os_string ? ffs->interfaces_count : 0;
3271 /* And we're done */
3272 ffs_event_add(ffs, FUNCTIONFS_BIND);
3276 /* XXX Do we need to release all claimed endpoints here? */
3280 static int ffs_func_bind(struct usb_configuration *c,
3281 struct usb_function *f)
3283 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
3284 struct ffs_function *func = ffs_func_from_usb(f);
3287 if (IS_ERR(ffs_opts))
3288 return PTR_ERR(ffs_opts);
3290 ret = _ffs_func_bind(c, f);
3291 if (ret && !--ffs_opts->refcnt)
3292 functionfs_unbind(func->ffs);
3298 /* Other USB function hooks *************************************************/
3300 static void ffs_reset_work(struct work_struct *work)
3302 struct ffs_data *ffs = container_of(work,
3303 struct ffs_data, reset_work);
3304 ffs_data_reset(ffs);
3307 static int ffs_func_set_alt(struct usb_function *f,
3308 unsigned interface, unsigned alt)
3310 struct ffs_function *func = ffs_func_from_usb(f);
3311 struct ffs_data *ffs = func->ffs;
3314 if (alt != (unsigned)-1) {
3315 intf = ffs_func_revmap_intf(func, interface);
3321 ffs_func_eps_disable(ffs->func);
3323 if (ffs->state == FFS_DEACTIVATED) {
3324 ffs->state = FFS_CLOSING;
3325 INIT_WORK(&ffs->reset_work, ffs_reset_work);
3326 schedule_work(&ffs->reset_work);
3330 if (ffs->state != FFS_ACTIVE)
3333 if (alt == (unsigned)-1) {
3335 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
3340 ret = ffs_func_eps_enable(func);
3342 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
3346 static void ffs_func_disable(struct usb_function *f)
3348 ffs_func_set_alt(f, 0, (unsigned)-1);
3351 static int ffs_func_setup(struct usb_function *f,
3352 const struct usb_ctrlrequest *creq)
3354 struct ffs_function *func = ffs_func_from_usb(f);
3355 struct ffs_data *ffs = func->ffs;
3356 unsigned long flags;
3361 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
3362 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
3363 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
3364 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
3365 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
3368 * Most requests directed to interface go through here
3369 * (notable exceptions are set/get interface) so we need to
3370 * handle them. All other either handled by composite or
3371 * passed to usb_configuration->setup() (if one is set). No
3372 * matter, we will handle requests directed to endpoint here
3373 * as well (as it's straightforward). Other request recipient
3374 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
3377 if (ffs->state != FFS_ACTIVE)
3380 switch (creq->bRequestType & USB_RECIP_MASK) {
3381 case USB_RECIP_INTERFACE:
3382 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
3387 case USB_RECIP_ENDPOINT:
3388 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
3391 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
3392 ret = func->ffs->eps_addrmap[ret];
3396 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
3397 ret = le16_to_cpu(creq->wIndex);
3402 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
3403 ffs->ev.setup = *creq;
3404 ffs->ev.setup.wIndex = cpu_to_le16(ret);
3405 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
3406 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
3408 return creq->wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0;
3411 static bool ffs_func_req_match(struct usb_function *f,
3412 const struct usb_ctrlrequest *creq,
3415 struct ffs_function *func = ffs_func_from_usb(f);
3417 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
3420 switch (creq->bRequestType & USB_RECIP_MASK) {
3421 case USB_RECIP_INTERFACE:
3422 return (ffs_func_revmap_intf(func,
3423 le16_to_cpu(creq->wIndex)) >= 0);
3424 case USB_RECIP_ENDPOINT:
3425 return (ffs_func_revmap_ep(func,
3426 le16_to_cpu(creq->wIndex)) >= 0);
3428 return (bool) (func->ffs->user_flags &
3429 FUNCTIONFS_ALL_CTRL_RECIP);
3433 static void ffs_func_suspend(struct usb_function *f)
3436 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
3439 static void ffs_func_resume(struct usb_function *f)
3442 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
3446 /* Endpoint and interface numbers reverse mapping ***************************/
3448 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
3450 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
3451 return num ? num : -EDOM;
3454 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
3456 short *nums = func->interfaces_nums;
3457 unsigned count = func->ffs->interfaces_count;
3459 for (; count; --count, ++nums) {
3460 if (*nums >= 0 && *nums == intf)
3461 return nums - func->interfaces_nums;
3468 /* Devices management *******************************************************/
3470 static LIST_HEAD(ffs_devices);
3472 static struct ffs_dev *_ffs_do_find_dev(const char *name)
3474 struct ffs_dev *dev;
3479 list_for_each_entry(dev, &ffs_devices, entry) {
3480 if (strcmp(dev->name, name) == 0)
3488 * ffs_lock must be taken by the caller of this function
3490 static struct ffs_dev *_ffs_get_single_dev(void)
3492 struct ffs_dev *dev;
3494 if (list_is_singular(&ffs_devices)) {
3495 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
3504 * ffs_lock must be taken by the caller of this function
3506 static struct ffs_dev *_ffs_find_dev(const char *name)
3508 struct ffs_dev *dev;
3510 dev = _ffs_get_single_dev();
3514 return _ffs_do_find_dev(name);
3517 /* Configfs support *********************************************************/
3519 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3521 return container_of(to_config_group(item), struct f_fs_opts,
3525 static void ffs_attr_release(struct config_item *item)
3527 struct f_fs_opts *opts = to_ffs_opts(item);
3529 usb_put_function_instance(&opts->func_inst);
3532 static struct configfs_item_operations ffs_item_ops = {
3533 .release = ffs_attr_release,
3536 static const struct config_item_type ffs_func_type = {
3537 .ct_item_ops = &ffs_item_ops,
3538 .ct_owner = THIS_MODULE,
3542 /* Function registration interface ******************************************/
3544 static void ffs_free_inst(struct usb_function_instance *f)
3546 struct f_fs_opts *opts;
3548 opts = to_f_fs_opts(f);
3549 ffs_release_dev(opts->dev);
3551 _ffs_free_dev(opts->dev);
3556 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3558 if (strlen(name) >= sizeof_field(struct ffs_dev, name))
3559 return -ENAMETOOLONG;
3560 return ffs_name_dev(to_f_fs_opts(fi)->dev, name);
3563 static struct usb_function_instance *ffs_alloc_inst(void)
3565 struct f_fs_opts *opts;
3566 struct ffs_dev *dev;
3568 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3570 return ERR_PTR(-ENOMEM);
3572 opts->func_inst.set_inst_name = ffs_set_inst_name;
3573 opts->func_inst.free_func_inst = ffs_free_inst;
3575 dev = _ffs_alloc_dev();
3579 return ERR_CAST(dev);
3584 config_group_init_type_name(&opts->func_inst.group, "",
3586 return &opts->func_inst;
3589 static void ffs_free(struct usb_function *f)
3591 kfree(ffs_func_from_usb(f));
3594 static void ffs_func_unbind(struct usb_configuration *c,
3595 struct usb_function *f)
3597 struct ffs_function *func = ffs_func_from_usb(f);
3598 struct ffs_data *ffs = func->ffs;
3599 struct f_fs_opts *opts =
3600 container_of(f->fi, struct f_fs_opts, func_inst);
3601 struct ffs_ep *ep = func->eps;
3602 unsigned count = ffs->eps_count;
3603 unsigned long flags;
3606 if (ffs->func == func) {
3607 ffs_func_eps_disable(func);
3611 /* Drain any pending AIO completions */
3612 drain_workqueue(ffs->io_completion_wq);
3614 if (!--opts->refcnt)
3615 functionfs_unbind(ffs);
3617 /* cleanup after autoconfig */
3618 spin_lock_irqsave(&func->ffs->eps_lock, flags);
3620 if (ep->ep && ep->req)
3621 usb_ep_free_request(ep->ep, ep->req);
3625 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3629 * eps, descriptors and interfaces_nums are allocated in the
3630 * same chunk so only one free is required.
3632 func->function.fs_descriptors = NULL;
3633 func->function.hs_descriptors = NULL;
3634 func->function.ss_descriptors = NULL;
3635 func->function.ssp_descriptors = NULL;
3636 func->interfaces_nums = NULL;
3638 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3641 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3643 struct ffs_function *func;
3647 func = kzalloc(sizeof(*func), GFP_KERNEL);
3649 return ERR_PTR(-ENOMEM);
3651 func->function.name = "Function FS Gadget";
3653 func->function.bind = ffs_func_bind;
3654 func->function.unbind = ffs_func_unbind;
3655 func->function.set_alt = ffs_func_set_alt;
3656 func->function.disable = ffs_func_disable;
3657 func->function.setup = ffs_func_setup;
3658 func->function.req_match = ffs_func_req_match;
3659 func->function.suspend = ffs_func_suspend;
3660 func->function.resume = ffs_func_resume;
3661 func->function.free_func = ffs_free;
3663 return &func->function;
3667 * ffs_lock must be taken by the caller of this function
3669 static struct ffs_dev *_ffs_alloc_dev(void)
3671 struct ffs_dev *dev;
3674 if (_ffs_get_single_dev())
3675 return ERR_PTR(-EBUSY);
3677 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3679 return ERR_PTR(-ENOMEM);
3681 if (list_empty(&ffs_devices)) {
3682 ret = functionfs_init();
3685 return ERR_PTR(ret);
3689 list_add(&dev->entry, &ffs_devices);
3694 int ffs_name_dev(struct ffs_dev *dev, const char *name)
3696 struct ffs_dev *existing;
3701 existing = _ffs_do_find_dev(name);
3703 strlcpy(dev->name, name, ARRAY_SIZE(dev->name));
3704 else if (existing != dev)
3711 EXPORT_SYMBOL_GPL(ffs_name_dev);
3713 int ffs_single_dev(struct ffs_dev *dev)
3720 if (!list_is_singular(&ffs_devices))
3728 EXPORT_SYMBOL_GPL(ffs_single_dev);
3731 * ffs_lock must be taken by the caller of this function
3733 static void _ffs_free_dev(struct ffs_dev *dev)
3735 list_del(&dev->entry);
3738 if (list_empty(&ffs_devices))
3739 functionfs_cleanup();
3742 static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data)
3745 struct ffs_dev *ffs_dev;
3750 ffs_dev = _ffs_find_dev(dev_name);
3753 } else if (ffs_dev->mounted) {
3755 } else if (ffs_dev->ffs_acquire_dev_callback &&
3756 ffs_dev->ffs_acquire_dev_callback(ffs_dev)) {
3759 ffs_dev->mounted = true;
3760 ffs_dev->ffs_data = ffs_data;
3761 ffs_data->private_data = ffs_dev;
3768 static void ffs_release_dev(struct ffs_dev *ffs_dev)
3773 if (ffs_dev && ffs_dev->mounted) {
3774 ffs_dev->mounted = false;
3775 if (ffs_dev->ffs_data) {
3776 ffs_dev->ffs_data->private_data = NULL;
3777 ffs_dev->ffs_data = NULL;
3780 if (ffs_dev->ffs_release_dev_callback)
3781 ffs_dev->ffs_release_dev_callback(ffs_dev);
3787 static int ffs_ready(struct ffs_data *ffs)
3789 struct ffs_dev *ffs_obj;
3795 ffs_obj = ffs->private_data;
3800 if (WARN_ON(ffs_obj->desc_ready)) {
3805 ffs_obj->desc_ready = true;
3807 if (ffs_obj->ffs_ready_callback) {
3808 ret = ffs_obj->ffs_ready_callback(ffs);
3813 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
3819 static void ffs_closed(struct ffs_data *ffs)
3821 struct ffs_dev *ffs_obj;
3822 struct f_fs_opts *opts;
3823 struct config_item *ci;
3828 ffs_obj = ffs->private_data;
3832 ffs_obj->desc_ready = false;
3834 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
3835 ffs_obj->ffs_closed_callback)
3836 ffs_obj->ffs_closed_callback(ffs);
3839 opts = ffs_obj->opts;
3843 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3844 || !kref_read(&opts->func_inst.group.cg_item.ci_kref))
3847 ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
3850 if (test_bit(FFS_FL_BOUND, &ffs->flags))
3851 unregister_gadget_item(ci);
3857 /* Misc helper functions ****************************************************/
3859 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3862 ? mutex_trylock(mutex) ? 0 : -EAGAIN
3863 : mutex_lock_interruptible(mutex);
3866 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
3873 data = memdup_user(buf, len);
3877 pr_vdebug("Buffer from user space:\n");
3878 ffs_dump_mem("", data, len);
3883 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3884 MODULE_LICENSE("GPL");
3885 MODULE_AUTHOR("Michal Nazarewicz");