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 <asm/unaligned.h>
28 #include <linux/usb/composite.h>
29 #include <linux/usb/functionfs.h>
31 #include <linux/aio.h>
32 #include <linux/mmu_context.h>
33 #include <linux/poll.h>
38 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
40 /* Variable Length Array Macros **********************************************/
41 #define vla_group(groupname) size_t groupname##__next = 0
42 #define vla_group_size(groupname) groupname##__next
44 #define vla_item(groupname, type, name, n) \
45 size_t groupname##_##name##__offset = ({ \
46 size_t align_mask = __alignof__(type) - 1; \
47 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
48 size_t size = (n) * sizeof(type); \
49 groupname##__next = offset + size; \
53 #define vla_item_with_sz(groupname, type, name, n) \
54 size_t groupname##_##name##__sz = (n) * sizeof(type); \
55 size_t groupname##_##name##__offset = ({ \
56 size_t align_mask = __alignof__(type) - 1; \
57 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
58 size_t size = groupname##_##name##__sz; \
59 groupname##__next = offset + size; \
63 #define vla_ptr(ptr, groupname, name) \
64 ((void *) ((char *)ptr + groupname##_##name##__offset))
66 /* Reference counter handling */
67 static void ffs_data_get(struct ffs_data *ffs);
68 static void ffs_data_put(struct ffs_data *ffs);
69 /* Creates new ffs_data object. */
70 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
72 /* Opened counter handling. */
73 static void ffs_data_opened(struct ffs_data *ffs);
74 static void ffs_data_closed(struct ffs_data *ffs);
76 /* Called with ffs->mutex held; take over ownership of data. */
77 static int __must_check
78 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
79 static int __must_check
80 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
83 /* The function structure ***************************************************/
88 struct usb_configuration *conf;
89 struct usb_gadget *gadget;
94 short *interfaces_nums;
96 struct usb_function function;
100 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
102 return container_of(f, struct ffs_function, function);
106 static inline enum ffs_setup_state
107 ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
109 return (enum ffs_setup_state)
110 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
114 static void ffs_func_eps_disable(struct ffs_function *func);
115 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
117 static int ffs_func_bind(struct usb_configuration *,
118 struct usb_function *);
119 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
120 static void ffs_func_disable(struct usb_function *);
121 static int ffs_func_setup(struct usb_function *,
122 const struct usb_ctrlrequest *);
123 static void ffs_func_suspend(struct usb_function *);
124 static void ffs_func_resume(struct usb_function *);
127 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
128 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
131 /* The endpoints structures *************************************************/
134 struct usb_ep *ep; /* P: ffs->eps_lock */
135 struct usb_request *req; /* P: epfile->mutex */
137 /* [0]: full speed, [1]: high speed, [2]: super speed */
138 struct usb_endpoint_descriptor *descs[3];
142 int status; /* P: epfile->mutex */
146 /* Protects ep->ep and ep->req. */
148 wait_queue_head_t wait;
150 struct ffs_data *ffs;
151 struct ffs_ep *ep; /* P: ffs->eps_lock */
153 struct dentry *dentry;
157 unsigned char in; /* P: ffs->eps_lock */
158 unsigned char isoc; /* P: ffs->eps_lock */
163 /* ffs_io_data structure ***************************************************/
170 const struct iovec *iovec;
171 unsigned long nr_segs;
175 struct mm_struct *mm;
176 struct work_struct work;
179 struct usb_request *req;
182 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
183 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
185 static struct inode *__must_check
186 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
187 const struct file_operations *fops,
188 struct dentry **dentry_p);
190 /* Devices management *******************************************************/
192 DEFINE_MUTEX(ffs_lock);
193 EXPORT_SYMBOL(ffs_lock);
195 static struct ffs_dev *_ffs_find_dev(const char *name);
196 static struct ffs_dev *_ffs_alloc_dev(void);
197 static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
198 static void _ffs_free_dev(struct ffs_dev *dev);
199 static void *ffs_acquire_dev(const char *dev_name);
200 static void ffs_release_dev(struct ffs_data *ffs_data);
201 static int ffs_ready(struct ffs_data *ffs);
202 static void ffs_closed(struct ffs_data *ffs);
204 /* Misc helper functions ****************************************************/
206 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
207 __attribute__((warn_unused_result, nonnull));
208 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
209 __attribute__((warn_unused_result, nonnull));
212 /* Control file aka ep0 *****************************************************/
214 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
216 struct ffs_data *ffs = req->context;
218 complete_all(&ffs->ep0req_completion);
221 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
223 struct usb_request *req = ffs->ep0req;
226 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
228 spin_unlock_irq(&ffs->ev.waitq.lock);
234 * UDC layer requires to provide a buffer even for ZLP, but should
235 * not use it at all. Let's provide some poisoned pointer to catch
236 * possible bug in the driver.
238 if (req->buf == NULL)
239 req->buf = (void *)0xDEADBABE;
241 reinit_completion(&ffs->ep0req_completion);
243 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
244 if (unlikely(ret < 0))
247 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
249 usb_ep_dequeue(ffs->gadget->ep0, req);
253 ffs->setup_state = FFS_NO_SETUP;
254 return req->status ? req->status : req->actual;
257 static int __ffs_ep0_stall(struct ffs_data *ffs)
259 if (ffs->ev.can_stall) {
260 pr_vdebug("ep0 stall\n");
261 usb_ep_set_halt(ffs->gadget->ep0);
262 ffs->setup_state = FFS_NO_SETUP;
265 pr_debug("bogus ep0 stall!\n");
270 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
271 size_t len, loff_t *ptr)
273 struct ffs_data *ffs = file->private_data;
279 /* Fast check if setup was canceled */
280 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
284 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
285 if (unlikely(ret < 0))
289 switch (ffs->state) {
290 case FFS_READ_DESCRIPTORS:
291 case FFS_READ_STRINGS:
293 if (unlikely(len < 16)) {
298 data = ffs_prepare_buffer(buf, len);
305 if (ffs->state == FFS_READ_DESCRIPTORS) {
306 pr_info("read descriptors\n");
307 ret = __ffs_data_got_descs(ffs, data, len);
308 if (unlikely(ret < 0))
311 ffs->state = FFS_READ_STRINGS;
314 pr_info("read strings\n");
315 ret = __ffs_data_got_strings(ffs, data, len);
316 if (unlikely(ret < 0))
319 ret = ffs_epfiles_create(ffs);
321 ffs->state = FFS_CLOSING;
325 ffs->state = FFS_ACTIVE;
326 mutex_unlock(&ffs->mutex);
328 ret = ffs_ready(ffs);
329 if (unlikely(ret < 0)) {
330 ffs->state = FFS_CLOSING;
334 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
342 * We're called from user space, we can use _irq
343 * rather then _irqsave
345 spin_lock_irq(&ffs->ev.waitq.lock);
346 switch (ffs_setup_state_clear_cancelled(ffs)) {
347 case FFS_SETUP_CANCELLED:
355 case FFS_SETUP_PENDING:
359 /* FFS_SETUP_PENDING */
360 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
361 spin_unlock_irq(&ffs->ev.waitq.lock);
362 ret = __ffs_ep0_stall(ffs);
366 /* FFS_SETUP_PENDING and not stall */
367 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
369 spin_unlock_irq(&ffs->ev.waitq.lock);
371 data = ffs_prepare_buffer(buf, len);
377 spin_lock_irq(&ffs->ev.waitq.lock);
380 * We are guaranteed to be still in FFS_ACTIVE state
381 * but the state of setup could have changed from
382 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
383 * to check for that. If that happened we copied data
384 * from user space in vain but it's unlikely.
386 * For sure we are not in FFS_NO_SETUP since this is
387 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
388 * transition can be performed and it's protected by
391 if (ffs_setup_state_clear_cancelled(ffs) ==
392 FFS_SETUP_CANCELLED) {
395 spin_unlock_irq(&ffs->ev.waitq.lock);
397 /* unlocks spinlock */
398 ret = __ffs_ep0_queue_wait(ffs, data, len);
408 mutex_unlock(&ffs->mutex);
412 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
416 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
419 struct usb_functionfs_event events[n];
422 memset(events, 0, sizeof events);
425 events[i].type = ffs->ev.types[i];
426 if (events[i].type == FUNCTIONFS_SETUP) {
427 events[i].u.setup = ffs->ev.setup;
428 ffs->setup_state = FFS_SETUP_PENDING;
432 if (n < ffs->ev.count) {
434 memmove(ffs->ev.types, ffs->ev.types + n,
435 ffs->ev.count * sizeof *ffs->ev.types);
440 spin_unlock_irq(&ffs->ev.waitq.lock);
441 mutex_unlock(&ffs->mutex);
443 return unlikely(__copy_to_user(buf, events, sizeof events))
444 ? -EFAULT : sizeof events;
447 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
448 size_t len, loff_t *ptr)
450 struct ffs_data *ffs = file->private_data;
457 /* Fast check if setup was canceled */
458 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
462 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
463 if (unlikely(ret < 0))
467 if (ffs->state != FFS_ACTIVE) {
473 * We're called from user space, we can use _irq rather then
476 spin_lock_irq(&ffs->ev.waitq.lock);
478 switch (ffs_setup_state_clear_cancelled(ffs)) {
479 case FFS_SETUP_CANCELLED:
484 n = len / sizeof(struct usb_functionfs_event);
490 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
495 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
501 return __ffs_ep0_read_events(ffs, buf,
502 min(n, (size_t)ffs->ev.count));
504 case FFS_SETUP_PENDING:
505 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
506 spin_unlock_irq(&ffs->ev.waitq.lock);
507 ret = __ffs_ep0_stall(ffs);
511 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
513 spin_unlock_irq(&ffs->ev.waitq.lock);
516 data = kmalloc(len, GFP_KERNEL);
517 if (unlikely(!data)) {
523 spin_lock_irq(&ffs->ev.waitq.lock);
525 /* See ffs_ep0_write() */
526 if (ffs_setup_state_clear_cancelled(ffs) ==
527 FFS_SETUP_CANCELLED) {
532 /* unlocks spinlock */
533 ret = __ffs_ep0_queue_wait(ffs, data, len);
534 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
543 spin_unlock_irq(&ffs->ev.waitq.lock);
545 mutex_unlock(&ffs->mutex);
550 static int ffs_ep0_open(struct inode *inode, struct file *file)
552 struct ffs_data *ffs = inode->i_private;
556 if (unlikely(ffs->state == FFS_CLOSING))
559 file->private_data = ffs;
560 ffs_data_opened(ffs);
565 static int ffs_ep0_release(struct inode *inode, struct file *file)
567 struct ffs_data *ffs = file->private_data;
571 ffs_data_closed(ffs);
576 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
578 struct ffs_data *ffs = file->private_data;
579 struct usb_gadget *gadget = ffs->gadget;
584 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
585 struct ffs_function *func = ffs->func;
586 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
587 } else if (gadget && gadget->ops->ioctl) {
588 ret = gadget->ops->ioctl(gadget, code, value);
596 static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
598 struct ffs_data *ffs = file->private_data;
599 unsigned int mask = POLLWRNORM;
602 poll_wait(file, &ffs->ev.waitq, wait);
604 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
605 if (unlikely(ret < 0))
608 switch (ffs->state) {
609 case FFS_READ_DESCRIPTORS:
610 case FFS_READ_STRINGS:
615 switch (ffs->setup_state) {
621 case FFS_SETUP_PENDING:
622 case FFS_SETUP_CANCELLED:
623 mask |= (POLLIN | POLLOUT);
630 mutex_unlock(&ffs->mutex);
635 static const struct file_operations ffs_ep0_operations = {
638 .open = ffs_ep0_open,
639 .write = ffs_ep0_write,
640 .read = ffs_ep0_read,
641 .release = ffs_ep0_release,
642 .unlocked_ioctl = ffs_ep0_ioctl,
643 .poll = ffs_ep0_poll,
647 /* "Normal" endpoints operations ********************************************/
649 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
652 if (likely(req->context)) {
653 struct ffs_ep *ep = _ep->driver_data;
654 ep->status = req->status ? req->status : req->actual;
655 complete(req->context);
659 static void ffs_user_copy_worker(struct work_struct *work)
661 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
663 int ret = io_data->req->status ? io_data->req->status :
664 io_data->req->actual;
666 if (io_data->read && ret > 0) {
670 for (i = 0; i < io_data->nr_segs; i++) {
671 if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
673 io_data->iovec[i].iov_len))) {
677 pos += io_data->iovec[i].iov_len;
679 unuse_mm(io_data->mm);
682 aio_complete(io_data->kiocb, ret, ret);
684 usb_ep_free_request(io_data->ep, io_data->req);
686 io_data->kiocb->private = NULL;
688 kfree(io_data->iovec);
693 static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
694 struct usb_request *req)
696 struct ffs_io_data *io_data = req->context;
700 INIT_WORK(&io_data->work, ffs_user_copy_worker);
701 schedule_work(&io_data->work);
704 static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
706 struct ffs_epfile *epfile = file->private_data;
709 ssize_t ret, data_len;
712 /* Are we still active? */
713 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
718 /* Wait for endpoint to be enabled */
721 if (file->f_flags & O_NONBLOCK) {
726 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
734 halt = (!io_data->read == !epfile->in);
735 if (halt && epfile->isoc) {
740 /* Allocate & copy */
743 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
744 * before the waiting completes, so do not assign to 'gadget' earlier
746 struct usb_gadget *gadget = epfile->ffs->gadget;
748 spin_lock_irq(&epfile->ffs->eps_lock);
749 /* In the meantime, endpoint got disabled or changed. */
750 if (epfile->ep != ep) {
751 spin_unlock_irq(&epfile->ffs->eps_lock);
755 * Controller may require buffer size to be aligned to
756 * maxpacketsize of an out endpoint.
758 data_len = io_data->read ?
759 usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
761 spin_unlock_irq(&epfile->ffs->eps_lock);
763 data = kmalloc(data_len, GFP_KERNEL);
766 if (io_data->aio && !io_data->read) {
769 for (i = 0; i < io_data->nr_segs; i++) {
770 if (unlikely(copy_from_user(&data[pos],
771 io_data->iovec[i].iov_base,
772 io_data->iovec[i].iov_len))) {
776 pos += io_data->iovec[i].iov_len;
779 if (!io_data->read &&
780 unlikely(__copy_from_user(data, io_data->buf,
788 /* We will be using request */
789 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
793 spin_lock_irq(&epfile->ffs->eps_lock);
795 if (epfile->ep != ep) {
796 /* In the meantime, endpoint got disabled or changed. */
798 spin_unlock_irq(&epfile->ffs->eps_lock);
801 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
802 usb_ep_set_halt(ep->ep);
803 spin_unlock_irq(&epfile->ffs->eps_lock);
806 /* Fire the request */
807 struct usb_request *req;
810 req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
815 req->length = io_data->len;
818 io_data->ep = ep->ep;
821 req->context = io_data;
822 req->complete = ffs_epfile_async_io_complete;
824 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
826 usb_ep_free_request(ep->ep, req);
831 spin_unlock_irq(&epfile->ffs->eps_lock);
833 DECLARE_COMPLETION_ONSTACK(done);
837 req->length = io_data->len;
839 req->context = &done;
840 req->complete = ffs_epfile_io_complete;
842 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
844 spin_unlock_irq(&epfile->ffs->eps_lock);
846 if (unlikely(ret < 0)) {
849 wait_for_completion_interruptible(&done))) {
851 usb_ep_dequeue(ep->ep, req);
854 * XXX We may end up silently droping data
855 * here. Since data_len (i.e. req->length) may
856 * be bigger than len (after being rounded up
857 * to maxpacketsize), we may end up with more
858 * data then user space has space for.
861 if (io_data->read && ret > 0) {
862 ret = min_t(size_t, ret, io_data->len);
864 if (unlikely(copy_to_user(io_data->buf,
873 mutex_unlock(&epfile->mutex);
877 spin_unlock_irq(&epfile->ffs->eps_lock);
878 mutex_unlock(&epfile->mutex);
885 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
888 struct ffs_io_data io_data;
893 io_data.read = false;
894 io_data.buf = (char * __user)buf;
897 return ffs_epfile_io(file, &io_data);
901 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
903 struct ffs_io_data io_data;
912 return ffs_epfile_io(file, &io_data);
916 ffs_epfile_open(struct inode *inode, struct file *file)
918 struct ffs_epfile *epfile = inode->i_private;
922 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
925 file->private_data = epfile;
926 ffs_data_opened(epfile->ffs);
931 static int ffs_aio_cancel(struct kiocb *kiocb)
933 struct ffs_io_data *io_data = kiocb->private;
934 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
939 spin_lock_irq(&epfile->ffs->eps_lock);
941 if (likely(io_data && io_data->ep && io_data->req))
942 value = usb_ep_dequeue(io_data->ep, io_data->req);
946 spin_unlock_irq(&epfile->ffs->eps_lock);
951 static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
952 const struct iovec *iovec,
953 unsigned long nr_segs, loff_t loff)
955 struct ffs_io_data *io_data;
959 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
960 if (unlikely(!io_data))
964 io_data->read = false;
965 io_data->kiocb = kiocb;
966 io_data->iovec = iovec;
967 io_data->nr_segs = nr_segs;
968 io_data->len = kiocb->ki_nbytes;
969 io_data->mm = current->mm;
971 kiocb->private = io_data;
973 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
975 return ffs_epfile_io(kiocb->ki_filp, io_data);
978 static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
979 const struct iovec *iovec,
980 unsigned long nr_segs, loff_t loff)
982 struct ffs_io_data *io_data;
983 struct iovec *iovec_copy;
987 iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
988 if (unlikely(!iovec_copy))
991 memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
993 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
994 if (unlikely(!io_data)) {
1000 io_data->read = true;
1001 io_data->kiocb = kiocb;
1002 io_data->iovec = iovec_copy;
1003 io_data->nr_segs = nr_segs;
1004 io_data->len = kiocb->ki_nbytes;
1005 io_data->mm = current->mm;
1007 kiocb->private = io_data;
1009 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1011 return ffs_epfile_io(kiocb->ki_filp, io_data);
1015 ffs_epfile_release(struct inode *inode, struct file *file)
1017 struct ffs_epfile *epfile = inode->i_private;
1021 ffs_data_closed(epfile->ffs);
1026 static long ffs_epfile_ioctl(struct file *file, unsigned code,
1027 unsigned long value)
1029 struct ffs_epfile *epfile = file->private_data;
1034 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1037 spin_lock_irq(&epfile->ffs->eps_lock);
1038 if (likely(epfile->ep)) {
1040 case FUNCTIONFS_FIFO_STATUS:
1041 ret = usb_ep_fifo_status(epfile->ep->ep);
1043 case FUNCTIONFS_FIFO_FLUSH:
1044 usb_ep_fifo_flush(epfile->ep->ep);
1047 case FUNCTIONFS_CLEAR_HALT:
1048 ret = usb_ep_clear_halt(epfile->ep->ep);
1050 case FUNCTIONFS_ENDPOINT_REVMAP:
1051 ret = epfile->ep->num;
1059 spin_unlock_irq(&epfile->ffs->eps_lock);
1064 static const struct file_operations ffs_epfile_operations = {
1065 .llseek = no_llseek,
1067 .open = ffs_epfile_open,
1068 .write = ffs_epfile_write,
1069 .read = ffs_epfile_read,
1070 .aio_write = ffs_epfile_aio_write,
1071 .aio_read = ffs_epfile_aio_read,
1072 .release = ffs_epfile_release,
1073 .unlocked_ioctl = ffs_epfile_ioctl,
1077 /* File system and super block operations ***********************************/
1080 * Mounting the file system creates a controller file, used first for
1081 * function configuration then later for event monitoring.
1084 static struct inode *__must_check
1085 ffs_sb_make_inode(struct super_block *sb, void *data,
1086 const struct file_operations *fops,
1087 const struct inode_operations *iops,
1088 struct ffs_file_perms *perms)
1090 struct inode *inode;
1094 inode = new_inode(sb);
1096 if (likely(inode)) {
1097 struct timespec current_time = CURRENT_TIME;
1099 inode->i_ino = get_next_ino();
1100 inode->i_mode = perms->mode;
1101 inode->i_uid = perms->uid;
1102 inode->i_gid = perms->gid;
1103 inode->i_atime = current_time;
1104 inode->i_mtime = current_time;
1105 inode->i_ctime = current_time;
1106 inode->i_private = data;
1108 inode->i_fop = fops;
1116 /* Create "regular" file */
1117 static struct inode *ffs_sb_create_file(struct super_block *sb,
1118 const char *name, void *data,
1119 const struct file_operations *fops,
1120 struct dentry **dentry_p)
1122 struct ffs_data *ffs = sb->s_fs_info;
1123 struct dentry *dentry;
1124 struct inode *inode;
1128 dentry = d_alloc_name(sb->s_root, name);
1129 if (unlikely(!dentry))
1132 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1133 if (unlikely(!inode)) {
1138 d_add(dentry, inode);
1146 static const struct super_operations ffs_sb_operations = {
1147 .statfs = simple_statfs,
1148 .drop_inode = generic_delete_inode,
1151 struct ffs_sb_fill_data {
1152 struct ffs_file_perms perms;
1154 const char *dev_name;
1155 struct ffs_data *ffs_data;
1158 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1160 struct ffs_sb_fill_data *data = _data;
1161 struct inode *inode;
1162 struct ffs_data *ffs = data->ffs_data;
1167 data->ffs_data = NULL;
1168 sb->s_fs_info = ffs;
1169 sb->s_blocksize = PAGE_CACHE_SIZE;
1170 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1171 sb->s_magic = FUNCTIONFS_MAGIC;
1172 sb->s_op = &ffs_sb_operations;
1173 sb->s_time_gran = 1;
1176 data->perms.mode = data->root_mode;
1177 inode = ffs_sb_make_inode(sb, NULL,
1178 &simple_dir_operations,
1179 &simple_dir_inode_operations,
1181 sb->s_root = d_make_root(inode);
1182 if (unlikely(!sb->s_root))
1186 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1187 &ffs_ep0_operations, NULL)))
1193 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1197 if (!opts || !*opts)
1201 unsigned long value;
1205 comma = strchr(opts, ',');
1210 eq = strchr(opts, '=');
1211 if (unlikely(!eq)) {
1212 pr_err("'=' missing in %s\n", opts);
1218 if (kstrtoul(eq + 1, 0, &value)) {
1219 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1223 /* Interpret option */
1224 switch (eq - opts) {
1226 if (!memcmp(opts, "rmode", 5))
1227 data->root_mode = (value & 0555) | S_IFDIR;
1228 else if (!memcmp(opts, "fmode", 5))
1229 data->perms.mode = (value & 0666) | S_IFREG;
1235 if (!memcmp(opts, "mode", 4)) {
1236 data->root_mode = (value & 0555) | S_IFDIR;
1237 data->perms.mode = (value & 0666) | S_IFREG;
1244 if (!memcmp(opts, "uid", 3)) {
1245 data->perms.uid = make_kuid(current_user_ns(), value);
1246 if (!uid_valid(data->perms.uid)) {
1247 pr_err("%s: unmapped value: %lu\n", opts, value);
1250 } else if (!memcmp(opts, "gid", 3)) {
1251 data->perms.gid = make_kgid(current_user_ns(), value);
1252 if (!gid_valid(data->perms.gid)) {
1253 pr_err("%s: unmapped value: %lu\n", opts, value);
1263 pr_err("%s: invalid option\n", opts);
1267 /* Next iteration */
1276 /* "mount -t functionfs dev_name /dev/function" ends up here */
1278 static struct dentry *
1279 ffs_fs_mount(struct file_system_type *t, int flags,
1280 const char *dev_name, void *opts)
1282 struct ffs_sb_fill_data data = {
1284 .mode = S_IFREG | 0600,
1285 .uid = GLOBAL_ROOT_UID,
1286 .gid = GLOBAL_ROOT_GID,
1288 .root_mode = S_IFDIR | 0500,
1293 struct ffs_data *ffs;
1297 ret = ffs_fs_parse_opts(&data, opts);
1298 if (unlikely(ret < 0))
1299 return ERR_PTR(ret);
1301 ffs = ffs_data_new();
1303 return ERR_PTR(-ENOMEM);
1304 ffs->file_perms = data.perms;
1306 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1307 if (unlikely(!ffs->dev_name)) {
1309 return ERR_PTR(-ENOMEM);
1312 ffs_dev = ffs_acquire_dev(dev_name);
1313 if (IS_ERR(ffs_dev)) {
1315 return ERR_CAST(ffs_dev);
1317 ffs->private_data = ffs_dev;
1318 data.ffs_data = ffs;
1320 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1321 if (IS_ERR(rv) && data.ffs_data) {
1322 ffs_release_dev(data.ffs_data);
1323 ffs_data_put(data.ffs_data);
1329 ffs_fs_kill_sb(struct super_block *sb)
1333 kill_litter_super(sb);
1334 if (sb->s_fs_info) {
1335 ffs_release_dev(sb->s_fs_info);
1336 ffs_data_put(sb->s_fs_info);
1340 static struct file_system_type ffs_fs_type = {
1341 .owner = THIS_MODULE,
1342 .name = "functionfs",
1343 .mount = ffs_fs_mount,
1344 .kill_sb = ffs_fs_kill_sb,
1346 MODULE_ALIAS_FS("functionfs");
1349 /* Driver's main init/cleanup functions *************************************/
1351 static int functionfs_init(void)
1357 ret = register_filesystem(&ffs_fs_type);
1359 pr_info("file system registered\n");
1361 pr_err("failed registering file system (%d)\n", ret);
1366 static void functionfs_cleanup(void)
1370 pr_info("unloading\n");
1371 unregister_filesystem(&ffs_fs_type);
1375 /* ffs_data and ffs_function construction and destruction code **************/
1377 static void ffs_data_clear(struct ffs_data *ffs);
1378 static void ffs_data_reset(struct ffs_data *ffs);
1380 static void ffs_data_get(struct ffs_data *ffs)
1384 atomic_inc(&ffs->ref);
1387 static void ffs_data_opened(struct ffs_data *ffs)
1391 atomic_inc(&ffs->ref);
1392 atomic_inc(&ffs->opened);
1395 static void ffs_data_put(struct ffs_data *ffs)
1399 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1400 pr_info("%s(): freeing\n", __func__);
1401 ffs_data_clear(ffs);
1402 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1403 waitqueue_active(&ffs->ep0req_completion.wait));
1404 kfree(ffs->dev_name);
1409 static void ffs_data_closed(struct ffs_data *ffs)
1413 if (atomic_dec_and_test(&ffs->opened)) {
1414 ffs->state = FFS_CLOSING;
1415 ffs_data_reset(ffs);
1421 static struct ffs_data *ffs_data_new(void)
1423 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1429 atomic_set(&ffs->ref, 1);
1430 atomic_set(&ffs->opened, 0);
1431 ffs->state = FFS_READ_DESCRIPTORS;
1432 mutex_init(&ffs->mutex);
1433 spin_lock_init(&ffs->eps_lock);
1434 init_waitqueue_head(&ffs->ev.waitq);
1435 init_completion(&ffs->ep0req_completion);
1437 /* XXX REVISIT need to update it in some places, or do we? */
1438 ffs->ev.can_stall = 1;
1443 static void ffs_data_clear(struct ffs_data *ffs)
1447 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1450 BUG_ON(ffs->gadget);
1453 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1455 kfree(ffs->raw_descs_data);
1456 kfree(ffs->raw_strings);
1457 kfree(ffs->stringtabs);
1460 static void ffs_data_reset(struct ffs_data *ffs)
1464 ffs_data_clear(ffs);
1466 ffs->epfiles = NULL;
1467 ffs->raw_descs_data = NULL;
1468 ffs->raw_descs = NULL;
1469 ffs->raw_strings = NULL;
1470 ffs->stringtabs = NULL;
1472 ffs->raw_descs_length = 0;
1473 ffs->fs_descs_count = 0;
1474 ffs->hs_descs_count = 0;
1475 ffs->ss_descs_count = 0;
1477 ffs->strings_count = 0;
1478 ffs->interfaces_count = 0;
1483 ffs->state = FFS_READ_DESCRIPTORS;
1484 ffs->setup_state = FFS_NO_SETUP;
1489 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1491 struct usb_gadget_strings **lang;
1496 if (WARN_ON(ffs->state != FFS_ACTIVE
1497 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1500 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1501 if (unlikely(first_id < 0))
1504 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1505 if (unlikely(!ffs->ep0req))
1507 ffs->ep0req->complete = ffs_ep0_complete;
1508 ffs->ep0req->context = ffs;
1510 lang = ffs->stringtabs;
1511 for (lang = ffs->stringtabs; *lang; ++lang) {
1512 struct usb_string *str = (*lang)->strings;
1514 for (; str->s; ++id, ++str)
1518 ffs->gadget = cdev->gadget;
1523 static void functionfs_unbind(struct ffs_data *ffs)
1527 if (!WARN_ON(!ffs->gadget)) {
1528 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1531 clear_bit(FFS_FL_BOUND, &ffs->flags);
1536 static int ffs_epfiles_create(struct ffs_data *ffs)
1538 struct ffs_epfile *epfile, *epfiles;
1543 count = ffs->eps_count;
1544 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1549 for (i = 1; i <= count; ++i, ++epfile) {
1551 mutex_init(&epfile->mutex);
1552 init_waitqueue_head(&epfile->wait);
1553 sprintf(epfiles->name, "ep%u", i);
1554 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1555 &ffs_epfile_operations,
1556 &epfile->dentry))) {
1557 ffs_epfiles_destroy(epfiles, i - 1);
1562 ffs->epfiles = epfiles;
1566 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1568 struct ffs_epfile *epfile = epfiles;
1572 for (; count; --count, ++epfile) {
1573 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1574 waitqueue_active(&epfile->wait));
1575 if (epfile->dentry) {
1576 d_delete(epfile->dentry);
1577 dput(epfile->dentry);
1578 epfile->dentry = NULL;
1586 static void ffs_func_eps_disable(struct ffs_function *func)
1588 struct ffs_ep *ep = func->eps;
1589 struct ffs_epfile *epfile = func->ffs->epfiles;
1590 unsigned count = func->ffs->eps_count;
1591 unsigned long flags;
1593 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1595 /* pending requests get nuked */
1597 usb_ep_disable(ep->ep);
1603 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1606 static int ffs_func_eps_enable(struct ffs_function *func)
1608 struct ffs_data *ffs = func->ffs;
1609 struct ffs_ep *ep = func->eps;
1610 struct ffs_epfile *epfile = ffs->epfiles;
1611 unsigned count = ffs->eps_count;
1612 unsigned long flags;
1615 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1617 struct usb_endpoint_descriptor *ds;
1620 if (ffs->gadget->speed == USB_SPEED_SUPER)
1622 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1627 /* fall-back to lower speed if desc missing for current speed */
1629 ds = ep->descs[desc_idx];
1630 } while (!ds && --desc_idx >= 0);
1637 ep->ep->driver_data = ep;
1639 ret = usb_ep_enable(ep->ep);
1642 epfile->in = usb_endpoint_dir_in(ds);
1643 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1648 wake_up(&epfile->wait);
1653 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1659 /* Parsing and building descriptors and strings *****************************/
1662 * This validates if data pointed by data is a valid USB descriptor as
1663 * well as record how many interfaces, endpoints and strings are
1664 * required by given configuration. Returns address after the
1665 * descriptor or NULL if data is invalid.
1668 enum ffs_entity_type {
1669 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1672 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1674 struct usb_descriptor_header *desc,
1677 static int __must_check ffs_do_desc(char *data, unsigned len,
1678 ffs_entity_callback entity, void *priv)
1680 struct usb_descriptor_header *_ds = (void *)data;
1686 /* At least two bytes are required: length and type */
1688 pr_vdebug("descriptor too short\n");
1692 /* If we have at least as many bytes as the descriptor takes? */
1693 length = _ds->bLength;
1695 pr_vdebug("descriptor longer then available data\n");
1699 #define __entity_check_INTERFACE(val) 1
1700 #define __entity_check_STRING(val) (val)
1701 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1702 #define __entity(type, val) do { \
1703 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1704 if (unlikely(!__entity_check_ ##type(val))) { \
1705 pr_vdebug("invalid entity's value\n"); \
1708 ret = entity(FFS_ ##type, &val, _ds, priv); \
1709 if (unlikely(ret < 0)) { \
1710 pr_debug("entity " #type "(%02x); ret = %d\n", \
1716 /* Parse descriptor depending on type. */
1717 switch (_ds->bDescriptorType) {
1721 case USB_DT_DEVICE_QUALIFIER:
1722 /* function can't have any of those */
1723 pr_vdebug("descriptor reserved for gadget: %d\n",
1724 _ds->bDescriptorType);
1727 case USB_DT_INTERFACE: {
1728 struct usb_interface_descriptor *ds = (void *)_ds;
1729 pr_vdebug("interface descriptor\n");
1730 if (length != sizeof *ds)
1733 __entity(INTERFACE, ds->bInterfaceNumber);
1735 __entity(STRING, ds->iInterface);
1739 case USB_DT_ENDPOINT: {
1740 struct usb_endpoint_descriptor *ds = (void *)_ds;
1741 pr_vdebug("endpoint descriptor\n");
1742 if (length != USB_DT_ENDPOINT_SIZE &&
1743 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1745 __entity(ENDPOINT, ds->bEndpointAddress);
1750 pr_vdebug("hid descriptor\n");
1751 if (length != sizeof(struct hid_descriptor))
1756 if (length != sizeof(struct usb_otg_descriptor))
1760 case USB_DT_INTERFACE_ASSOCIATION: {
1761 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1762 pr_vdebug("interface association descriptor\n");
1763 if (length != sizeof *ds)
1766 __entity(STRING, ds->iFunction);
1770 case USB_DT_SS_ENDPOINT_COMP:
1771 pr_vdebug("EP SS companion descriptor\n");
1772 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1776 case USB_DT_OTHER_SPEED_CONFIG:
1777 case USB_DT_INTERFACE_POWER:
1779 case USB_DT_SECURITY:
1780 case USB_DT_CS_RADIO_CONTROL:
1782 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1786 /* We should never be here */
1787 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1791 pr_vdebug("invalid length: %d (descriptor %d)\n",
1792 _ds->bLength, _ds->bDescriptorType);
1797 #undef __entity_check_DESCRIPTOR
1798 #undef __entity_check_INTERFACE
1799 #undef __entity_check_STRING
1800 #undef __entity_check_ENDPOINT
1805 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1806 ffs_entity_callback entity, void *priv)
1808 const unsigned _len = len;
1809 unsigned long num = 0;
1819 /* Record "descriptor" entity */
1820 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1821 if (unlikely(ret < 0)) {
1822 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1830 ret = ffs_do_desc(data, len, entity, priv);
1831 if (unlikely(ret < 0)) {
1832 pr_debug("%s returns %d\n", __func__, ret);
1842 static int __ffs_data_do_entity(enum ffs_entity_type type,
1843 u8 *valuep, struct usb_descriptor_header *desc,
1846 struct ffs_data *ffs = priv;
1851 case FFS_DESCRIPTOR:
1856 * Interfaces are indexed from zero so if we
1857 * encountered interface "n" then there are at least
1860 if (*valuep >= ffs->interfaces_count)
1861 ffs->interfaces_count = *valuep + 1;
1866 * Strings are indexed from 1 (0 is magic ;) reserved
1867 * for languages list or some such)
1869 if (*valuep > ffs->strings_count)
1870 ffs->strings_count = *valuep;
1874 /* Endpoints are indexed from 1 as well. */
1875 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1876 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1883 static int __ffs_data_got_descs(struct ffs_data *ffs,
1884 char *const _data, size_t len)
1886 char *data = _data, *raw_descs;
1887 unsigned counts[3], flags;
1888 int ret = -EINVAL, i;
1892 if (get_unaligned_le32(data + 4) != len)
1895 switch (get_unaligned_le32(data)) {
1896 case FUNCTIONFS_DESCRIPTORS_MAGIC:
1897 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
1901 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
1902 flags = get_unaligned_le32(data + 8);
1903 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
1904 FUNCTIONFS_HAS_HS_DESC |
1905 FUNCTIONFS_HAS_SS_DESC)) {
1916 /* Read fs_count, hs_count and ss_count (if present) */
1917 for (i = 0; i < 3; ++i) {
1918 if (!(flags & (1 << i))) {
1920 } else if (len < 4) {
1923 counts[i] = get_unaligned_le32(data);
1929 /* Read descriptors */
1931 for (i = 0; i < 3; ++i) {
1934 ret = ffs_do_descs(counts[i], data, len,
1935 __ffs_data_do_entity, ffs);
1942 if (raw_descs == data || len) {
1947 ffs->raw_descs_data = _data;
1948 ffs->raw_descs = raw_descs;
1949 ffs->raw_descs_length = data - raw_descs;
1950 ffs->fs_descs_count = counts[0];
1951 ffs->hs_descs_count = counts[1];
1952 ffs->ss_descs_count = counts[2];
1961 static int __ffs_data_got_strings(struct ffs_data *ffs,
1962 char *const _data, size_t len)
1964 u32 str_count, needed_count, lang_count;
1965 struct usb_gadget_strings **stringtabs, *t;
1966 struct usb_string *strings, *s;
1967 const char *data = _data;
1971 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1972 get_unaligned_le32(data + 4) != len))
1974 str_count = get_unaligned_le32(data + 8);
1975 lang_count = get_unaligned_le32(data + 12);
1977 /* if one is zero the other must be zero */
1978 if (unlikely(!str_count != !lang_count))
1981 /* Do we have at least as many strings as descriptors need? */
1982 needed_count = ffs->strings_count;
1983 if (unlikely(str_count < needed_count))
1987 * If we don't need any strings just return and free all
1990 if (!needed_count) {
1995 /* Allocate everything in one chunk so there's less maintenance. */
1999 vla_item(d, struct usb_gadget_strings *, stringtabs,
2001 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2002 vla_item(d, struct usb_string, strings,
2003 lang_count*(needed_count+1));
2005 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2007 if (unlikely(!vlabuf)) {
2012 /* Initialize the VLA pointers */
2013 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2014 t = vla_ptr(vlabuf, d, stringtab);
2017 *stringtabs++ = t++;
2021 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2022 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2023 t = vla_ptr(vlabuf, d, stringtab);
2024 s = vla_ptr(vlabuf, d, strings);
2028 /* For each language */
2032 do { /* lang_count > 0 so we can use do-while */
2033 unsigned needed = needed_count;
2035 if (unlikely(len < 3))
2037 t->language = get_unaligned_le16(data);
2044 /* For each string */
2045 do { /* str_count > 0 so we can use do-while */
2046 size_t length = strnlen(data, len);
2048 if (unlikely(length == len))
2052 * User may provide more strings then we need,
2053 * if that's the case we simply ignore the
2056 if (likely(needed)) {
2058 * s->id will be set while adding
2059 * function to configuration so for
2060 * now just leave garbage here.
2069 } while (--str_count);
2071 s->id = 0; /* terminator */
2075 } while (--lang_count);
2077 /* Some garbage left? */
2082 ffs->stringtabs = stringtabs;
2083 ffs->raw_strings = _data;
2095 /* Events handling and management *******************************************/
2097 static void __ffs_event_add(struct ffs_data *ffs,
2098 enum usb_functionfs_event_type type)
2100 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2104 * Abort any unhandled setup
2106 * We do not need to worry about some cmpxchg() changing value
2107 * of ffs->setup_state without holding the lock because when
2108 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2109 * the source does nothing.
2111 if (ffs->setup_state == FFS_SETUP_PENDING)
2112 ffs->setup_state = FFS_SETUP_CANCELLED;
2115 case FUNCTIONFS_RESUME:
2116 rem_type2 = FUNCTIONFS_SUSPEND;
2118 case FUNCTIONFS_SUSPEND:
2119 case FUNCTIONFS_SETUP:
2121 /* Discard all similar events */
2124 case FUNCTIONFS_BIND:
2125 case FUNCTIONFS_UNBIND:
2126 case FUNCTIONFS_DISABLE:
2127 case FUNCTIONFS_ENABLE:
2128 /* Discard everything other then power management. */
2129 rem_type1 = FUNCTIONFS_SUSPEND;
2130 rem_type2 = FUNCTIONFS_RESUME;
2139 u8 *ev = ffs->ev.types, *out = ev;
2140 unsigned n = ffs->ev.count;
2141 for (; n; --n, ++ev)
2142 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2145 pr_vdebug("purging event %d\n", *ev);
2146 ffs->ev.count = out - ffs->ev.types;
2149 pr_vdebug("adding event %d\n", type);
2150 ffs->ev.types[ffs->ev.count++] = type;
2151 wake_up_locked(&ffs->ev.waitq);
2154 static void ffs_event_add(struct ffs_data *ffs,
2155 enum usb_functionfs_event_type type)
2157 unsigned long flags;
2158 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2159 __ffs_event_add(ffs, type);
2160 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2164 /* Bind/unbind USB function hooks *******************************************/
2166 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2167 struct usb_descriptor_header *desc,
2170 struct usb_endpoint_descriptor *ds = (void *)desc;
2171 struct ffs_function *func = priv;
2172 struct ffs_ep *ffs_ep;
2173 unsigned ep_desc_id, idx;
2174 static const char *speed_names[] = { "full", "high", "super" };
2176 if (type != FFS_DESCRIPTOR)
2180 * If ss_descriptors is not NULL, we are reading super speed
2181 * descriptors; if hs_descriptors is not NULL, we are reading high
2182 * speed descriptors; otherwise, we are reading full speed
2185 if (func->function.ss_descriptors) {
2187 func->function.ss_descriptors[(long)valuep] = desc;
2188 } else if (func->function.hs_descriptors) {
2190 func->function.hs_descriptors[(long)valuep] = desc;
2193 func->function.fs_descriptors[(long)valuep] = desc;
2196 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2199 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2200 ffs_ep = func->eps + idx;
2202 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2203 pr_err("two %sspeed descriptors for EP %d\n",
2204 speed_names[ep_desc_id],
2205 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2208 ffs_ep->descs[ep_desc_id] = ds;
2210 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2212 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2213 if (!ds->wMaxPacketSize)
2214 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2216 struct usb_request *req;
2219 pr_vdebug("autoconfig\n");
2220 ep = usb_ep_autoconfig(func->gadget, ds);
2223 ep->driver_data = func->eps + idx;
2225 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2231 func->eps_revmap[ds->bEndpointAddress &
2232 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2234 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2239 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2240 struct usb_descriptor_header *desc,
2243 struct ffs_function *func = priv;
2249 case FFS_DESCRIPTOR:
2250 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2255 if (func->interfaces_nums[idx] < 0) {
2256 int id = usb_interface_id(func->conf, &func->function);
2257 if (unlikely(id < 0))
2259 func->interfaces_nums[idx] = id;
2261 newValue = func->interfaces_nums[idx];
2265 /* String' IDs are allocated when fsf_data is bound to cdev */
2266 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2271 * USB_DT_ENDPOINT are handled in
2272 * __ffs_func_bind_do_descs().
2274 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2277 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2278 if (unlikely(!func->eps[idx].ep))
2282 struct usb_endpoint_descriptor **descs;
2283 descs = func->eps[idx].descs;
2284 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2289 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2294 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2295 struct usb_configuration *c)
2297 struct ffs_function *func = ffs_func_from_usb(f);
2298 struct f_fs_opts *ffs_opts =
2299 container_of(f->fi, struct f_fs_opts, func_inst);
2305 * Legacy gadget triggers binding in functionfs_ready_callback,
2306 * which already uses locking; taking the same lock here would
2309 * Configfs-enabled gadgets however do need ffs_dev_lock.
2311 if (!ffs_opts->no_configfs)
2313 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2314 func->ffs = ffs_opts->dev->ffs_data;
2315 if (!ffs_opts->no_configfs)
2318 return ERR_PTR(ret);
2321 func->gadget = c->cdev->gadget;
2323 ffs_data_get(func->ffs);
2326 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2327 * configurations are bound in sequence with list_for_each_entry,
2328 * in each configuration its functions are bound in sequence
2329 * with list_for_each_entry, so we assume no race condition
2330 * with regard to ffs_opts->bound access
2332 if (!ffs_opts->refcnt) {
2333 ret = functionfs_bind(func->ffs, c->cdev);
2335 return ERR_PTR(ret);
2338 func->function.strings = func->ffs->stringtabs;
2343 static int _ffs_func_bind(struct usb_configuration *c,
2344 struct usb_function *f)
2346 struct ffs_function *func = ffs_func_from_usb(f);
2347 struct ffs_data *ffs = func->ffs;
2349 const int full = !!func->ffs->fs_descs_count;
2350 const int high = gadget_is_dualspeed(func->gadget) &&
2351 func->ffs->hs_descs_count;
2352 const int super = gadget_is_superspeed(func->gadget) &&
2353 func->ffs->ss_descs_count;
2355 int fs_len, hs_len, ret;
2357 /* Make it a single chunk, less management later on */
2359 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2360 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2361 full ? ffs->fs_descs_count + 1 : 0);
2362 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2363 high ? ffs->hs_descs_count + 1 : 0);
2364 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2365 super ? ffs->ss_descs_count + 1 : 0);
2366 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2367 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
2372 /* Has descriptors only for speeds gadget does not support */
2373 if (unlikely(!(full | high | super)))
2376 /* Allocate a single chunk, less management later on */
2377 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2378 if (unlikely(!vlabuf))
2382 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2383 /* Copy descriptors */
2384 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2385 ffs->raw_descs_length);
2387 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2388 for (ret = ffs->eps_count; ret; --ret) {
2391 ptr = vla_ptr(vlabuf, d, eps);
2396 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2398 func->eps = vla_ptr(vlabuf, d, eps);
2399 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
2402 * Go through all the endpoint descriptors and allocate
2403 * endpoints first, so that later we can rewrite the endpoint
2404 * numbers without worrying that it may be described later on.
2407 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2408 fs_len = ffs_do_descs(ffs->fs_descs_count,
2409 vla_ptr(vlabuf, d, raw_descs),
2411 __ffs_func_bind_do_descs, func);
2412 if (unlikely(fs_len < 0)) {
2421 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2422 hs_len = ffs_do_descs(ffs->hs_descs_count,
2423 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2424 d_raw_descs__sz - fs_len,
2425 __ffs_func_bind_do_descs, func);
2426 if (unlikely(hs_len < 0)) {
2434 if (likely(super)) {
2435 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2436 ret = ffs_do_descs(ffs->ss_descs_count,
2437 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2438 d_raw_descs__sz - fs_len - hs_len,
2439 __ffs_func_bind_do_descs, func);
2440 if (unlikely(ret < 0))
2445 * Now handle interface numbers allocation and interface and
2446 * endpoint numbers rewriting. We can do that in one go
2449 ret = ffs_do_descs(ffs->fs_descs_count +
2450 (high ? ffs->hs_descs_count : 0) +
2451 (super ? ffs->ss_descs_count : 0),
2452 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
2453 __ffs_func_bind_do_nums, func);
2454 if (unlikely(ret < 0))
2457 /* And we're done */
2458 ffs_event_add(ffs, FUNCTIONFS_BIND);
2462 /* XXX Do we need to release all claimed endpoints here? */
2466 static int ffs_func_bind(struct usb_configuration *c,
2467 struct usb_function *f)
2469 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2471 if (IS_ERR(ffs_opts))
2472 return PTR_ERR(ffs_opts);
2474 return _ffs_func_bind(c, f);
2478 /* Other USB function hooks *************************************************/
2480 static int ffs_func_set_alt(struct usb_function *f,
2481 unsigned interface, unsigned alt)
2483 struct ffs_function *func = ffs_func_from_usb(f);
2484 struct ffs_data *ffs = func->ffs;
2487 if (alt != (unsigned)-1) {
2488 intf = ffs_func_revmap_intf(func, interface);
2489 if (unlikely(intf < 0))
2494 ffs_func_eps_disable(ffs->func);
2496 if (ffs->state != FFS_ACTIVE)
2499 if (alt == (unsigned)-1) {
2501 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2506 ret = ffs_func_eps_enable(func);
2507 if (likely(ret >= 0))
2508 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2512 static void ffs_func_disable(struct usb_function *f)
2514 ffs_func_set_alt(f, 0, (unsigned)-1);
2517 static int ffs_func_setup(struct usb_function *f,
2518 const struct usb_ctrlrequest *creq)
2520 struct ffs_function *func = ffs_func_from_usb(f);
2521 struct ffs_data *ffs = func->ffs;
2522 unsigned long flags;
2527 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2528 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2529 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2530 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2531 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2534 * Most requests directed to interface go through here
2535 * (notable exceptions are set/get interface) so we need to
2536 * handle them. All other either handled by composite or
2537 * passed to usb_configuration->setup() (if one is set). No
2538 * matter, we will handle requests directed to endpoint here
2539 * as well (as it's straightforward) but what to do with any
2542 if (ffs->state != FFS_ACTIVE)
2545 switch (creq->bRequestType & USB_RECIP_MASK) {
2546 case USB_RECIP_INTERFACE:
2547 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2548 if (unlikely(ret < 0))
2552 case USB_RECIP_ENDPOINT:
2553 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2554 if (unlikely(ret < 0))
2562 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2563 ffs->ev.setup = *creq;
2564 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2565 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2566 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2571 static void ffs_func_suspend(struct usb_function *f)
2574 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2577 static void ffs_func_resume(struct usb_function *f)
2580 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2584 /* Endpoint and interface numbers reverse mapping ***************************/
2586 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2588 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2589 return num ? num : -EDOM;
2592 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2594 short *nums = func->interfaces_nums;
2595 unsigned count = func->ffs->interfaces_count;
2597 for (; count; --count, ++nums) {
2598 if (*nums >= 0 && *nums == intf)
2599 return nums - func->interfaces_nums;
2606 /* Devices management *******************************************************/
2608 static LIST_HEAD(ffs_devices);
2610 static struct ffs_dev *_ffs_do_find_dev(const char *name)
2612 struct ffs_dev *dev;
2614 list_for_each_entry(dev, &ffs_devices, entry) {
2615 if (!dev->name || !name)
2617 if (strcmp(dev->name, name) == 0)
2625 * ffs_lock must be taken by the caller of this function
2627 static struct ffs_dev *_ffs_get_single_dev(void)
2629 struct ffs_dev *dev;
2631 if (list_is_singular(&ffs_devices)) {
2632 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2641 * ffs_lock must be taken by the caller of this function
2643 static struct ffs_dev *_ffs_find_dev(const char *name)
2645 struct ffs_dev *dev;
2647 dev = _ffs_get_single_dev();
2651 return _ffs_do_find_dev(name);
2654 /* Configfs support *********************************************************/
2656 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2658 return container_of(to_config_group(item), struct f_fs_opts,
2662 static void ffs_attr_release(struct config_item *item)
2664 struct f_fs_opts *opts = to_ffs_opts(item);
2666 usb_put_function_instance(&opts->func_inst);
2669 static struct configfs_item_operations ffs_item_ops = {
2670 .release = ffs_attr_release,
2673 static struct config_item_type ffs_func_type = {
2674 .ct_item_ops = &ffs_item_ops,
2675 .ct_owner = THIS_MODULE,
2679 /* Function registration interface ******************************************/
2681 static void ffs_free_inst(struct usb_function_instance *f)
2683 struct f_fs_opts *opts;
2685 opts = to_f_fs_opts(f);
2687 _ffs_free_dev(opts->dev);
2692 #define MAX_INST_NAME_LEN 40
2694 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2696 struct f_fs_opts *opts;
2701 name_len = strlen(name) + 1;
2702 if (name_len > MAX_INST_NAME_LEN)
2703 return -ENAMETOOLONG;
2705 ptr = kstrndup(name, name_len, GFP_KERNEL);
2709 opts = to_f_fs_opts(fi);
2714 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2715 ret = _ffs_name_dev(opts->dev, ptr);
2721 opts->dev->name_allocated = true;
2730 static struct usb_function_instance *ffs_alloc_inst(void)
2732 struct f_fs_opts *opts;
2733 struct ffs_dev *dev;
2735 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2737 return ERR_PTR(-ENOMEM);
2739 opts->func_inst.set_inst_name = ffs_set_inst_name;
2740 opts->func_inst.free_func_inst = ffs_free_inst;
2742 dev = _ffs_alloc_dev();
2746 return ERR_CAST(dev);
2751 config_group_init_type_name(&opts->func_inst.group, "",
2753 return &opts->func_inst;
2756 static void ffs_free(struct usb_function *f)
2758 kfree(ffs_func_from_usb(f));
2761 static void ffs_func_unbind(struct usb_configuration *c,
2762 struct usb_function *f)
2764 struct ffs_function *func = ffs_func_from_usb(f);
2765 struct ffs_data *ffs = func->ffs;
2766 struct f_fs_opts *opts =
2767 container_of(f->fi, struct f_fs_opts, func_inst);
2768 struct ffs_ep *ep = func->eps;
2769 unsigned count = ffs->eps_count;
2770 unsigned long flags;
2773 if (ffs->func == func) {
2774 ffs_func_eps_disable(func);
2778 if (!--opts->refcnt)
2779 functionfs_unbind(ffs);
2781 /* cleanup after autoconfig */
2782 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2784 if (ep->ep && ep->req)
2785 usb_ep_free_request(ep->ep, ep->req);
2789 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2793 * eps, descriptors and interfaces_nums are allocated in the
2794 * same chunk so only one free is required.
2796 func->function.fs_descriptors = NULL;
2797 func->function.hs_descriptors = NULL;
2798 func->function.ss_descriptors = NULL;
2799 func->interfaces_nums = NULL;
2801 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2804 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2806 struct ffs_function *func;
2810 func = kzalloc(sizeof(*func), GFP_KERNEL);
2811 if (unlikely(!func))
2812 return ERR_PTR(-ENOMEM);
2814 func->function.name = "Function FS Gadget";
2816 func->function.bind = ffs_func_bind;
2817 func->function.unbind = ffs_func_unbind;
2818 func->function.set_alt = ffs_func_set_alt;
2819 func->function.disable = ffs_func_disable;
2820 func->function.setup = ffs_func_setup;
2821 func->function.suspend = ffs_func_suspend;
2822 func->function.resume = ffs_func_resume;
2823 func->function.free_func = ffs_free;
2825 return &func->function;
2829 * ffs_lock must be taken by the caller of this function
2831 static struct ffs_dev *_ffs_alloc_dev(void)
2833 struct ffs_dev *dev;
2836 if (_ffs_get_single_dev())
2837 return ERR_PTR(-EBUSY);
2839 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2841 return ERR_PTR(-ENOMEM);
2843 if (list_empty(&ffs_devices)) {
2844 ret = functionfs_init();
2847 return ERR_PTR(ret);
2851 list_add(&dev->entry, &ffs_devices);
2857 * ffs_lock must be taken by the caller of this function
2858 * The caller is responsible for "name" being available whenever f_fs needs it
2860 static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2862 struct ffs_dev *existing;
2864 existing = _ffs_do_find_dev(name);
2874 * The caller is responsible for "name" being available whenever f_fs needs it
2876 int ffs_name_dev(struct ffs_dev *dev, const char *name)
2881 ret = _ffs_name_dev(dev, name);
2886 EXPORT_SYMBOL(ffs_name_dev);
2888 int ffs_single_dev(struct ffs_dev *dev)
2895 if (!list_is_singular(&ffs_devices))
2903 EXPORT_SYMBOL(ffs_single_dev);
2906 * ffs_lock must be taken by the caller of this function
2908 static void _ffs_free_dev(struct ffs_dev *dev)
2910 list_del(&dev->entry);
2911 if (dev->name_allocated)
2914 if (list_empty(&ffs_devices))
2915 functionfs_cleanup();
2918 static void *ffs_acquire_dev(const char *dev_name)
2920 struct ffs_dev *ffs_dev;
2925 ffs_dev = _ffs_find_dev(dev_name);
2927 ffs_dev = ERR_PTR(-ENODEV);
2928 else if (ffs_dev->mounted)
2929 ffs_dev = ERR_PTR(-EBUSY);
2930 else if (ffs_dev->ffs_acquire_dev_callback &&
2931 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2932 ffs_dev = ERR_PTR(-ENODEV);
2934 ffs_dev->mounted = true;
2940 static void ffs_release_dev(struct ffs_data *ffs_data)
2942 struct ffs_dev *ffs_dev;
2947 ffs_dev = ffs_data->private_data;
2949 ffs_dev->mounted = false;
2951 if (ffs_dev->ffs_release_dev_callback)
2952 ffs_dev->ffs_release_dev_callback(ffs_dev);
2958 static int ffs_ready(struct ffs_data *ffs)
2960 struct ffs_dev *ffs_obj;
2966 ffs_obj = ffs->private_data;
2971 if (WARN_ON(ffs_obj->desc_ready)) {
2976 ffs_obj->desc_ready = true;
2977 ffs_obj->ffs_data = ffs;
2979 if (ffs_obj->ffs_ready_callback)
2980 ret = ffs_obj->ffs_ready_callback(ffs);
2987 static void ffs_closed(struct ffs_data *ffs)
2989 struct ffs_dev *ffs_obj;
2994 ffs_obj = ffs->private_data;
2998 ffs_obj->desc_ready = false;
3000 if (ffs_obj->ffs_closed_callback)
3001 ffs_obj->ffs_closed_callback(ffs);
3003 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
3004 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
3007 unregister_gadget_item(ffs_obj->opts->
3008 func_inst.group.cg_item.ci_parent->ci_parent);
3013 /* Misc helper functions ****************************************************/
3015 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3018 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3019 : mutex_lock_interruptible(mutex);
3022 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
3029 data = kmalloc(len, GFP_KERNEL);
3030 if (unlikely(!data))
3031 return ERR_PTR(-ENOMEM);
3033 if (unlikely(__copy_from_user(data, buf, len))) {
3035 return ERR_PTR(-EFAULT);
3038 pr_vdebug("Buffer from user space:\n");
3039 ffs_dump_mem("", data, len);
3044 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3045 MODULE_LICENSE("GPL");
3046 MODULE_AUTHOR("Michal Nazarewicz");