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/poll.h>
36 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
38 /* Variable Length Array Macros **********************************************/
39 #define vla_group(groupname) size_t groupname##__next = 0
40 #define vla_group_size(groupname) groupname##__next
42 #define vla_item(groupname, type, name, n) \
43 size_t groupname##_##name##__offset = ({ \
44 size_t align_mask = __alignof__(type) - 1; \
45 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
46 size_t size = (n) * sizeof(type); \
47 groupname##__next = offset + size; \
51 #define vla_item_with_sz(groupname, type, name, n) \
52 size_t groupname##_##name##__sz = (n) * sizeof(type); \
53 size_t groupname##_##name##__offset = ({ \
54 size_t align_mask = __alignof__(type) - 1; \
55 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
56 size_t size = groupname##_##name##__sz; \
57 groupname##__next = offset + size; \
61 #define vla_ptr(ptr, groupname, name) \
62 ((void *) ((char *)ptr + groupname##_##name##__offset))
64 /* Reference counter handling */
65 static void ffs_data_get(struct ffs_data *ffs);
66 static void ffs_data_put(struct ffs_data *ffs);
67 /* Creates new ffs_data object. */
68 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
70 /* Opened counter handling. */
71 static void ffs_data_opened(struct ffs_data *ffs);
72 static void ffs_data_closed(struct ffs_data *ffs);
74 /* Called with ffs->mutex held; take over ownership of data. */
75 static int __must_check
76 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
77 static int __must_check
78 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
81 /* The function structure ***************************************************/
86 struct usb_configuration *conf;
87 struct usb_gadget *gadget;
92 short *interfaces_nums;
94 struct usb_function function;
98 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
100 return container_of(f, struct ffs_function, function);
104 static inline enum ffs_setup_state
105 ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
107 return (enum ffs_setup_state)
108 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
112 static void ffs_func_eps_disable(struct ffs_function *func);
113 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
115 static int ffs_func_bind(struct usb_configuration *,
116 struct usb_function *);
117 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
118 static void ffs_func_disable(struct usb_function *);
119 static int ffs_func_setup(struct usb_function *,
120 const struct usb_ctrlrequest *);
121 static void ffs_func_suspend(struct usb_function *);
122 static void ffs_func_resume(struct usb_function *);
125 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
126 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
129 /* The endpoints structures *************************************************/
132 struct usb_ep *ep; /* P: ffs->eps_lock */
133 struct usb_request *req; /* P: epfile->mutex */
135 /* [0]: full speed, [1]: high speed */
136 struct usb_endpoint_descriptor *descs[2];
140 int status; /* P: epfile->mutex */
144 /* Protects ep->ep and ep->req. */
146 wait_queue_head_t wait;
148 struct ffs_data *ffs;
149 struct ffs_ep *ep; /* P: ffs->eps_lock */
151 struct dentry *dentry;
155 unsigned char in; /* P: ffs->eps_lock */
156 unsigned char isoc; /* P: ffs->eps_lock */
161 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
162 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
164 static struct inode *__must_check
165 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
166 const struct file_operations *fops,
167 struct dentry **dentry_p);
169 /* Devices management *******************************************************/
171 DEFINE_MUTEX(ffs_lock);
172 EXPORT_SYMBOL(ffs_lock);
174 static struct ffs_dev *_ffs_find_dev(const char *name);
175 static struct ffs_dev *_ffs_alloc_dev(void);
176 static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
177 static void _ffs_free_dev(struct ffs_dev *dev);
178 static void *ffs_acquire_dev(const char *dev_name);
179 static void ffs_release_dev(struct ffs_data *ffs_data);
180 static int ffs_ready(struct ffs_data *ffs);
181 static void ffs_closed(struct ffs_data *ffs);
183 /* Misc helper functions ****************************************************/
185 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
186 __attribute__((warn_unused_result, nonnull));
187 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
188 __attribute__((warn_unused_result, nonnull));
191 /* Control file aka ep0 *****************************************************/
193 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
195 struct ffs_data *ffs = req->context;
197 complete_all(&ffs->ep0req_completion);
200 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
202 struct usb_request *req = ffs->ep0req;
205 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
207 spin_unlock_irq(&ffs->ev.waitq.lock);
213 * UDC layer requires to provide a buffer even for ZLP, but should
214 * not use it at all. Let's provide some poisoned pointer to catch
215 * possible bug in the driver.
217 if (req->buf == NULL)
218 req->buf = (void *)0xDEADBABE;
220 reinit_completion(&ffs->ep0req_completion);
222 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
223 if (unlikely(ret < 0))
226 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
228 usb_ep_dequeue(ffs->gadget->ep0, req);
232 ffs->setup_state = FFS_NO_SETUP;
233 return req->status ? req->status : req->actual;
236 static int __ffs_ep0_stall(struct ffs_data *ffs)
238 if (ffs->ev.can_stall) {
239 pr_vdebug("ep0 stall\n");
240 usb_ep_set_halt(ffs->gadget->ep0);
241 ffs->setup_state = FFS_NO_SETUP;
244 pr_debug("bogus ep0 stall!\n");
249 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
250 size_t len, loff_t *ptr)
252 struct ffs_data *ffs = file->private_data;
258 /* Fast check if setup was canceled */
259 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
263 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
264 if (unlikely(ret < 0))
268 switch (ffs->state) {
269 case FFS_READ_DESCRIPTORS:
270 case FFS_READ_STRINGS:
272 if (unlikely(len < 16)) {
277 data = ffs_prepare_buffer(buf, len);
284 if (ffs->state == FFS_READ_DESCRIPTORS) {
285 pr_info("read descriptors\n");
286 ret = __ffs_data_got_descs(ffs, data, len);
287 if (unlikely(ret < 0))
290 ffs->state = FFS_READ_STRINGS;
293 pr_info("read strings\n");
294 ret = __ffs_data_got_strings(ffs, data, len);
295 if (unlikely(ret < 0))
298 ret = ffs_epfiles_create(ffs);
300 ffs->state = FFS_CLOSING;
304 ffs->state = FFS_ACTIVE;
305 mutex_unlock(&ffs->mutex);
307 ret = ffs_ready(ffs);
308 if (unlikely(ret < 0)) {
309 ffs->state = FFS_CLOSING;
313 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
321 * We're called from user space, we can use _irq
322 * rather then _irqsave
324 spin_lock_irq(&ffs->ev.waitq.lock);
325 switch (ffs_setup_state_clear_cancelled(ffs)) {
326 case FFS_SETUP_CANCELLED:
334 case FFS_SETUP_PENDING:
338 /* FFS_SETUP_PENDING */
339 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
340 spin_unlock_irq(&ffs->ev.waitq.lock);
341 ret = __ffs_ep0_stall(ffs);
345 /* FFS_SETUP_PENDING and not stall */
346 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
348 spin_unlock_irq(&ffs->ev.waitq.lock);
350 data = ffs_prepare_buffer(buf, len);
356 spin_lock_irq(&ffs->ev.waitq.lock);
359 * We are guaranteed to be still in FFS_ACTIVE state
360 * but the state of setup could have changed from
361 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
362 * to check for that. If that happened we copied data
363 * from user space in vain but it's unlikely.
365 * For sure we are not in FFS_NO_SETUP since this is
366 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
367 * transition can be performed and it's protected by
370 if (ffs_setup_state_clear_cancelled(ffs) ==
371 FFS_SETUP_CANCELLED) {
374 spin_unlock_irq(&ffs->ev.waitq.lock);
376 /* unlocks spinlock */
377 ret = __ffs_ep0_queue_wait(ffs, data, len);
387 mutex_unlock(&ffs->mutex);
391 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
395 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
398 struct usb_functionfs_event events[n];
401 memset(events, 0, sizeof events);
404 events[i].type = ffs->ev.types[i];
405 if (events[i].type == FUNCTIONFS_SETUP) {
406 events[i].u.setup = ffs->ev.setup;
407 ffs->setup_state = FFS_SETUP_PENDING;
411 if (n < ffs->ev.count) {
413 memmove(ffs->ev.types, ffs->ev.types + n,
414 ffs->ev.count * sizeof *ffs->ev.types);
419 spin_unlock_irq(&ffs->ev.waitq.lock);
420 mutex_unlock(&ffs->mutex);
422 return unlikely(__copy_to_user(buf, events, sizeof events))
423 ? -EFAULT : sizeof events;
426 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
427 size_t len, loff_t *ptr)
429 struct ffs_data *ffs = file->private_data;
436 /* Fast check if setup was canceled */
437 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
441 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
442 if (unlikely(ret < 0))
446 if (ffs->state != FFS_ACTIVE) {
452 * We're called from user space, we can use _irq rather then
455 spin_lock_irq(&ffs->ev.waitq.lock);
457 switch (ffs_setup_state_clear_cancelled(ffs)) {
458 case FFS_SETUP_CANCELLED:
463 n = len / sizeof(struct usb_functionfs_event);
469 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
474 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
480 return __ffs_ep0_read_events(ffs, buf,
481 min(n, (size_t)ffs->ev.count));
483 case FFS_SETUP_PENDING:
484 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
485 spin_unlock_irq(&ffs->ev.waitq.lock);
486 ret = __ffs_ep0_stall(ffs);
490 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
492 spin_unlock_irq(&ffs->ev.waitq.lock);
495 data = kmalloc(len, GFP_KERNEL);
496 if (unlikely(!data)) {
502 spin_lock_irq(&ffs->ev.waitq.lock);
504 /* See ffs_ep0_write() */
505 if (ffs_setup_state_clear_cancelled(ffs) ==
506 FFS_SETUP_CANCELLED) {
511 /* unlocks spinlock */
512 ret = __ffs_ep0_queue_wait(ffs, data, len);
513 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
522 spin_unlock_irq(&ffs->ev.waitq.lock);
524 mutex_unlock(&ffs->mutex);
529 static int ffs_ep0_open(struct inode *inode, struct file *file)
531 struct ffs_data *ffs = inode->i_private;
535 if (unlikely(ffs->state == FFS_CLOSING))
538 file->private_data = ffs;
539 ffs_data_opened(ffs);
544 static int ffs_ep0_release(struct inode *inode, struct file *file)
546 struct ffs_data *ffs = file->private_data;
550 ffs_data_closed(ffs);
555 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
557 struct ffs_data *ffs = file->private_data;
558 struct usb_gadget *gadget = ffs->gadget;
563 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
564 struct ffs_function *func = ffs->func;
565 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
566 } else if (gadget && gadget->ops->ioctl) {
567 ret = gadget->ops->ioctl(gadget, code, value);
575 static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
577 struct ffs_data *ffs = file->private_data;
578 unsigned int mask = POLLWRNORM;
581 poll_wait(file, &ffs->ev.waitq, wait);
583 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
584 if (unlikely(ret < 0))
587 switch (ffs->state) {
588 case FFS_READ_DESCRIPTORS:
589 case FFS_READ_STRINGS:
594 switch (ffs->setup_state) {
600 case FFS_SETUP_PENDING:
601 case FFS_SETUP_CANCELLED:
602 mask |= (POLLIN | POLLOUT);
609 mutex_unlock(&ffs->mutex);
614 static const struct file_operations ffs_ep0_operations = {
617 .open = ffs_ep0_open,
618 .write = ffs_ep0_write,
619 .read = ffs_ep0_read,
620 .release = ffs_ep0_release,
621 .unlocked_ioctl = ffs_ep0_ioctl,
622 .poll = ffs_ep0_poll,
626 /* "Normal" endpoints operations ********************************************/
628 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
631 if (likely(req->context)) {
632 struct ffs_ep *ep = _ep->driver_data;
633 ep->status = req->status ? req->status : req->actual;
634 complete(req->context);
638 static ssize_t ffs_epfile_io(struct file *file,
639 char __user *buf, size_t len, int read)
641 struct ffs_epfile *epfile = file->private_data;
642 struct usb_gadget *gadget = epfile->ffs->gadget;
645 ssize_t ret, data_len;
648 /* Are we still active? */
649 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
654 /* Wait for endpoint to be enabled */
657 if (file->f_flags & O_NONBLOCK) {
662 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
670 halt = !read == !epfile->in;
671 if (halt && epfile->isoc) {
676 /* Allocate & copy */
679 * Controller may require buffer size to be aligned to
680 * maxpacketsize of an out endpoint.
682 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
684 data = kmalloc(data_len, GFP_KERNEL);
688 if (!read && unlikely(copy_from_user(data, buf, len))) {
694 /* We will be using request */
695 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
699 spin_lock_irq(&epfile->ffs->eps_lock);
701 if (epfile->ep != ep) {
702 /* In the meantime, endpoint got disabled or changed. */
704 spin_unlock_irq(&epfile->ffs->eps_lock);
707 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
708 usb_ep_set_halt(ep->ep);
709 spin_unlock_irq(&epfile->ffs->eps_lock);
712 /* Fire the request */
713 DECLARE_COMPLETION_ONSTACK(done);
715 struct usb_request *req = ep->req;
716 req->context = &done;
717 req->complete = ffs_epfile_io_complete;
719 req->length = data_len;
721 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
723 spin_unlock_irq(&epfile->ffs->eps_lock);
725 if (unlikely(ret < 0)) {
727 } else if (unlikely(wait_for_completion_interruptible(&done))) {
729 usb_ep_dequeue(ep->ep, req);
732 * XXX We may end up silently droping data here.
733 * Since data_len (i.e. req->length) may be bigger
734 * than len (after being rounded up to maxpacketsize),
735 * we may end up with more data then user space has
739 if (read && ret > 0 &&
740 unlikely(copy_to_user(buf, data,
741 min_t(size_t, ret, len))))
746 mutex_unlock(&epfile->mutex);
753 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
758 return ffs_epfile_io(file, (char __user *)buf, len, 0);
762 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
766 return ffs_epfile_io(file, buf, len, 1);
770 ffs_epfile_open(struct inode *inode, struct file *file)
772 struct ffs_epfile *epfile = inode->i_private;
776 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
779 file->private_data = epfile;
780 ffs_data_opened(epfile->ffs);
786 ffs_epfile_release(struct inode *inode, struct file *file)
788 struct ffs_epfile *epfile = inode->i_private;
792 ffs_data_closed(epfile->ffs);
797 static long ffs_epfile_ioctl(struct file *file, unsigned code,
800 struct ffs_epfile *epfile = file->private_data;
805 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
808 spin_lock_irq(&epfile->ffs->eps_lock);
809 if (likely(epfile->ep)) {
811 case FUNCTIONFS_FIFO_STATUS:
812 ret = usb_ep_fifo_status(epfile->ep->ep);
814 case FUNCTIONFS_FIFO_FLUSH:
815 usb_ep_fifo_flush(epfile->ep->ep);
818 case FUNCTIONFS_CLEAR_HALT:
819 ret = usb_ep_clear_halt(epfile->ep->ep);
821 case FUNCTIONFS_ENDPOINT_REVMAP:
822 ret = epfile->ep->num;
830 spin_unlock_irq(&epfile->ffs->eps_lock);
835 static const struct file_operations ffs_epfile_operations = {
838 .open = ffs_epfile_open,
839 .write = ffs_epfile_write,
840 .read = ffs_epfile_read,
841 .release = ffs_epfile_release,
842 .unlocked_ioctl = ffs_epfile_ioctl,
846 /* File system and super block operations ***********************************/
849 * Mounting the file system creates a controller file, used first for
850 * function configuration then later for event monitoring.
853 static struct inode *__must_check
854 ffs_sb_make_inode(struct super_block *sb, void *data,
855 const struct file_operations *fops,
856 const struct inode_operations *iops,
857 struct ffs_file_perms *perms)
863 inode = new_inode(sb);
866 struct timespec current_time = CURRENT_TIME;
868 inode->i_ino = get_next_ino();
869 inode->i_mode = perms->mode;
870 inode->i_uid = perms->uid;
871 inode->i_gid = perms->gid;
872 inode->i_atime = current_time;
873 inode->i_mtime = current_time;
874 inode->i_ctime = current_time;
875 inode->i_private = data;
885 /* Create "regular" file */
886 static struct inode *ffs_sb_create_file(struct super_block *sb,
887 const char *name, void *data,
888 const struct file_operations *fops,
889 struct dentry **dentry_p)
891 struct ffs_data *ffs = sb->s_fs_info;
892 struct dentry *dentry;
897 dentry = d_alloc_name(sb->s_root, name);
898 if (unlikely(!dentry))
901 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
902 if (unlikely(!inode)) {
907 d_add(dentry, inode);
915 static const struct super_operations ffs_sb_operations = {
916 .statfs = simple_statfs,
917 .drop_inode = generic_delete_inode,
920 struct ffs_sb_fill_data {
921 struct ffs_file_perms perms;
923 const char *dev_name;
924 struct ffs_data *ffs_data;
927 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
929 struct ffs_sb_fill_data *data = _data;
931 struct ffs_data *ffs = data->ffs_data;
936 data->ffs_data = NULL;
938 sb->s_blocksize = PAGE_CACHE_SIZE;
939 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
940 sb->s_magic = FUNCTIONFS_MAGIC;
941 sb->s_op = &ffs_sb_operations;
945 data->perms.mode = data->root_mode;
946 inode = ffs_sb_make_inode(sb, NULL,
947 &simple_dir_operations,
948 &simple_dir_inode_operations,
950 sb->s_root = d_make_root(inode);
951 if (unlikely(!sb->s_root))
955 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
956 &ffs_ep0_operations, NULL)))
962 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
974 comma = strchr(opts, ',');
979 eq = strchr(opts, '=');
981 pr_err("'=' missing in %s\n", opts);
987 if (kstrtoul(eq + 1, 0, &value)) {
988 pr_err("%s: invalid value: %s\n", opts, eq + 1);
992 /* Interpret option */
995 if (!memcmp(opts, "rmode", 5))
996 data->root_mode = (value & 0555) | S_IFDIR;
997 else if (!memcmp(opts, "fmode", 5))
998 data->perms.mode = (value & 0666) | S_IFREG;
1004 if (!memcmp(opts, "mode", 4)) {
1005 data->root_mode = (value & 0555) | S_IFDIR;
1006 data->perms.mode = (value & 0666) | S_IFREG;
1013 if (!memcmp(opts, "uid", 3)) {
1014 data->perms.uid = make_kuid(current_user_ns(), value);
1015 if (!uid_valid(data->perms.uid)) {
1016 pr_err("%s: unmapped value: %lu\n", opts, value);
1019 } else if (!memcmp(opts, "gid", 3)) {
1020 data->perms.gid = make_kgid(current_user_ns(), value);
1021 if (!gid_valid(data->perms.gid)) {
1022 pr_err("%s: unmapped value: %lu\n", opts, value);
1032 pr_err("%s: invalid option\n", opts);
1036 /* Next iteration */
1045 /* "mount -t functionfs dev_name /dev/function" ends up here */
1047 static struct dentry *
1048 ffs_fs_mount(struct file_system_type *t, int flags,
1049 const char *dev_name, void *opts)
1051 struct ffs_sb_fill_data data = {
1053 .mode = S_IFREG | 0600,
1054 .uid = GLOBAL_ROOT_UID,
1055 .gid = GLOBAL_ROOT_GID,
1057 .root_mode = S_IFDIR | 0500,
1062 struct ffs_data *ffs;
1066 ret = ffs_fs_parse_opts(&data, opts);
1067 if (unlikely(ret < 0))
1068 return ERR_PTR(ret);
1070 ffs = ffs_data_new();
1072 return ERR_PTR(-ENOMEM);
1073 ffs->file_perms = data.perms;
1075 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1076 if (unlikely(!ffs->dev_name)) {
1078 return ERR_PTR(-ENOMEM);
1081 ffs_dev = ffs_acquire_dev(dev_name);
1082 if (IS_ERR(ffs_dev)) {
1084 return ERR_CAST(ffs_dev);
1086 ffs->private_data = ffs_dev;
1087 data.ffs_data = ffs;
1089 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1090 if (IS_ERR(rv) && data.ffs_data) {
1091 ffs_release_dev(data.ffs_data);
1092 ffs_data_put(data.ffs_data);
1098 ffs_fs_kill_sb(struct super_block *sb)
1102 kill_litter_super(sb);
1103 if (sb->s_fs_info) {
1104 ffs_release_dev(sb->s_fs_info);
1105 ffs_data_put(sb->s_fs_info);
1109 static struct file_system_type ffs_fs_type = {
1110 .owner = THIS_MODULE,
1111 .name = "functionfs",
1112 .mount = ffs_fs_mount,
1113 .kill_sb = ffs_fs_kill_sb,
1115 MODULE_ALIAS_FS("functionfs");
1118 /* Driver's main init/cleanup functions *************************************/
1120 static int functionfs_init(void)
1126 ret = register_filesystem(&ffs_fs_type);
1128 pr_info("file system registered\n");
1130 pr_err("failed registering file system (%d)\n", ret);
1135 static void functionfs_cleanup(void)
1139 pr_info("unloading\n");
1140 unregister_filesystem(&ffs_fs_type);
1144 /* ffs_data and ffs_function construction and destruction code **************/
1146 static void ffs_data_clear(struct ffs_data *ffs);
1147 static void ffs_data_reset(struct ffs_data *ffs);
1149 static void ffs_data_get(struct ffs_data *ffs)
1153 atomic_inc(&ffs->ref);
1156 static void ffs_data_opened(struct ffs_data *ffs)
1160 atomic_inc(&ffs->ref);
1161 atomic_inc(&ffs->opened);
1164 static void ffs_data_put(struct ffs_data *ffs)
1168 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1169 pr_info("%s(): freeing\n", __func__);
1170 ffs_data_clear(ffs);
1171 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1172 waitqueue_active(&ffs->ep0req_completion.wait));
1173 kfree(ffs->dev_name);
1178 static void ffs_data_closed(struct ffs_data *ffs)
1182 if (atomic_dec_and_test(&ffs->opened)) {
1183 ffs->state = FFS_CLOSING;
1184 ffs_data_reset(ffs);
1190 static struct ffs_data *ffs_data_new(void)
1192 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1198 atomic_set(&ffs->ref, 1);
1199 atomic_set(&ffs->opened, 0);
1200 ffs->state = FFS_READ_DESCRIPTORS;
1201 mutex_init(&ffs->mutex);
1202 spin_lock_init(&ffs->eps_lock);
1203 init_waitqueue_head(&ffs->ev.waitq);
1204 init_completion(&ffs->ep0req_completion);
1206 /* XXX REVISIT need to update it in some places, or do we? */
1207 ffs->ev.can_stall = 1;
1212 static void ffs_data_clear(struct ffs_data *ffs)
1216 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1219 BUG_ON(ffs->gadget);
1222 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1224 kfree(ffs->raw_descs);
1225 kfree(ffs->raw_strings);
1226 kfree(ffs->stringtabs);
1229 static void ffs_data_reset(struct ffs_data *ffs)
1233 ffs_data_clear(ffs);
1235 ffs->epfiles = NULL;
1236 ffs->raw_descs = NULL;
1237 ffs->raw_strings = NULL;
1238 ffs->stringtabs = NULL;
1240 ffs->raw_descs_length = 0;
1241 ffs->raw_fs_descs_length = 0;
1242 ffs->fs_descs_count = 0;
1243 ffs->hs_descs_count = 0;
1245 ffs->strings_count = 0;
1246 ffs->interfaces_count = 0;
1251 ffs->state = FFS_READ_DESCRIPTORS;
1252 ffs->setup_state = FFS_NO_SETUP;
1257 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1259 struct usb_gadget_strings **lang;
1264 if (WARN_ON(ffs->state != FFS_ACTIVE
1265 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1268 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1269 if (unlikely(first_id < 0))
1272 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1273 if (unlikely(!ffs->ep0req))
1275 ffs->ep0req->complete = ffs_ep0_complete;
1276 ffs->ep0req->context = ffs;
1278 lang = ffs->stringtabs;
1279 for (lang = ffs->stringtabs; *lang; ++lang) {
1280 struct usb_string *str = (*lang)->strings;
1282 for (; str->s; ++id, ++str)
1286 ffs->gadget = cdev->gadget;
1291 static void functionfs_unbind(struct ffs_data *ffs)
1295 if (!WARN_ON(!ffs->gadget)) {
1296 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1299 clear_bit(FFS_FL_BOUND, &ffs->flags);
1304 static int ffs_epfiles_create(struct ffs_data *ffs)
1306 struct ffs_epfile *epfile, *epfiles;
1311 count = ffs->eps_count;
1312 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1317 for (i = 1; i <= count; ++i, ++epfile) {
1319 mutex_init(&epfile->mutex);
1320 init_waitqueue_head(&epfile->wait);
1321 sprintf(epfiles->name, "ep%u", i);
1322 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1323 &ffs_epfile_operations,
1324 &epfile->dentry))) {
1325 ffs_epfiles_destroy(epfiles, i - 1);
1330 ffs->epfiles = epfiles;
1334 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1336 struct ffs_epfile *epfile = epfiles;
1340 for (; count; --count, ++epfile) {
1341 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1342 waitqueue_active(&epfile->wait));
1343 if (epfile->dentry) {
1344 d_delete(epfile->dentry);
1345 dput(epfile->dentry);
1346 epfile->dentry = NULL;
1354 static void ffs_func_eps_disable(struct ffs_function *func)
1356 struct ffs_ep *ep = func->eps;
1357 struct ffs_epfile *epfile = func->ffs->epfiles;
1358 unsigned count = func->ffs->eps_count;
1359 unsigned long flags;
1361 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1363 /* pending requests get nuked */
1365 usb_ep_disable(ep->ep);
1371 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1374 static int ffs_func_eps_enable(struct ffs_function *func)
1376 struct ffs_data *ffs = func->ffs;
1377 struct ffs_ep *ep = func->eps;
1378 struct ffs_epfile *epfile = ffs->epfiles;
1379 unsigned count = ffs->eps_count;
1380 unsigned long flags;
1383 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1385 struct usb_endpoint_descriptor *ds;
1386 ds = ep->descs[ep->descs[1] ? 1 : 0];
1388 ep->ep->driver_data = ep;
1390 ret = usb_ep_enable(ep->ep);
1393 epfile->in = usb_endpoint_dir_in(ds);
1394 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1399 wake_up(&epfile->wait);
1404 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1410 /* Parsing and building descriptors and strings *****************************/
1413 * This validates if data pointed by data is a valid USB descriptor as
1414 * well as record how many interfaces, endpoints and strings are
1415 * required by given configuration. Returns address after the
1416 * descriptor or NULL if data is invalid.
1419 enum ffs_entity_type {
1420 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1423 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1425 struct usb_descriptor_header *desc,
1428 static int __must_check ffs_do_desc(char *data, unsigned len,
1429 ffs_entity_callback entity, void *priv)
1431 struct usb_descriptor_header *_ds = (void *)data;
1437 /* At least two bytes are required: length and type */
1439 pr_vdebug("descriptor too short\n");
1443 /* If we have at least as many bytes as the descriptor takes? */
1444 length = _ds->bLength;
1446 pr_vdebug("descriptor longer then available data\n");
1450 #define __entity_check_INTERFACE(val) 1
1451 #define __entity_check_STRING(val) (val)
1452 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1453 #define __entity(type, val) do { \
1454 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1455 if (unlikely(!__entity_check_ ##type(val))) { \
1456 pr_vdebug("invalid entity's value\n"); \
1459 ret = entity(FFS_ ##type, &val, _ds, priv); \
1460 if (unlikely(ret < 0)) { \
1461 pr_debug("entity " #type "(%02x); ret = %d\n", \
1467 /* Parse descriptor depending on type. */
1468 switch (_ds->bDescriptorType) {
1472 case USB_DT_DEVICE_QUALIFIER:
1473 /* function can't have any of those */
1474 pr_vdebug("descriptor reserved for gadget: %d\n",
1475 _ds->bDescriptorType);
1478 case USB_DT_INTERFACE: {
1479 struct usb_interface_descriptor *ds = (void *)_ds;
1480 pr_vdebug("interface descriptor\n");
1481 if (length != sizeof *ds)
1484 __entity(INTERFACE, ds->bInterfaceNumber);
1486 __entity(STRING, ds->iInterface);
1490 case USB_DT_ENDPOINT: {
1491 struct usb_endpoint_descriptor *ds = (void *)_ds;
1492 pr_vdebug("endpoint descriptor\n");
1493 if (length != USB_DT_ENDPOINT_SIZE &&
1494 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1496 __entity(ENDPOINT, ds->bEndpointAddress);
1501 pr_vdebug("hid descriptor\n");
1502 if (length != sizeof(struct hid_descriptor))
1507 if (length != sizeof(struct usb_otg_descriptor))
1511 case USB_DT_INTERFACE_ASSOCIATION: {
1512 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1513 pr_vdebug("interface association descriptor\n");
1514 if (length != sizeof *ds)
1517 __entity(STRING, ds->iFunction);
1521 case USB_DT_OTHER_SPEED_CONFIG:
1522 case USB_DT_INTERFACE_POWER:
1524 case USB_DT_SECURITY:
1525 case USB_DT_CS_RADIO_CONTROL:
1527 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1531 /* We should never be here */
1532 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1536 pr_vdebug("invalid length: %d (descriptor %d)\n",
1537 _ds->bLength, _ds->bDescriptorType);
1542 #undef __entity_check_DESCRIPTOR
1543 #undef __entity_check_INTERFACE
1544 #undef __entity_check_STRING
1545 #undef __entity_check_ENDPOINT
1550 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1551 ffs_entity_callback entity, void *priv)
1553 const unsigned _len = len;
1554 unsigned long num = 0;
1564 /* Record "descriptor" entity */
1565 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1566 if (unlikely(ret < 0)) {
1567 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1575 ret = ffs_do_desc(data, len, entity, priv);
1576 if (unlikely(ret < 0)) {
1577 pr_debug("%s returns %d\n", __func__, ret);
1587 static int __ffs_data_do_entity(enum ffs_entity_type type,
1588 u8 *valuep, struct usb_descriptor_header *desc,
1591 struct ffs_data *ffs = priv;
1596 case FFS_DESCRIPTOR:
1601 * Interfaces are indexed from zero so if we
1602 * encountered interface "n" then there are at least
1605 if (*valuep >= ffs->interfaces_count)
1606 ffs->interfaces_count = *valuep + 1;
1611 * Strings are indexed from 1 (0 is magic ;) reserved
1612 * for languages list or some such)
1614 if (*valuep > ffs->strings_count)
1615 ffs->strings_count = *valuep;
1619 /* Endpoints are indexed from 1 as well. */
1620 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1621 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1628 static int __ffs_data_got_descs(struct ffs_data *ffs,
1629 char *const _data, size_t len)
1631 unsigned fs_count, hs_count;
1632 int fs_len, ret = -EINVAL;
1637 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1638 get_unaligned_le32(data + 4) != len))
1640 fs_count = get_unaligned_le32(data + 8);
1641 hs_count = get_unaligned_le32(data + 12);
1643 if (!fs_count && !hs_count)
1649 if (likely(fs_count)) {
1650 fs_len = ffs_do_descs(fs_count, data, len,
1651 __ffs_data_do_entity, ffs);
1652 if (unlikely(fs_len < 0)) {
1663 if (likely(hs_count)) {
1664 ret = ffs_do_descs(hs_count, data, len,
1665 __ffs_data_do_entity, ffs);
1666 if (unlikely(ret < 0))
1672 if (unlikely(len != ret))
1675 ffs->raw_fs_descs_length = fs_len;
1676 ffs->raw_descs_length = fs_len + ret;
1677 ffs->raw_descs = _data;
1678 ffs->fs_descs_count = fs_count;
1679 ffs->hs_descs_count = hs_count;
1690 static int __ffs_data_got_strings(struct ffs_data *ffs,
1691 char *const _data, size_t len)
1693 u32 str_count, needed_count, lang_count;
1694 struct usb_gadget_strings **stringtabs, *t;
1695 struct usb_string *strings, *s;
1696 const char *data = _data;
1700 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1701 get_unaligned_le32(data + 4) != len))
1703 str_count = get_unaligned_le32(data + 8);
1704 lang_count = get_unaligned_le32(data + 12);
1706 /* if one is zero the other must be zero */
1707 if (unlikely(!str_count != !lang_count))
1710 /* Do we have at least as many strings as descriptors need? */
1711 needed_count = ffs->strings_count;
1712 if (unlikely(str_count < needed_count))
1716 * If we don't need any strings just return and free all
1719 if (!needed_count) {
1724 /* Allocate everything in one chunk so there's less maintenance. */
1728 vla_item(d, struct usb_gadget_strings *, stringtabs,
1730 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
1731 vla_item(d, struct usb_string, strings,
1732 lang_count*(needed_count+1));
1734 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
1736 if (unlikely(!vlabuf)) {
1741 /* Initialize the VLA pointers */
1742 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1743 t = vla_ptr(vlabuf, d, stringtab);
1746 *stringtabs++ = t++;
1750 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1751 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1752 t = vla_ptr(vlabuf, d, stringtab);
1753 s = vla_ptr(vlabuf, d, strings);
1757 /* For each language */
1761 do { /* lang_count > 0 so we can use do-while */
1762 unsigned needed = needed_count;
1764 if (unlikely(len < 3))
1766 t->language = get_unaligned_le16(data);
1773 /* For each string */
1774 do { /* str_count > 0 so we can use do-while */
1775 size_t length = strnlen(data, len);
1777 if (unlikely(length == len))
1781 * User may provide more strings then we need,
1782 * if that's the case we simply ignore the
1785 if (likely(needed)) {
1787 * s->id will be set while adding
1788 * function to configuration so for
1789 * now just leave garbage here.
1798 } while (--str_count);
1800 s->id = 0; /* terminator */
1804 } while (--lang_count);
1806 /* Some garbage left? */
1811 ffs->stringtabs = stringtabs;
1812 ffs->raw_strings = _data;
1824 /* Events handling and management *******************************************/
1826 static void __ffs_event_add(struct ffs_data *ffs,
1827 enum usb_functionfs_event_type type)
1829 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1833 * Abort any unhandled setup
1835 * We do not need to worry about some cmpxchg() changing value
1836 * of ffs->setup_state without holding the lock because when
1837 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1838 * the source does nothing.
1840 if (ffs->setup_state == FFS_SETUP_PENDING)
1841 ffs->setup_state = FFS_SETUP_CANCELLED;
1844 case FUNCTIONFS_RESUME:
1845 rem_type2 = FUNCTIONFS_SUSPEND;
1847 case FUNCTIONFS_SUSPEND:
1848 case FUNCTIONFS_SETUP:
1850 /* Discard all similar events */
1853 case FUNCTIONFS_BIND:
1854 case FUNCTIONFS_UNBIND:
1855 case FUNCTIONFS_DISABLE:
1856 case FUNCTIONFS_ENABLE:
1857 /* Discard everything other then power management. */
1858 rem_type1 = FUNCTIONFS_SUSPEND;
1859 rem_type2 = FUNCTIONFS_RESUME;
1868 u8 *ev = ffs->ev.types, *out = ev;
1869 unsigned n = ffs->ev.count;
1870 for (; n; --n, ++ev)
1871 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
1874 pr_vdebug("purging event %d\n", *ev);
1875 ffs->ev.count = out - ffs->ev.types;
1878 pr_vdebug("adding event %d\n", type);
1879 ffs->ev.types[ffs->ev.count++] = type;
1880 wake_up_locked(&ffs->ev.waitq);
1883 static void ffs_event_add(struct ffs_data *ffs,
1884 enum usb_functionfs_event_type type)
1886 unsigned long flags;
1887 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
1888 __ffs_event_add(ffs, type);
1889 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
1893 /* Bind/unbind USB function hooks *******************************************/
1895 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1896 struct usb_descriptor_header *desc,
1899 struct usb_endpoint_descriptor *ds = (void *)desc;
1900 struct ffs_function *func = priv;
1901 struct ffs_ep *ffs_ep;
1904 * If hs_descriptors is not NULL then we are reading hs
1907 const int isHS = func->function.hs_descriptors != NULL;
1910 if (type != FFS_DESCRIPTOR)
1914 func->function.hs_descriptors[(long)valuep] = desc;
1916 func->function.fs_descriptors[(long)valuep] = desc;
1918 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1921 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
1922 ffs_ep = func->eps + idx;
1924 if (unlikely(ffs_ep->descs[isHS])) {
1925 pr_vdebug("two %sspeed descriptors for EP %d\n",
1926 isHS ? "high" : "full",
1927 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1930 ffs_ep->descs[isHS] = ds;
1932 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
1934 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
1935 if (!ds->wMaxPacketSize)
1936 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
1938 struct usb_request *req;
1941 pr_vdebug("autoconfig\n");
1942 ep = usb_ep_autoconfig(func->gadget, ds);
1945 ep->driver_data = func->eps + idx;
1947 req = usb_ep_alloc_request(ep, GFP_KERNEL);
1953 func->eps_revmap[ds->bEndpointAddress &
1954 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
1956 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
1961 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
1962 struct usb_descriptor_header *desc,
1965 struct ffs_function *func = priv;
1971 case FFS_DESCRIPTOR:
1972 /* Handled in previous pass by __ffs_func_bind_do_descs() */
1977 if (func->interfaces_nums[idx] < 0) {
1978 int id = usb_interface_id(func->conf, &func->function);
1979 if (unlikely(id < 0))
1981 func->interfaces_nums[idx] = id;
1983 newValue = func->interfaces_nums[idx];
1987 /* String' IDs are allocated when fsf_data is bound to cdev */
1988 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
1993 * USB_DT_ENDPOINT are handled in
1994 * __ffs_func_bind_do_descs().
1996 if (desc->bDescriptorType == USB_DT_ENDPOINT)
1999 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2000 if (unlikely(!func->eps[idx].ep))
2004 struct usb_endpoint_descriptor **descs;
2005 descs = func->eps[idx].descs;
2006 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2011 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2016 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2017 struct usb_configuration *c)
2019 struct ffs_function *func = ffs_func_from_usb(f);
2020 struct f_fs_opts *ffs_opts =
2021 container_of(f->fi, struct f_fs_opts, func_inst);
2027 * Legacy gadget triggers binding in functionfs_ready_callback,
2028 * which already uses locking; taking the same lock here would
2031 * Configfs-enabled gadgets however do need ffs_dev_lock.
2033 if (!ffs_opts->no_configfs)
2035 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2036 func->ffs = ffs_opts->dev->ffs_data;
2037 if (!ffs_opts->no_configfs)
2040 return ERR_PTR(ret);
2043 func->gadget = c->cdev->gadget;
2045 ffs_data_get(func->ffs);
2048 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2049 * configurations are bound in sequence with list_for_each_entry,
2050 * in each configuration its functions are bound in sequence
2051 * with list_for_each_entry, so we assume no race condition
2052 * with regard to ffs_opts->bound access
2054 if (!ffs_opts->refcnt) {
2055 ret = functionfs_bind(func->ffs, c->cdev);
2057 return ERR_PTR(ret);
2060 func->function.strings = func->ffs->stringtabs;
2065 static int _ffs_func_bind(struct usb_configuration *c,
2066 struct usb_function *f)
2068 struct ffs_function *func = ffs_func_from_usb(f);
2069 struct ffs_data *ffs = func->ffs;
2071 const int full = !!func->ffs->fs_descs_count;
2072 const int high = gadget_is_dualspeed(func->gadget) &&
2073 func->ffs->hs_descs_count;
2077 /* Make it a single chunk, less management later on */
2079 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2080 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2081 full ? ffs->fs_descs_count + 1 : 0);
2082 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2083 high ? ffs->hs_descs_count + 1 : 0);
2084 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2085 vla_item_with_sz(d, char, raw_descs,
2086 high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2091 /* Only high speed but not supported by gadget? */
2092 if (unlikely(!(full | high)))
2095 /* Allocate a single chunk, less management later on */
2096 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2097 if (unlikely(!vlabuf))
2101 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2102 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
2104 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2105 for (ret = ffs->eps_count; ret; --ret) {
2108 ptr = vla_ptr(vlabuf, d, eps);
2113 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2115 func->eps = vla_ptr(vlabuf, d, eps);
2116 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
2119 * Go through all the endpoint descriptors and allocate
2120 * endpoints first, so that later we can rewrite the endpoint
2121 * numbers without worrying that it may be described later on.
2124 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
2125 ret = ffs_do_descs(ffs->fs_descs_count,
2126 vla_ptr(vlabuf, d, raw_descs),
2128 __ffs_func_bind_do_descs, func);
2129 if (unlikely(ret < 0))
2136 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
2137 ret = ffs_do_descs(ffs->hs_descs_count,
2138 vla_ptr(vlabuf, d, raw_descs) + ret,
2139 d_raw_descs__sz - ret,
2140 __ffs_func_bind_do_descs, func);
2141 if (unlikely(ret < 0))
2146 * Now handle interface numbers allocation and interface and
2147 * endpoint numbers rewriting. We can do that in one go
2150 ret = ffs_do_descs(ffs->fs_descs_count +
2151 (high ? ffs->hs_descs_count : 0),
2152 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
2153 __ffs_func_bind_do_nums, func);
2154 if (unlikely(ret < 0))
2157 /* And we're done */
2158 ffs_event_add(ffs, FUNCTIONFS_BIND);
2162 /* XXX Do we need to release all claimed endpoints here? */
2166 static int ffs_func_bind(struct usb_configuration *c,
2167 struct usb_function *f)
2169 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2171 if (IS_ERR(ffs_opts))
2172 return PTR_ERR(ffs_opts);
2174 return _ffs_func_bind(c, f);
2178 /* Other USB function hooks *************************************************/
2180 static int ffs_func_set_alt(struct usb_function *f,
2181 unsigned interface, unsigned alt)
2183 struct ffs_function *func = ffs_func_from_usb(f);
2184 struct ffs_data *ffs = func->ffs;
2187 if (alt != (unsigned)-1) {
2188 intf = ffs_func_revmap_intf(func, interface);
2189 if (unlikely(intf < 0))
2194 ffs_func_eps_disable(ffs->func);
2196 if (ffs->state != FFS_ACTIVE)
2199 if (alt == (unsigned)-1) {
2201 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2206 ret = ffs_func_eps_enable(func);
2207 if (likely(ret >= 0))
2208 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2212 static void ffs_func_disable(struct usb_function *f)
2214 ffs_func_set_alt(f, 0, (unsigned)-1);
2217 static int ffs_func_setup(struct usb_function *f,
2218 const struct usb_ctrlrequest *creq)
2220 struct ffs_function *func = ffs_func_from_usb(f);
2221 struct ffs_data *ffs = func->ffs;
2222 unsigned long flags;
2227 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2228 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2229 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2230 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2231 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2234 * Most requests directed to interface go through here
2235 * (notable exceptions are set/get interface) so we need to
2236 * handle them. All other either handled by composite or
2237 * passed to usb_configuration->setup() (if one is set). No
2238 * matter, we will handle requests directed to endpoint here
2239 * as well (as it's straightforward) but what to do with any
2242 if (ffs->state != FFS_ACTIVE)
2245 switch (creq->bRequestType & USB_RECIP_MASK) {
2246 case USB_RECIP_INTERFACE:
2247 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2248 if (unlikely(ret < 0))
2252 case USB_RECIP_ENDPOINT:
2253 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2254 if (unlikely(ret < 0))
2262 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2263 ffs->ev.setup = *creq;
2264 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2265 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2266 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2271 static void ffs_func_suspend(struct usb_function *f)
2274 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2277 static void ffs_func_resume(struct usb_function *f)
2280 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2284 /* Endpoint and interface numbers reverse mapping ***************************/
2286 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2288 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2289 return num ? num : -EDOM;
2292 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2294 short *nums = func->interfaces_nums;
2295 unsigned count = func->ffs->interfaces_count;
2297 for (; count; --count, ++nums) {
2298 if (*nums >= 0 && *nums == intf)
2299 return nums - func->interfaces_nums;
2306 /* Devices management *******************************************************/
2308 static LIST_HEAD(ffs_devices);
2310 static struct ffs_dev *_ffs_do_find_dev(const char *name)
2312 struct ffs_dev *dev;
2314 list_for_each_entry(dev, &ffs_devices, entry) {
2315 if (!dev->name || !name)
2317 if (strcmp(dev->name, name) == 0)
2325 * ffs_lock must be taken by the caller of this function
2327 static struct ffs_dev *_ffs_get_single_dev(void)
2329 struct ffs_dev *dev;
2331 if (list_is_singular(&ffs_devices)) {
2332 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2341 * ffs_lock must be taken by the caller of this function
2343 static struct ffs_dev *_ffs_find_dev(const char *name)
2345 struct ffs_dev *dev;
2347 dev = _ffs_get_single_dev();
2351 return _ffs_do_find_dev(name);
2354 /* Configfs support *********************************************************/
2356 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2358 return container_of(to_config_group(item), struct f_fs_opts,
2362 static void ffs_attr_release(struct config_item *item)
2364 struct f_fs_opts *opts = to_ffs_opts(item);
2366 usb_put_function_instance(&opts->func_inst);
2369 static struct configfs_item_operations ffs_item_ops = {
2370 .release = ffs_attr_release,
2373 static struct config_item_type ffs_func_type = {
2374 .ct_item_ops = &ffs_item_ops,
2375 .ct_owner = THIS_MODULE,
2379 /* Function registration interface ******************************************/
2381 static void ffs_free_inst(struct usb_function_instance *f)
2383 struct f_fs_opts *opts;
2385 opts = to_f_fs_opts(f);
2387 _ffs_free_dev(opts->dev);
2392 #define MAX_INST_NAME_LEN 40
2394 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2396 struct f_fs_opts *opts;
2401 name_len = strlen(name) + 1;
2402 if (name_len > MAX_INST_NAME_LEN)
2403 return -ENAMETOOLONG;
2405 ptr = kstrndup(name, name_len, GFP_KERNEL);
2409 opts = to_f_fs_opts(fi);
2414 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2415 ret = _ffs_name_dev(opts->dev, ptr);
2421 opts->dev->name_allocated = true;
2430 static struct usb_function_instance *ffs_alloc_inst(void)
2432 struct f_fs_opts *opts;
2433 struct ffs_dev *dev;
2435 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2437 return ERR_PTR(-ENOMEM);
2439 opts->func_inst.set_inst_name = ffs_set_inst_name;
2440 opts->func_inst.free_func_inst = ffs_free_inst;
2442 dev = _ffs_alloc_dev();
2446 return ERR_CAST(dev);
2451 config_group_init_type_name(&opts->func_inst.group, "",
2453 return &opts->func_inst;
2456 static void ffs_free(struct usb_function *f)
2458 kfree(ffs_func_from_usb(f));
2461 static void ffs_func_unbind(struct usb_configuration *c,
2462 struct usb_function *f)
2464 struct ffs_function *func = ffs_func_from_usb(f);
2465 struct ffs_data *ffs = func->ffs;
2466 struct f_fs_opts *opts =
2467 container_of(f->fi, struct f_fs_opts, func_inst);
2468 struct ffs_ep *ep = func->eps;
2469 unsigned count = ffs->eps_count;
2470 unsigned long flags;
2473 if (ffs->func == func) {
2474 ffs_func_eps_disable(func);
2478 if (!--opts->refcnt)
2479 functionfs_unbind(ffs);
2481 /* cleanup after autoconfig */
2482 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2484 if (ep->ep && ep->req)
2485 usb_ep_free_request(ep->ep, ep->req);
2489 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2493 * eps, descriptors and interfaces_nums are allocated in the
2494 * same chunk so only one free is required.
2496 func->function.fs_descriptors = NULL;
2497 func->function.hs_descriptors = NULL;
2498 func->interfaces_nums = NULL;
2500 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2503 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2505 struct ffs_function *func;
2509 func = kzalloc(sizeof(*func), GFP_KERNEL);
2510 if (unlikely(!func))
2511 return ERR_PTR(-ENOMEM);
2513 func->function.name = "Function FS Gadget";
2515 func->function.bind = ffs_func_bind;
2516 func->function.unbind = ffs_func_unbind;
2517 func->function.set_alt = ffs_func_set_alt;
2518 func->function.disable = ffs_func_disable;
2519 func->function.setup = ffs_func_setup;
2520 func->function.suspend = ffs_func_suspend;
2521 func->function.resume = ffs_func_resume;
2522 func->function.free_func = ffs_free;
2524 return &func->function;
2528 * ffs_lock must be taken by the caller of this function
2530 static struct ffs_dev *_ffs_alloc_dev(void)
2532 struct ffs_dev *dev;
2535 if (_ffs_get_single_dev())
2536 return ERR_PTR(-EBUSY);
2538 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2540 return ERR_PTR(-ENOMEM);
2542 if (list_empty(&ffs_devices)) {
2543 ret = functionfs_init();
2546 return ERR_PTR(ret);
2550 list_add(&dev->entry, &ffs_devices);
2556 * ffs_lock must be taken by the caller of this function
2557 * The caller is responsible for "name" being available whenever f_fs needs it
2559 static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2561 struct ffs_dev *existing;
2563 existing = _ffs_do_find_dev(name);
2573 * The caller is responsible for "name" being available whenever f_fs needs it
2575 int ffs_name_dev(struct ffs_dev *dev, const char *name)
2580 ret = _ffs_name_dev(dev, name);
2585 EXPORT_SYMBOL(ffs_name_dev);
2587 int ffs_single_dev(struct ffs_dev *dev)
2594 if (!list_is_singular(&ffs_devices))
2602 EXPORT_SYMBOL(ffs_single_dev);
2605 * ffs_lock must be taken by the caller of this function
2607 static void _ffs_free_dev(struct ffs_dev *dev)
2609 list_del(&dev->entry);
2610 if (dev->name_allocated)
2613 if (list_empty(&ffs_devices))
2614 functionfs_cleanup();
2617 static void *ffs_acquire_dev(const char *dev_name)
2619 struct ffs_dev *ffs_dev;
2624 ffs_dev = _ffs_find_dev(dev_name);
2626 ffs_dev = ERR_PTR(-ENODEV);
2627 else if (ffs_dev->mounted)
2628 ffs_dev = ERR_PTR(-EBUSY);
2629 else if (ffs_dev->ffs_acquire_dev_callback &&
2630 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2631 ffs_dev = ERR_PTR(-ENODEV);
2633 ffs_dev->mounted = true;
2639 static void ffs_release_dev(struct ffs_data *ffs_data)
2641 struct ffs_dev *ffs_dev;
2646 ffs_dev = ffs_data->private_data;
2648 ffs_dev->mounted = false;
2650 if (ffs_dev->ffs_release_dev_callback)
2651 ffs_dev->ffs_release_dev_callback(ffs_dev);
2657 static int ffs_ready(struct ffs_data *ffs)
2659 struct ffs_dev *ffs_obj;
2665 ffs_obj = ffs->private_data;
2670 if (WARN_ON(ffs_obj->desc_ready)) {
2675 ffs_obj->desc_ready = true;
2676 ffs_obj->ffs_data = ffs;
2678 if (ffs_obj->ffs_ready_callback)
2679 ret = ffs_obj->ffs_ready_callback(ffs);
2686 static void ffs_closed(struct ffs_data *ffs)
2688 struct ffs_dev *ffs_obj;
2693 ffs_obj = ffs->private_data;
2697 ffs_obj->desc_ready = false;
2699 if (ffs_obj->ffs_closed_callback)
2700 ffs_obj->ffs_closed_callback(ffs);
2702 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
2703 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
2706 unregister_gadget_item(ffs_obj->opts->
2707 func_inst.group.cg_item.ci_parent->ci_parent);
2712 /* Misc helper functions ****************************************************/
2714 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2717 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2718 : mutex_lock_interruptible(mutex);
2721 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
2728 data = kmalloc(len, GFP_KERNEL);
2729 if (unlikely(!data))
2730 return ERR_PTR(-ENOMEM);
2732 if (unlikely(__copy_from_user(data, buf, len))) {
2734 return ERR_PTR(-EFAULT);
2737 pr_vdebug("Buffer from user space:\n");
2738 ffs_dump_mem("", data, len);
2743 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
2744 MODULE_LICENSE("GPL");
2745 MODULE_AUTHOR("Michal Nazarewicz");