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 <asm/unaligned.h>
27 #include <linux/usb/composite.h>
28 #include <linux/usb/functionfs.h>
31 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
34 /* Debugging ****************************************************************/
38 # define pr_vdebug pr_debug
39 #endif /* pr_vdebug */
40 # define ffs_dump_mem(prefix, ptr, len) \
41 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
44 # define pr_vdebug(...) do { } while (0)
45 #endif /* pr_vdebug */
46 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
47 #endif /* VERBOSE_DEBUG */
49 #define ENTER() pr_vdebug("%s()\n", __func__)
52 /* The data structure and setup file ****************************************/
56 * Waiting for descriptors and strings.
58 * In this state no open(2), read(2) or write(2) on epfiles
59 * may succeed (which should not be the problem as there
60 * should be no such files opened in the first place).
66 * We've got descriptors and strings. We are or have called
67 * functionfs_ready_callback(). functionfs_bind() may have
68 * been called but we don't know.
70 * This is the only state in which operations on epfiles may
76 * All endpoints have been closed. This state is also set if
77 * we encounter an unrecoverable error. The only
78 * unrecoverable error is situation when after reading strings
79 * from user space we fail to initialise epfiles or
80 * functionfs_ready_callback() returns with error (<0).
82 * In this state no open(2), read(2) or write(2) (both on ep0
83 * as well as epfile) may succeed (at this point epfiles are
84 * unlinked and all closed so this is not a problem; ep0 is
85 * also closed but ep0 file exists and so open(2) on ep0 must
92 enum ffs_setup_state {
93 /* There is no setup request pending. */
96 * User has read events and there was a setup request event
97 * there. The next read/write on ep0 will handle the
102 * There was event pending but before user space handled it
103 * some other event was introduced which canceled existing
104 * setup. If this state is set read/write on ep0 return
105 * -EIDRM. This state is only set when adding event.
116 struct usb_gadget *gadget;
119 * Protect access read/write operations, only one read/write
120 * at a time. As a consequence protects ep0req and company.
121 * While setup request is being processed (queued) this is
127 * Protect access to endpoint related structures (basically
128 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
134 * XXX REVISIT do we need our own request? Since we are not
135 * handling setup requests immediately user space may be so
136 * slow that another setup will be sent to the gadget but this
137 * time not to us but another function and then there could be
138 * a race. Is that the case? Or maybe we can use cdev->req
139 * after all, maybe we just need some spinlock for that?
141 struct usb_request *ep0req; /* P: mutex */
142 struct completion ep0req_completion; /* P: mutex */
143 int ep0req_status; /* P: mutex */
145 /* reference counter */
147 /* how many files are opened (EP0 and others) */
151 enum ffs_state state;
154 * Possible transitions:
155 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
156 * happens only in ep0 read which is P: mutex
157 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
158 * happens only in ep0 i/o which is P: mutex
159 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
160 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
162 enum ffs_setup_state setup_state;
164 #define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
171 unsigned short count;
172 /* XXX REVISIT need to update it in some places, or do we? */
173 unsigned short can_stall;
174 struct usb_ctrlrequest setup;
176 wait_queue_head_t waitq;
177 } ev; /* the whole structure, P: ev.waitq.lock */
181 #define FFS_FL_CALL_CLOSED_CALLBACK 0
182 #define FFS_FL_BOUND 1
184 /* Active function */
185 struct ffs_function *func;
188 * Device name, write once when file system is mounted.
189 * Intended for user to read if she wants.
191 const char *dev_name;
192 /* Private data for our user (ie. gadget). Managed by user. */
195 /* filled by __ffs_data_got_descs() */
197 * Real descriptors are 16 bytes after raw_descs (so you need
198 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
199 * first full speed descriptor). raw_descs_length and
200 * raw_fs_descs_length do not have those 16 bytes added.
202 const void *raw_descs;
203 unsigned raw_descs_length;
204 unsigned raw_fs_descs_length;
205 unsigned fs_descs_count;
206 unsigned hs_descs_count;
208 unsigned short strings_count;
209 unsigned short interfaces_count;
210 unsigned short eps_count;
211 unsigned short _pad1;
213 /* filled by __ffs_data_got_strings() */
214 /* ids in stringtabs are set in functionfs_bind() */
215 const void *raw_strings;
216 struct usb_gadget_strings **stringtabs;
219 * File system's super block, write once when file system is
222 struct super_block *sb;
224 /* File permissions, written once when fs is mounted */
225 struct ffs_file_perms {
232 * The endpoint files, filled by ffs_epfiles_create(),
233 * destroyed by ffs_epfiles_destroy().
235 struct ffs_epfile *epfiles;
238 /* Reference counter handling */
239 static void ffs_data_get(struct ffs_data *ffs);
240 static void ffs_data_put(struct ffs_data *ffs);
241 /* Creates new ffs_data object. */
242 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
244 /* Opened counter handling. */
245 static void ffs_data_opened(struct ffs_data *ffs);
246 static void ffs_data_closed(struct ffs_data *ffs);
248 /* Called with ffs->mutex held; take over ownership of data. */
249 static int __must_check
250 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
251 static int __must_check
252 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
255 /* The function structure ***************************************************/
259 struct ffs_function {
260 struct usb_configuration *conf;
261 struct usb_gadget *gadget;
262 struct ffs_data *ffs;
266 short *interfaces_nums;
268 struct usb_function function;
272 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
274 return container_of(f, struct ffs_function, function);
277 static void ffs_func_free(struct ffs_function *func);
279 static void ffs_func_eps_disable(struct ffs_function *func);
280 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
282 static int ffs_func_bind(struct usb_configuration *,
283 struct usb_function *);
284 static void ffs_func_unbind(struct usb_configuration *,
285 struct usb_function *);
286 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
287 static void ffs_func_disable(struct usb_function *);
288 static int ffs_func_setup(struct usb_function *,
289 const struct usb_ctrlrequest *);
290 static void ffs_func_suspend(struct usb_function *);
291 static void ffs_func_resume(struct usb_function *);
294 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
295 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
298 /* The endpoints structures *************************************************/
301 struct usb_ep *ep; /* P: ffs->eps_lock */
302 struct usb_request *req; /* P: epfile->mutex */
304 /* [0]: full speed, [1]: high speed */
305 struct usb_endpoint_descriptor *descs[2];
309 int status; /* P: epfile->mutex */
313 /* Protects ep->ep and ep->req. */
315 wait_queue_head_t wait;
317 struct ffs_data *ffs;
318 struct ffs_ep *ep; /* P: ffs->eps_lock */
320 struct dentry *dentry;
324 unsigned char in; /* P: ffs->eps_lock */
325 unsigned char isoc; /* P: ffs->eps_lock */
330 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
331 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
333 static struct inode *__must_check
334 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
335 const struct file_operations *fops,
336 struct dentry **dentry_p);
339 /* Misc helper functions ****************************************************/
341 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
342 __attribute__((warn_unused_result, nonnull));
343 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
344 __attribute__((warn_unused_result, nonnull));
347 /* Control file aka ep0 *****************************************************/
349 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
351 struct ffs_data *ffs = req->context;
353 complete_all(&ffs->ep0req_completion);
356 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
358 struct usb_request *req = ffs->ep0req;
361 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
363 spin_unlock_irq(&ffs->ev.waitq.lock);
369 * UDC layer requires to provide a buffer even for ZLP, but should
370 * not use it at all. Let's provide some poisoned pointer to catch
371 * possible bug in the driver.
373 if (req->buf == NULL)
374 req->buf = (void *)0xDEADBABE;
376 reinit_completion(&ffs->ep0req_completion);
378 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
379 if (unlikely(ret < 0))
382 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
384 usb_ep_dequeue(ffs->gadget->ep0, req);
388 ffs->setup_state = FFS_NO_SETUP;
389 return ffs->ep0req_status;
392 static int __ffs_ep0_stall(struct ffs_data *ffs)
394 if (ffs->ev.can_stall) {
395 pr_vdebug("ep0 stall\n");
396 usb_ep_set_halt(ffs->gadget->ep0);
397 ffs->setup_state = FFS_NO_SETUP;
400 pr_debug("bogus ep0 stall!\n");
405 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
406 size_t len, loff_t *ptr)
408 struct ffs_data *ffs = file->private_data;
414 /* Fast check if setup was canceled */
415 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
419 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
420 if (unlikely(ret < 0))
424 switch (ffs->state) {
425 case FFS_READ_DESCRIPTORS:
426 case FFS_READ_STRINGS:
428 if (unlikely(len < 16)) {
433 data = ffs_prepare_buffer(buf, len);
440 if (ffs->state == FFS_READ_DESCRIPTORS) {
441 pr_info("read descriptors\n");
442 ret = __ffs_data_got_descs(ffs, data, len);
443 if (unlikely(ret < 0))
446 ffs->state = FFS_READ_STRINGS;
449 pr_info("read strings\n");
450 ret = __ffs_data_got_strings(ffs, data, len);
451 if (unlikely(ret < 0))
454 ret = ffs_epfiles_create(ffs);
456 ffs->state = FFS_CLOSING;
460 ffs->state = FFS_ACTIVE;
461 mutex_unlock(&ffs->mutex);
463 ret = functionfs_ready_callback(ffs);
464 if (unlikely(ret < 0)) {
465 ffs->state = FFS_CLOSING;
469 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
477 * We're called from user space, we can use _irq
478 * rather then _irqsave
480 spin_lock_irq(&ffs->ev.waitq.lock);
481 switch (FFS_SETUP_STATE(ffs)) {
482 case FFS_SETUP_CANCELED:
490 case FFS_SETUP_PENDING:
494 /* FFS_SETUP_PENDING */
495 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
496 spin_unlock_irq(&ffs->ev.waitq.lock);
497 ret = __ffs_ep0_stall(ffs);
501 /* FFS_SETUP_PENDING and not stall */
502 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
504 spin_unlock_irq(&ffs->ev.waitq.lock);
506 data = ffs_prepare_buffer(buf, len);
512 spin_lock_irq(&ffs->ev.waitq.lock);
515 * We are guaranteed to be still in FFS_ACTIVE state
516 * but the state of setup could have changed from
517 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
518 * to check for that. If that happened we copied data
519 * from user space in vain but it's unlikely.
521 * For sure we are not in FFS_NO_SETUP since this is
522 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
523 * transition can be performed and it's protected by
526 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
529 spin_unlock_irq(&ffs->ev.waitq.lock);
531 /* unlocks spinlock */
532 ret = __ffs_ep0_queue_wait(ffs, data, len);
542 mutex_unlock(&ffs->mutex);
546 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
550 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
553 struct usb_functionfs_event events[n];
556 memset(events, 0, sizeof events);
559 events[i].type = ffs->ev.types[i];
560 if (events[i].type == FUNCTIONFS_SETUP) {
561 events[i].u.setup = ffs->ev.setup;
562 ffs->setup_state = FFS_SETUP_PENDING;
566 if (n < ffs->ev.count) {
568 memmove(ffs->ev.types, ffs->ev.types + n,
569 ffs->ev.count * sizeof *ffs->ev.types);
574 spin_unlock_irq(&ffs->ev.waitq.lock);
575 mutex_unlock(&ffs->mutex);
577 return unlikely(__copy_to_user(buf, events, sizeof events))
578 ? -EFAULT : sizeof events;
581 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
582 size_t len, loff_t *ptr)
584 struct ffs_data *ffs = file->private_data;
591 /* Fast check if setup was canceled */
592 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
596 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
597 if (unlikely(ret < 0))
601 if (ffs->state != FFS_ACTIVE) {
607 * We're called from user space, we can use _irq rather then
610 spin_lock_irq(&ffs->ev.waitq.lock);
612 switch (FFS_SETUP_STATE(ffs)) {
613 case FFS_SETUP_CANCELED:
618 n = len / sizeof(struct usb_functionfs_event);
624 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
629 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
635 return __ffs_ep0_read_events(ffs, buf,
636 min(n, (size_t)ffs->ev.count));
638 case FFS_SETUP_PENDING:
639 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
640 spin_unlock_irq(&ffs->ev.waitq.lock);
641 ret = __ffs_ep0_stall(ffs);
645 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
647 spin_unlock_irq(&ffs->ev.waitq.lock);
650 data = kmalloc(len, GFP_KERNEL);
651 if (unlikely(!data)) {
657 spin_lock_irq(&ffs->ev.waitq.lock);
659 /* See ffs_ep0_write() */
660 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
665 /* unlocks spinlock */
666 ret = __ffs_ep0_queue_wait(ffs, data, len);
667 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
676 spin_unlock_irq(&ffs->ev.waitq.lock);
678 mutex_unlock(&ffs->mutex);
683 static int ffs_ep0_open(struct inode *inode, struct file *file)
685 struct ffs_data *ffs = inode->i_private;
689 if (unlikely(ffs->state == FFS_CLOSING))
692 file->private_data = ffs;
693 ffs_data_opened(ffs);
698 static int ffs_ep0_release(struct inode *inode, struct file *file)
700 struct ffs_data *ffs = file->private_data;
704 ffs_data_closed(ffs);
709 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
711 struct ffs_data *ffs = file->private_data;
712 struct usb_gadget *gadget = ffs->gadget;
717 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
718 struct ffs_function *func = ffs->func;
719 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
720 } else if (gadget && gadget->ops->ioctl) {
721 ret = gadget->ops->ioctl(gadget, code, value);
729 static const struct file_operations ffs_ep0_operations = {
732 .open = ffs_ep0_open,
733 .write = ffs_ep0_write,
734 .read = ffs_ep0_read,
735 .release = ffs_ep0_release,
736 .unlocked_ioctl = ffs_ep0_ioctl,
740 /* "Normal" endpoints operations ********************************************/
742 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
745 if (likely(req->context)) {
746 struct ffs_ep *ep = _ep->driver_data;
747 ep->status = req->status ? req->status : req->actual;
748 complete(req->context);
752 static ssize_t ffs_epfile_io(struct file *file,
753 char __user *buf, size_t len, int read)
755 struct ffs_epfile *epfile = file->private_data;
761 /* Are we still active? */
762 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
767 /* Wait for endpoint to be enabled */
770 if (file->f_flags & O_NONBLOCK) {
775 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
783 halt = !read == !epfile->in;
784 if (halt && epfile->isoc) {
789 /* Allocate & copy */
791 data = kmalloc(len, GFP_KERNEL);
795 if (!read && unlikely(copy_from_user(data, buf, len))) {
801 /* We will be using request */
802 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
806 spin_lock_irq(&epfile->ffs->eps_lock);
808 if (epfile->ep != ep) {
809 /* In the meantime, endpoint got disabled or changed. */
811 spin_unlock_irq(&epfile->ffs->eps_lock);
814 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
815 usb_ep_set_halt(ep->ep);
816 spin_unlock_irq(&epfile->ffs->eps_lock);
819 /* Fire the request */
820 DECLARE_COMPLETION_ONSTACK(done);
822 struct usb_request *req = ep->req;
823 req->context = &done;
824 req->complete = ffs_epfile_io_complete;
828 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
830 spin_unlock_irq(&epfile->ffs->eps_lock);
832 if (unlikely(ret < 0)) {
834 } else if (unlikely(wait_for_completion_interruptible(&done))) {
836 usb_ep_dequeue(ep->ep, req);
839 if (read && ret > 0 &&
840 unlikely(copy_to_user(buf, data, ret)))
845 mutex_unlock(&epfile->mutex);
852 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
857 return ffs_epfile_io(file, (char __user *)buf, len, 0);
861 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
865 return ffs_epfile_io(file, buf, len, 1);
869 ffs_epfile_open(struct inode *inode, struct file *file)
871 struct ffs_epfile *epfile = inode->i_private;
875 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
878 file->private_data = epfile;
879 ffs_data_opened(epfile->ffs);
885 ffs_epfile_release(struct inode *inode, struct file *file)
887 struct ffs_epfile *epfile = inode->i_private;
891 ffs_data_closed(epfile->ffs);
896 static long ffs_epfile_ioctl(struct file *file, unsigned code,
899 struct ffs_epfile *epfile = file->private_data;
904 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
907 spin_lock_irq(&epfile->ffs->eps_lock);
908 if (likely(epfile->ep)) {
910 case FUNCTIONFS_FIFO_STATUS:
911 ret = usb_ep_fifo_status(epfile->ep->ep);
913 case FUNCTIONFS_FIFO_FLUSH:
914 usb_ep_fifo_flush(epfile->ep->ep);
917 case FUNCTIONFS_CLEAR_HALT:
918 ret = usb_ep_clear_halt(epfile->ep->ep);
920 case FUNCTIONFS_ENDPOINT_REVMAP:
921 ret = epfile->ep->num;
929 spin_unlock_irq(&epfile->ffs->eps_lock);
934 static const struct file_operations ffs_epfile_operations = {
937 .open = ffs_epfile_open,
938 .write = ffs_epfile_write,
939 .read = ffs_epfile_read,
940 .release = ffs_epfile_release,
941 .unlocked_ioctl = ffs_epfile_ioctl,
945 /* File system and super block operations ***********************************/
948 * Mounting the file system creates a controller file, used first for
949 * function configuration then later for event monitoring.
952 static struct inode *__must_check
953 ffs_sb_make_inode(struct super_block *sb, void *data,
954 const struct file_operations *fops,
955 const struct inode_operations *iops,
956 struct ffs_file_perms *perms)
962 inode = new_inode(sb);
965 struct timespec current_time = CURRENT_TIME;
967 inode->i_ino = get_next_ino();
968 inode->i_mode = perms->mode;
969 inode->i_uid = perms->uid;
970 inode->i_gid = perms->gid;
971 inode->i_atime = current_time;
972 inode->i_mtime = current_time;
973 inode->i_ctime = current_time;
974 inode->i_private = data;
984 /* Create "regular" file */
985 static struct inode *ffs_sb_create_file(struct super_block *sb,
986 const char *name, void *data,
987 const struct file_operations *fops,
988 struct dentry **dentry_p)
990 struct ffs_data *ffs = sb->s_fs_info;
991 struct dentry *dentry;
996 dentry = d_alloc_name(sb->s_root, name);
997 if (unlikely(!dentry))
1000 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1001 if (unlikely(!inode)) {
1006 d_add(dentry, inode);
1014 static const struct super_operations ffs_sb_operations = {
1015 .statfs = simple_statfs,
1016 .drop_inode = generic_delete_inode,
1019 struct ffs_sb_fill_data {
1020 struct ffs_file_perms perms;
1022 const char *dev_name;
1023 struct ffs_data *ffs_data;
1026 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1028 struct ffs_sb_fill_data *data = _data;
1029 struct inode *inode;
1030 struct ffs_data *ffs = data->ffs_data;
1035 data->ffs_data = NULL;
1036 sb->s_fs_info = ffs;
1037 sb->s_blocksize = PAGE_CACHE_SIZE;
1038 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1039 sb->s_magic = FUNCTIONFS_MAGIC;
1040 sb->s_op = &ffs_sb_operations;
1041 sb->s_time_gran = 1;
1044 data->perms.mode = data->root_mode;
1045 inode = ffs_sb_make_inode(sb, NULL,
1046 &simple_dir_operations,
1047 &simple_dir_inode_operations,
1049 sb->s_root = d_make_root(inode);
1050 if (unlikely(!sb->s_root))
1054 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1055 &ffs_ep0_operations, NULL)))
1061 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1065 if (!opts || !*opts)
1069 unsigned long value;
1073 comma = strchr(opts, ',');
1078 eq = strchr(opts, '=');
1079 if (unlikely(!eq)) {
1080 pr_err("'=' missing in %s\n", opts);
1086 if (kstrtoul(eq + 1, 0, &value)) {
1087 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1091 /* Interpret option */
1092 switch (eq - opts) {
1094 if (!memcmp(opts, "rmode", 5))
1095 data->root_mode = (value & 0555) | S_IFDIR;
1096 else if (!memcmp(opts, "fmode", 5))
1097 data->perms.mode = (value & 0666) | S_IFREG;
1103 if (!memcmp(opts, "mode", 4)) {
1104 data->root_mode = (value & 0555) | S_IFDIR;
1105 data->perms.mode = (value & 0666) | S_IFREG;
1112 if (!memcmp(opts, "uid", 3)) {
1113 data->perms.uid = make_kuid(current_user_ns(), value);
1114 if (!uid_valid(data->perms.uid)) {
1115 pr_err("%s: unmapped value: %lu\n", opts, value);
1118 } else if (!memcmp(opts, "gid", 3)) {
1119 data->perms.gid = make_kgid(current_user_ns(), value);
1120 if (!gid_valid(data->perms.gid)) {
1121 pr_err("%s: unmapped value: %lu\n", opts, value);
1131 pr_err("%s: invalid option\n", opts);
1135 /* Next iteration */
1144 /* "mount -t functionfs dev_name /dev/function" ends up here */
1146 static struct dentry *
1147 ffs_fs_mount(struct file_system_type *t, int flags,
1148 const char *dev_name, void *opts)
1150 struct ffs_sb_fill_data data = {
1152 .mode = S_IFREG | 0600,
1153 .uid = GLOBAL_ROOT_UID,
1154 .gid = GLOBAL_ROOT_GID,
1156 .root_mode = S_IFDIR | 0500,
1161 struct ffs_data *ffs;
1165 ret = ffs_fs_parse_opts(&data, opts);
1166 if (unlikely(ret < 0))
1167 return ERR_PTR(ret);
1169 ffs = ffs_data_new();
1171 return ERR_PTR(-ENOMEM);
1172 ffs->file_perms = data.perms;
1174 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1175 if (unlikely(!ffs->dev_name)) {
1177 return ERR_PTR(-ENOMEM);
1180 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1181 if (IS_ERR(ffs_dev)) {
1183 return ERR_CAST(ffs_dev);
1185 ffs->private_data = ffs_dev;
1186 data.ffs_data = ffs;
1188 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1189 if (IS_ERR(rv) && data.ffs_data) {
1190 functionfs_release_dev_callback(data.ffs_data);
1191 ffs_data_put(data.ffs_data);
1197 ffs_fs_kill_sb(struct super_block *sb)
1201 kill_litter_super(sb);
1202 if (sb->s_fs_info) {
1203 functionfs_release_dev_callback(sb->s_fs_info);
1204 ffs_data_put(sb->s_fs_info);
1208 static struct file_system_type ffs_fs_type = {
1209 .owner = THIS_MODULE,
1210 .name = "functionfs",
1211 .mount = ffs_fs_mount,
1212 .kill_sb = ffs_fs_kill_sb,
1214 MODULE_ALIAS_FS("functionfs");
1217 /* Driver's main init/cleanup functions *************************************/
1219 static int functionfs_init(void)
1225 ret = register_filesystem(&ffs_fs_type);
1227 pr_info("file system registered\n");
1229 pr_err("failed registering file system (%d)\n", ret);
1234 static void functionfs_cleanup(void)
1238 pr_info("unloading\n");
1239 unregister_filesystem(&ffs_fs_type);
1243 /* ffs_data and ffs_function construction and destruction code **************/
1245 static void ffs_data_clear(struct ffs_data *ffs);
1246 static void ffs_data_reset(struct ffs_data *ffs);
1248 static void ffs_data_get(struct ffs_data *ffs)
1252 atomic_inc(&ffs->ref);
1255 static void ffs_data_opened(struct ffs_data *ffs)
1259 atomic_inc(&ffs->ref);
1260 atomic_inc(&ffs->opened);
1263 static void ffs_data_put(struct ffs_data *ffs)
1267 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1268 pr_info("%s(): freeing\n", __func__);
1269 ffs_data_clear(ffs);
1270 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1271 waitqueue_active(&ffs->ep0req_completion.wait));
1272 kfree(ffs->dev_name);
1277 static void ffs_data_closed(struct ffs_data *ffs)
1281 if (atomic_dec_and_test(&ffs->opened)) {
1282 ffs->state = FFS_CLOSING;
1283 ffs_data_reset(ffs);
1289 static struct ffs_data *ffs_data_new(void)
1291 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1297 atomic_set(&ffs->ref, 1);
1298 atomic_set(&ffs->opened, 0);
1299 ffs->state = FFS_READ_DESCRIPTORS;
1300 mutex_init(&ffs->mutex);
1301 spin_lock_init(&ffs->eps_lock);
1302 init_waitqueue_head(&ffs->ev.waitq);
1303 init_completion(&ffs->ep0req_completion);
1305 /* XXX REVISIT need to update it in some places, or do we? */
1306 ffs->ev.can_stall = 1;
1311 static void ffs_data_clear(struct ffs_data *ffs)
1315 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1316 functionfs_closed_callback(ffs);
1318 BUG_ON(ffs->gadget);
1321 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1323 kfree(ffs->raw_descs);
1324 kfree(ffs->raw_strings);
1325 kfree(ffs->stringtabs);
1328 static void ffs_data_reset(struct ffs_data *ffs)
1332 ffs_data_clear(ffs);
1334 ffs->epfiles = NULL;
1335 ffs->raw_descs = NULL;
1336 ffs->raw_strings = NULL;
1337 ffs->stringtabs = NULL;
1339 ffs->raw_descs_length = 0;
1340 ffs->raw_fs_descs_length = 0;
1341 ffs->fs_descs_count = 0;
1342 ffs->hs_descs_count = 0;
1344 ffs->strings_count = 0;
1345 ffs->interfaces_count = 0;
1350 ffs->state = FFS_READ_DESCRIPTORS;
1351 ffs->setup_state = FFS_NO_SETUP;
1356 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1358 struct usb_gadget_strings **lang;
1363 if (WARN_ON(ffs->state != FFS_ACTIVE
1364 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1367 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1368 if (unlikely(first_id < 0))
1371 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1372 if (unlikely(!ffs->ep0req))
1374 ffs->ep0req->complete = ffs_ep0_complete;
1375 ffs->ep0req->context = ffs;
1377 lang = ffs->stringtabs;
1378 for (lang = ffs->stringtabs; *lang; ++lang) {
1379 struct usb_string *str = (*lang)->strings;
1381 for (; str->s; ++id, ++str)
1385 ffs->gadget = cdev->gadget;
1390 static void functionfs_unbind(struct ffs_data *ffs)
1394 if (!WARN_ON(!ffs->gadget)) {
1395 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1398 clear_bit(FFS_FL_BOUND, &ffs->flags);
1403 static int ffs_epfiles_create(struct ffs_data *ffs)
1405 struct ffs_epfile *epfile, *epfiles;
1410 count = ffs->eps_count;
1411 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1416 for (i = 1; i <= count; ++i, ++epfile) {
1418 mutex_init(&epfile->mutex);
1419 init_waitqueue_head(&epfile->wait);
1420 sprintf(epfiles->name, "ep%u", i);
1421 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1422 &ffs_epfile_operations,
1423 &epfile->dentry))) {
1424 ffs_epfiles_destroy(epfiles, i - 1);
1429 ffs->epfiles = epfiles;
1433 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1435 struct ffs_epfile *epfile = epfiles;
1439 for (; count; --count, ++epfile) {
1440 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1441 waitqueue_active(&epfile->wait));
1442 if (epfile->dentry) {
1443 d_delete(epfile->dentry);
1444 dput(epfile->dentry);
1445 epfile->dentry = NULL;
1452 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1453 struct usb_configuration *c,
1454 struct ffs_data *ffs)
1456 struct ffs_function *func;
1461 func = kzalloc(sizeof *func, GFP_KERNEL);
1462 if (unlikely(!func))
1465 func->function.name = "Function FS Gadget";
1466 func->function.strings = ffs->stringtabs;
1468 func->function.bind = ffs_func_bind;
1469 func->function.unbind = ffs_func_unbind;
1470 func->function.set_alt = ffs_func_set_alt;
1471 func->function.disable = ffs_func_disable;
1472 func->function.setup = ffs_func_setup;
1473 func->function.suspend = ffs_func_suspend;
1474 func->function.resume = ffs_func_resume;
1477 func->gadget = cdev->gadget;
1481 ret = usb_add_function(c, &func->function);
1483 ffs_func_free(func);
1488 static void ffs_func_free(struct ffs_function *func)
1490 struct ffs_ep *ep = func->eps;
1491 unsigned count = func->ffs->eps_count;
1492 unsigned long flags;
1496 /* cleanup after autoconfig */
1497 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1499 if (ep->ep && ep->req)
1500 usb_ep_free_request(ep->ep, ep->req);
1504 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1506 ffs_data_put(func->ffs);
1510 * eps and interfaces_nums are allocated in the same chunk so
1511 * only one free is required. Descriptors are also allocated
1512 * in the same chunk.
1518 static void ffs_func_eps_disable(struct ffs_function *func)
1520 struct ffs_ep *ep = func->eps;
1521 struct ffs_epfile *epfile = func->ffs->epfiles;
1522 unsigned count = func->ffs->eps_count;
1523 unsigned long flags;
1525 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1527 /* pending requests get nuked */
1529 usb_ep_disable(ep->ep);
1535 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1538 static int ffs_func_eps_enable(struct ffs_function *func)
1540 struct ffs_data *ffs = func->ffs;
1541 struct ffs_ep *ep = func->eps;
1542 struct ffs_epfile *epfile = ffs->epfiles;
1543 unsigned count = ffs->eps_count;
1544 unsigned long flags;
1547 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1549 struct usb_endpoint_descriptor *ds;
1550 ds = ep->descs[ep->descs[1] ? 1 : 0];
1552 ep->ep->driver_data = ep;
1554 ret = usb_ep_enable(ep->ep);
1557 epfile->in = usb_endpoint_dir_in(ds);
1558 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1563 wake_up(&epfile->wait);
1568 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1574 /* Parsing and building descriptors and strings *****************************/
1577 * This validates if data pointed by data is a valid USB descriptor as
1578 * well as record how many interfaces, endpoints and strings are
1579 * required by given configuration. Returns address after the
1580 * descriptor or NULL if data is invalid.
1583 enum ffs_entity_type {
1584 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1587 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1589 struct usb_descriptor_header *desc,
1592 static int __must_check ffs_do_desc(char *data, unsigned len,
1593 ffs_entity_callback entity, void *priv)
1595 struct usb_descriptor_header *_ds = (void *)data;
1601 /* At least two bytes are required: length and type */
1603 pr_vdebug("descriptor too short\n");
1607 /* If we have at least as many bytes as the descriptor takes? */
1608 length = _ds->bLength;
1610 pr_vdebug("descriptor longer then available data\n");
1614 #define __entity_check_INTERFACE(val) 1
1615 #define __entity_check_STRING(val) (val)
1616 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1617 #define __entity(type, val) do { \
1618 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1619 if (unlikely(!__entity_check_ ##type(val))) { \
1620 pr_vdebug("invalid entity's value\n"); \
1623 ret = entity(FFS_ ##type, &val, _ds, priv); \
1624 if (unlikely(ret < 0)) { \
1625 pr_debug("entity " #type "(%02x); ret = %d\n", \
1631 /* Parse descriptor depending on type. */
1632 switch (_ds->bDescriptorType) {
1636 case USB_DT_DEVICE_QUALIFIER:
1637 /* function can't have any of those */
1638 pr_vdebug("descriptor reserved for gadget: %d\n",
1639 _ds->bDescriptorType);
1642 case USB_DT_INTERFACE: {
1643 struct usb_interface_descriptor *ds = (void *)_ds;
1644 pr_vdebug("interface descriptor\n");
1645 if (length != sizeof *ds)
1648 __entity(INTERFACE, ds->bInterfaceNumber);
1650 __entity(STRING, ds->iInterface);
1654 case USB_DT_ENDPOINT: {
1655 struct usb_endpoint_descriptor *ds = (void *)_ds;
1656 pr_vdebug("endpoint descriptor\n");
1657 if (length != USB_DT_ENDPOINT_SIZE &&
1658 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1660 __entity(ENDPOINT, ds->bEndpointAddress);
1665 pr_vdebug("hid descriptor\n");
1666 if (length != sizeof(struct hid_descriptor))
1671 if (length != sizeof(struct usb_otg_descriptor))
1675 case USB_DT_INTERFACE_ASSOCIATION: {
1676 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1677 pr_vdebug("interface association descriptor\n");
1678 if (length != sizeof *ds)
1681 __entity(STRING, ds->iFunction);
1685 case USB_DT_OTHER_SPEED_CONFIG:
1686 case USB_DT_INTERFACE_POWER:
1688 case USB_DT_SECURITY:
1689 case USB_DT_CS_RADIO_CONTROL:
1691 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1695 /* We should never be here */
1696 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1700 pr_vdebug("invalid length: %d (descriptor %d)\n",
1701 _ds->bLength, _ds->bDescriptorType);
1706 #undef __entity_check_DESCRIPTOR
1707 #undef __entity_check_INTERFACE
1708 #undef __entity_check_STRING
1709 #undef __entity_check_ENDPOINT
1714 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1715 ffs_entity_callback entity, void *priv)
1717 const unsigned _len = len;
1718 unsigned long num = 0;
1728 /* Record "descriptor" entity */
1729 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1730 if (unlikely(ret < 0)) {
1731 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1739 ret = ffs_do_desc(data, len, entity, priv);
1740 if (unlikely(ret < 0)) {
1741 pr_debug("%s returns %d\n", __func__, ret);
1751 static int __ffs_data_do_entity(enum ffs_entity_type type,
1752 u8 *valuep, struct usb_descriptor_header *desc,
1755 struct ffs_data *ffs = priv;
1760 case FFS_DESCRIPTOR:
1765 * Interfaces are indexed from zero so if we
1766 * encountered interface "n" then there are at least
1769 if (*valuep >= ffs->interfaces_count)
1770 ffs->interfaces_count = *valuep + 1;
1775 * Strings are indexed from 1 (0 is magic ;) reserved
1776 * for languages list or some such)
1778 if (*valuep > ffs->strings_count)
1779 ffs->strings_count = *valuep;
1783 /* Endpoints are indexed from 1 as well. */
1784 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1785 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1792 static int __ffs_data_got_descs(struct ffs_data *ffs,
1793 char *const _data, size_t len)
1795 unsigned fs_count, hs_count;
1796 int fs_len, ret = -EINVAL;
1801 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1802 get_unaligned_le32(data + 4) != len))
1804 fs_count = get_unaligned_le32(data + 8);
1805 hs_count = get_unaligned_le32(data + 12);
1807 if (!fs_count && !hs_count)
1813 if (likely(fs_count)) {
1814 fs_len = ffs_do_descs(fs_count, data, len,
1815 __ffs_data_do_entity, ffs);
1816 if (unlikely(fs_len < 0)) {
1827 if (likely(hs_count)) {
1828 ret = ffs_do_descs(hs_count, data, len,
1829 __ffs_data_do_entity, ffs);
1830 if (unlikely(ret < 0))
1836 if (unlikely(len != ret))
1839 ffs->raw_fs_descs_length = fs_len;
1840 ffs->raw_descs_length = fs_len + ret;
1841 ffs->raw_descs = _data;
1842 ffs->fs_descs_count = fs_count;
1843 ffs->hs_descs_count = hs_count;
1854 static int __ffs_data_got_strings(struct ffs_data *ffs,
1855 char *const _data, size_t len)
1857 u32 str_count, needed_count, lang_count;
1858 struct usb_gadget_strings **stringtabs, *t;
1859 struct usb_string *strings, *s;
1860 const char *data = _data;
1864 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1865 get_unaligned_le32(data + 4) != len))
1867 str_count = get_unaligned_le32(data + 8);
1868 lang_count = get_unaligned_le32(data + 12);
1870 /* if one is zero the other must be zero */
1871 if (unlikely(!str_count != !lang_count))
1874 /* Do we have at least as many strings as descriptors need? */
1875 needed_count = ffs->strings_count;
1876 if (unlikely(str_count < needed_count))
1880 * If we don't need any strings just return and free all
1883 if (!needed_count) {
1888 /* Allocate everything in one chunk so there's less maintenance. */
1891 struct usb_gadget_strings *stringtabs[lang_count + 1];
1892 struct usb_gadget_strings stringtab[lang_count];
1893 struct usb_string strings[lang_count*(needed_count+1)];
1897 d = kmalloc(sizeof *d, GFP_KERNEL);
1903 stringtabs = d->stringtabs;
1907 *stringtabs++ = t++;
1911 stringtabs = d->stringtabs;
1917 /* For each language */
1921 do { /* lang_count > 0 so we can use do-while */
1922 unsigned needed = needed_count;
1924 if (unlikely(len < 3))
1926 t->language = get_unaligned_le16(data);
1933 /* For each string */
1934 do { /* str_count > 0 so we can use do-while */
1935 size_t length = strnlen(data, len);
1937 if (unlikely(length == len))
1941 * User may provide more strings then we need,
1942 * if that's the case we simply ignore the
1945 if (likely(needed)) {
1947 * s->id will be set while adding
1948 * function to configuration so for
1949 * now just leave garbage here.
1958 } while (--str_count);
1960 s->id = 0; /* terminator */
1964 } while (--lang_count);
1966 /* Some garbage left? */
1971 ffs->stringtabs = stringtabs;
1972 ffs->raw_strings = _data;
1984 /* Events handling and management *******************************************/
1986 static void __ffs_event_add(struct ffs_data *ffs,
1987 enum usb_functionfs_event_type type)
1989 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1993 * Abort any unhandled setup
1995 * We do not need to worry about some cmpxchg() changing value
1996 * of ffs->setup_state without holding the lock because when
1997 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1998 * the source does nothing.
2000 if (ffs->setup_state == FFS_SETUP_PENDING)
2001 ffs->setup_state = FFS_SETUP_CANCELED;
2004 case FUNCTIONFS_RESUME:
2005 rem_type2 = FUNCTIONFS_SUSPEND;
2007 case FUNCTIONFS_SUSPEND:
2008 case FUNCTIONFS_SETUP:
2010 /* Discard all similar events */
2013 case FUNCTIONFS_BIND:
2014 case FUNCTIONFS_UNBIND:
2015 case FUNCTIONFS_DISABLE:
2016 case FUNCTIONFS_ENABLE:
2017 /* Discard everything other then power management. */
2018 rem_type1 = FUNCTIONFS_SUSPEND;
2019 rem_type2 = FUNCTIONFS_RESUME;
2028 u8 *ev = ffs->ev.types, *out = ev;
2029 unsigned n = ffs->ev.count;
2030 for (; n; --n, ++ev)
2031 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2034 pr_vdebug("purging event %d\n", *ev);
2035 ffs->ev.count = out - ffs->ev.types;
2038 pr_vdebug("adding event %d\n", type);
2039 ffs->ev.types[ffs->ev.count++] = type;
2040 wake_up_locked(&ffs->ev.waitq);
2043 static void ffs_event_add(struct ffs_data *ffs,
2044 enum usb_functionfs_event_type type)
2046 unsigned long flags;
2047 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2048 __ffs_event_add(ffs, type);
2049 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2053 /* Bind/unbind USB function hooks *******************************************/
2055 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2056 struct usb_descriptor_header *desc,
2059 struct usb_endpoint_descriptor *ds = (void *)desc;
2060 struct ffs_function *func = priv;
2061 struct ffs_ep *ffs_ep;
2064 * If hs_descriptors is not NULL then we are reading hs
2067 const int isHS = func->function.hs_descriptors != NULL;
2070 if (type != FFS_DESCRIPTOR)
2074 func->function.hs_descriptors[(long)valuep] = desc;
2076 func->function.fs_descriptors[(long)valuep] = desc;
2078 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2081 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2082 ffs_ep = func->eps + idx;
2084 if (unlikely(ffs_ep->descs[isHS])) {
2085 pr_vdebug("two %sspeed descriptors for EP %d\n",
2086 isHS ? "high" : "full",
2087 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2090 ffs_ep->descs[isHS] = ds;
2092 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2094 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2095 if (!ds->wMaxPacketSize)
2096 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2098 struct usb_request *req;
2101 pr_vdebug("autoconfig\n");
2102 ep = usb_ep_autoconfig(func->gadget, ds);
2105 ep->driver_data = func->eps + idx;
2107 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2113 func->eps_revmap[ds->bEndpointAddress &
2114 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2116 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2121 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2122 struct usb_descriptor_header *desc,
2125 struct ffs_function *func = priv;
2131 case FFS_DESCRIPTOR:
2132 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2137 if (func->interfaces_nums[idx] < 0) {
2138 int id = usb_interface_id(func->conf, &func->function);
2139 if (unlikely(id < 0))
2141 func->interfaces_nums[idx] = id;
2143 newValue = func->interfaces_nums[idx];
2147 /* String' IDs are allocated when fsf_data is bound to cdev */
2148 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2153 * USB_DT_ENDPOINT are handled in
2154 * __ffs_func_bind_do_descs().
2156 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2159 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2160 if (unlikely(!func->eps[idx].ep))
2164 struct usb_endpoint_descriptor **descs;
2165 descs = func->eps[idx].descs;
2166 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2171 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2176 static int ffs_func_bind(struct usb_configuration *c,
2177 struct usb_function *f)
2179 struct ffs_function *func = ffs_func_from_usb(f);
2180 struct ffs_data *ffs = func->ffs;
2182 const int full = !!func->ffs->fs_descs_count;
2183 const int high = gadget_is_dualspeed(func->gadget) &&
2184 func->ffs->hs_descs_count;
2188 /* Make it a single chunk, less management later on */
2190 struct ffs_ep eps[ffs->eps_count];
2191 struct usb_descriptor_header
2192 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2193 struct usb_descriptor_header
2194 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2195 short inums[ffs->interfaces_count];
2196 char raw_descs[high ? ffs->raw_descs_length
2197 : ffs->raw_fs_descs_length];
2202 /* Only high speed but not supported by gadget? */
2203 if (unlikely(!(full | high)))
2207 data = kmalloc(sizeof *data, GFP_KERNEL);
2208 if (unlikely(!data))
2212 memset(data->eps, 0, sizeof data->eps);
2213 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2214 memset(data->inums, 0xff, sizeof data->inums);
2215 for (ret = ffs->eps_count; ret; --ret)
2216 data->eps[ret].num = -1;
2219 func->eps = data->eps;
2220 func->interfaces_nums = data->inums;
2223 * Go through all the endpoint descriptors and allocate
2224 * endpoints first, so that later we can rewrite the endpoint
2225 * numbers without worrying that it may be described later on.
2228 func->function.fs_descriptors = data->fs_descs;
2229 ret = ffs_do_descs(ffs->fs_descs_count,
2231 sizeof data->raw_descs,
2232 __ffs_func_bind_do_descs, func);
2233 if (unlikely(ret < 0))
2240 func->function.hs_descriptors = data->hs_descs;
2241 ret = ffs_do_descs(ffs->hs_descs_count,
2242 data->raw_descs + ret,
2243 (sizeof data->raw_descs) - ret,
2244 __ffs_func_bind_do_descs, func);
2245 if (unlikely(ret < 0))
2250 * Now handle interface numbers allocation and interface and
2251 * endpoint numbers rewriting. We can do that in one go
2254 ret = ffs_do_descs(ffs->fs_descs_count +
2255 (high ? ffs->hs_descs_count : 0),
2256 data->raw_descs, sizeof data->raw_descs,
2257 __ffs_func_bind_do_nums, func);
2258 if (unlikely(ret < 0))
2261 /* And we're done */
2262 ffs_event_add(ffs, FUNCTIONFS_BIND);
2266 /* XXX Do we need to release all claimed endpoints here? */
2271 /* Other USB function hooks *************************************************/
2273 static void ffs_func_unbind(struct usb_configuration *c,
2274 struct usb_function *f)
2276 struct ffs_function *func = ffs_func_from_usb(f);
2277 struct ffs_data *ffs = func->ffs;
2281 if (ffs->func == func) {
2282 ffs_func_eps_disable(func);
2286 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2288 ffs_func_free(func);
2291 static int ffs_func_set_alt(struct usb_function *f,
2292 unsigned interface, unsigned alt)
2294 struct ffs_function *func = ffs_func_from_usb(f);
2295 struct ffs_data *ffs = func->ffs;
2298 if (alt != (unsigned)-1) {
2299 intf = ffs_func_revmap_intf(func, interface);
2300 if (unlikely(intf < 0))
2305 ffs_func_eps_disable(ffs->func);
2307 if (ffs->state != FFS_ACTIVE)
2310 if (alt == (unsigned)-1) {
2312 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2317 ret = ffs_func_eps_enable(func);
2318 if (likely(ret >= 0))
2319 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2323 static void ffs_func_disable(struct usb_function *f)
2325 ffs_func_set_alt(f, 0, (unsigned)-1);
2328 static int ffs_func_setup(struct usb_function *f,
2329 const struct usb_ctrlrequest *creq)
2331 struct ffs_function *func = ffs_func_from_usb(f);
2332 struct ffs_data *ffs = func->ffs;
2333 unsigned long flags;
2338 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2339 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2340 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2341 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2342 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2345 * Most requests directed to interface go through here
2346 * (notable exceptions are set/get interface) so we need to
2347 * handle them. All other either handled by composite or
2348 * passed to usb_configuration->setup() (if one is set). No
2349 * matter, we will handle requests directed to endpoint here
2350 * as well (as it's straightforward) but what to do with any
2353 if (ffs->state != FFS_ACTIVE)
2356 switch (creq->bRequestType & USB_RECIP_MASK) {
2357 case USB_RECIP_INTERFACE:
2358 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2359 if (unlikely(ret < 0))
2363 case USB_RECIP_ENDPOINT:
2364 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2365 if (unlikely(ret < 0))
2373 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2374 ffs->ev.setup = *creq;
2375 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2376 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2377 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2382 static void ffs_func_suspend(struct usb_function *f)
2385 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2388 static void ffs_func_resume(struct usb_function *f)
2391 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2395 /* Endpoint and interface numbers reverse mapping ***************************/
2397 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2399 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2400 return num ? num : -EDOM;
2403 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2405 short *nums = func->interfaces_nums;
2406 unsigned count = func->ffs->interfaces_count;
2408 for (; count; --count, ++nums) {
2409 if (*nums >= 0 && *nums == intf)
2410 return nums - func->interfaces_nums;
2417 /* Misc helper functions ****************************************************/
2419 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2422 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2423 : mutex_lock_interruptible(mutex);
2426 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
2433 data = kmalloc(len, GFP_KERNEL);
2434 if (unlikely(!data))
2435 return ERR_PTR(-ENOMEM);
2437 if (unlikely(__copy_from_user(data, buf, len))) {
2439 return ERR_PTR(-EFAULT);
2442 pr_vdebug("Buffer from user space:\n");
2443 ffs_dump_mem("", data, len);