2 * f_fs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
7 * Based on inode.c (GadgetFS) which was:
8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
19 /* #define VERBOSE_DEBUG */
21 #include <linux/blkdev.h>
22 #include <linux/pagemap.h>
23 #include <linux/export.h>
24 #include <linux/hid.h>
25 #include <asm/unaligned.h>
27 #include <linux/usb/composite.h>
28 #include <linux/usb/functionfs.h>
31 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
34 /* Debugging ****************************************************************/
38 # define pr_vdebug pr_debug
39 #endif /* pr_vdebug */
40 # define ffs_dump_mem(prefix, ptr, len) \
41 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
44 # define pr_vdebug(...) do { } while (0)
45 #endif /* pr_vdebug */
46 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
47 #endif /* VERBOSE_DEBUG */
49 #define ENTER() pr_vdebug("%s()\n", __func__)
52 /* The data structure and setup file ****************************************/
56 * Waiting for descriptors and strings.
58 * In this state no open(2), read(2) or write(2) on epfiles
59 * may succeed (which should not be the problem as there
60 * should be no such files opened in the first place).
66 * We've got descriptors and strings. We are or have called
67 * functionfs_ready_callback(). functionfs_bind() may have
68 * been called but we don't know.
70 * This is the only state in which operations on epfiles may
76 * All endpoints have been closed. This state is also set if
77 * we encounter an unrecoverable error. The only
78 * unrecoverable error is situation when after reading strings
79 * from user space we fail to initialise epfiles or
80 * functionfs_ready_callback() returns with error (<0).
82 * In this state no open(2), read(2) or write(2) (both on ep0
83 * as well as epfile) may succeed (at this point epfiles are
84 * unlinked and all closed so this is not a problem; ep0 is
85 * also closed but ep0 file exists and so open(2) on ep0 must
88 /*add by chunhou.wang for kernel crash bug 378800*/
90 /*end add by chunhou.wang*/
95 enum ffs_setup_state {
96 /* There is no setup request pending. */
99 * User has read events and there was a setup request event
100 * there. The next read/write on ep0 will handle the
105 * There was event pending but before user space handled it
106 * some other event was introduced which canceled existing
107 * setup. If this state is set read/write on ep0 return
108 * -EIDRM. This state is only set when adding event.
119 struct usb_gadget *gadget;
122 * Protect access read/write operations, only one read/write
123 * at a time. As a consequence protects ep0req and company.
124 * While setup request is being processed (queued) this is
130 * Protect access to endpoint related structures (basically
131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
137 * XXX REVISIT do we need our own request? Since we are not
138 * handling setup requests immediately user space may be so
139 * slow that another setup will be sent to the gadget but this
140 * time not to us but another function and then there could be
141 * a race. Is that the case? Or maybe we can use cdev->req
142 * after all, maybe we just need some spinlock for that?
144 struct usb_request *ep0req; /* P: mutex */
145 struct completion ep0req_completion; /* P: mutex */
146 int ep0req_status; /* P: mutex */
147 struct completion epin_completion; /* P: mutex */
148 struct completion epout_completion; /* P: mutex */
150 /* reference counter */
152 /* how many files are opened (EP0 and others) */
156 enum ffs_state state;
159 * Possible transitions:
160 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
161 * happens only in ep0 read which is P: mutex
162 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
163 * happens only in ep0 i/o which is P: mutex
164 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
165 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
167 enum ffs_setup_state setup_state;
169 #define FFS_SETUP_STATE(ffs) \
170 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
171 FFS_SETUP_CANCELED, FFS_NO_SETUP))
176 unsigned short count;
177 /* XXX REVISIT need to update it in some places, or do we? */
178 unsigned short can_stall;
179 struct usb_ctrlrequest setup;
181 wait_queue_head_t waitq;
182 } ev; /* the whole structure, P: ev.waitq.lock */
186 #define FFS_FL_CALL_CLOSED_CALLBACK 0
187 #define FFS_FL_BOUND 1
189 /* Active function */
190 struct ffs_function *func;
193 * Device name, write once when file system is mounted.
194 * Intended for user to read if she wants.
196 const char *dev_name;
197 /* Private data for our user (ie. gadget). Managed by user. */
200 /* filled by __ffs_data_got_descs() */
202 * Real descriptors are 16 bytes after raw_descs (so you need
203 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
204 * first full speed descriptor). raw_descs_length and
205 * raw_fs_descs_length do not have those 16 bytes added.
207 const void *raw_descs;
208 unsigned raw_descs_length;
209 unsigned raw_fs_descs_length;
210 unsigned fs_descs_count;
211 unsigned hs_descs_count;
213 unsigned short strings_count;
214 unsigned short interfaces_count;
215 unsigned short eps_count;
216 unsigned short _pad1;
218 /* filled by __ffs_data_got_strings() */
219 /* ids in stringtabs are set in functionfs_bind() */
220 const void *raw_strings;
221 struct usb_gadget_strings **stringtabs;
224 * File system's super block, write once when file system is
227 struct super_block *sb;
229 /* File permissions, written once when fs is mounted */
230 struct ffs_file_perms {
237 * The endpoint files, filled by ffs_epfiles_create(),
238 * destroyed by ffs_epfiles_destroy().
240 struct ffs_epfile *epfiles;
241 /*add by chunhou.wang for kernel crash bug 378800*/
243 /*end add by chunhou.wang*/
246 /* Reference counter handling */
247 static void ffs_data_get(struct ffs_data *ffs);
248 static void ffs_data_put(struct ffs_data *ffs);
249 /* Creates new ffs_data object. */
250 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
252 /* Opened counter handling. */
253 static void ffs_data_opened(struct ffs_data *ffs);
254 static void ffs_data_closed(struct ffs_data *ffs);
256 /* Called with ffs->mutex held; take over ownership of data. */
257 static int __must_check
258 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
259 static int __must_check
260 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
263 /* The function structure ***************************************************/
267 struct ffs_function {
268 struct usb_configuration *conf;
269 struct usb_gadget *gadget;
270 struct ffs_data *ffs;
274 short *interfaces_nums;
276 struct usb_function function;
280 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
282 return container_of(f, struct ffs_function, function);
285 static void ffs_func_free(struct ffs_function *func);
287 static void ffs_func_eps_disable(struct ffs_function *func);
288 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
290 static int ffs_func_bind(struct usb_configuration *,
291 struct usb_function *);
292 static void ffs_func_unbind(struct usb_configuration *,
293 struct usb_function *);
294 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
295 static void ffs_func_disable(struct usb_function *);
296 static int ffs_func_setup(struct usb_function *,
297 const struct usb_ctrlrequest *);
298 static void ffs_func_suspend(struct usb_function *);
299 static void ffs_func_resume(struct usb_function *);
302 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
303 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
306 /* The endpoints structures *************************************************/
308 static unsigned int send_buffer[160+2048];
309 static unsigned int recv_buffer[160+2048];
311 struct usb_ep *ep; /* P: ffs->eps_lock */
312 struct usb_request *req; /* P: epfile->mutex */
314 /* [0]: full speed, [1]: high speed */
315 struct usb_endpoint_descriptor *descs[2];
319 int status; /* P: epfile->mutex */
323 /* Protects ep->ep and ep->req. */
325 wait_queue_head_t wait;
327 struct ffs_data *ffs;
328 struct ffs_ep *ep; /* P: ffs->eps_lock */
330 struct dentry *dentry;
334 unsigned char in; /* P: ffs->eps_lock */
335 unsigned char isoc; /* P: ffs->eps_lock */
340 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
341 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
343 static struct inode *__must_check
344 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
345 const struct file_operations *fops,
346 struct dentry **dentry_p);
349 /* Misc helper functions ****************************************************/
351 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
352 __attribute__((warn_unused_result, nonnull));
353 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
354 __attribute__((warn_unused_result, nonnull));
357 /* Control file aka ep0 *****************************************************/
359 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
361 struct ffs_data *ffs = req->context;
363 complete_all(&ffs->ep0req_completion);
366 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
368 struct usb_request *req = ffs->ep0req;
371 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
373 spin_unlock_irq(&ffs->ev.waitq.lock);
379 * UDC layer requires to provide a buffer even for ZLP, but should
380 * not use it at all. Let's provide some poisoned pointer to catch
381 * possible bug in the driver.
383 if (req->buf == NULL)
384 req->buf = (void *)0xDEADBABE;
386 INIT_COMPLETION(ffs->ep0req_completion);
388 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
389 if (unlikely(ret < 0))
392 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
394 usb_ep_dequeue(ffs->gadget->ep0, req);
398 ffs->setup_state = FFS_NO_SETUP;
399 return ffs->ep0req_status;
402 static int __ffs_ep0_stall(struct ffs_data *ffs)
404 if (ffs->ev.can_stall) {
405 pr_vdebug("ep0 stall\n");
406 usb_ep_set_halt(ffs->gadget->ep0);
407 ffs->setup_state = FFS_NO_SETUP;
410 pr_debug("bogus ep0 stall!\n");
415 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
416 size_t len, loff_t *ptr)
418 struct ffs_data *ffs = file->private_data;
424 /* Fast check if setup was canceled */
425 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
429 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
430 if (unlikely(ret < 0))
434 switch (ffs->state) {
435 case FFS_READ_DESCRIPTORS:
436 case FFS_READ_STRINGS:
438 if (unlikely(len < 16)) {
443 data = ffs_prepare_buffer(buf, len);
450 if (ffs->state == FFS_READ_DESCRIPTORS) {
451 pr_info("read descriptors\n");
452 ret = __ffs_data_got_descs(ffs, data, len);
453 if (unlikely(ret < 0))
456 ffs->state = FFS_READ_STRINGS;
459 pr_info("read strings\n");
460 ret = __ffs_data_got_strings(ffs, data, len);
461 if (unlikely(ret < 0))
464 ret = ffs_epfiles_create(ffs);
466 ffs->state = FFS_CLOSING;
470 ffs->state = FFS_ACTIVE;
471 mutex_unlock(&ffs->mutex);
473 ret = functionfs_ready_callback(ffs);
474 if (unlikely(ret < 0)) {
475 ffs->state = FFS_CLOSING;
479 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
487 * We're called from user space, we can use _irq
488 * rather then _irqsave
490 spin_lock_irq(&ffs->ev.waitq.lock);
491 switch (FFS_SETUP_STATE(ffs)) {
492 case FFS_SETUP_CANCELED:
500 case FFS_SETUP_PENDING:
504 /* FFS_SETUP_PENDING */
505 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
506 spin_unlock_irq(&ffs->ev.waitq.lock);
507 ret = __ffs_ep0_stall(ffs);
511 /* FFS_SETUP_PENDING and not stall */
512 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
514 spin_unlock_irq(&ffs->ev.waitq.lock);
516 data = ffs_prepare_buffer(buf, len);
522 spin_lock_irq(&ffs->ev.waitq.lock);
525 * We are guaranteed to be still in FFS_ACTIVE state
526 * but the state of setup could have changed from
527 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
528 * to check for that. If that happened we copied data
529 * from user space in vain but it's unlikely.
531 * For sure we are not in FFS_NO_SETUP since this is
532 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
533 * transition can be performed and it's protected by
536 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
539 spin_unlock_irq(&ffs->ev.waitq.lock);
541 /* unlocks spinlock */
542 ret = __ffs_ep0_queue_wait(ffs, data, len);
552 mutex_unlock(&ffs->mutex);
556 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
560 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
563 struct usb_functionfs_event events[n];
566 memset(events, 0, sizeof events);
569 events[i].type = ffs->ev.types[i];
570 if (events[i].type == FUNCTIONFS_SETUP) {
571 events[i].u.setup = ffs->ev.setup;
572 ffs->setup_state = FFS_SETUP_PENDING;
576 if (n < ffs->ev.count) {
578 memmove(ffs->ev.types, ffs->ev.types + n,
579 ffs->ev.count * sizeof *ffs->ev.types);
584 spin_unlock_irq(&ffs->ev.waitq.lock);
585 mutex_unlock(&ffs->mutex);
587 return unlikely(__copy_to_user(buf, events, sizeof events))
588 ? -EFAULT : sizeof events;
591 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
592 size_t len, loff_t *ptr)
594 struct ffs_data *ffs = file->private_data;
601 /* Fast check if setup was canceled */
602 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
606 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
607 if (unlikely(ret < 0))
611 if (ffs->state != FFS_ACTIVE) {
617 * We're called from user space, we can use _irq rather then
620 spin_lock_irq(&ffs->ev.waitq.lock);
622 switch (FFS_SETUP_STATE(ffs)) {
623 case FFS_SETUP_CANCELED:
628 n = len / sizeof(struct usb_functionfs_event);
634 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
639 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
645 return __ffs_ep0_read_events(ffs, buf,
646 min(n, (size_t)ffs->ev.count));
648 case FFS_SETUP_PENDING:
649 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
650 spin_unlock_irq(&ffs->ev.waitq.lock);
651 ret = __ffs_ep0_stall(ffs);
655 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
657 spin_unlock_irq(&ffs->ev.waitq.lock);
660 data = kmalloc(len, GFP_KERNEL);
661 if (unlikely(!data)) {
667 spin_lock_irq(&ffs->ev.waitq.lock);
669 /* See ffs_ep0_write() */
670 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
675 /* unlocks spinlock */
676 ret = __ffs_ep0_queue_wait(ffs, data, len);
677 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
686 spin_unlock_irq(&ffs->ev.waitq.lock);
688 mutex_unlock(&ffs->mutex);
693 static int ffs_ep0_open(struct inode *inode, struct file *file)
695 struct ffs_data *ffs = inode->i_private;
699 if (unlikely(ffs->state == FFS_CLOSING))
702 file->private_data = ffs;
703 ffs_data_opened(ffs);
708 static int ffs_ep0_release(struct inode *inode, struct file *file)
710 struct ffs_data *ffs = file->private_data;
714 ffs_data_closed(ffs);
719 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
721 struct ffs_data *ffs = file->private_data;
722 struct usb_gadget *gadget = ffs->gadget;
727 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
728 struct ffs_function *func = ffs->func;
729 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
730 } else if (gadget && gadget->ops->ioctl) {
731 ret = gadget->ops->ioctl(gadget, code, value);
739 static const struct file_operations ffs_ep0_operations = {
742 .open = ffs_ep0_open,
743 .write = ffs_ep0_write,
744 .read = ffs_ep0_read,
745 .release = ffs_ep0_release,
746 .unlocked_ioctl = ffs_ep0_ioctl,
750 /* "Normal" endpoints operations ********************************************/
752 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
754 struct ffs_ep *ep = _ep->driver_data;
756 /* req may be freed during unbind */
757 if (ep && ep->req && likely(req->context)) {
758 struct ffs_ep *ep = _ep->driver_data;
759 ep->status = req->status ? req->status : req->actual;
760 complete(req->context);
764 static ssize_t ffs_epfile_io(struct file *file,
765 char __user *buf, size_t len, int read)
767 struct ffs_epfile *epfile = file->private_data;
768 struct ffs_data *ffs = epfile->ffs;
771 ssize_t ret, data_len;
775 data = (char *)&recv_buffer[8];
777 data = (char *)&send_buffer[8];
780 spin_unlock_irq(&epfile->ffs->eps_lock);
781 mutex_unlock(&epfile->mutex);
784 /* Are we still active? */
785 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
790 /* Wait for endpoint to be enabled */
793 if (file->f_flags & O_NONBLOCK) {
798 if (wait_event_interruptible(epfile->wait,
799 (ep = epfile->ep))) {
806 halt = !read == !epfile->in;
807 if (halt && epfile->isoc) {
812 /* Allocate & copy */
820 unlikely(__copy_from_user(data, buf, data_len))) {
826 /* We will be using request */
827 ret = ffs_mutex_lock(&epfile->mutex,
828 file->f_flags & O_NONBLOCK);
833 * We're called from user space, we can use _irq rather then
836 spin_lock_irq(&epfile->ffs->eps_lock);
839 * While we were acquiring mutex endpoint got disabled
842 } while (unlikely(epfile->ep != ep));
845 if (unlikely(halt)) {
846 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
847 usb_ep_set_halt(ep->ep);
848 spin_unlock_irq(&epfile->ffs->eps_lock);
851 /* Fire the request */
852 struct completion *done;
854 struct usb_request *req = ep->req;
855 req->complete = ffs_epfile_io_complete;
857 req->length = data_len;
860 INIT_COMPLETION(ffs->epout_completion);
861 req->context = done = &ffs->epout_completion;
863 INIT_COMPLETION(ffs->epin_completion);
864 req->context = done = &ffs->epin_completion;
867 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
869 spin_unlock_irq(&epfile->ffs->eps_lock);
871 if (unlikely(ret < 0)) {
873 } else if (unlikely(wait_for_completion_interruptible(done))) {
875 usb_ep_dequeue(ep->ep, req);
878 if (read && ret > 0){
879 ret = min_t(size_t, ret, len);
880 if(unlikely(copy_to_user(buf, data, ret)))
886 mutex_unlock(&epfile->mutex);
892 ffs_epfile_write(struct file *file, const char __user *buf, size_t len, loff_t *ptr)
900 sent_len = ffs_epfile_io(file, (char __user *)buf, temp_len,0);
903 temp_len -= sent_len;
904 } else return sent_len;
911 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
915 return ffs_epfile_io(file, buf, len, 1);
919 ffs_epfile_open(struct inode *inode, struct file *file)
921 struct ffs_epfile *epfile = inode->i_private;
925 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
928 file->private_data = epfile;
929 ffs_data_opened(epfile->ffs);
935 ffs_epfile_release(struct inode *inode, struct file *file)
937 struct ffs_epfile *epfile = inode->i_private;
941 ffs_data_closed(epfile->ffs);
946 static long ffs_epfile_ioctl(struct file *file, unsigned code,
949 struct ffs_epfile *epfile = file->private_data;
954 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
957 spin_lock_irq(&epfile->ffs->eps_lock);
958 if (likely(epfile->ep)) {
960 case FUNCTIONFS_FIFO_STATUS:
961 ret = usb_ep_fifo_status(epfile->ep->ep);
963 case FUNCTIONFS_FIFO_FLUSH:
964 usb_ep_fifo_flush(epfile->ep->ep);
967 case FUNCTIONFS_CLEAR_HALT:
968 ret = usb_ep_clear_halt(epfile->ep->ep);
970 case FUNCTIONFS_ENDPOINT_REVMAP:
971 ret = epfile->ep->num;
979 spin_unlock_irq(&epfile->ffs->eps_lock);
984 static const struct file_operations ffs_epfile_operations = {
987 .open = ffs_epfile_open,
988 .write = ffs_epfile_write,
989 .read = ffs_epfile_read,
990 .release = ffs_epfile_release,
991 .unlocked_ioctl = ffs_epfile_ioctl,
995 /* File system and super block operations ***********************************/
998 * Mounting the file system creates a controller file, used first for
999 * function configuration then later for event monitoring.
1002 static struct inode *__must_check
1003 ffs_sb_make_inode(struct super_block *sb, void *data,
1004 const struct file_operations *fops,
1005 const struct inode_operations *iops,
1006 struct ffs_file_perms *perms)
1008 struct inode *inode;
1012 inode = new_inode(sb);
1014 if (likely(inode)) {
1015 struct timespec current_time = CURRENT_TIME;
1017 inode->i_ino = get_next_ino();
1018 inode->i_mode = perms->mode;
1019 inode->i_uid = perms->uid;
1020 inode->i_gid = perms->gid;
1021 inode->i_atime = current_time;
1022 inode->i_mtime = current_time;
1023 inode->i_ctime = current_time;
1024 inode->i_private = data;
1026 inode->i_fop = fops;
1034 /* Create "regular" file */
1035 static struct inode *ffs_sb_create_file(struct super_block *sb,
1036 const char *name, void *data,
1037 const struct file_operations *fops,
1038 struct dentry **dentry_p)
1040 struct ffs_data *ffs = sb->s_fs_info;
1041 struct dentry *dentry;
1042 struct inode *inode;
1046 dentry = d_alloc_name(sb->s_root, name);
1047 if (unlikely(!dentry))
1050 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1051 if (unlikely(!inode)) {
1056 d_add(dentry, inode);
1064 static const struct super_operations ffs_sb_operations = {
1065 .statfs = simple_statfs,
1066 .drop_inode = generic_delete_inode,
1069 struct ffs_sb_fill_data {
1070 struct ffs_file_perms perms;
1072 const char *dev_name;
1073 /*add by chunhou.wang for kernel crash bug 378800*/
1075 /*end add by chunhou.wang*/
1076 struct ffs_data *ffs_data;
1079 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1081 struct ffs_sb_fill_data *data = _data;
1082 struct inode *inode;
1083 struct ffs_data *ffs = data->ffs_data;
1088 data->ffs_data = NULL;
1089 sb->s_fs_info = ffs;
1090 sb->s_blocksize = PAGE_CACHE_SIZE;
1091 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1092 sb->s_magic = FUNCTIONFS_MAGIC;
1093 sb->s_op = &ffs_sb_operations;
1094 sb->s_time_gran = 1;
1097 data->perms.mode = data->root_mode;
1098 inode = ffs_sb_make_inode(sb, NULL,
1099 &simple_dir_operations,
1100 &simple_dir_inode_operations,
1102 sb->s_root = d_make_root(inode);
1103 if (unlikely(!sb->s_root))
1107 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1108 &ffs_ep0_operations, NULL)))
1114 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1118 if (!opts || !*opts)
1122 unsigned long value;
1126 comma = strchr(opts, ',');
1131 eq = strchr(opts, '=');
1132 if (unlikely(!eq)) {
1133 pr_err("'=' missing in %s\n", opts);
1139 if (kstrtoul(eq + 1, 0, &value)) {
1140 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1144 /* Interpret option */
1145 switch (eq - opts) {
1146 /*add by chunhou.wang for kernel crash bug 378800*/
1148 if (!memcmp(opts, "no_disconnect", 13))
1149 data->no_disconnect = !!value;
1153 /*end add by chunhou.wang*/
1155 if (!memcmp(opts, "rmode", 5))
1156 data->root_mode = (value & 0555) | S_IFDIR;
1157 else if (!memcmp(opts, "fmode", 5))
1158 data->perms.mode = (value & 0666) | S_IFREG;
1164 if (!memcmp(opts, "mode", 4)) {
1165 data->root_mode = (value & 0555) | S_IFDIR;
1166 data->perms.mode = (value & 0666) | S_IFREG;
1173 if (!memcmp(opts, "uid", 3)) {
1174 data->perms.uid = make_kuid(current_user_ns(), value);
1175 if (!uid_valid(data->perms.uid)) {
1176 pr_err("%s: unmapped value: %lu\n", opts, value);
1179 } else if (!memcmp(opts, "gid", 3)) {
1180 data->perms.gid = make_kgid(current_user_ns(), value);
1181 if (!gid_valid(data->perms.gid)) {
1182 pr_err("%s: unmapped value: %lu\n", opts, value);
1192 pr_err("%s: invalid option\n", opts);
1196 /* Next iteration */
1205 /* "mount -t functionfs dev_name /dev/function" ends up here */
1207 static struct dentry *
1208 ffs_fs_mount(struct file_system_type *t, int flags,
1209 const char *dev_name, void *opts)
1211 struct ffs_sb_fill_data data = {
1213 .mode = S_IFREG | 0600,
1214 .uid = GLOBAL_ROOT_UID,
1215 .gid = GLOBAL_ROOT_GID,
1217 .root_mode = S_IFDIR | 0500,
1218 /*add by chunhou.wang for kernel crash bug 378800*/
1219 .no_disconnect = false,
1220 /*end add by chunhou.wang*/
1225 struct ffs_data *ffs;
1229 ret = ffs_fs_parse_opts(&data, opts);
1230 if (unlikely(ret < 0))
1231 return ERR_PTR(ret);
1233 ffs = ffs_data_new();
1235 return ERR_PTR(-ENOMEM);
1236 ffs->file_perms = data.perms;
1237 /*add by chunhou.wang for kernel crash bug 378800*/
1238 ffs->no_disconnect = data.no_disconnect;
1239 /*end add by chunhou.wang*/
1241 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1242 if (unlikely(!ffs->dev_name)) {
1244 return ERR_PTR(-ENOMEM);
1247 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1248 if (IS_ERR(ffs_dev)) {
1250 return ERR_CAST(ffs_dev);
1252 ffs->private_data = ffs_dev;
1253 data.ffs_data = ffs;
1255 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1256 if (IS_ERR(rv) && data.ffs_data) {
1257 functionfs_release_dev_callback(data.ffs_data);
1258 ffs_data_put(data.ffs_data);
1264 ffs_fs_kill_sb(struct super_block *sb)
1268 kill_litter_super(sb);
1269 if (sb->s_fs_info) {
1270 functionfs_release_dev_callback(sb->s_fs_info);
1271 /*add by chunhou.wang for kernel crash bug 378800*/
1272 ffs_data_closed(sb->s_fs_info);
1273 /*end add by chunhou.wang*/
1274 ffs_data_put(sb->s_fs_info);
1278 static struct file_system_type ffs_fs_type = {
1279 .owner = THIS_MODULE,
1280 .name = "functionfs",
1281 .mount = ffs_fs_mount,
1282 .kill_sb = ffs_fs_kill_sb,
1284 MODULE_ALIAS_FS("functionfs");
1287 /* Driver's main init/cleanup functions *************************************/
1289 static int functionfs_init(void)
1295 ret = register_filesystem(&ffs_fs_type);
1297 pr_info("file system registered\n");
1299 pr_err("failed registering file system (%d)\n", ret);
1304 static void functionfs_cleanup(void)
1308 pr_info("unloading\n");
1309 unregister_filesystem(&ffs_fs_type);
1313 /* ffs_data and ffs_function construction and destruction code **************/
1315 static void ffs_data_clear(struct ffs_data *ffs);
1316 static void ffs_data_reset(struct ffs_data *ffs);
1318 static void ffs_data_get(struct ffs_data *ffs)
1322 atomic_inc(&ffs->ref);
1325 static void ffs_data_opened(struct ffs_data *ffs)
1329 atomic_inc(&ffs->ref);
1330 /*add by chunhou.wang for kernel crash bug 378800*/
1331 //atomic_inc(&ffs->opened);
1332 if (atomic_add_return(1, &ffs->opened) == 1)
1333 if (ffs->state == FFS_DEACTIVATED)
1334 ffs_data_reset(ffs);
1335 /*end add by chunhou.wang*/
1338 static void ffs_data_put(struct ffs_data *ffs)
1342 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1343 pr_info("%s(): freeing\n", __func__);
1344 ffs_data_clear(ffs);
1345 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1346 waitqueue_active(&ffs->ep0req_completion.wait));
1347 kfree(ffs->dev_name);
1352 static void ffs_data_closed(struct ffs_data *ffs)
1356 if (atomic_dec_and_test(&ffs->opened)) {
1357 /*add by chunhou.wang for kernel crash bug 378800*/
1358 if (ffs->no_disconnect) {
1359 ffs->state = FFS_DEACTIVATED;
1361 ffs_epfiles_destroy(ffs->epfiles,ffs->eps_count);
1362 ffs->epfiles = NULL;
1364 if (ffs->setup_state == FFS_SETUP_PENDING)
1365 __ffs_ep0_stall(ffs);
1367 ffs->state = FFS_CLOSING;
1368 ffs_data_reset(ffs);
1371 if (atomic_read(&ffs->opened) < 0)
1372 ffs_data_clear(ffs);
1373 /*end add by chunhou.wang*/
1377 static struct ffs_data *ffs_data_new(void)
1379 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1385 atomic_set(&ffs->ref, 1);
1386 atomic_set(&ffs->opened, 0);
1387 ffs->state = FFS_READ_DESCRIPTORS;
1388 mutex_init(&ffs->mutex);
1389 spin_lock_init(&ffs->eps_lock);
1390 init_waitqueue_head(&ffs->ev.waitq);
1391 init_completion(&ffs->ep0req_completion);
1392 init_completion(&ffs->epin_completion);
1393 init_completion(&ffs->epout_completion);
1395 /* XXX REVISIT need to update it in some places, or do we? */
1396 ffs->ev.can_stall = 1;
1401 static void ffs_data_clear(struct ffs_data *ffs)
1405 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1406 functionfs_closed_callback(ffs);
1408 BUG_ON(ffs->gadget);
1411 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1413 kfree(ffs->raw_descs);
1414 kfree(ffs->raw_strings);
1415 kfree(ffs->stringtabs);
1418 static void ffs_data_reset(struct ffs_data *ffs)
1422 ffs_data_clear(ffs);
1424 ffs->epfiles = NULL;
1425 ffs->raw_descs = NULL;
1426 ffs->raw_strings = NULL;
1427 ffs->stringtabs = NULL;
1429 ffs->raw_descs_length = 0;
1430 ffs->raw_fs_descs_length = 0;
1431 ffs->fs_descs_count = 0;
1432 ffs->hs_descs_count = 0;
1434 ffs->strings_count = 0;
1435 ffs->interfaces_count = 0;
1440 ffs->state = FFS_READ_DESCRIPTORS;
1441 ffs->setup_state = FFS_NO_SETUP;
1446 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1448 struct usb_gadget_strings **lang;
1453 if (WARN_ON(ffs->state != FFS_ACTIVE
1454 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1457 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1458 if (unlikely(first_id < 0))
1461 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1462 if (unlikely(!ffs->ep0req))
1464 ffs->ep0req->complete = ffs_ep0_complete;
1465 ffs->ep0req->context = ffs;
1467 lang = ffs->stringtabs;
1469 for (; *lang; ++lang) {
1470 struct usb_string *str = (*lang)->strings;
1472 for (; str->s; ++id, ++str)
1477 ffs->gadget = cdev->gadget;
1482 static void functionfs_unbind(struct ffs_data *ffs)
1486 if (!WARN_ON(!ffs->gadget)) {
1487 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1491 clear_bit(FFS_FL_BOUND, &ffs->flags);
1495 static int ffs_epfiles_create(struct ffs_data *ffs)
1497 struct ffs_epfile *epfile, *epfiles;
1502 count = ffs->eps_count;
1503 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1508 for (i = 1; i <= count; ++i, ++epfile) {
1510 mutex_init(&epfile->mutex);
1511 init_waitqueue_head(&epfile->wait);
1512 sprintf(epfiles->name, "ep%u", i);
1513 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1514 &ffs_epfile_operations,
1515 &epfile->dentry))) {
1516 ffs_epfiles_destroy(epfiles, i - 1);
1521 ffs->epfiles = epfiles;
1525 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1527 struct ffs_epfile *epfile = epfiles;
1531 for (; count; --count, ++epfile) {
1532 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1533 waitqueue_active(&epfile->wait));
1534 if (epfile->dentry) {
1535 d_delete(epfile->dentry);
1536 dput(epfile->dentry);
1537 epfile->dentry = NULL;
1544 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1545 struct usb_configuration *c,
1546 struct ffs_data *ffs)
1548 struct ffs_function *func;
1553 func = kzalloc(sizeof *func, GFP_KERNEL);
1554 if (unlikely(!func))
1557 func->function.name = "Function FS Gadget";
1558 func->function.strings = ffs->stringtabs;
1560 func->function.bind = ffs_func_bind;
1561 func->function.unbind = ffs_func_unbind;
1562 func->function.set_alt = ffs_func_set_alt;
1563 func->function.disable = ffs_func_disable;
1564 func->function.setup = ffs_func_setup;
1565 func->function.suspend = ffs_func_suspend;
1566 func->function.resume = ffs_func_resume;
1569 func->gadget = cdev->gadget;
1573 ret = usb_add_function(c, &func->function);
1575 ffs_func_free(func);
1580 static void ffs_func_free(struct ffs_function *func)
1582 struct ffs_ep *ep = func->eps;
1583 unsigned count = func->ffs->eps_count;
1584 unsigned long flags;
1588 /* cleanup after autoconfig */
1589 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1591 if (ep->ep && ep->req)
1592 usb_ep_free_request(ep->ep, ep->req);
1596 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1598 ffs_data_put(func->ffs);
1602 * eps and interfaces_nums are allocated in the same chunk so
1603 * only one free is required. Descriptors are also allocated
1604 * in the same chunk.
1610 static void ffs_func_eps_disable(struct ffs_function *func)
1612 struct ffs_ep *ep = func->eps;
1613 struct ffs_epfile *epfile = func->ffs->epfiles;
1614 unsigned count = func->ffs->eps_count;
1615 unsigned long flags;
1617 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1619 /* pending requests get nuked */
1621 usb_ep_disable(ep->ep);
1624 /*add by chunhou.wang for kernel crash bug 378800*/
1630 /*end add by chunhou.wang*/
1632 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1635 static int ffs_func_eps_enable(struct ffs_function *func)
1637 struct ffs_data *ffs = func->ffs;
1638 struct ffs_ep *ep = func->eps;
1639 struct ffs_epfile *epfile = ffs->epfiles;
1640 unsigned count = ffs->eps_count;
1641 unsigned long flags;
1644 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1646 struct usb_endpoint_descriptor *ds;
1647 int desc_idx = ffs->gadget->speed == USB_SPEED_HIGH ? 1 : 0;
1648 ds = ep->descs[desc_idx];
1654 ep->ep->driver_data = ep;
1656 ret = usb_ep_enable(ep->ep);
1659 epfile->in = usb_endpoint_dir_in(ds);
1660 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1665 wake_up(&epfile->wait);
1670 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1676 /* Parsing and building descriptors and strings *****************************/
1679 * This validates if data pointed by data is a valid USB descriptor as
1680 * well as record how many interfaces, endpoints and strings are
1681 * required by given configuration. Returns address after the
1682 * descriptor or NULL if data is invalid.
1685 enum ffs_entity_type {
1686 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1689 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1691 struct usb_descriptor_header *desc,
1694 static int __must_check ffs_do_desc(char *data, unsigned len,
1695 ffs_entity_callback entity, void *priv)
1697 struct usb_descriptor_header *_ds = (void *)data;
1703 /* At least two bytes are required: length and type */
1705 pr_vdebug("descriptor too short\n");
1709 /* If we have at least as many bytes as the descriptor takes? */
1710 length = _ds->bLength;
1712 pr_vdebug("descriptor longer then available data\n");
1716 #define __entity_check_INTERFACE(val) 1
1717 #define __entity_check_STRING(val) (val)
1718 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1719 #define __entity(type, val) do { \
1720 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1721 if (unlikely(!__entity_check_ ##type(val))) { \
1722 pr_vdebug("invalid entity's value\n"); \
1725 ret = entity(FFS_ ##type, &val, _ds, priv); \
1726 if (unlikely(ret < 0)) { \
1727 pr_debug("entity " #type "(%02x); ret = %d\n", \
1733 /* Parse descriptor depending on type. */
1734 switch (_ds->bDescriptorType) {
1738 case USB_DT_DEVICE_QUALIFIER:
1739 /* function can't have any of those */
1740 pr_vdebug("descriptor reserved for gadget: %d\n",
1741 _ds->bDescriptorType);
1744 case USB_DT_INTERFACE: {
1745 struct usb_interface_descriptor *ds = (void *)_ds;
1746 pr_vdebug("interface descriptor\n");
1747 if (length != sizeof *ds)
1750 __entity(INTERFACE, ds->bInterfaceNumber);
1752 __entity(STRING, ds->iInterface);
1756 case USB_DT_ENDPOINT: {
1757 struct usb_endpoint_descriptor *ds = (void *)_ds;
1758 pr_vdebug("endpoint descriptor\n");
1759 if (length != USB_DT_ENDPOINT_SIZE &&
1760 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1762 __entity(ENDPOINT, ds->bEndpointAddress);
1767 pr_vdebug("hid descriptor\n");
1768 if (length != sizeof(struct hid_descriptor))
1773 if (length != sizeof(struct usb_otg_descriptor))
1777 case USB_DT_INTERFACE_ASSOCIATION: {
1778 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1779 pr_vdebug("interface association descriptor\n");
1780 if (length != sizeof *ds)
1783 __entity(STRING, ds->iFunction);
1787 case USB_DT_OTHER_SPEED_CONFIG:
1788 case USB_DT_INTERFACE_POWER:
1790 case USB_DT_SECURITY:
1791 case USB_DT_CS_RADIO_CONTROL:
1793 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1797 /* We should never be here */
1798 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1802 pr_vdebug("invalid length: %d (descriptor %d)\n",
1803 _ds->bLength, _ds->bDescriptorType);
1808 #undef __entity_check_DESCRIPTOR
1809 #undef __entity_check_INTERFACE
1810 #undef __entity_check_STRING
1811 #undef __entity_check_ENDPOINT
1816 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1817 ffs_entity_callback entity, void *priv)
1819 const unsigned _len = len;
1820 unsigned long num = 0;
1830 /* Record "descriptor" entity */
1831 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1832 if (unlikely(ret < 0)) {
1833 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1841 ret = ffs_do_desc(data, len, entity, priv);
1842 if (unlikely(ret < 0)) {
1843 pr_debug("%s returns %d\n", __func__, ret);
1853 static int __ffs_data_do_entity(enum ffs_entity_type type,
1854 u8 *valuep, struct usb_descriptor_header *desc,
1857 struct ffs_data *ffs = priv;
1862 case FFS_DESCRIPTOR:
1867 * Interfaces are indexed from zero so if we
1868 * encountered interface "n" then there are at least
1871 if (*valuep >= ffs->interfaces_count)
1872 ffs->interfaces_count = *valuep + 1;
1877 * Strings are indexed from 1 (0 is magic ;) reserved
1878 * for languages list or some such)
1880 if (*valuep > ffs->strings_count)
1881 ffs->strings_count = *valuep;
1885 /* Endpoints are indexed from 1 as well. */
1886 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1887 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1894 static int __ffs_data_got_descs(struct ffs_data *ffs,
1895 char *const _data, size_t len)
1897 unsigned fs_count, hs_count;
1898 int fs_len, ret = -EINVAL;
1903 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1904 get_unaligned_le32(data + 4) != len))
1906 fs_count = get_unaligned_le32(data + 8);
1907 hs_count = get_unaligned_le32(data + 12);
1909 if (!fs_count && !hs_count)
1915 if (likely(fs_count)) {
1916 fs_len = ffs_do_descs(fs_count, data, len,
1917 __ffs_data_do_entity, ffs);
1918 if (unlikely(fs_len < 0)) {
1929 if (likely(hs_count)) {
1930 ret = ffs_do_descs(hs_count, data, len,
1931 __ffs_data_do_entity, ffs);
1932 if (unlikely(ret < 0))
1938 if (unlikely(len != ret))
1941 ffs->raw_fs_descs_length = fs_len;
1942 ffs->raw_descs_length = fs_len + ret;
1943 ffs->raw_descs = _data;
1944 ffs->fs_descs_count = fs_count;
1945 ffs->hs_descs_count = hs_count;
1956 static int __ffs_data_got_strings(struct ffs_data *ffs,
1957 char *const _data, size_t len)
1959 u32 str_count, needed_count, lang_count;
1960 struct usb_gadget_strings **stringtabs, *t;
1961 struct usb_string *strings, *s;
1962 const char *data = _data;
1966 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1967 get_unaligned_le32(data + 4) != len))
1969 str_count = get_unaligned_le32(data + 8);
1970 lang_count = get_unaligned_le32(data + 12);
1972 /* if one is zero the other must be zero */
1973 if (unlikely(!str_count != !lang_count))
1976 /* Do we have at least as many strings as descriptors need? */
1977 needed_count = ffs->strings_count;
1978 if (unlikely(str_count < needed_count))
1982 * If we don't need any strings just return and free all
1985 if (!needed_count) {
1990 /* Allocate everything in one chunk so there's less maintenance. */
1993 struct usb_gadget_strings *stringtabs[lang_count + 1];
1994 struct usb_gadget_strings stringtab[lang_count];
1995 struct usb_string strings[lang_count*(needed_count+1)];
1999 d = kmalloc(sizeof *d, GFP_KERNEL);
2005 stringtabs = d->stringtabs;
2009 *stringtabs++ = t++;
2013 stringtabs = d->stringtabs;
2019 /* For each language */
2023 do { /* lang_count > 0 so we can use do-while */
2024 unsigned needed = needed_count;
2026 if (unlikely(len < 3))
2028 t->language = get_unaligned_le16(data);
2035 /* For each string */
2036 do { /* str_count > 0 so we can use do-while */
2037 size_t length = strnlen(data, len);
2039 if (unlikely(length == len))
2043 * User may provide more strings then we need,
2044 * if that's the case we simply ignore the
2047 if (likely(needed)) {
2049 * s->id will be set while adding
2050 * function to configuration so for
2051 * now just leave garbage here.
2060 } while (--str_count);
2062 s->id = 0; /* terminator */
2066 } while (--lang_count);
2068 /* Some garbage left? */
2073 ffs->stringtabs = stringtabs;
2074 ffs->raw_strings = _data;
2086 /* Events handling and management *******************************************/
2088 static void __ffs_event_add(struct ffs_data *ffs,
2089 enum usb_functionfs_event_type type)
2091 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2095 * Abort any unhandled setup
2097 * We do not need to worry about some cmpxchg() changing value
2098 * of ffs->setup_state without holding the lock because when
2099 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2100 * the source does nothing.
2102 if (ffs->setup_state == FFS_SETUP_PENDING)
2103 ffs->setup_state = FFS_SETUP_CANCELED;
2106 case FUNCTIONFS_RESUME:
2107 rem_type2 = FUNCTIONFS_SUSPEND;
2109 case FUNCTIONFS_SUSPEND:
2110 case FUNCTIONFS_SETUP:
2112 /* Discard all similar events */
2115 case FUNCTIONFS_BIND:
2116 case FUNCTIONFS_UNBIND:
2117 case FUNCTIONFS_DISABLE:
2118 case FUNCTIONFS_ENABLE:
2119 /* Discard everything other then power management. */
2120 rem_type1 = FUNCTIONFS_SUSPEND;
2121 rem_type2 = FUNCTIONFS_RESUME;
2130 u8 *ev = ffs->ev.types, *out = ev;
2131 unsigned n = ffs->ev.count;
2132 for (; n; --n, ++ev)
2133 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2136 pr_vdebug("purging event %d\n", *ev);
2137 ffs->ev.count = out - ffs->ev.types;
2140 pr_vdebug("adding event %d\n", type);
2141 ffs->ev.types[ffs->ev.count++] = type;
2142 wake_up_locked(&ffs->ev.waitq);
2145 static void ffs_event_add(struct ffs_data *ffs,
2146 enum usb_functionfs_event_type type)
2148 unsigned long flags;
2149 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2150 __ffs_event_add(ffs, type);
2151 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2155 /* Bind/unbind USB function hooks *******************************************/
2157 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2158 struct usb_descriptor_header *desc,
2161 struct usb_endpoint_descriptor *ds = (void *)desc;
2162 struct ffs_function *func = priv;
2163 struct ffs_ep *ffs_ep;
2166 * If hs_descriptors is not NULL then we are reading hs
2169 const int isHS = func->function.hs_descriptors != NULL;
2172 if (type != FFS_DESCRIPTOR)
2176 func->function.hs_descriptors[(long)valuep] = desc;
2178 func->function.fs_descriptors[(long)valuep] = desc;
2180 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2183 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2184 ffs_ep = func->eps + idx;
2186 if (unlikely(ffs_ep->descs[isHS])) {
2187 pr_vdebug("two %sspeed descriptors for EP %d\n",
2188 isHS ? "high" : "full",
2189 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2192 ffs_ep->descs[isHS] = ds;
2194 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2196 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2197 if (!ds->wMaxPacketSize)
2198 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2200 struct usb_request *req;
2203 pr_vdebug("autoconfig\n");
2204 ep = usb_ep_autoconfig(func->gadget, ds);
2207 ep->driver_data = func->eps + idx;
2209 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2215 func->eps_revmap[ds->bEndpointAddress &
2216 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2218 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2223 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2224 struct usb_descriptor_header *desc,
2227 struct ffs_function *func = priv;
2233 case FFS_DESCRIPTOR:
2234 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2239 if (func->interfaces_nums[idx] < 0) {
2240 int id = usb_interface_id(func->conf, &func->function);
2241 if (unlikely(id < 0))
2243 func->interfaces_nums[idx] = id;
2245 newValue = func->interfaces_nums[idx];
2249 /* String' IDs are allocated when fsf_data is bound to cdev */
2250 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2255 * USB_DT_ENDPOINT are handled in
2256 * __ffs_func_bind_do_descs().
2258 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2261 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2262 if (unlikely(!func->eps[idx].ep))
2266 struct usb_endpoint_descriptor **descs;
2267 descs = func->eps[idx].descs;
2268 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2273 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2278 static int ffs_func_bind(struct usb_configuration *c,
2279 struct usb_function *f)
2281 struct ffs_function *func = ffs_func_from_usb(f);
2282 struct ffs_data *ffs = func->ffs;
2284 const int full = !!func->ffs->fs_descs_count;
2285 const int high = gadget_is_dualspeed(func->gadget) &&
2286 func->ffs->hs_descs_count;
2290 /* Make it a single chunk, less management later on */
2292 struct ffs_ep eps[ffs->eps_count];
2293 struct usb_descriptor_header
2294 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2295 struct usb_descriptor_header
2296 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2297 short inums[ffs->interfaces_count];
2298 char raw_descs[high ? ffs->raw_descs_length
2299 : ffs->raw_fs_descs_length];
2304 /* Only high speed but not supported by gadget? */
2305 if (unlikely(!(full | high)))
2309 data = kmalloc(sizeof *data, GFP_KERNEL);
2310 if (unlikely(!data))
2314 memset(data->eps, 0, sizeof data->eps);
2315 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2317 memset(data->inums, 0xff, sizeof data->inums);
2318 for (ret = ffs->eps_count; ret; --ret)
2319 data->eps[ret].num = -1;
2322 func->eps = data->eps;
2323 func->interfaces_nums = data->inums;
2326 * Go through all the endpoint descriptors and allocate
2327 * endpoints first, so that later we can rewrite the endpoint
2328 * numbers without worrying that it may be described later on.
2331 func->function.fs_descriptors = data->fs_descs;
2332 ret = ffs_do_descs(ffs->fs_descs_count,
2334 sizeof data->raw_descs,
2335 __ffs_func_bind_do_descs, func);
2336 if (unlikely(ret < 0))
2343 func->function.hs_descriptors = data->hs_descs;
2344 ret = ffs_do_descs(ffs->hs_descs_count,
2345 data->raw_descs + ret,
2346 (sizeof data->raw_descs) - ret,
2347 __ffs_func_bind_do_descs, func);
2351 * Now handle interface numbers allocation and interface and
2352 * endpoint numbers rewriting. We can do that in one go
2355 ret = ffs_do_descs(ffs->fs_descs_count +
2356 (high ? ffs->hs_descs_count : 0),
2357 data->raw_descs, sizeof data->raw_descs,
2358 __ffs_func_bind_do_nums, func);
2359 if (unlikely(ret < 0))
2362 /* And we're done */
2363 ffs_event_add(ffs, FUNCTIONFS_BIND);
2367 /* XXX Do we need to release all claimed endpoints here? */
2373 /* Other USB function hooks *************************************************/
2375 static void ffs_func_unbind(struct usb_configuration *c,
2376 struct usb_function *f)
2378 struct ffs_function *func = ffs_func_from_usb(f);
2379 struct ffs_data *ffs = func->ffs;
2383 if (ffs->func == func) {
2384 ffs_func_eps_disable(func);
2388 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2390 ffs_func_free(func);
2393 static int ffs_func_set_alt(struct usb_function *f,
2394 unsigned interface, unsigned alt)
2396 struct ffs_function *func = ffs_func_from_usb(f);
2397 struct ffs_data *ffs = func->ffs;
2400 if (alt != (unsigned)-1) {
2401 intf = ffs_func_revmap_intf(func, interface);
2402 if (unlikely(intf < 0))
2407 ffs_func_eps_disable(ffs->func);
2409 if (ffs->state != FFS_ACTIVE)
2412 if (alt == (unsigned)-1) {
2414 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2419 ret = ffs_func_eps_enable(func);
2420 if (likely(ret >= 0))
2421 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2425 static void ffs_func_disable(struct usb_function *f)
2427 ffs_func_set_alt(f, 0, (unsigned)-1);
2430 static int ffs_func_setup(struct usb_function *f,
2431 const struct usb_ctrlrequest *creq)
2433 struct ffs_function *func = ffs_func_from_usb(f);
2434 struct ffs_data *ffs = func->ffs;
2435 unsigned long flags;
2440 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2441 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2442 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2443 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2444 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2447 * Most requests directed to interface go through here
2448 * (notable exceptions are set/get interface) so we need to
2449 * handle them. All other either handled by composite or
2450 * passed to usb_configuration->setup() (if one is set). No
2451 * matter, we will handle requests directed to endpoint here
2452 * as well (as it's straightforward) but what to do with any
2455 if (ffs->state != FFS_ACTIVE)
2458 switch (creq->bRequestType & USB_RECIP_MASK) {
2459 case USB_RECIP_INTERFACE:
2460 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2461 if (unlikely(ret < 0))
2465 case USB_RECIP_ENDPOINT:
2466 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2467 if (unlikely(ret < 0))
2475 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2476 ffs->ev.setup = *creq;
2477 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2478 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2479 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2484 static void ffs_func_suspend(struct usb_function *f)
2487 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2490 static void ffs_func_resume(struct usb_function *f)
2493 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2497 /* Endpoint and interface numbers reverse mapping ***************************/
2499 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2501 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2502 return num ? num : -EDOM;
2505 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2507 short *nums = func->interfaces_nums;
2508 unsigned count = func->ffs->interfaces_count;
2510 for (; count; --count, ++nums) {
2511 if (*nums >= 0 && *nums == intf)
2512 return nums - func->interfaces_nums;
2519 /* Misc helper functions ****************************************************/
2521 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2524 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2525 : mutex_lock_interruptible(mutex);
2528 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
2535 data = kmalloc(len, GFP_KERNEL);
2536 if (unlikely(!data))
2537 return ERR_PTR(-ENOMEM);
2539 if (unlikely(__copy_from_user(data, buf, len))) {
2541 return ERR_PTR(-EFAULT);
2544 pr_vdebug("Buffer from user space:\n");
2545 ffs_dump_mem("", data, len);