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 <asm/unaligned.h>
26 #include <linux/usb/composite.h>
27 #include <linux/usb/functionfs.h>
30 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
33 /* Debugging ****************************************************************/
36 # define pr_vdebug pr_debug
37 # define ffs_dump_mem(prefix, ptr, len) \
38 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
40 # define pr_vdebug(...) do { } while (0)
41 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
42 #endif /* VERBOSE_DEBUG */
44 #define ENTER() pr_vdebug("%s()\n", __func__)
47 /* The data structure and setup file ****************************************/
51 * Waiting for descriptors and strings.
53 * In this state no open(2), read(2) or write(2) on epfiles
54 * may succeed (which should not be the problem as there
55 * should be no such files opened in the first place).
61 * We've got descriptors and strings. We are or have called
62 * functionfs_ready_callback(). functionfs_bind() may have
63 * been called but we don't know.
65 * This is the only state in which operations on epfiles may
71 * All endpoints have been closed. This state is also set if
72 * we encounter an unrecoverable error. The only
73 * unrecoverable error is situation when after reading strings
74 * from user space we fail to initialise epfiles or
75 * functionfs_ready_callback() returns with error (<0).
77 * In this state no open(2), read(2) or write(2) (both on ep0
78 * as well as epfile) may succeed (at this point epfiles are
79 * unlinked and all closed so this is not a problem; ep0 is
80 * also closed but ep0 file exists and so open(2) on ep0 must
87 enum ffs_setup_state {
88 /* There is no setup request pending. */
91 * User has read events and there was a setup request event
92 * there. The next read/write on ep0 will handle the
97 * There was event pending but before user space handled it
98 * some other event was introduced which canceled existing
99 * setup. If this state is set read/write on ep0 return
100 * -EIDRM. This state is only set when adding event.
111 struct usb_gadget *gadget;
114 * Protect access read/write operations, only one read/write
115 * at a time. As a consequence protects ep0req and company.
116 * While setup request is being processed (queued) this is
122 * Protect access to endpoint related structures (basically
123 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
129 * XXX REVISIT do we need our own request? Since we are not
130 * handling setup requests immediately user space may be so
131 * slow that another setup will be sent to the gadget but this
132 * time not to us but another function and then there could be
133 * a race. Is that the case? Or maybe we can use cdev->req
134 * after all, maybe we just need some spinlock for that?
136 struct usb_request *ep0req; /* P: mutex */
137 struct completion ep0req_completion; /* P: mutex */
138 int ep0req_status; /* P: mutex */
140 /* reference counter */
142 /* how many files are opened (EP0 and others) */
146 enum ffs_state state;
149 * Possible transitions:
150 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
151 * happens only in ep0 read which is P: mutex
152 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
153 * happens only in ep0 i/o which is P: mutex
154 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
155 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
157 enum ffs_setup_state setup_state;
159 #define FFS_SETUP_STATE(ffs) \
160 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
161 FFS_SETUP_CANCELED, FFS_NO_SETUP))
166 unsigned short count;
167 /* XXX REVISIT need to update it in some places, or do we? */
168 unsigned short can_stall;
169 struct usb_ctrlrequest setup;
171 wait_queue_head_t waitq;
172 } ev; /* the whole structure, P: ev.waitq.lock */
176 #define FFS_FL_CALL_CLOSED_CALLBACK 0
177 #define FFS_FL_BOUND 1
179 /* Active function */
180 struct ffs_function *func;
183 * Device name, write once when file system is mounted.
184 * Intended for user to read if she wants.
186 const char *dev_name;
187 /* Private data for our user (ie. gadget). Managed by user. */
190 /* filled by __ffs_data_got_descs() */
192 * Real descriptors are 16 bytes after raw_descs (so you need
193 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
194 * first full speed descriptor). raw_descs_length and
195 * raw_fs_descs_length do not have those 16 bytes added.
197 const void *raw_descs;
198 unsigned raw_descs_length;
199 unsigned raw_fs_descs_length;
200 unsigned fs_descs_count;
201 unsigned hs_descs_count;
203 unsigned short strings_count;
204 unsigned short interfaces_count;
205 unsigned short eps_count;
206 unsigned short _pad1;
208 /* filled by __ffs_data_got_strings() */
209 /* ids in stringtabs are set in functionfs_bind() */
210 const void *raw_strings;
211 struct usb_gadget_strings **stringtabs;
214 * File system's super block, write once when file system is
217 struct super_block *sb;
219 /* File permissions, written once when fs is mounted */
220 struct ffs_file_perms {
227 * The endpoint files, filled by ffs_epfiles_create(),
228 * destroyed by ffs_epfiles_destroy().
230 struct ffs_epfile *epfiles;
233 /* Reference counter handling */
234 static void ffs_data_get(struct ffs_data *ffs);
235 static void ffs_data_put(struct ffs_data *ffs);
236 /* Creates new ffs_data object. */
237 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
239 /* Opened counter handling. */
240 static void ffs_data_opened(struct ffs_data *ffs);
241 static void ffs_data_closed(struct ffs_data *ffs);
243 /* Called with ffs->mutex held; take over ownership of data. */
244 static int __must_check
245 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
246 static int __must_check
247 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
250 /* The function structure ***************************************************/
254 struct ffs_function {
255 struct usb_configuration *conf;
256 struct usb_gadget *gadget;
257 struct ffs_data *ffs;
261 short *interfaces_nums;
263 struct usb_function function;
267 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
269 return container_of(f, struct ffs_function, function);
272 static void ffs_func_free(struct ffs_function *func);
274 static void ffs_func_eps_disable(struct ffs_function *func);
275 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
277 static int ffs_func_bind(struct usb_configuration *,
278 struct usb_function *);
279 static void ffs_func_unbind(struct usb_configuration *,
280 struct usb_function *);
281 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
282 static void ffs_func_disable(struct usb_function *);
283 static int ffs_func_setup(struct usb_function *,
284 const struct usb_ctrlrequest *);
285 static void ffs_func_suspend(struct usb_function *);
286 static void ffs_func_resume(struct usb_function *);
289 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
290 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
293 /* The endpoints structures *************************************************/
296 struct usb_ep *ep; /* P: ffs->eps_lock */
297 struct usb_request *req; /* P: epfile->mutex */
299 /* [0]: full speed, [1]: high speed */
300 struct usb_endpoint_descriptor *descs[2];
304 int status; /* P: epfile->mutex */
308 /* Protects ep->ep and ep->req. */
310 wait_queue_head_t wait;
312 struct ffs_data *ffs;
313 struct ffs_ep *ep; /* P: ffs->eps_lock */
315 struct dentry *dentry;
319 unsigned char in; /* P: ffs->eps_lock */
320 unsigned char isoc; /* P: ffs->eps_lock */
325 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
326 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
328 static struct inode *__must_check
329 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
330 const struct file_operations *fops,
331 struct dentry **dentry_p);
334 /* Misc helper functions ****************************************************/
336 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
337 __attribute__((warn_unused_result, nonnull));
338 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
339 __attribute__((warn_unused_result, nonnull));
342 /* Control file aka ep0 *****************************************************/
344 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
346 struct ffs_data *ffs = req->context;
348 complete_all(&ffs->ep0req_completion);
351 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
353 struct usb_request *req = ffs->ep0req;
356 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
358 spin_unlock_irq(&ffs->ev.waitq.lock);
364 * UDC layer requires to provide a buffer even for ZLP, but should
365 * not use it at all. Let's provide some poisoned pointer to catch
366 * possible bug in the driver.
368 if (req->buf == NULL)
369 req->buf = (void *)0xDEADBABE;
371 INIT_COMPLETION(ffs->ep0req_completion);
373 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
374 if (unlikely(ret < 0))
377 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
379 usb_ep_dequeue(ffs->gadget->ep0, req);
383 ffs->setup_state = FFS_NO_SETUP;
384 return ffs->ep0req_status;
387 static int __ffs_ep0_stall(struct ffs_data *ffs)
389 if (ffs->ev.can_stall) {
390 pr_vdebug("ep0 stall\n");
391 usb_ep_set_halt(ffs->gadget->ep0);
392 ffs->setup_state = FFS_NO_SETUP;
395 pr_debug("bogus ep0 stall!\n");
400 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
401 size_t len, loff_t *ptr)
403 struct ffs_data *ffs = file->private_data;
409 /* Fast check if setup was canceled */
410 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
414 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
415 if (unlikely(ret < 0))
419 switch (ffs->state) {
420 case FFS_READ_DESCRIPTORS:
421 case FFS_READ_STRINGS:
423 if (unlikely(len < 16)) {
428 data = ffs_prepare_buffer(buf, len);
435 if (ffs->state == FFS_READ_DESCRIPTORS) {
436 pr_info("read descriptors\n");
437 ret = __ffs_data_got_descs(ffs, data, len);
438 if (unlikely(ret < 0))
441 ffs->state = FFS_READ_STRINGS;
444 pr_info("read strings\n");
445 ret = __ffs_data_got_strings(ffs, data, len);
446 if (unlikely(ret < 0))
449 ret = ffs_epfiles_create(ffs);
451 ffs->state = FFS_CLOSING;
455 ffs->state = FFS_ACTIVE;
456 mutex_unlock(&ffs->mutex);
458 ret = functionfs_ready_callback(ffs);
459 if (unlikely(ret < 0)) {
460 ffs->state = FFS_CLOSING;
464 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
472 * We're called from user space, we can use _irq
473 * rather then _irqsave
475 spin_lock_irq(&ffs->ev.waitq.lock);
476 switch (FFS_SETUP_STATE(ffs)) {
477 case FFS_SETUP_CANCELED:
485 case FFS_SETUP_PENDING:
489 /* FFS_SETUP_PENDING */
490 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
491 spin_unlock_irq(&ffs->ev.waitq.lock);
492 ret = __ffs_ep0_stall(ffs);
496 /* FFS_SETUP_PENDING and not stall */
497 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
499 spin_unlock_irq(&ffs->ev.waitq.lock);
501 data = ffs_prepare_buffer(buf, len);
507 spin_lock_irq(&ffs->ev.waitq.lock);
510 * We are guaranteed to be still in FFS_ACTIVE state
511 * but the state of setup could have changed from
512 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
513 * to check for that. If that happened we copied data
514 * from user space in vain but it's unlikely.
516 * For sure we are not in FFS_NO_SETUP since this is
517 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
518 * transition can be performed and it's protected by
521 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
524 spin_unlock_irq(&ffs->ev.waitq.lock);
526 /* unlocks spinlock */
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
537 mutex_unlock(&ffs->mutex);
541 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
545 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
548 struct usb_functionfs_event events[n];
551 memset(events, 0, sizeof events);
554 events[i].type = ffs->ev.types[i];
555 if (events[i].type == FUNCTIONFS_SETUP) {
556 events[i].u.setup = ffs->ev.setup;
557 ffs->setup_state = FFS_SETUP_PENDING;
561 if (n < ffs->ev.count) {
563 memmove(ffs->ev.types, ffs->ev.types + n,
564 ffs->ev.count * sizeof *ffs->ev.types);
569 spin_unlock_irq(&ffs->ev.waitq.lock);
570 mutex_unlock(&ffs->mutex);
572 return unlikely(__copy_to_user(buf, events, sizeof events))
573 ? -EFAULT : sizeof events;
576 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
577 size_t len, loff_t *ptr)
579 struct ffs_data *ffs = file->private_data;
586 /* Fast check if setup was canceled */
587 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
591 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
592 if (unlikely(ret < 0))
596 if (ffs->state != FFS_ACTIVE) {
602 * We're called from user space, we can use _irq rather then
605 spin_lock_irq(&ffs->ev.waitq.lock);
607 switch (FFS_SETUP_STATE(ffs)) {
608 case FFS_SETUP_CANCELED:
613 n = len / sizeof(struct usb_functionfs_event);
619 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
624 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
630 return __ffs_ep0_read_events(ffs, buf,
631 min(n, (size_t)ffs->ev.count));
633 case FFS_SETUP_PENDING:
634 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
635 spin_unlock_irq(&ffs->ev.waitq.lock);
636 ret = __ffs_ep0_stall(ffs);
640 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
642 spin_unlock_irq(&ffs->ev.waitq.lock);
645 data = kmalloc(len, GFP_KERNEL);
646 if (unlikely(!data)) {
652 spin_lock_irq(&ffs->ev.waitq.lock);
654 /* See ffs_ep0_write() */
655 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
660 /* unlocks spinlock */
661 ret = __ffs_ep0_queue_wait(ffs, data, len);
662 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
671 spin_unlock_irq(&ffs->ev.waitq.lock);
673 mutex_unlock(&ffs->mutex);
678 static int ffs_ep0_open(struct inode *inode, struct file *file)
680 struct ffs_data *ffs = inode->i_private;
684 if (unlikely(ffs->state == FFS_CLOSING))
687 file->private_data = ffs;
688 ffs_data_opened(ffs);
693 static int ffs_ep0_release(struct inode *inode, struct file *file)
695 struct ffs_data *ffs = file->private_data;
699 ffs_data_closed(ffs);
704 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706 struct ffs_data *ffs = file->private_data;
707 struct usb_gadget *gadget = ffs->gadget;
712 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
713 struct ffs_function *func = ffs->func;
714 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
715 } else if (gadget && gadget->ops->ioctl) {
716 ret = gadget->ops->ioctl(gadget, code, value);
724 static const struct file_operations ffs_ep0_operations = {
725 .owner = THIS_MODULE,
728 .open = ffs_ep0_open,
729 .write = ffs_ep0_write,
730 .read = ffs_ep0_read,
731 .release = ffs_ep0_release,
732 .unlocked_ioctl = ffs_ep0_ioctl,
736 /* "Normal" endpoints operations ********************************************/
738 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
741 if (likely(req->context)) {
742 struct ffs_ep *ep = _ep->driver_data;
743 ep->status = req->status ? req->status : req->actual;
744 complete(req->context);
748 static ssize_t ffs_epfile_io(struct file *file,
749 char __user *buf, size_t len, int read)
751 struct ffs_epfile *epfile = file->private_data;
759 spin_unlock_irq(&epfile->ffs->eps_lock);
760 mutex_unlock(&epfile->mutex);
763 /* Are we still active? */
764 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
769 /* Wait for endpoint to be enabled */
772 if (file->f_flags & O_NONBLOCK) {
777 if (wait_event_interruptible(epfile->wait,
778 (ep = epfile->ep))) {
785 halt = !read == !epfile->in;
786 if (halt && epfile->isoc) {
791 /* Allocate & copy */
792 if (!halt && !data) {
793 data = kzalloc(len, GFP_KERNEL);
798 unlikely(__copy_from_user(data, buf, len))) {
804 /* We will be using request */
805 ret = ffs_mutex_lock(&epfile->mutex,
806 file->f_flags & O_NONBLOCK);
811 * We're called from user space, we can use _irq rather then
814 spin_lock_irq(&epfile->ffs->eps_lock);
817 * While we were acquiring mutex endpoint got disabled
820 } while (unlikely(epfile->ep != ep));
823 if (unlikely(halt)) {
824 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
825 usb_ep_set_halt(ep->ep);
826 spin_unlock_irq(&epfile->ffs->eps_lock);
829 /* Fire the request */
830 DECLARE_COMPLETION_ONSTACK(done);
832 struct usb_request *req = ep->req;
833 req->context = &done;
834 req->complete = ffs_epfile_io_complete;
838 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
840 spin_unlock_irq(&epfile->ffs->eps_lock);
842 if (unlikely(ret < 0)) {
844 } else if (unlikely(wait_for_completion_interruptible(&done))) {
846 usb_ep_dequeue(ep->ep, req);
849 if (read && ret > 0 &&
850 unlikely(copy_to_user(buf, data, ret)))
855 mutex_unlock(&epfile->mutex);
862 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
867 return ffs_epfile_io(file, (char __user *)buf, len, 0);
871 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
875 return ffs_epfile_io(file, buf, len, 1);
879 ffs_epfile_open(struct inode *inode, struct file *file)
881 struct ffs_epfile *epfile = inode->i_private;
885 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
888 file->private_data = epfile;
889 ffs_data_opened(epfile->ffs);
895 ffs_epfile_release(struct inode *inode, struct file *file)
897 struct ffs_epfile *epfile = inode->i_private;
901 ffs_data_closed(epfile->ffs);
906 static long ffs_epfile_ioctl(struct file *file, unsigned code,
909 struct ffs_epfile *epfile = file->private_data;
914 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
917 spin_lock_irq(&epfile->ffs->eps_lock);
918 if (likely(epfile->ep)) {
920 case FUNCTIONFS_FIFO_STATUS:
921 ret = usb_ep_fifo_status(epfile->ep->ep);
923 case FUNCTIONFS_FIFO_FLUSH:
924 usb_ep_fifo_flush(epfile->ep->ep);
927 case FUNCTIONFS_CLEAR_HALT:
928 ret = usb_ep_clear_halt(epfile->ep->ep);
930 case FUNCTIONFS_ENDPOINT_REVMAP:
931 ret = epfile->ep->num;
939 spin_unlock_irq(&epfile->ffs->eps_lock);
944 static const struct file_operations ffs_epfile_operations = {
945 .owner = THIS_MODULE,
948 .open = ffs_epfile_open,
949 .write = ffs_epfile_write,
950 .read = ffs_epfile_read,
951 .release = ffs_epfile_release,
952 .unlocked_ioctl = ffs_epfile_ioctl,
956 /* File system and super block operations ***********************************/
959 * Mounting the file system creates a controller file, used first for
960 * function configuration then later for event monitoring.
963 static struct inode *__must_check
964 ffs_sb_make_inode(struct super_block *sb, void *data,
965 const struct file_operations *fops,
966 const struct inode_operations *iops,
967 struct ffs_file_perms *perms)
973 inode = new_inode(sb);
976 struct timespec current_time = CURRENT_TIME;
978 inode->i_ino = get_next_ino();
979 inode->i_mode = perms->mode;
980 inode->i_uid = perms->uid;
981 inode->i_gid = perms->gid;
982 inode->i_atime = current_time;
983 inode->i_mtime = current_time;
984 inode->i_ctime = current_time;
985 inode->i_private = data;
995 /* Create "regular" file */
996 static struct inode *ffs_sb_create_file(struct super_block *sb,
997 const char *name, void *data,
998 const struct file_operations *fops,
999 struct dentry **dentry_p)
1001 struct ffs_data *ffs = sb->s_fs_info;
1002 struct dentry *dentry;
1003 struct inode *inode;
1007 dentry = d_alloc_name(sb->s_root, name);
1008 if (unlikely(!dentry))
1011 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1012 if (unlikely(!inode)) {
1017 d_add(dentry, inode);
1025 static const struct super_operations ffs_sb_operations = {
1026 .statfs = simple_statfs,
1027 .drop_inode = generic_delete_inode,
1030 struct ffs_sb_fill_data {
1031 struct ffs_file_perms perms;
1033 const char *dev_name;
1035 /* set by ffs_fs_mount(), read by ffs_sb_fill() */
1037 /* set by ffs_sb_fill(), read by ffs_fs_mount */
1038 struct ffs_data *ffs_data;
1042 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1044 struct ffs_sb_fill_data *data = _data;
1045 struct inode *inode;
1046 struct ffs_data *ffs;
1050 /* Initialise data */
1051 ffs = ffs_data_new();
1056 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1057 if (unlikely(!ffs->dev_name))
1059 ffs->file_perms = data->perms;
1060 ffs->private_data = data->private_data;
1062 /* used by the caller of this function */
1063 data->ffs_data = ffs;
1065 sb->s_fs_info = ffs;
1066 sb->s_blocksize = PAGE_CACHE_SIZE;
1067 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1068 sb->s_magic = FUNCTIONFS_MAGIC;
1069 sb->s_op = &ffs_sb_operations;
1070 sb->s_time_gran = 1;
1073 data->perms.mode = data->root_mode;
1074 inode = ffs_sb_make_inode(sb, NULL,
1075 &simple_dir_operations,
1076 &simple_dir_inode_operations,
1078 sb->s_root = d_make_root(inode);
1079 if (unlikely(!sb->s_root))
1083 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1084 &ffs_ep0_operations, NULL)))
1093 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1097 if (!opts || !*opts)
1101 char *end, *eq, *comma;
1102 unsigned long value;
1105 comma = strchr(opts, ',');
1110 eq = strchr(opts, '=');
1111 if (unlikely(!eq)) {
1112 pr_err("'=' missing in %s\n", opts);
1118 value = simple_strtoul(eq + 1, &end, 0);
1119 if (unlikely(*end != ',' && *end != 0)) {
1120 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1124 /* Interpret option */
1125 switch (eq - opts) {
1127 if (!memcmp(opts, "rmode", 5))
1128 data->root_mode = (value & 0555) | S_IFDIR;
1129 else if (!memcmp(opts, "fmode", 5))
1130 data->perms.mode = (value & 0666) | S_IFREG;
1136 if (!memcmp(opts, "mode", 4)) {
1137 data->root_mode = (value & 0555) | S_IFDIR;
1138 data->perms.mode = (value & 0666) | S_IFREG;
1145 if (!memcmp(opts, "uid", 3))
1146 data->perms.uid = value;
1147 else if (!memcmp(opts, "gid", 3))
1148 data->perms.gid = value;
1155 pr_err("%s: invalid option\n", opts);
1159 /* Next iteration */
1168 /* "mount -t functionfs dev_name /dev/function" ends up here */
1170 static struct dentry *
1171 ffs_fs_mount(struct file_system_type *t, int flags,
1172 const char *dev_name, void *opts)
1174 struct ffs_sb_fill_data data = {
1176 .mode = S_IFREG | 0600,
1180 .root_mode = S_IFDIR | 0500,
1188 ret = ffs_fs_parse_opts(&data, opts);
1189 if (unlikely(ret < 0))
1190 return ERR_PTR(ret);
1192 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1193 if (IS_ERR(ffs_dev))
1196 data.dev_name = dev_name;
1197 data.private_data = ffs_dev;
1198 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1200 /* data.ffs_data is set by ffs_sb_fill */
1202 functionfs_release_dev_callback(data.ffs_data);
1208 ffs_fs_kill_sb(struct super_block *sb)
1212 kill_litter_super(sb);
1213 if (sb->s_fs_info) {
1214 functionfs_release_dev_callback(sb->s_fs_info);
1215 ffs_data_put(sb->s_fs_info);
1219 static struct file_system_type ffs_fs_type = {
1220 .owner = THIS_MODULE,
1221 .name = "functionfs",
1222 .mount = ffs_fs_mount,
1223 .kill_sb = ffs_fs_kill_sb,
1227 /* Driver's main init/cleanup functions *************************************/
1229 static int functionfs_init(void)
1235 ret = register_filesystem(&ffs_fs_type);
1237 pr_info("file system registered\n");
1239 pr_err("failed registering file system (%d)\n", ret);
1244 static void functionfs_cleanup(void)
1248 pr_info("unloading\n");
1249 unregister_filesystem(&ffs_fs_type);
1253 /* ffs_data and ffs_function construction and destruction code **************/
1255 static void ffs_data_clear(struct ffs_data *ffs);
1256 static void ffs_data_reset(struct ffs_data *ffs);
1258 static void ffs_data_get(struct ffs_data *ffs)
1262 atomic_inc(&ffs->ref);
1265 static void ffs_data_opened(struct ffs_data *ffs)
1269 atomic_inc(&ffs->ref);
1270 atomic_inc(&ffs->opened);
1273 static void ffs_data_put(struct ffs_data *ffs)
1277 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1278 pr_info("%s(): freeing\n", __func__);
1279 ffs_data_clear(ffs);
1280 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1281 waitqueue_active(&ffs->ep0req_completion.wait));
1282 kfree(ffs->dev_name);
1287 static void ffs_data_closed(struct ffs_data *ffs)
1291 if (atomic_dec_and_test(&ffs->opened)) {
1292 ffs->state = FFS_CLOSING;
1293 ffs_data_reset(ffs);
1299 static struct ffs_data *ffs_data_new(void)
1301 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1307 atomic_set(&ffs->ref, 1);
1308 atomic_set(&ffs->opened, 0);
1309 ffs->state = FFS_READ_DESCRIPTORS;
1310 mutex_init(&ffs->mutex);
1311 spin_lock_init(&ffs->eps_lock);
1312 init_waitqueue_head(&ffs->ev.waitq);
1313 init_completion(&ffs->ep0req_completion);
1315 /* XXX REVISIT need to update it in some places, or do we? */
1316 ffs->ev.can_stall = 1;
1321 static void ffs_data_clear(struct ffs_data *ffs)
1325 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1326 functionfs_closed_callback(ffs);
1328 BUG_ON(ffs->gadget);
1331 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1333 kfree(ffs->raw_descs);
1334 kfree(ffs->raw_strings);
1335 kfree(ffs->stringtabs);
1338 static void ffs_data_reset(struct ffs_data *ffs)
1342 ffs_data_clear(ffs);
1344 ffs->epfiles = NULL;
1345 ffs->raw_descs = NULL;
1346 ffs->raw_strings = NULL;
1347 ffs->stringtabs = NULL;
1349 ffs->raw_descs_length = 0;
1350 ffs->raw_fs_descs_length = 0;
1351 ffs->fs_descs_count = 0;
1352 ffs->hs_descs_count = 0;
1354 ffs->strings_count = 0;
1355 ffs->interfaces_count = 0;
1360 ffs->state = FFS_READ_DESCRIPTORS;
1361 ffs->setup_state = FFS_NO_SETUP;
1366 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1368 struct usb_gadget_strings **lang;
1373 if (WARN_ON(ffs->state != FFS_ACTIVE
1374 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1377 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1378 if (unlikely(first_id < 0))
1381 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1382 if (unlikely(!ffs->ep0req))
1384 ffs->ep0req->complete = ffs_ep0_complete;
1385 ffs->ep0req->context = ffs;
1387 lang = ffs->stringtabs;
1388 for (lang = ffs->stringtabs; *lang; ++lang) {
1389 struct usb_string *str = (*lang)->strings;
1391 for (; str->s; ++id, ++str)
1395 ffs->gadget = cdev->gadget;
1400 static void functionfs_unbind(struct ffs_data *ffs)
1404 if (!WARN_ON(!ffs->gadget)) {
1405 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1409 clear_bit(FFS_FL_BOUND, &ffs->flags);
1413 static int ffs_epfiles_create(struct ffs_data *ffs)
1415 struct ffs_epfile *epfile, *epfiles;
1420 count = ffs->eps_count;
1421 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1426 for (i = 1; i <= count; ++i, ++epfile) {
1428 mutex_init(&epfile->mutex);
1429 init_waitqueue_head(&epfile->wait);
1430 sprintf(epfiles->name, "ep%u", i);
1431 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1432 &ffs_epfile_operations,
1433 &epfile->dentry))) {
1434 ffs_epfiles_destroy(epfiles, i - 1);
1439 ffs->epfiles = epfiles;
1443 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1445 struct ffs_epfile *epfile = epfiles;
1449 for (; count; --count, ++epfile) {
1450 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1451 waitqueue_active(&epfile->wait));
1452 if (epfile->dentry) {
1453 d_delete(epfile->dentry);
1454 dput(epfile->dentry);
1455 epfile->dentry = NULL;
1462 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1463 struct usb_configuration *c,
1464 struct ffs_data *ffs)
1466 struct ffs_function *func;
1471 func = kzalloc(sizeof *func, GFP_KERNEL);
1472 if (unlikely(!func))
1475 func->function.name = "Function FS Gadget";
1476 func->function.strings = ffs->stringtabs;
1478 func->function.bind = ffs_func_bind;
1479 func->function.unbind = ffs_func_unbind;
1480 func->function.set_alt = ffs_func_set_alt;
1481 func->function.disable = ffs_func_disable;
1482 func->function.setup = ffs_func_setup;
1483 func->function.suspend = ffs_func_suspend;
1484 func->function.resume = ffs_func_resume;
1487 func->gadget = cdev->gadget;
1491 ret = usb_add_function(c, &func->function);
1493 ffs_func_free(func);
1498 static void ffs_func_free(struct ffs_function *func)
1500 struct ffs_ep *ep = func->eps;
1501 unsigned count = func->ffs->eps_count;
1502 unsigned long flags;
1506 /* cleanup after autoconfig */
1507 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1509 if (ep->ep && ep->req)
1510 usb_ep_free_request(ep->ep, ep->req);
1514 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1516 ffs_data_put(func->ffs);
1520 * eps and interfaces_nums are allocated in the same chunk so
1521 * only one free is required. Descriptors are also allocated
1522 * in the same chunk.
1528 static void ffs_func_eps_disable(struct ffs_function *func)
1530 struct ffs_ep *ep = func->eps;
1531 struct ffs_epfile *epfile = func->ffs->epfiles;
1532 unsigned count = func->ffs->eps_count;
1533 unsigned long flags;
1535 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1537 /* pending requests get nuked */
1539 usb_ep_disable(ep->ep);
1545 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1548 static int ffs_func_eps_enable(struct ffs_function *func)
1550 struct ffs_data *ffs = func->ffs;
1551 struct ffs_ep *ep = func->eps;
1552 struct ffs_epfile *epfile = ffs->epfiles;
1553 unsigned count = ffs->eps_count;
1554 unsigned long flags;
1557 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1559 struct usb_endpoint_descriptor *ds;
1560 ds = ep->descs[ep->descs[1] ? 1 : 0];
1562 ep->ep->driver_data = ep;
1564 ret = usb_ep_enable(ep->ep);
1567 epfile->in = usb_endpoint_dir_in(ds);
1568 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1573 wake_up(&epfile->wait);
1578 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1584 /* Parsing and building descriptors and strings *****************************/
1587 * This validates if data pointed by data is a valid USB descriptor as
1588 * well as record how many interfaces, endpoints and strings are
1589 * required by given configuration. Returns address after the
1590 * descriptor or NULL if data is invalid.
1593 enum ffs_entity_type {
1594 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1597 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1599 struct usb_descriptor_header *desc,
1602 static int __must_check ffs_do_desc(char *data, unsigned len,
1603 ffs_entity_callback entity, void *priv)
1605 struct usb_descriptor_header *_ds = (void *)data;
1611 /* At least two bytes are required: length and type */
1613 pr_vdebug("descriptor too short\n");
1617 /* If we have at least as many bytes as the descriptor takes? */
1618 length = _ds->bLength;
1620 pr_vdebug("descriptor longer then available data\n");
1624 #define __entity_check_INTERFACE(val) 1
1625 #define __entity_check_STRING(val) (val)
1626 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1627 #define __entity(type, val) do { \
1628 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1629 if (unlikely(!__entity_check_ ##type(val))) { \
1630 pr_vdebug("invalid entity's value\n"); \
1633 ret = entity(FFS_ ##type, &val, _ds, priv); \
1634 if (unlikely(ret < 0)) { \
1635 pr_debug("entity " #type "(%02x); ret = %d\n", \
1641 /* Parse descriptor depending on type. */
1642 switch (_ds->bDescriptorType) {
1646 case USB_DT_DEVICE_QUALIFIER:
1647 /* function can't have any of those */
1648 pr_vdebug("descriptor reserved for gadget: %d\n",
1649 _ds->bDescriptorType);
1652 case USB_DT_INTERFACE: {
1653 struct usb_interface_descriptor *ds = (void *)_ds;
1654 pr_vdebug("interface descriptor\n");
1655 if (length != sizeof *ds)
1658 __entity(INTERFACE, ds->bInterfaceNumber);
1660 __entity(STRING, ds->iInterface);
1664 case USB_DT_ENDPOINT: {
1665 struct usb_endpoint_descriptor *ds = (void *)_ds;
1666 pr_vdebug("endpoint descriptor\n");
1667 if (length != USB_DT_ENDPOINT_SIZE &&
1668 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1670 __entity(ENDPOINT, ds->bEndpointAddress);
1675 if (length != sizeof(struct usb_otg_descriptor))
1679 case USB_DT_INTERFACE_ASSOCIATION: {
1680 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1681 pr_vdebug("interface association descriptor\n");
1682 if (length != sizeof *ds)
1685 __entity(STRING, ds->iFunction);
1689 case USB_DT_OTHER_SPEED_CONFIG:
1690 case USB_DT_INTERFACE_POWER:
1692 case USB_DT_SECURITY:
1693 case USB_DT_CS_RADIO_CONTROL:
1695 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1699 /* We should never be here */
1700 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1704 pr_vdebug("invalid length: %d (descriptor %d)\n",
1705 _ds->bLength, _ds->bDescriptorType);
1710 #undef __entity_check_DESCRIPTOR
1711 #undef __entity_check_INTERFACE
1712 #undef __entity_check_STRING
1713 #undef __entity_check_ENDPOINT
1718 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1719 ffs_entity_callback entity, void *priv)
1721 const unsigned _len = len;
1722 unsigned long num = 0;
1732 /* Record "descriptor" entity */
1733 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1734 if (unlikely(ret < 0)) {
1735 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1743 ret = ffs_do_desc(data, len, entity, priv);
1744 if (unlikely(ret < 0)) {
1745 pr_debug("%s returns %d\n", __func__, ret);
1755 static int __ffs_data_do_entity(enum ffs_entity_type type,
1756 u8 *valuep, struct usb_descriptor_header *desc,
1759 struct ffs_data *ffs = priv;
1764 case FFS_DESCRIPTOR:
1769 * Interfaces are indexed from zero so if we
1770 * encountered interface "n" then there are at least
1773 if (*valuep >= ffs->interfaces_count)
1774 ffs->interfaces_count = *valuep + 1;
1779 * Strings are indexed from 1 (0 is magic ;) reserved
1780 * for languages list or some such)
1782 if (*valuep > ffs->strings_count)
1783 ffs->strings_count = *valuep;
1787 /* Endpoints are indexed from 1 as well. */
1788 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1789 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1796 static int __ffs_data_got_descs(struct ffs_data *ffs,
1797 char *const _data, size_t len)
1799 unsigned fs_count, hs_count;
1800 int fs_len, ret = -EINVAL;
1805 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1806 get_unaligned_le32(data + 4) != len))
1808 fs_count = get_unaligned_le32(data + 8);
1809 hs_count = get_unaligned_le32(data + 12);
1811 if (!fs_count && !hs_count)
1817 if (likely(fs_count)) {
1818 fs_len = ffs_do_descs(fs_count, data, len,
1819 __ffs_data_do_entity, ffs);
1820 if (unlikely(fs_len < 0)) {
1831 if (likely(hs_count)) {
1832 ret = ffs_do_descs(hs_count, data, len,
1833 __ffs_data_do_entity, ffs);
1834 if (unlikely(ret < 0))
1840 if (unlikely(len != ret))
1843 ffs->raw_fs_descs_length = fs_len;
1844 ffs->raw_descs_length = fs_len + ret;
1845 ffs->raw_descs = _data;
1846 ffs->fs_descs_count = fs_count;
1847 ffs->hs_descs_count = hs_count;
1858 static int __ffs_data_got_strings(struct ffs_data *ffs,
1859 char *const _data, size_t len)
1861 u32 str_count, needed_count, lang_count;
1862 struct usb_gadget_strings **stringtabs, *t;
1863 struct usb_string *strings, *s;
1864 const char *data = _data;
1868 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1869 get_unaligned_le32(data + 4) != len))
1871 str_count = get_unaligned_le32(data + 8);
1872 lang_count = get_unaligned_le32(data + 12);
1874 /* if one is zero the other must be zero */
1875 if (unlikely(!str_count != !lang_count))
1878 /* Do we have at least as many strings as descriptors need? */
1879 needed_count = ffs->strings_count;
1880 if (unlikely(str_count < needed_count))
1884 * If we don't need any strings just return and free all
1887 if (!needed_count) {
1892 /* Allocate everything in one chunk so there's less maintenance. */
1895 struct usb_gadget_strings *stringtabs[lang_count + 1];
1896 struct usb_gadget_strings stringtab[lang_count];
1897 struct usb_string strings[lang_count*(needed_count+1)];
1901 d = kmalloc(sizeof *d, GFP_KERNEL);
1907 stringtabs = d->stringtabs;
1911 *stringtabs++ = t++;
1915 stringtabs = d->stringtabs;
1921 /* For each language */
1925 do { /* lang_count > 0 so we can use do-while */
1926 unsigned needed = needed_count;
1928 if (unlikely(len < 3))
1930 t->language = get_unaligned_le16(data);
1937 /* For each string */
1938 do { /* str_count > 0 so we can use do-while */
1939 size_t length = strnlen(data, len);
1941 if (unlikely(length == len))
1945 * User may provide more strings then we need,
1946 * if that's the case we simply ignore the
1949 if (likely(needed)) {
1951 * s->id will be set while adding
1952 * function to configuration so for
1953 * now just leave garbage here.
1962 } while (--str_count);
1964 s->id = 0; /* terminator */
1968 } while (--lang_count);
1970 /* Some garbage left? */
1975 ffs->stringtabs = stringtabs;
1976 ffs->raw_strings = _data;
1988 /* Events handling and management *******************************************/
1990 static void __ffs_event_add(struct ffs_data *ffs,
1991 enum usb_functionfs_event_type type)
1993 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1997 * Abort any unhandled setup
1999 * We do not need to worry about some cmpxchg() changing value
2000 * of ffs->setup_state without holding the lock because when
2001 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2002 * the source does nothing.
2004 if (ffs->setup_state == FFS_SETUP_PENDING)
2005 ffs->setup_state = FFS_SETUP_CANCELED;
2008 case FUNCTIONFS_RESUME:
2009 rem_type2 = FUNCTIONFS_SUSPEND;
2011 case FUNCTIONFS_SUSPEND:
2012 case FUNCTIONFS_SETUP:
2014 /* Discard all similar events */
2017 case FUNCTIONFS_BIND:
2018 case FUNCTIONFS_UNBIND:
2019 case FUNCTIONFS_DISABLE:
2020 case FUNCTIONFS_ENABLE:
2021 /* Discard everything other then power management. */
2022 rem_type1 = FUNCTIONFS_SUSPEND;
2023 rem_type2 = FUNCTIONFS_RESUME;
2032 u8 *ev = ffs->ev.types, *out = ev;
2033 unsigned n = ffs->ev.count;
2034 for (; n; --n, ++ev)
2035 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2038 pr_vdebug("purging event %d\n", *ev);
2039 ffs->ev.count = out - ffs->ev.types;
2042 pr_vdebug("adding event %d\n", type);
2043 ffs->ev.types[ffs->ev.count++] = type;
2044 wake_up_locked(&ffs->ev.waitq);
2047 static void ffs_event_add(struct ffs_data *ffs,
2048 enum usb_functionfs_event_type type)
2050 unsigned long flags;
2051 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2052 __ffs_event_add(ffs, type);
2053 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2057 /* Bind/unbind USB function hooks *******************************************/
2059 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2060 struct usb_descriptor_header *desc,
2063 struct usb_endpoint_descriptor *ds = (void *)desc;
2064 struct ffs_function *func = priv;
2065 struct ffs_ep *ffs_ep;
2068 * If hs_descriptors is not NULL then we are reading hs
2071 const int isHS = func->function.hs_descriptors != NULL;
2074 if (type != FFS_DESCRIPTOR)
2078 func->function.hs_descriptors[(long)valuep] = desc;
2080 func->function.descriptors[(long)valuep] = desc;
2082 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2085 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2086 ffs_ep = func->eps + idx;
2088 if (unlikely(ffs_ep->descs[isHS])) {
2089 pr_vdebug("two %sspeed descriptors for EP %d\n",
2090 isHS ? "high" : "full",
2091 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2094 ffs_ep->descs[isHS] = ds;
2096 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2098 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2099 if (!ds->wMaxPacketSize)
2100 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2102 struct usb_request *req;
2105 pr_vdebug("autoconfig\n");
2106 ep = usb_ep_autoconfig(func->gadget, ds);
2109 ep->driver_data = func->eps + idx;
2111 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2117 func->eps_revmap[ds->bEndpointAddress &
2118 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2120 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2125 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2126 struct usb_descriptor_header *desc,
2129 struct ffs_function *func = priv;
2135 case FFS_DESCRIPTOR:
2136 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2141 if (func->interfaces_nums[idx] < 0) {
2142 int id = usb_interface_id(func->conf, &func->function);
2143 if (unlikely(id < 0))
2145 func->interfaces_nums[idx] = id;
2147 newValue = func->interfaces_nums[idx];
2151 /* String' IDs are allocated when fsf_data is bound to cdev */
2152 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2157 * USB_DT_ENDPOINT are handled in
2158 * __ffs_func_bind_do_descs().
2160 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2163 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2164 if (unlikely(!func->eps[idx].ep))
2168 struct usb_endpoint_descriptor **descs;
2169 descs = func->eps[idx].descs;
2170 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2175 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2180 static int ffs_func_bind(struct usb_configuration *c,
2181 struct usb_function *f)
2183 struct ffs_function *func = ffs_func_from_usb(f);
2184 struct ffs_data *ffs = func->ffs;
2186 const int full = !!func->ffs->fs_descs_count;
2187 const int high = gadget_is_dualspeed(func->gadget) &&
2188 func->ffs->hs_descs_count;
2192 /* Make it a single chunk, less management later on */
2194 struct ffs_ep eps[ffs->eps_count];
2195 struct usb_descriptor_header
2196 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2197 struct usb_descriptor_header
2198 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2199 short inums[ffs->interfaces_count];
2200 char raw_descs[high ? ffs->raw_descs_length
2201 : ffs->raw_fs_descs_length];
2206 /* Only high speed but not supported by gadget? */
2207 if (unlikely(!(full | high)))
2211 data = kmalloc(sizeof *data, GFP_KERNEL);
2212 if (unlikely(!data))
2216 memset(data->eps, 0, sizeof data->eps);
2217 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2218 memset(data->inums, 0xff, sizeof data->inums);
2219 for (ret = ffs->eps_count; ret; --ret)
2220 data->eps[ret].num = -1;
2223 func->eps = data->eps;
2224 func->interfaces_nums = data->inums;
2227 * Go through all the endpoint descriptors and allocate
2228 * endpoints first, so that later we can rewrite the endpoint
2229 * numbers without worrying that it may be described later on.
2232 func->function.descriptors = data->fs_descs;
2233 ret = ffs_do_descs(ffs->fs_descs_count,
2235 sizeof data->raw_descs,
2236 __ffs_func_bind_do_descs, func);
2237 if (unlikely(ret < 0))
2244 func->function.hs_descriptors = data->hs_descs;
2245 ret = ffs_do_descs(ffs->hs_descs_count,
2246 data->raw_descs + ret,
2247 (sizeof data->raw_descs) - ret,
2248 __ffs_func_bind_do_descs, func);
2252 * Now handle interface numbers allocation and interface and
2253 * endpoint numbers rewriting. We can do that in one go
2256 ret = ffs_do_descs(ffs->fs_descs_count +
2257 (high ? ffs->hs_descs_count : 0),
2258 data->raw_descs, sizeof data->raw_descs,
2259 __ffs_func_bind_do_nums, func);
2260 if (unlikely(ret < 0))
2263 /* And we're done */
2264 ffs_event_add(ffs, FUNCTIONFS_BIND);
2268 /* XXX Do we need to release all claimed endpoints here? */
2273 /* Other USB function hooks *************************************************/
2275 static void ffs_func_unbind(struct usb_configuration *c,
2276 struct usb_function *f)
2278 struct ffs_function *func = ffs_func_from_usb(f);
2279 struct ffs_data *ffs = func->ffs;
2283 if (ffs->func == func) {
2284 ffs_func_eps_disable(func);
2288 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2290 ffs_func_free(func);
2293 static int ffs_func_set_alt(struct usb_function *f,
2294 unsigned interface, unsigned alt)
2296 struct ffs_function *func = ffs_func_from_usb(f);
2297 struct ffs_data *ffs = func->ffs;
2300 if (alt != (unsigned)-1) {
2301 intf = ffs_func_revmap_intf(func, interface);
2302 if (unlikely(intf < 0))
2307 ffs_func_eps_disable(ffs->func);
2309 if (ffs->state != FFS_ACTIVE)
2312 if (alt == (unsigned)-1) {
2314 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2319 ret = ffs_func_eps_enable(func);
2320 if (likely(ret >= 0))
2321 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2325 static void ffs_func_disable(struct usb_function *f)
2327 ffs_func_set_alt(f, 0, (unsigned)-1);
2330 static int ffs_func_setup(struct usb_function *f,
2331 const struct usb_ctrlrequest *creq)
2333 struct ffs_function *func = ffs_func_from_usb(f);
2334 struct ffs_data *ffs = func->ffs;
2335 unsigned long flags;
2340 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2341 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2342 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2343 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2344 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2347 * Most requests directed to interface go through here
2348 * (notable exceptions are set/get interface) so we need to
2349 * handle them. All other either handled by composite or
2350 * passed to usb_configuration->setup() (if one is set). No
2351 * matter, we will handle requests directed to endpoint here
2352 * as well (as it's straightforward) but what to do with any
2355 if (ffs->state != FFS_ACTIVE)
2358 switch (creq->bRequestType & USB_RECIP_MASK) {
2359 case USB_RECIP_INTERFACE:
2360 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2361 if (unlikely(ret < 0))
2365 case USB_RECIP_ENDPOINT:
2366 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2367 if (unlikely(ret < 0))
2375 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2376 ffs->ev.setup = *creq;
2377 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2378 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2379 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2384 static void ffs_func_suspend(struct usb_function *f)
2387 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2390 static void ffs_func_resume(struct usb_function *f)
2393 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2397 /* Endpoint and interface numbers reverse mapping ***************************/
2399 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2401 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2402 return num ? num : -EDOM;
2405 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2407 short *nums = func->interfaces_nums;
2408 unsigned count = func->ffs->interfaces_count;
2410 for (; count; --count, ++nums) {
2411 if (*nums >= 0 && *nums == intf)
2412 return nums - func->interfaces_nums;
2419 /* Misc helper functions ****************************************************/
2421 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2424 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2425 : mutex_lock_interruptible(mutex);
2428 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2435 data = kmalloc(len, GFP_KERNEL);
2436 if (unlikely(!data))
2437 return ERR_PTR(-ENOMEM);
2439 if (unlikely(__copy_from_user(data, buf, len))) {
2441 return ERR_PTR(-EFAULT);
2444 pr_vdebug("Buffer from user space:\n");
2445 ffs_dump_mem("", data, len);