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,
1226 MODULE_ALIAS_FS("functionfs");
1229 /* Driver's main init/cleanup functions *************************************/
1231 static int functionfs_init(void)
1237 ret = register_filesystem(&ffs_fs_type);
1239 pr_info("file system registered\n");
1241 pr_err("failed registering file system (%d)\n", ret);
1246 static void functionfs_cleanup(void)
1250 pr_info("unloading\n");
1251 unregister_filesystem(&ffs_fs_type);
1255 /* ffs_data and ffs_function construction and destruction code **************/
1257 static void ffs_data_clear(struct ffs_data *ffs);
1258 static void ffs_data_reset(struct ffs_data *ffs);
1260 static void ffs_data_get(struct ffs_data *ffs)
1264 atomic_inc(&ffs->ref);
1267 static void ffs_data_opened(struct ffs_data *ffs)
1271 atomic_inc(&ffs->ref);
1272 atomic_inc(&ffs->opened);
1275 static void ffs_data_put(struct ffs_data *ffs)
1279 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1280 pr_info("%s(): freeing\n", __func__);
1281 ffs_data_clear(ffs);
1282 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1283 waitqueue_active(&ffs->ep0req_completion.wait));
1284 kfree(ffs->dev_name);
1289 static void ffs_data_closed(struct ffs_data *ffs)
1293 if (atomic_dec_and_test(&ffs->opened)) {
1294 ffs->state = FFS_CLOSING;
1295 ffs_data_reset(ffs);
1301 static struct ffs_data *ffs_data_new(void)
1303 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1309 atomic_set(&ffs->ref, 1);
1310 atomic_set(&ffs->opened, 0);
1311 ffs->state = FFS_READ_DESCRIPTORS;
1312 mutex_init(&ffs->mutex);
1313 spin_lock_init(&ffs->eps_lock);
1314 init_waitqueue_head(&ffs->ev.waitq);
1315 init_completion(&ffs->ep0req_completion);
1317 /* XXX REVISIT need to update it in some places, or do we? */
1318 ffs->ev.can_stall = 1;
1323 static void ffs_data_clear(struct ffs_data *ffs)
1327 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1328 functionfs_closed_callback(ffs);
1330 BUG_ON(ffs->gadget);
1333 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1335 kfree(ffs->raw_descs);
1336 kfree(ffs->raw_strings);
1337 kfree(ffs->stringtabs);
1340 static void ffs_data_reset(struct ffs_data *ffs)
1344 ffs_data_clear(ffs);
1346 ffs->epfiles = NULL;
1347 ffs->raw_descs = NULL;
1348 ffs->raw_strings = NULL;
1349 ffs->stringtabs = NULL;
1351 ffs->raw_descs_length = 0;
1352 ffs->raw_fs_descs_length = 0;
1353 ffs->fs_descs_count = 0;
1354 ffs->hs_descs_count = 0;
1356 ffs->strings_count = 0;
1357 ffs->interfaces_count = 0;
1362 ffs->state = FFS_READ_DESCRIPTORS;
1363 ffs->setup_state = FFS_NO_SETUP;
1368 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1370 struct usb_gadget_strings **lang;
1375 if (WARN_ON(ffs->state != FFS_ACTIVE
1376 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1379 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1380 if (unlikely(first_id < 0))
1383 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1384 if (unlikely(!ffs->ep0req))
1386 ffs->ep0req->complete = ffs_ep0_complete;
1387 ffs->ep0req->context = ffs;
1389 lang = ffs->stringtabs;
1390 for (lang = ffs->stringtabs; *lang; ++lang) {
1391 struct usb_string *str = (*lang)->strings;
1393 for (; str->s; ++id, ++str)
1397 ffs->gadget = cdev->gadget;
1402 static void functionfs_unbind(struct ffs_data *ffs)
1406 if (!WARN_ON(!ffs->gadget)) {
1407 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1411 clear_bit(FFS_FL_BOUND, &ffs->flags);
1415 static int ffs_epfiles_create(struct ffs_data *ffs)
1417 struct ffs_epfile *epfile, *epfiles;
1422 count = ffs->eps_count;
1423 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1428 for (i = 1; i <= count; ++i, ++epfile) {
1430 mutex_init(&epfile->mutex);
1431 init_waitqueue_head(&epfile->wait);
1432 sprintf(epfiles->name, "ep%u", i);
1433 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1434 &ffs_epfile_operations,
1435 &epfile->dentry))) {
1436 ffs_epfiles_destroy(epfiles, i - 1);
1441 ffs->epfiles = epfiles;
1445 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1447 struct ffs_epfile *epfile = epfiles;
1451 for (; count; --count, ++epfile) {
1452 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1453 waitqueue_active(&epfile->wait));
1454 if (epfile->dentry) {
1455 d_delete(epfile->dentry);
1456 dput(epfile->dentry);
1457 epfile->dentry = NULL;
1464 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1465 struct usb_configuration *c,
1466 struct ffs_data *ffs)
1468 struct ffs_function *func;
1473 func = kzalloc(sizeof *func, GFP_KERNEL);
1474 if (unlikely(!func))
1477 func->function.name = "Function FS Gadget";
1478 func->function.strings = ffs->stringtabs;
1480 func->function.bind = ffs_func_bind;
1481 func->function.unbind = ffs_func_unbind;
1482 func->function.set_alt = ffs_func_set_alt;
1483 func->function.disable = ffs_func_disable;
1484 func->function.setup = ffs_func_setup;
1485 func->function.suspend = ffs_func_suspend;
1486 func->function.resume = ffs_func_resume;
1489 func->gadget = cdev->gadget;
1493 ret = usb_add_function(c, &func->function);
1495 ffs_func_free(func);
1500 static void ffs_func_free(struct ffs_function *func)
1502 struct ffs_ep *ep = func->eps;
1503 unsigned count = func->ffs->eps_count;
1504 unsigned long flags;
1508 /* cleanup after autoconfig */
1509 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1511 if (ep->ep && ep->req)
1512 usb_ep_free_request(ep->ep, ep->req);
1516 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1518 ffs_data_put(func->ffs);
1522 * eps and interfaces_nums are allocated in the same chunk so
1523 * only one free is required. Descriptors are also allocated
1524 * in the same chunk.
1530 static void ffs_func_eps_disable(struct ffs_function *func)
1532 struct ffs_ep *ep = func->eps;
1533 struct ffs_epfile *epfile = func->ffs->epfiles;
1534 unsigned count = func->ffs->eps_count;
1535 unsigned long flags;
1537 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1539 /* pending requests get nuked */
1541 usb_ep_disable(ep->ep);
1547 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1550 static int ffs_func_eps_enable(struct ffs_function *func)
1552 struct ffs_data *ffs = func->ffs;
1553 struct ffs_ep *ep = func->eps;
1554 struct ffs_epfile *epfile = ffs->epfiles;
1555 unsigned count = ffs->eps_count;
1556 unsigned long flags;
1559 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1561 struct usb_endpoint_descriptor *ds;
1562 ds = ep->descs[ep->descs[1] ? 1 : 0];
1564 ep->ep->driver_data = ep;
1566 ret = usb_ep_enable(ep->ep);
1569 epfile->in = usb_endpoint_dir_in(ds);
1570 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1575 wake_up(&epfile->wait);
1580 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1586 /* Parsing and building descriptors and strings *****************************/
1589 * This validates if data pointed by data is a valid USB descriptor as
1590 * well as record how many interfaces, endpoints and strings are
1591 * required by given configuration. Returns address after the
1592 * descriptor or NULL if data is invalid.
1595 enum ffs_entity_type {
1596 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1599 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1601 struct usb_descriptor_header *desc,
1604 static int __must_check ffs_do_desc(char *data, unsigned len,
1605 ffs_entity_callback entity, void *priv)
1607 struct usb_descriptor_header *_ds = (void *)data;
1613 /* At least two bytes are required: length and type */
1615 pr_vdebug("descriptor too short\n");
1619 /* If we have at least as many bytes as the descriptor takes? */
1620 length = _ds->bLength;
1622 pr_vdebug("descriptor longer then available data\n");
1626 #define __entity_check_INTERFACE(val) 1
1627 #define __entity_check_STRING(val) (val)
1628 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1629 #define __entity(type, val) do { \
1630 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1631 if (unlikely(!__entity_check_ ##type(val))) { \
1632 pr_vdebug("invalid entity's value\n"); \
1635 ret = entity(FFS_ ##type, &val, _ds, priv); \
1636 if (unlikely(ret < 0)) { \
1637 pr_debug("entity " #type "(%02x); ret = %d\n", \
1643 /* Parse descriptor depending on type. */
1644 switch (_ds->bDescriptorType) {
1648 case USB_DT_DEVICE_QUALIFIER:
1649 /* function can't have any of those */
1650 pr_vdebug("descriptor reserved for gadget: %d\n",
1651 _ds->bDescriptorType);
1654 case USB_DT_INTERFACE: {
1655 struct usb_interface_descriptor *ds = (void *)_ds;
1656 pr_vdebug("interface descriptor\n");
1657 if (length != sizeof *ds)
1660 __entity(INTERFACE, ds->bInterfaceNumber);
1662 __entity(STRING, ds->iInterface);
1666 case USB_DT_ENDPOINT: {
1667 struct usb_endpoint_descriptor *ds = (void *)_ds;
1668 pr_vdebug("endpoint descriptor\n");
1669 if (length != USB_DT_ENDPOINT_SIZE &&
1670 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1672 __entity(ENDPOINT, ds->bEndpointAddress);
1677 pr_vdebug("hid descriptor\n");
1678 if (length != sizeof(struct hid_descriptor))
1683 if (length != sizeof(struct usb_otg_descriptor))
1687 case USB_DT_INTERFACE_ASSOCIATION: {
1688 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1689 pr_vdebug("interface association descriptor\n");
1690 if (length != sizeof *ds)
1693 __entity(STRING, ds->iFunction);
1697 case USB_DT_OTHER_SPEED_CONFIG:
1698 case USB_DT_INTERFACE_POWER:
1700 case USB_DT_SECURITY:
1701 case USB_DT_CS_RADIO_CONTROL:
1703 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1707 /* We should never be here */
1708 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1712 pr_vdebug("invalid length: %d (descriptor %d)\n",
1713 _ds->bLength, _ds->bDescriptorType);
1718 #undef __entity_check_DESCRIPTOR
1719 #undef __entity_check_INTERFACE
1720 #undef __entity_check_STRING
1721 #undef __entity_check_ENDPOINT
1726 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1727 ffs_entity_callback entity, void *priv)
1729 const unsigned _len = len;
1730 unsigned long num = 0;
1740 /* Record "descriptor" entity */
1741 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1742 if (unlikely(ret < 0)) {
1743 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1751 ret = ffs_do_desc(data, len, entity, priv);
1752 if (unlikely(ret < 0)) {
1753 pr_debug("%s returns %d\n", __func__, ret);
1763 static int __ffs_data_do_entity(enum ffs_entity_type type,
1764 u8 *valuep, struct usb_descriptor_header *desc,
1767 struct ffs_data *ffs = priv;
1772 case FFS_DESCRIPTOR:
1777 * Interfaces are indexed from zero so if we
1778 * encountered interface "n" then there are at least
1781 if (*valuep >= ffs->interfaces_count)
1782 ffs->interfaces_count = *valuep + 1;
1787 * Strings are indexed from 1 (0 is magic ;) reserved
1788 * for languages list or some such)
1790 if (*valuep > ffs->strings_count)
1791 ffs->strings_count = *valuep;
1795 /* Endpoints are indexed from 1 as well. */
1796 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1797 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1804 static int __ffs_data_got_descs(struct ffs_data *ffs,
1805 char *const _data, size_t len)
1807 unsigned fs_count, hs_count;
1808 int fs_len, ret = -EINVAL;
1813 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1814 get_unaligned_le32(data + 4) != len))
1816 fs_count = get_unaligned_le32(data + 8);
1817 hs_count = get_unaligned_le32(data + 12);
1819 if (!fs_count && !hs_count)
1825 if (likely(fs_count)) {
1826 fs_len = ffs_do_descs(fs_count, data, len,
1827 __ffs_data_do_entity, ffs);
1828 if (unlikely(fs_len < 0)) {
1839 if (likely(hs_count)) {
1840 ret = ffs_do_descs(hs_count, data, len,
1841 __ffs_data_do_entity, ffs);
1842 if (unlikely(ret < 0))
1848 if (unlikely(len != ret))
1851 ffs->raw_fs_descs_length = fs_len;
1852 ffs->raw_descs_length = fs_len + ret;
1853 ffs->raw_descs = _data;
1854 ffs->fs_descs_count = fs_count;
1855 ffs->hs_descs_count = hs_count;
1866 static int __ffs_data_got_strings(struct ffs_data *ffs,
1867 char *const _data, size_t len)
1869 u32 str_count, needed_count, lang_count;
1870 struct usb_gadget_strings **stringtabs, *t;
1871 struct usb_string *strings, *s;
1872 const char *data = _data;
1876 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1877 get_unaligned_le32(data + 4) != len))
1879 str_count = get_unaligned_le32(data + 8);
1880 lang_count = get_unaligned_le32(data + 12);
1882 /* if one is zero the other must be zero */
1883 if (unlikely(!str_count != !lang_count))
1886 /* Do we have at least as many strings as descriptors need? */
1887 needed_count = ffs->strings_count;
1888 if (unlikely(str_count < needed_count))
1892 * If we don't need any strings just return and free all
1895 if (!needed_count) {
1900 /* Allocate everything in one chunk so there's less maintenance. */
1903 struct usb_gadget_strings *stringtabs[lang_count + 1];
1904 struct usb_gadget_strings stringtab[lang_count];
1905 struct usb_string strings[lang_count*(needed_count+1)];
1909 d = kmalloc(sizeof *d, GFP_KERNEL);
1915 stringtabs = d->stringtabs;
1919 *stringtabs++ = t++;
1923 stringtabs = d->stringtabs;
1929 /* For each language */
1933 do { /* lang_count > 0 so we can use do-while */
1934 unsigned needed = needed_count;
1936 if (unlikely(len < 3))
1938 t->language = get_unaligned_le16(data);
1945 /* For each string */
1946 do { /* str_count > 0 so we can use do-while */
1947 size_t length = strnlen(data, len);
1949 if (unlikely(length == len))
1953 * User may provide more strings then we need,
1954 * if that's the case we simply ignore the
1957 if (likely(needed)) {
1959 * s->id will be set while adding
1960 * function to configuration so for
1961 * now just leave garbage here.
1970 } while (--str_count);
1972 s->id = 0; /* terminator */
1976 } while (--lang_count);
1978 /* Some garbage left? */
1983 ffs->stringtabs = stringtabs;
1984 ffs->raw_strings = _data;
1996 /* Events handling and management *******************************************/
1998 static void __ffs_event_add(struct ffs_data *ffs,
1999 enum usb_functionfs_event_type type)
2001 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2005 * Abort any unhandled setup
2007 * We do not need to worry about some cmpxchg() changing value
2008 * of ffs->setup_state without holding the lock because when
2009 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2010 * the source does nothing.
2012 if (ffs->setup_state == FFS_SETUP_PENDING)
2013 ffs->setup_state = FFS_SETUP_CANCELED;
2016 case FUNCTIONFS_RESUME:
2017 rem_type2 = FUNCTIONFS_SUSPEND;
2019 case FUNCTIONFS_SUSPEND:
2020 case FUNCTIONFS_SETUP:
2022 /* Discard all similar events */
2025 case FUNCTIONFS_BIND:
2026 case FUNCTIONFS_UNBIND:
2027 case FUNCTIONFS_DISABLE:
2028 case FUNCTIONFS_ENABLE:
2029 /* Discard everything other then power management. */
2030 rem_type1 = FUNCTIONFS_SUSPEND;
2031 rem_type2 = FUNCTIONFS_RESUME;
2040 u8 *ev = ffs->ev.types, *out = ev;
2041 unsigned n = ffs->ev.count;
2042 for (; n; --n, ++ev)
2043 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2046 pr_vdebug("purging event %d\n", *ev);
2047 ffs->ev.count = out - ffs->ev.types;
2050 pr_vdebug("adding event %d\n", type);
2051 ffs->ev.types[ffs->ev.count++] = type;
2052 wake_up_locked(&ffs->ev.waitq);
2055 static void ffs_event_add(struct ffs_data *ffs,
2056 enum usb_functionfs_event_type type)
2058 unsigned long flags;
2059 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2060 __ffs_event_add(ffs, type);
2061 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2065 /* Bind/unbind USB function hooks *******************************************/
2067 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2068 struct usb_descriptor_header *desc,
2071 struct usb_endpoint_descriptor *ds = (void *)desc;
2072 struct ffs_function *func = priv;
2073 struct ffs_ep *ffs_ep;
2076 * If hs_descriptors is not NULL then we are reading hs
2079 const int isHS = func->function.hs_descriptors != NULL;
2082 if (type != FFS_DESCRIPTOR)
2086 func->function.hs_descriptors[(long)valuep] = desc;
2088 func->function.descriptors[(long)valuep] = desc;
2090 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2093 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2094 ffs_ep = func->eps + idx;
2096 if (unlikely(ffs_ep->descs[isHS])) {
2097 pr_vdebug("two %sspeed descriptors for EP %d\n",
2098 isHS ? "high" : "full",
2099 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2102 ffs_ep->descs[isHS] = ds;
2104 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2106 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2107 if (!ds->wMaxPacketSize)
2108 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2110 struct usb_request *req;
2113 pr_vdebug("autoconfig\n");
2114 ep = usb_ep_autoconfig(func->gadget, ds);
2117 ep->driver_data = func->eps + idx;
2119 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2125 func->eps_revmap[ds->bEndpointAddress &
2126 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2128 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2133 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2134 struct usb_descriptor_header *desc,
2137 struct ffs_function *func = priv;
2143 case FFS_DESCRIPTOR:
2144 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2149 if (func->interfaces_nums[idx] < 0) {
2150 int id = usb_interface_id(func->conf, &func->function);
2151 if (unlikely(id < 0))
2153 func->interfaces_nums[idx] = id;
2155 newValue = func->interfaces_nums[idx];
2159 /* String' IDs are allocated when fsf_data is bound to cdev */
2160 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2165 * USB_DT_ENDPOINT are handled in
2166 * __ffs_func_bind_do_descs().
2168 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2171 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2172 if (unlikely(!func->eps[idx].ep))
2176 struct usb_endpoint_descriptor **descs;
2177 descs = func->eps[idx].descs;
2178 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2183 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2188 static int ffs_func_bind(struct usb_configuration *c,
2189 struct usb_function *f)
2191 struct ffs_function *func = ffs_func_from_usb(f);
2192 struct ffs_data *ffs = func->ffs;
2194 const int full = !!func->ffs->fs_descs_count;
2195 const int high = gadget_is_dualspeed(func->gadget) &&
2196 func->ffs->hs_descs_count;
2200 /* Make it a single chunk, less management later on */
2202 struct ffs_ep eps[ffs->eps_count];
2203 struct usb_descriptor_header
2204 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2205 struct usb_descriptor_header
2206 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2207 short inums[ffs->interfaces_count];
2208 char raw_descs[high ? ffs->raw_descs_length
2209 : ffs->raw_fs_descs_length];
2214 /* Only high speed but not supported by gadget? */
2215 if (unlikely(!(full | high)))
2219 data = kmalloc(sizeof *data, GFP_KERNEL);
2220 if (unlikely(!data))
2224 memset(data->eps, 0, sizeof data->eps);
2225 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2226 memset(data->inums, 0xff, sizeof data->inums);
2227 for (ret = ffs->eps_count; ret; --ret)
2228 data->eps[ret].num = -1;
2231 func->eps = data->eps;
2232 func->interfaces_nums = data->inums;
2235 * Go through all the endpoint descriptors and allocate
2236 * endpoints first, so that later we can rewrite the endpoint
2237 * numbers without worrying that it may be described later on.
2240 func->function.descriptors = data->fs_descs;
2241 ret = ffs_do_descs(ffs->fs_descs_count,
2243 sizeof data->raw_descs,
2244 __ffs_func_bind_do_descs, func);
2245 if (unlikely(ret < 0))
2252 func->function.hs_descriptors = data->hs_descs;
2253 ret = ffs_do_descs(ffs->hs_descs_count,
2254 data->raw_descs + ret,
2255 (sizeof data->raw_descs) - ret,
2256 __ffs_func_bind_do_descs, func);
2260 * Now handle interface numbers allocation and interface and
2261 * endpoint numbers rewriting. We can do that in one go
2264 ret = ffs_do_descs(ffs->fs_descs_count +
2265 (high ? ffs->hs_descs_count : 0),
2266 data->raw_descs, sizeof data->raw_descs,
2267 __ffs_func_bind_do_nums, func);
2268 if (unlikely(ret < 0))
2271 /* And we're done */
2272 ffs_event_add(ffs, FUNCTIONFS_BIND);
2276 /* XXX Do we need to release all claimed endpoints here? */
2281 /* Other USB function hooks *************************************************/
2283 static void ffs_func_unbind(struct usb_configuration *c,
2284 struct usb_function *f)
2286 struct ffs_function *func = ffs_func_from_usb(f);
2287 struct ffs_data *ffs = func->ffs;
2291 if (ffs->func == func) {
2292 ffs_func_eps_disable(func);
2296 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2298 ffs_func_free(func);
2301 static int ffs_func_set_alt(struct usb_function *f,
2302 unsigned interface, unsigned alt)
2304 struct ffs_function *func = ffs_func_from_usb(f);
2305 struct ffs_data *ffs = func->ffs;
2308 if (alt != (unsigned)-1) {
2309 intf = ffs_func_revmap_intf(func, interface);
2310 if (unlikely(intf < 0))
2315 ffs_func_eps_disable(ffs->func);
2317 if (ffs->state != FFS_ACTIVE)
2320 if (alt == (unsigned)-1) {
2322 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2327 ret = ffs_func_eps_enable(func);
2328 if (likely(ret >= 0))
2329 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2333 static void ffs_func_disable(struct usb_function *f)
2335 ffs_func_set_alt(f, 0, (unsigned)-1);
2338 static int ffs_func_setup(struct usb_function *f,
2339 const struct usb_ctrlrequest *creq)
2341 struct ffs_function *func = ffs_func_from_usb(f);
2342 struct ffs_data *ffs = func->ffs;
2343 unsigned long flags;
2348 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2349 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2350 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2351 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2352 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2355 * Most requests directed to interface go through here
2356 * (notable exceptions are set/get interface) so we need to
2357 * handle them. All other either handled by composite or
2358 * passed to usb_configuration->setup() (if one is set). No
2359 * matter, we will handle requests directed to endpoint here
2360 * as well (as it's straightforward) but what to do with any
2363 if (ffs->state != FFS_ACTIVE)
2366 switch (creq->bRequestType & USB_RECIP_MASK) {
2367 case USB_RECIP_INTERFACE:
2368 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2369 if (unlikely(ret < 0))
2373 case USB_RECIP_ENDPOINT:
2374 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2375 if (unlikely(ret < 0))
2383 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2384 ffs->ev.setup = *creq;
2385 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2386 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2387 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2392 static void ffs_func_suspend(struct usb_function *f)
2395 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2398 static void ffs_func_resume(struct usb_function *f)
2401 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2405 /* Endpoint and interface numbers reverse mapping ***************************/
2407 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2409 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2410 return num ? num : -EDOM;
2413 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2415 short *nums = func->interfaces_nums;
2416 unsigned count = func->ffs->interfaces_count;
2418 for (; count; --count, ++nums) {
2419 if (*nums >= 0 && *nums == intf)
2420 return nums - func->interfaces_nums;
2427 /* Misc helper functions ****************************************************/
2429 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2432 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2433 : mutex_lock_interruptible(mutex);
2436 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2443 data = kmalloc(len, GFP_KERNEL);
2444 if (unlikely(!data))
2445 return ERR_PTR(-ENOMEM);
2447 if (unlikely(__copy_from_user(data, buf, len))) {
2449 return ERR_PTR(-EFAULT);
2452 pr_vdebug("Buffer from user space:\n");
2453 ffs_dump_mem("", data, len);