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 ****************************************************************/
37 # define pr_vdebug pr_debug
38 # define ffs_dump_mem(prefix, ptr, len) \
39 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
41 # define pr_vdebug(...) do { } while (0)
42 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
43 #endif /* VERBOSE_DEBUG */
45 #define ENTER() pr_vdebug("%s()\n", __func__)
48 /* The data structure and setup file ****************************************/
52 * Waiting for descriptors and strings.
54 * In this state no open(2), read(2) or write(2) on epfiles
55 * may succeed (which should not be the problem as there
56 * should be no such files opened in the first place).
62 * We've got descriptors and strings. We are or have called
63 * functionfs_ready_callback(). functionfs_bind() may have
64 * been called but we don't know.
66 * This is the only state in which operations on epfiles may
72 * All endpoints have been closed. This state is also set if
73 * we encounter an unrecoverable error. The only
74 * unrecoverable error is situation when after reading strings
75 * from user space we fail to initialise epfiles or
76 * functionfs_ready_callback() returns with error (<0).
78 * In this state no open(2), read(2) or write(2) (both on ep0
79 * as well as epfile) may succeed (at this point epfiles are
80 * unlinked and all closed so this is not a problem; ep0 is
81 * also closed but ep0 file exists and so open(2) on ep0 must
88 enum ffs_setup_state {
89 /* There is no setup request pending. */
92 * User has read events and there was a setup request event
93 * there. The next read/write on ep0 will handle the
98 * There was event pending but before user space handled it
99 * some other event was introduced which canceled existing
100 * setup. If this state is set read/write on ep0 return
101 * -EIDRM. This state is only set when adding event.
112 struct usb_gadget *gadget;
115 * Protect access read/write operations, only one read/write
116 * at a time. As a consequence protects ep0req and company.
117 * While setup request is being processed (queued) this is
123 * Protect access to endpoint related structures (basically
124 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
130 * XXX REVISIT do we need our own request? Since we are not
131 * handling setup requests immediately user space may be so
132 * slow that another setup will be sent to the gadget but this
133 * time not to us but another function and then there could be
134 * a race. Is that the case? Or maybe we can use cdev->req
135 * after all, maybe we just need some spinlock for that?
137 struct usb_request *ep0req; /* P: mutex */
138 struct completion ep0req_completion; /* P: mutex */
139 int ep0req_status; /* P: mutex */
141 /* reference counter */
143 /* how many files are opened (EP0 and others) */
147 enum ffs_state state;
150 * Possible transitions:
151 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
152 * happens only in ep0 read which is P: mutex
153 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
154 * happens only in ep0 i/o which is P: mutex
155 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
156 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
158 enum ffs_setup_state setup_state;
160 #define FFS_SETUP_STATE(ffs) \
161 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
162 FFS_SETUP_CANCELED, FFS_NO_SETUP))
167 unsigned short count;
168 /* XXX REVISIT need to update it in some places, or do we? */
169 unsigned short can_stall;
170 struct usb_ctrlrequest setup;
172 wait_queue_head_t waitq;
173 } ev; /* the whole structure, P: ev.waitq.lock */
177 #define FFS_FL_CALL_CLOSED_CALLBACK 0
178 #define FFS_FL_BOUND 1
180 /* Active function */
181 struct ffs_function *func;
184 * Device name, write once when file system is mounted.
185 * Intended for user to read if she wants.
187 const char *dev_name;
188 /* Private data for our user (ie. gadget). Managed by user. */
191 /* filled by __ffs_data_got_descs() */
193 * Real descriptors are 16 bytes after raw_descs (so you need
194 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
195 * first full speed descriptor). raw_descs_length and
196 * raw_fs_descs_length do not have those 16 bytes added.
198 const void *raw_descs;
199 unsigned raw_descs_length;
200 unsigned raw_fs_descs_length;
201 unsigned fs_descs_count;
202 unsigned hs_descs_count;
204 unsigned short strings_count;
205 unsigned short interfaces_count;
206 unsigned short eps_count;
207 unsigned short _pad1;
209 /* filled by __ffs_data_got_strings() */
210 /* ids in stringtabs are set in functionfs_bind() */
211 const void *raw_strings;
212 struct usb_gadget_strings **stringtabs;
215 * File system's super block, write once when file system is
218 struct super_block *sb;
220 /* File permissions, written once when fs is mounted */
221 struct ffs_file_perms {
228 * The endpoint files, filled by ffs_epfiles_create(),
229 * destroyed by ffs_epfiles_destroy().
231 struct ffs_epfile *epfiles;
234 /* Reference counter handling */
235 static void ffs_data_get(struct ffs_data *ffs);
236 static void ffs_data_put(struct ffs_data *ffs);
237 /* Creates new ffs_data object. */
238 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
240 /* Opened counter handling. */
241 static void ffs_data_opened(struct ffs_data *ffs);
242 static void ffs_data_closed(struct ffs_data *ffs);
244 /* Called with ffs->mutex held; take over ownership of data. */
245 static int __must_check
246 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
247 static int __must_check
248 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
251 /* The function structure ***************************************************/
255 struct ffs_function {
256 struct usb_configuration *conf;
257 struct usb_gadget *gadget;
258 struct ffs_data *ffs;
262 short *interfaces_nums;
264 struct usb_function function;
268 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
270 return container_of(f, struct ffs_function, function);
273 static void ffs_func_free(struct ffs_function *func);
275 static void ffs_func_eps_disable(struct ffs_function *func);
276 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
278 static int ffs_func_bind(struct usb_configuration *,
279 struct usb_function *);
280 static void ffs_func_unbind(struct usb_configuration *,
281 struct usb_function *);
282 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
283 static void ffs_func_disable(struct usb_function *);
284 static int ffs_func_setup(struct usb_function *,
285 const struct usb_ctrlrequest *);
286 static void ffs_func_suspend(struct usb_function *);
287 static void ffs_func_resume(struct usb_function *);
290 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
291 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
294 /* The endpoints structures *************************************************/
297 struct usb_ep *ep; /* P: ffs->eps_lock */
298 struct usb_request *req; /* P: epfile->mutex */
300 /* [0]: full speed, [1]: high speed */
301 struct usb_endpoint_descriptor *descs[2];
305 int status; /* P: epfile->mutex */
309 /* Protects ep->ep and ep->req. */
311 wait_queue_head_t wait;
313 struct ffs_data *ffs;
314 struct ffs_ep *ep; /* P: ffs->eps_lock */
316 struct dentry *dentry;
320 unsigned char in; /* P: ffs->eps_lock */
321 unsigned char isoc; /* P: ffs->eps_lock */
326 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
327 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
329 static struct inode *__must_check
330 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
331 const struct file_operations *fops,
332 struct dentry **dentry_p);
335 /* Misc helper functions ****************************************************/
337 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
338 __attribute__((warn_unused_result, nonnull));
339 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
340 __attribute__((warn_unused_result, nonnull));
343 /* Control file aka ep0 *****************************************************/
345 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
347 struct ffs_data *ffs = req->context;
349 complete_all(&ffs->ep0req_completion);
352 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
354 struct usb_request *req = ffs->ep0req;
357 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
359 spin_unlock_irq(&ffs->ev.waitq.lock);
365 * UDC layer requires to provide a buffer even for ZLP, but should
366 * not use it at all. Let's provide some poisoned pointer to catch
367 * possible bug in the driver.
369 if (req->buf == NULL)
370 req->buf = (void *)0xDEADBABE;
372 INIT_COMPLETION(ffs->ep0req_completion);
374 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
375 if (unlikely(ret < 0))
378 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
380 usb_ep_dequeue(ffs->gadget->ep0, req);
384 ffs->setup_state = FFS_NO_SETUP;
385 return ffs->ep0req_status;
388 static int __ffs_ep0_stall(struct ffs_data *ffs)
390 if (ffs->ev.can_stall) {
391 pr_vdebug("ep0 stall\n");
392 usb_ep_set_halt(ffs->gadget->ep0);
393 ffs->setup_state = FFS_NO_SETUP;
396 pr_debug("bogus ep0 stall!\n");
401 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
402 size_t len, loff_t *ptr)
404 struct ffs_data *ffs = file->private_data;
410 /* Fast check if setup was canceled */
411 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
415 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
416 if (unlikely(ret < 0))
420 switch (ffs->state) {
421 case FFS_READ_DESCRIPTORS:
422 case FFS_READ_STRINGS:
424 if (unlikely(len < 16)) {
429 data = ffs_prepare_buffer(buf, len);
436 if (ffs->state == FFS_READ_DESCRIPTORS) {
437 pr_info("read descriptors\n");
438 ret = __ffs_data_got_descs(ffs, data, len);
439 if (unlikely(ret < 0))
442 ffs->state = FFS_READ_STRINGS;
445 pr_info("read strings\n");
446 ret = __ffs_data_got_strings(ffs, data, len);
447 if (unlikely(ret < 0))
450 ret = ffs_epfiles_create(ffs);
452 ffs->state = FFS_CLOSING;
456 ffs->state = FFS_ACTIVE;
457 mutex_unlock(&ffs->mutex);
459 ret = functionfs_ready_callback(ffs);
460 if (unlikely(ret < 0)) {
461 ffs->state = FFS_CLOSING;
465 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
473 * We're called from user space, we can use _irq
474 * rather then _irqsave
476 spin_lock_irq(&ffs->ev.waitq.lock);
477 switch (FFS_SETUP_STATE(ffs)) {
478 case FFS_SETUP_CANCELED:
486 case FFS_SETUP_PENDING:
490 /* FFS_SETUP_PENDING */
491 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
492 spin_unlock_irq(&ffs->ev.waitq.lock);
493 ret = __ffs_ep0_stall(ffs);
497 /* FFS_SETUP_PENDING and not stall */
498 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
500 spin_unlock_irq(&ffs->ev.waitq.lock);
502 data = ffs_prepare_buffer(buf, len);
508 spin_lock_irq(&ffs->ev.waitq.lock);
511 * We are guaranteed to be still in FFS_ACTIVE state
512 * but the state of setup could have changed from
513 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
514 * to check for that. If that happened we copied data
515 * from user space in vain but it's unlikely.
517 * For sure we are not in FFS_NO_SETUP since this is
518 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
519 * transition can be performed and it's protected by
522 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
525 spin_unlock_irq(&ffs->ev.waitq.lock);
527 /* unlocks spinlock */
528 ret = __ffs_ep0_queue_wait(ffs, data, len);
538 mutex_unlock(&ffs->mutex);
542 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
546 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
549 struct usb_functionfs_event events[n];
552 memset(events, 0, sizeof events);
555 events[i].type = ffs->ev.types[i];
556 if (events[i].type == FUNCTIONFS_SETUP) {
557 events[i].u.setup = ffs->ev.setup;
558 ffs->setup_state = FFS_SETUP_PENDING;
562 if (n < ffs->ev.count) {
564 memmove(ffs->ev.types, ffs->ev.types + n,
565 ffs->ev.count * sizeof *ffs->ev.types);
570 spin_unlock_irq(&ffs->ev.waitq.lock);
571 mutex_unlock(&ffs->mutex);
573 return unlikely(__copy_to_user(buf, events, sizeof events))
574 ? -EFAULT : sizeof events;
577 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
578 size_t len, loff_t *ptr)
580 struct ffs_data *ffs = file->private_data;
587 /* Fast check if setup was canceled */
588 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
592 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
593 if (unlikely(ret < 0))
597 if (ffs->state != FFS_ACTIVE) {
603 * We're called from user space, we can use _irq rather then
606 spin_lock_irq(&ffs->ev.waitq.lock);
608 switch (FFS_SETUP_STATE(ffs)) {
609 case FFS_SETUP_CANCELED:
614 n = len / sizeof(struct usb_functionfs_event);
620 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
625 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
631 return __ffs_ep0_read_events(ffs, buf,
632 min(n, (size_t)ffs->ev.count));
634 case FFS_SETUP_PENDING:
635 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
636 spin_unlock_irq(&ffs->ev.waitq.lock);
637 ret = __ffs_ep0_stall(ffs);
641 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
643 spin_unlock_irq(&ffs->ev.waitq.lock);
646 data = kmalloc(len, GFP_KERNEL);
647 if (unlikely(!data)) {
653 spin_lock_irq(&ffs->ev.waitq.lock);
655 /* See ffs_ep0_write() */
656 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
661 /* unlocks spinlock */
662 ret = __ffs_ep0_queue_wait(ffs, data, len);
663 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
672 spin_unlock_irq(&ffs->ev.waitq.lock);
674 mutex_unlock(&ffs->mutex);
679 static int ffs_ep0_open(struct inode *inode, struct file *file)
681 struct ffs_data *ffs = inode->i_private;
685 if (unlikely(ffs->state == FFS_CLOSING))
688 file->private_data = ffs;
689 ffs_data_opened(ffs);
694 static int ffs_ep0_release(struct inode *inode, struct file *file)
696 struct ffs_data *ffs = file->private_data;
700 ffs_data_closed(ffs);
705 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
707 struct ffs_data *ffs = file->private_data;
708 struct usb_gadget *gadget = ffs->gadget;
713 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
714 struct ffs_function *func = ffs->func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
716 } else if (gadget && gadget->ops->ioctl) {
717 ret = gadget->ops->ioctl(gadget, code, value);
725 static const struct file_operations ffs_ep0_operations = {
726 .owner = THIS_MODULE,
729 .open = ffs_ep0_open,
730 .write = ffs_ep0_write,
731 .read = ffs_ep0_read,
732 .release = ffs_ep0_release,
733 .unlocked_ioctl = ffs_ep0_ioctl,
737 /* "Normal" endpoints operations ********************************************/
739 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
742 if (likely(req->context)) {
743 struct ffs_ep *ep = _ep->driver_data;
744 ep->status = req->status ? req->status : req->actual;
745 complete(req->context);
749 static ssize_t ffs_epfile_io(struct file *file,
750 char __user *buf, size_t len, int read)
752 struct ffs_epfile *epfile = file->private_data;
760 spin_unlock_irq(&epfile->ffs->eps_lock);
761 mutex_unlock(&epfile->mutex);
764 /* Are we still active? */
765 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
770 /* Wait for endpoint to be enabled */
773 if (file->f_flags & O_NONBLOCK) {
778 if (wait_event_interruptible(epfile->wait,
779 (ep = epfile->ep))) {
786 halt = !read == !epfile->in;
787 if (halt && epfile->isoc) {
792 /* Allocate & copy */
793 if (!halt && !data) {
794 data = kzalloc(len, GFP_KERNEL);
799 unlikely(__copy_from_user(data, buf, len))) {
805 /* We will be using request */
806 ret = ffs_mutex_lock(&epfile->mutex,
807 file->f_flags & O_NONBLOCK);
812 * We're called from user space, we can use _irq rather then
815 spin_lock_irq(&epfile->ffs->eps_lock);
818 * While we were acquiring mutex endpoint got disabled
821 } while (unlikely(epfile->ep != ep));
824 if (unlikely(halt)) {
825 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
826 usb_ep_set_halt(ep->ep);
827 spin_unlock_irq(&epfile->ffs->eps_lock);
830 /* Fire the request */
831 DECLARE_COMPLETION_ONSTACK(done);
833 struct usb_request *req = ep->req;
834 req->context = &done;
835 req->complete = ffs_epfile_io_complete;
839 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
841 spin_unlock_irq(&epfile->ffs->eps_lock);
843 if (unlikely(ret < 0)) {
845 } else if (unlikely(wait_for_completion_interruptible(&done))) {
847 usb_ep_dequeue(ep->ep, req);
850 if (read && ret > 0 &&
851 unlikely(copy_to_user(buf, data, ret)))
856 mutex_unlock(&epfile->mutex);
863 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
868 return ffs_epfile_io(file, (char __user *)buf, len, 0);
872 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
876 return ffs_epfile_io(file, buf, len, 1);
880 ffs_epfile_open(struct inode *inode, struct file *file)
882 struct ffs_epfile *epfile = inode->i_private;
886 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
889 file->private_data = epfile;
890 ffs_data_opened(epfile->ffs);
896 ffs_epfile_release(struct inode *inode, struct file *file)
898 struct ffs_epfile *epfile = inode->i_private;
902 ffs_data_closed(epfile->ffs);
907 static long ffs_epfile_ioctl(struct file *file, unsigned code,
910 struct ffs_epfile *epfile = file->private_data;
915 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
918 spin_lock_irq(&epfile->ffs->eps_lock);
919 if (likely(epfile->ep)) {
921 case FUNCTIONFS_FIFO_STATUS:
922 ret = usb_ep_fifo_status(epfile->ep->ep);
924 case FUNCTIONFS_FIFO_FLUSH:
925 usb_ep_fifo_flush(epfile->ep->ep);
928 case FUNCTIONFS_CLEAR_HALT:
929 ret = usb_ep_clear_halt(epfile->ep->ep);
931 case FUNCTIONFS_ENDPOINT_REVMAP:
932 ret = epfile->ep->num;
940 spin_unlock_irq(&epfile->ffs->eps_lock);
945 static const struct file_operations ffs_epfile_operations = {
946 .owner = THIS_MODULE,
949 .open = ffs_epfile_open,
950 .write = ffs_epfile_write,
951 .read = ffs_epfile_read,
952 .release = ffs_epfile_release,
953 .unlocked_ioctl = ffs_epfile_ioctl,
957 /* File system and super block operations ***********************************/
960 * Mounting the file system creates a controller file, used first for
961 * function configuration then later for event monitoring.
964 static struct inode *__must_check
965 ffs_sb_make_inode(struct super_block *sb, void *data,
966 const struct file_operations *fops,
967 const struct inode_operations *iops,
968 struct ffs_file_perms *perms)
974 inode = new_inode(sb);
977 struct timespec current_time = CURRENT_TIME;
979 inode->i_ino = get_next_ino();
980 inode->i_mode = perms->mode;
981 inode->i_uid = perms->uid;
982 inode->i_gid = perms->gid;
983 inode->i_atime = current_time;
984 inode->i_mtime = current_time;
985 inode->i_ctime = current_time;
986 inode->i_private = data;
996 /* Create "regular" file */
997 static struct inode *ffs_sb_create_file(struct super_block *sb,
998 const char *name, void *data,
999 const struct file_operations *fops,
1000 struct dentry **dentry_p)
1002 struct ffs_data *ffs = sb->s_fs_info;
1003 struct dentry *dentry;
1004 struct inode *inode;
1008 dentry = d_alloc_name(sb->s_root, name);
1009 if (unlikely(!dentry))
1012 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1013 if (unlikely(!inode)) {
1018 d_add(dentry, inode);
1026 static const struct super_operations ffs_sb_operations = {
1027 .statfs = simple_statfs,
1028 .drop_inode = generic_delete_inode,
1031 struct ffs_sb_fill_data {
1032 struct ffs_file_perms perms;
1034 const char *dev_name;
1036 /* set by ffs_fs_mount(), read by ffs_sb_fill() */
1038 /* set by ffs_sb_fill(), read by ffs_fs_mount */
1039 struct ffs_data *ffs_data;
1043 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1045 struct ffs_sb_fill_data *data = _data;
1046 struct inode *inode;
1047 struct ffs_data *ffs;
1051 /* Initialise data */
1052 ffs = ffs_data_new();
1057 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1058 if (unlikely(!ffs->dev_name))
1060 ffs->file_perms = data->perms;
1061 ffs->private_data = data->private_data;
1063 /* used by the caller of this function */
1064 data->ffs_data = ffs;
1066 sb->s_fs_info = ffs;
1067 sb->s_blocksize = PAGE_CACHE_SIZE;
1068 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1069 sb->s_magic = FUNCTIONFS_MAGIC;
1070 sb->s_op = &ffs_sb_operations;
1071 sb->s_time_gran = 1;
1074 data->perms.mode = data->root_mode;
1075 inode = ffs_sb_make_inode(sb, NULL,
1076 &simple_dir_operations,
1077 &simple_dir_inode_operations,
1079 sb->s_root = d_make_root(inode);
1080 if (unlikely(!sb->s_root))
1084 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1085 &ffs_ep0_operations, NULL)))
1094 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1098 if (!opts || !*opts)
1102 char *end, *eq, *comma;
1103 unsigned long value;
1106 comma = strchr(opts, ',');
1111 eq = strchr(opts, '=');
1112 if (unlikely(!eq)) {
1113 pr_err("'=' missing in %s\n", opts);
1119 value = simple_strtoul(eq + 1, &end, 0);
1120 if (unlikely(*end != ',' && *end != 0)) {
1121 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1125 /* Interpret option */
1126 switch (eq - opts) {
1128 if (!memcmp(opts, "rmode", 5))
1129 data->root_mode = (value & 0555) | S_IFDIR;
1130 else if (!memcmp(opts, "fmode", 5))
1131 data->perms.mode = (value & 0666) | S_IFREG;
1137 if (!memcmp(opts, "mode", 4)) {
1138 data->root_mode = (value & 0555) | S_IFDIR;
1139 data->perms.mode = (value & 0666) | S_IFREG;
1146 if (!memcmp(opts, "uid", 3))
1147 data->perms.uid = value;
1148 else if (!memcmp(opts, "gid", 3))
1149 data->perms.gid = value;
1156 pr_err("%s: invalid option\n", opts);
1160 /* Next iteration */
1169 /* "mount -t functionfs dev_name /dev/function" ends up here */
1171 static struct dentry *
1172 ffs_fs_mount(struct file_system_type *t, int flags,
1173 const char *dev_name, void *opts)
1175 struct ffs_sb_fill_data data = {
1177 .mode = S_IFREG | 0600,
1181 .root_mode = S_IFDIR | 0500,
1189 ret = ffs_fs_parse_opts(&data, opts);
1190 if (unlikely(ret < 0))
1191 return ERR_PTR(ret);
1193 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1194 if (IS_ERR(ffs_dev))
1197 data.dev_name = dev_name;
1198 data.private_data = ffs_dev;
1199 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1201 /* data.ffs_data is set by ffs_sb_fill */
1203 functionfs_release_dev_callback(data.ffs_data);
1209 ffs_fs_kill_sb(struct super_block *sb)
1213 kill_litter_super(sb);
1214 if (sb->s_fs_info) {
1215 functionfs_release_dev_callback(sb->s_fs_info);
1216 ffs_data_put(sb->s_fs_info);
1220 static struct file_system_type ffs_fs_type = {
1221 .owner = THIS_MODULE,
1222 .name = "functionfs",
1223 .mount = ffs_fs_mount,
1224 .kill_sb = ffs_fs_kill_sb,
1228 /* Driver's main init/cleanup functions *************************************/
1230 static int functionfs_init(void)
1236 ret = register_filesystem(&ffs_fs_type);
1238 pr_info("file system registered\n");
1240 pr_err("failed registering file system (%d)\n", ret);
1245 static void functionfs_cleanup(void)
1249 pr_info("unloading\n");
1250 unregister_filesystem(&ffs_fs_type);
1254 /* ffs_data and ffs_function construction and destruction code **************/
1256 static void ffs_data_clear(struct ffs_data *ffs);
1257 static void ffs_data_reset(struct ffs_data *ffs);
1259 static void ffs_data_get(struct ffs_data *ffs)
1263 atomic_inc(&ffs->ref);
1266 static void ffs_data_opened(struct ffs_data *ffs)
1270 atomic_inc(&ffs->ref);
1271 atomic_inc(&ffs->opened);
1274 static void ffs_data_put(struct ffs_data *ffs)
1278 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1279 pr_info("%s(): freeing\n", __func__);
1280 ffs_data_clear(ffs);
1281 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1282 waitqueue_active(&ffs->ep0req_completion.wait));
1283 kfree(ffs->dev_name);
1288 static void ffs_data_closed(struct ffs_data *ffs)
1292 if (atomic_dec_and_test(&ffs->opened)) {
1293 ffs->state = FFS_CLOSING;
1294 ffs_data_reset(ffs);
1300 static struct ffs_data *ffs_data_new(void)
1302 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1308 atomic_set(&ffs->ref, 1);
1309 atomic_set(&ffs->opened, 0);
1310 ffs->state = FFS_READ_DESCRIPTORS;
1311 mutex_init(&ffs->mutex);
1312 spin_lock_init(&ffs->eps_lock);
1313 init_waitqueue_head(&ffs->ev.waitq);
1314 init_completion(&ffs->ep0req_completion);
1316 /* XXX REVISIT need to update it in some places, or do we? */
1317 ffs->ev.can_stall = 1;
1322 static void ffs_data_clear(struct ffs_data *ffs)
1326 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1327 functionfs_closed_callback(ffs);
1329 BUG_ON(ffs->gadget);
1332 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1334 kfree(ffs->raw_descs);
1335 kfree(ffs->raw_strings);
1336 kfree(ffs->stringtabs);
1339 static void ffs_data_reset(struct ffs_data *ffs)
1343 ffs_data_clear(ffs);
1345 ffs->epfiles = NULL;
1346 ffs->raw_descs = NULL;
1347 ffs->raw_strings = NULL;
1348 ffs->stringtabs = NULL;
1350 ffs->raw_descs_length = 0;
1351 ffs->raw_fs_descs_length = 0;
1352 ffs->fs_descs_count = 0;
1353 ffs->hs_descs_count = 0;
1355 ffs->strings_count = 0;
1356 ffs->interfaces_count = 0;
1361 ffs->state = FFS_READ_DESCRIPTORS;
1362 ffs->setup_state = FFS_NO_SETUP;
1367 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1369 struct usb_gadget_strings **lang;
1374 if (WARN_ON(ffs->state != FFS_ACTIVE
1375 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1378 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1379 if (unlikely(first_id < 0))
1382 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1383 if (unlikely(!ffs->ep0req))
1385 ffs->ep0req->complete = ffs_ep0_complete;
1386 ffs->ep0req->context = ffs;
1388 lang = ffs->stringtabs;
1389 for (lang = ffs->stringtabs; *lang; ++lang) {
1390 struct usb_string *str = (*lang)->strings;
1392 for (; str->s; ++id, ++str)
1396 ffs->gadget = cdev->gadget;
1401 static void functionfs_unbind(struct ffs_data *ffs)
1405 if (!WARN_ON(!ffs->gadget)) {
1406 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1410 clear_bit(FFS_FL_BOUND, &ffs->flags);
1414 static int ffs_epfiles_create(struct ffs_data *ffs)
1416 struct ffs_epfile *epfile, *epfiles;
1421 count = ffs->eps_count;
1422 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1427 for (i = 1; i <= count; ++i, ++epfile) {
1429 mutex_init(&epfile->mutex);
1430 init_waitqueue_head(&epfile->wait);
1431 sprintf(epfiles->name, "ep%u", i);
1432 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1433 &ffs_epfile_operations,
1434 &epfile->dentry))) {
1435 ffs_epfiles_destroy(epfiles, i - 1);
1440 ffs->epfiles = epfiles;
1444 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1446 struct ffs_epfile *epfile = epfiles;
1450 for (; count; --count, ++epfile) {
1451 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1452 waitqueue_active(&epfile->wait));
1453 if (epfile->dentry) {
1454 d_delete(epfile->dentry);
1455 dput(epfile->dentry);
1456 epfile->dentry = NULL;
1463 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1464 struct usb_configuration *c,
1465 struct ffs_data *ffs)
1467 struct ffs_function *func;
1472 func = kzalloc(sizeof *func, GFP_KERNEL);
1473 if (unlikely(!func))
1476 func->function.name = "Function FS Gadget";
1477 func->function.strings = ffs->stringtabs;
1479 func->function.bind = ffs_func_bind;
1480 func->function.unbind = ffs_func_unbind;
1481 func->function.set_alt = ffs_func_set_alt;
1482 func->function.disable = ffs_func_disable;
1483 func->function.setup = ffs_func_setup;
1484 func->function.suspend = ffs_func_suspend;
1485 func->function.resume = ffs_func_resume;
1488 func->gadget = cdev->gadget;
1492 ret = usb_add_function(c, &func->function);
1494 ffs_func_free(func);
1499 static void ffs_func_free(struct ffs_function *func)
1501 struct ffs_ep *ep = func->eps;
1502 unsigned count = func->ffs->eps_count;
1503 unsigned long flags;
1507 /* cleanup after autoconfig */
1508 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1510 if (ep->ep && ep->req)
1511 usb_ep_free_request(ep->ep, ep->req);
1515 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1517 ffs_data_put(func->ffs);
1521 * eps and interfaces_nums are allocated in the same chunk so
1522 * only one free is required. Descriptors are also allocated
1523 * in the same chunk.
1529 static void ffs_func_eps_disable(struct ffs_function *func)
1531 struct ffs_ep *ep = func->eps;
1532 struct ffs_epfile *epfile = func->ffs->epfiles;
1533 unsigned count = func->ffs->eps_count;
1534 unsigned long flags;
1536 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1538 /* pending requests get nuked */
1540 usb_ep_disable(ep->ep);
1546 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1549 static int ffs_func_eps_enable(struct ffs_function *func)
1551 struct ffs_data *ffs = func->ffs;
1552 struct ffs_ep *ep = func->eps;
1553 struct ffs_epfile *epfile = ffs->epfiles;
1554 unsigned count = ffs->eps_count;
1555 unsigned long flags;
1558 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1560 struct usb_endpoint_descriptor *ds;
1561 ds = ep->descs[ep->descs[1] ? 1 : 0];
1563 ep->ep->driver_data = ep;
1565 ret = usb_ep_enable(ep->ep);
1568 epfile->in = usb_endpoint_dir_in(ds);
1569 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1574 wake_up(&epfile->wait);
1579 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1585 /* Parsing and building descriptors and strings *****************************/
1588 * This validates if data pointed by data is a valid USB descriptor as
1589 * well as record how many interfaces, endpoints and strings are
1590 * required by given configuration. Returns address after the
1591 * descriptor or NULL if data is invalid.
1594 enum ffs_entity_type {
1595 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1598 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1600 struct usb_descriptor_header *desc,
1603 static int __must_check ffs_do_desc(char *data, unsigned len,
1604 ffs_entity_callback entity, void *priv)
1606 struct usb_descriptor_header *_ds = (void *)data;
1612 /* At least two bytes are required: length and type */
1614 pr_vdebug("descriptor too short\n");
1618 /* If we have at least as many bytes as the descriptor takes? */
1619 length = _ds->bLength;
1621 pr_vdebug("descriptor longer then available data\n");
1625 #define __entity_check_INTERFACE(val) 1
1626 #define __entity_check_STRING(val) (val)
1627 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1628 #define __entity(type, val) do { \
1629 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1630 if (unlikely(!__entity_check_ ##type(val))) { \
1631 pr_vdebug("invalid entity's value\n"); \
1634 ret = entity(FFS_ ##type, &val, _ds, priv); \
1635 if (unlikely(ret < 0)) { \
1636 pr_debug("entity " #type "(%02x); ret = %d\n", \
1642 /* Parse descriptor depending on type. */
1643 switch (_ds->bDescriptorType) {
1647 case USB_DT_DEVICE_QUALIFIER:
1648 /* function can't have any of those */
1649 pr_vdebug("descriptor reserved for gadget: %d\n",
1650 _ds->bDescriptorType);
1653 case USB_DT_INTERFACE: {
1654 struct usb_interface_descriptor *ds = (void *)_ds;
1655 pr_vdebug("interface descriptor\n");
1656 if (length != sizeof *ds)
1659 __entity(INTERFACE, ds->bInterfaceNumber);
1661 __entity(STRING, ds->iInterface);
1665 case USB_DT_ENDPOINT: {
1666 struct usb_endpoint_descriptor *ds = (void *)_ds;
1667 pr_vdebug("endpoint descriptor\n");
1668 if (length != USB_DT_ENDPOINT_SIZE &&
1669 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1671 __entity(ENDPOINT, ds->bEndpointAddress);
1676 pr_vdebug("hid descriptor\n");
1677 if (length != sizeof(struct hid_descriptor))
1682 if (length != sizeof(struct usb_otg_descriptor))
1686 case USB_DT_INTERFACE_ASSOCIATION: {
1687 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1688 pr_vdebug("interface association descriptor\n");
1689 if (length != sizeof *ds)
1692 __entity(STRING, ds->iFunction);
1696 case USB_DT_OTHER_SPEED_CONFIG:
1697 case USB_DT_INTERFACE_POWER:
1699 case USB_DT_SECURITY:
1700 case USB_DT_CS_RADIO_CONTROL:
1702 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1706 /* We should never be here */
1707 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1711 pr_vdebug("invalid length: %d (descriptor %d)\n",
1712 _ds->bLength, _ds->bDescriptorType);
1717 #undef __entity_check_DESCRIPTOR
1718 #undef __entity_check_INTERFACE
1719 #undef __entity_check_STRING
1720 #undef __entity_check_ENDPOINT
1725 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1726 ffs_entity_callback entity, void *priv)
1728 const unsigned _len = len;
1729 unsigned long num = 0;
1739 /* Record "descriptor" entity */
1740 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1741 if (unlikely(ret < 0)) {
1742 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1750 ret = ffs_do_desc(data, len, entity, priv);
1751 if (unlikely(ret < 0)) {
1752 pr_debug("%s returns %d\n", __func__, ret);
1762 static int __ffs_data_do_entity(enum ffs_entity_type type,
1763 u8 *valuep, struct usb_descriptor_header *desc,
1766 struct ffs_data *ffs = priv;
1771 case FFS_DESCRIPTOR:
1776 * Interfaces are indexed from zero so if we
1777 * encountered interface "n" then there are at least
1780 if (*valuep >= ffs->interfaces_count)
1781 ffs->interfaces_count = *valuep + 1;
1786 * Strings are indexed from 1 (0 is magic ;) reserved
1787 * for languages list or some such)
1789 if (*valuep > ffs->strings_count)
1790 ffs->strings_count = *valuep;
1794 /* Endpoints are indexed from 1 as well. */
1795 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1796 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1803 static int __ffs_data_got_descs(struct ffs_data *ffs,
1804 char *const _data, size_t len)
1806 unsigned fs_count, hs_count;
1807 int fs_len, ret = -EINVAL;
1812 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1813 get_unaligned_le32(data + 4) != len))
1815 fs_count = get_unaligned_le32(data + 8);
1816 hs_count = get_unaligned_le32(data + 12);
1818 if (!fs_count && !hs_count)
1824 if (likely(fs_count)) {
1825 fs_len = ffs_do_descs(fs_count, data, len,
1826 __ffs_data_do_entity, ffs);
1827 if (unlikely(fs_len < 0)) {
1838 if (likely(hs_count)) {
1839 ret = ffs_do_descs(hs_count, data, len,
1840 __ffs_data_do_entity, ffs);
1841 if (unlikely(ret < 0))
1847 if (unlikely(len != ret))
1850 ffs->raw_fs_descs_length = fs_len;
1851 ffs->raw_descs_length = fs_len + ret;
1852 ffs->raw_descs = _data;
1853 ffs->fs_descs_count = fs_count;
1854 ffs->hs_descs_count = hs_count;
1865 static int __ffs_data_got_strings(struct ffs_data *ffs,
1866 char *const _data, size_t len)
1868 u32 str_count, needed_count, lang_count;
1869 struct usb_gadget_strings **stringtabs, *t;
1870 struct usb_string *strings, *s;
1871 const char *data = _data;
1875 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1876 get_unaligned_le32(data + 4) != len))
1878 str_count = get_unaligned_le32(data + 8);
1879 lang_count = get_unaligned_le32(data + 12);
1881 /* if one is zero the other must be zero */
1882 if (unlikely(!str_count != !lang_count))
1885 /* Do we have at least as many strings as descriptors need? */
1886 needed_count = ffs->strings_count;
1887 if (unlikely(str_count < needed_count))
1891 * If we don't need any strings just return and free all
1894 if (!needed_count) {
1899 /* Allocate everything in one chunk so there's less maintenance. */
1902 struct usb_gadget_strings *stringtabs[lang_count + 1];
1903 struct usb_gadget_strings stringtab[lang_count];
1904 struct usb_string strings[lang_count*(needed_count+1)];
1908 d = kmalloc(sizeof *d, GFP_KERNEL);
1914 stringtabs = d->stringtabs;
1918 *stringtabs++ = t++;
1922 stringtabs = d->stringtabs;
1928 /* For each language */
1932 do { /* lang_count > 0 so we can use do-while */
1933 unsigned needed = needed_count;
1935 if (unlikely(len < 3))
1937 t->language = get_unaligned_le16(data);
1944 /* For each string */
1945 do { /* str_count > 0 so we can use do-while */
1946 size_t length = strnlen(data, len);
1948 if (unlikely(length == len))
1952 * User may provide more strings then we need,
1953 * if that's the case we simply ignore the
1956 if (likely(needed)) {
1958 * s->id will be set while adding
1959 * function to configuration so for
1960 * now just leave garbage here.
1969 } while (--str_count);
1971 s->id = 0; /* terminator */
1975 } while (--lang_count);
1977 /* Some garbage left? */
1982 ffs->stringtabs = stringtabs;
1983 ffs->raw_strings = _data;
1995 /* Events handling and management *******************************************/
1997 static void __ffs_event_add(struct ffs_data *ffs,
1998 enum usb_functionfs_event_type type)
2000 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2004 * Abort any unhandled setup
2006 * We do not need to worry about some cmpxchg() changing value
2007 * of ffs->setup_state without holding the lock because when
2008 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2009 * the source does nothing.
2011 if (ffs->setup_state == FFS_SETUP_PENDING)
2012 ffs->setup_state = FFS_SETUP_CANCELED;
2015 case FUNCTIONFS_RESUME:
2016 rem_type2 = FUNCTIONFS_SUSPEND;
2018 case FUNCTIONFS_SUSPEND:
2019 case FUNCTIONFS_SETUP:
2021 /* Discard all similar events */
2024 case FUNCTIONFS_BIND:
2025 case FUNCTIONFS_UNBIND:
2026 case FUNCTIONFS_DISABLE:
2027 case FUNCTIONFS_ENABLE:
2028 /* Discard everything other then power management. */
2029 rem_type1 = FUNCTIONFS_SUSPEND;
2030 rem_type2 = FUNCTIONFS_RESUME;
2039 u8 *ev = ffs->ev.types, *out = ev;
2040 unsigned n = ffs->ev.count;
2041 for (; n; --n, ++ev)
2042 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2045 pr_vdebug("purging event %d\n", *ev);
2046 ffs->ev.count = out - ffs->ev.types;
2049 pr_vdebug("adding event %d\n", type);
2050 ffs->ev.types[ffs->ev.count++] = type;
2051 wake_up_locked(&ffs->ev.waitq);
2054 static void ffs_event_add(struct ffs_data *ffs,
2055 enum usb_functionfs_event_type type)
2057 unsigned long flags;
2058 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2059 __ffs_event_add(ffs, type);
2060 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2064 /* Bind/unbind USB function hooks *******************************************/
2066 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2067 struct usb_descriptor_header *desc,
2070 struct usb_endpoint_descriptor *ds = (void *)desc;
2071 struct ffs_function *func = priv;
2072 struct ffs_ep *ffs_ep;
2075 * If hs_descriptors is not NULL then we are reading hs
2078 const int isHS = func->function.hs_descriptors != NULL;
2081 if (type != FFS_DESCRIPTOR)
2085 func->function.hs_descriptors[(long)valuep] = desc;
2087 func->function.descriptors[(long)valuep] = desc;
2089 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2092 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2093 ffs_ep = func->eps + idx;
2095 if (unlikely(ffs_ep->descs[isHS])) {
2096 pr_vdebug("two %sspeed descriptors for EP %d\n",
2097 isHS ? "high" : "full",
2098 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2101 ffs_ep->descs[isHS] = ds;
2103 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2105 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2106 if (!ds->wMaxPacketSize)
2107 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2109 struct usb_request *req;
2112 pr_vdebug("autoconfig\n");
2113 ep = usb_ep_autoconfig(func->gadget, ds);
2116 ep->driver_data = func->eps + idx;
2118 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2124 func->eps_revmap[ds->bEndpointAddress &
2125 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2127 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2132 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2133 struct usb_descriptor_header *desc,
2136 struct ffs_function *func = priv;
2142 case FFS_DESCRIPTOR:
2143 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2148 if (func->interfaces_nums[idx] < 0) {
2149 int id = usb_interface_id(func->conf, &func->function);
2150 if (unlikely(id < 0))
2152 func->interfaces_nums[idx] = id;
2154 newValue = func->interfaces_nums[idx];
2158 /* String' IDs are allocated when fsf_data is bound to cdev */
2159 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2164 * USB_DT_ENDPOINT are handled in
2165 * __ffs_func_bind_do_descs().
2167 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2170 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2171 if (unlikely(!func->eps[idx].ep))
2175 struct usb_endpoint_descriptor **descs;
2176 descs = func->eps[idx].descs;
2177 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2182 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2187 static int ffs_func_bind(struct usb_configuration *c,
2188 struct usb_function *f)
2190 struct ffs_function *func = ffs_func_from_usb(f);
2191 struct ffs_data *ffs = func->ffs;
2193 const int full = !!func->ffs->fs_descs_count;
2194 const int high = gadget_is_dualspeed(func->gadget) &&
2195 func->ffs->hs_descs_count;
2199 /* Make it a single chunk, less management later on */
2201 struct ffs_ep eps[ffs->eps_count];
2202 struct usb_descriptor_header
2203 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2204 struct usb_descriptor_header
2205 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2206 short inums[ffs->interfaces_count];
2207 char raw_descs[high ? ffs->raw_descs_length
2208 : ffs->raw_fs_descs_length];
2213 /* Only high speed but not supported by gadget? */
2214 if (unlikely(!(full | high)))
2218 data = kmalloc(sizeof *data, GFP_KERNEL);
2219 if (unlikely(!data))
2223 memset(data->eps, 0, sizeof data->eps);
2224 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2225 memset(data->inums, 0xff, sizeof data->inums);
2226 for (ret = ffs->eps_count; ret; --ret)
2227 data->eps[ret].num = -1;
2230 func->eps = data->eps;
2231 func->interfaces_nums = data->inums;
2234 * Go through all the endpoint descriptors and allocate
2235 * endpoints first, so that later we can rewrite the endpoint
2236 * numbers without worrying that it may be described later on.
2239 func->function.descriptors = data->fs_descs;
2240 ret = ffs_do_descs(ffs->fs_descs_count,
2242 sizeof data->raw_descs,
2243 __ffs_func_bind_do_descs, func);
2244 if (unlikely(ret < 0))
2251 func->function.hs_descriptors = data->hs_descs;
2252 ret = ffs_do_descs(ffs->hs_descs_count,
2253 data->raw_descs + ret,
2254 (sizeof data->raw_descs) - ret,
2255 __ffs_func_bind_do_descs, func);
2259 * Now handle interface numbers allocation and interface and
2260 * endpoint numbers rewriting. We can do that in one go
2263 ret = ffs_do_descs(ffs->fs_descs_count +
2264 (high ? ffs->hs_descs_count : 0),
2265 data->raw_descs, sizeof data->raw_descs,
2266 __ffs_func_bind_do_nums, func);
2267 if (unlikely(ret < 0))
2270 /* And we're done */
2271 ffs_event_add(ffs, FUNCTIONFS_BIND);
2275 /* XXX Do we need to release all claimed endpoints here? */
2280 /* Other USB function hooks *************************************************/
2282 static void ffs_func_unbind(struct usb_configuration *c,
2283 struct usb_function *f)
2285 struct ffs_function *func = ffs_func_from_usb(f);
2286 struct ffs_data *ffs = func->ffs;
2290 if (ffs->func == func) {
2291 ffs_func_eps_disable(func);
2295 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2297 ffs_func_free(func);
2300 static int ffs_func_set_alt(struct usb_function *f,
2301 unsigned interface, unsigned alt)
2303 struct ffs_function *func = ffs_func_from_usb(f);
2304 struct ffs_data *ffs = func->ffs;
2307 if (alt != (unsigned)-1) {
2308 intf = ffs_func_revmap_intf(func, interface);
2309 if (unlikely(intf < 0))
2314 ffs_func_eps_disable(ffs->func);
2316 if (ffs->state != FFS_ACTIVE)
2319 if (alt == (unsigned)-1) {
2321 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2326 ret = ffs_func_eps_enable(func);
2327 if (likely(ret >= 0))
2328 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2332 static void ffs_func_disable(struct usb_function *f)
2334 ffs_func_set_alt(f, 0, (unsigned)-1);
2337 static int ffs_func_setup(struct usb_function *f,
2338 const struct usb_ctrlrequest *creq)
2340 struct ffs_function *func = ffs_func_from_usb(f);
2341 struct ffs_data *ffs = func->ffs;
2342 unsigned long flags;
2347 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2348 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2349 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2350 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2351 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2354 * Most requests directed to interface go through here
2355 * (notable exceptions are set/get interface) so we need to
2356 * handle them. All other either handled by composite or
2357 * passed to usb_configuration->setup() (if one is set). No
2358 * matter, we will handle requests directed to endpoint here
2359 * as well (as it's straightforward) but what to do with any
2362 if (ffs->state != FFS_ACTIVE)
2365 switch (creq->bRequestType & USB_RECIP_MASK) {
2366 case USB_RECIP_INTERFACE:
2367 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2368 if (unlikely(ret < 0))
2372 case USB_RECIP_ENDPOINT:
2373 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2374 if (unlikely(ret < 0))
2382 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2383 ffs->ev.setup = *creq;
2384 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2385 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2386 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2391 static void ffs_func_suspend(struct usb_function *f)
2394 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2397 static void ffs_func_resume(struct usb_function *f)
2400 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2404 /* Endpoint and interface numbers reverse mapping ***************************/
2406 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2408 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2409 return num ? num : -EDOM;
2412 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2414 short *nums = func->interfaces_nums;
2415 unsigned count = func->ffs->interfaces_count;
2417 for (; count; --count, ++nums) {
2418 if (*nums >= 0 && *nums == intf)
2419 return nums - func->interfaces_nums;
2426 /* Misc helper functions ****************************************************/
2428 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2431 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2432 : mutex_lock_interruptible(mutex);
2435 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2442 data = kmalloc(len, GFP_KERNEL);
2443 if (unlikely(!data))
2444 return ERR_PTR(-ENOMEM);
2446 if (unlikely(__copy_from_user(data, buf, len))) {
2448 return ERR_PTR(-EFAULT);
2451 pr_vdebug("Buffer from user space:\n");
2452 ffs_dump_mem("", data, len);