2 * f_fs.c -- user mode filesystem api for usb composite funtcion controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7 * Based on inode.c (GadgetFS):
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.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* #define VERBOSE_DEBUG */
30 #include <linux/blkdev.h>
31 #include <linux/pagemap.h>
32 #include <asm/unaligned.h>
33 #include <linux/smp_lock.h>
35 #include <linux/usb/composite.h>
36 #include <linux/usb/functionfs.h>
39 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
42 /* Debuging *****************************************************************/
44 #define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
46 #define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
47 #define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
50 # define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
52 # define FDBG(...) do { } while (0)
58 # define FVDBG(...) do { } while (0)
59 #endif /* VERBOSE_DEBUG */
61 #define ENTER() FVDBG("%s()", __func__)
64 # define ffs_dump_mem(prefix, ptr, len) \
65 print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len)
67 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
71 /* The data structure and setup file ****************************************/
74 /* Waiting for descriptors and strings. */
75 /* In this state no open(2), read(2) or write(2) on epfiles
76 * may succeed (which should not be the problem as there
77 * should be no such files opened in the firts place). */
81 /* We've got descriptors and strings. We are or have called
82 * functionfs_ready_callback(). functionfs_bind() may have
83 * been called but we don't know. */
84 /* This is the only state in which operations on epfiles may
88 /* All endpoints have been closed. This state is also set if
89 * we encounter an unrecoverable error. The only
90 * unrecoverable error is situation when after reading strings
91 * from user space we fail to initialise EP files or
92 * functionfs_ready_callback() returns with error (<0). */
93 /* In this state no open(2), read(2) or write(2) (both on ep0
94 * as well as epfile) may succeed (at this point epfiles are
95 * unlinked and all closed so this is not a problem; ep0 is
96 * also closed but ep0 file exists and so open(2) on ep0 must
102 enum ffs_setup_state
{
103 /* There is no setup request pending. */
105 /* User has read events and there was a setup request event
106 * there. The next read/write on ep0 will handle the
109 /* There was event pending but before user space handled it
110 * some other event was introduced which canceled existing
111 * setup. If this state is set read/write on ep0 return
112 * -EIDRM. This state is only set when adding event. */
122 struct usb_gadget
*gadget
;
124 /* Protect access read/write operations, only one read/write
125 * at a time. As a consequence protects ep0req and company.
126 * While setup request is being processed (queued) this is
130 /* Protect access to enpoint related structures (basically
131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
135 /* XXX REVISIT do we need our own request? Since we are not
136 * handling setup requests immidiatelly user space may be so
137 * slow that another setup will be sent to the gadget but this
138 * time not to us but another function and then there could be
139 * a race. Is that the case? Or maybe we can use cdev->req
140 * after all, maybe we just need some spinlock for that? */
141 struct usb_request
*ep0req
; /* P: mutex */
142 struct completion ep0req_completion
; /* P: mutex */
143 int ep0req_status
; /* P: mutex */
145 /* reference counter */
147 /* how many files are opened (EP0 and others) */
151 enum ffs_state state
;
154 * Possible transations:
155 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
156 * happens only in ep0 read which is P: mutex
157 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
158 * happens only in ep0 i/o which is P: mutex
159 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
160 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
162 enum ffs_setup_state setup_state
;
164 #define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
171 unsigned short count
;
172 /* XXX REVISIT need to update it in some places, or do we? */
173 unsigned short can_stall
;
174 struct usb_ctrlrequest setup
;
176 wait_queue_head_t waitq
;
177 } ev
; /* the whole structure, P: ev.waitq.lock */
181 #define FFS_FL_CALL_CLOSED_CALLBACK 0
182 #define FFS_FL_BOUND 1
184 /* Active function */
185 struct ffs_function
*func
;
187 /* Device name, write once when file system is mounted.
188 * Intendet for user to read if she wants. */
189 const char *dev_name
;
190 /* Private data for our user (ie. gadget). Managed by
194 /* filled by __ffs_data_got_descs() */
195 /* real descriptors are 16 bytes after raw_descs (so you need
196 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
197 * first full speed descriptor). raw_descs_length and
198 * raw_fs_descs_length do not have those 16 bytes added. */
199 const void *raw_descs
;
200 unsigned raw_descs_length
;
201 unsigned raw_fs_descs_length
;
202 unsigned fs_descs_count
;
203 unsigned hs_descs_count
;
205 unsigned short strings_count
;
206 unsigned short interfaces_count
;
207 unsigned short eps_count
;
208 unsigned short _pad1
;
210 /* filled by __ffs_data_got_strings() */
211 /* ids in stringtabs are set in functionfs_bind() */
212 const void *raw_strings
;
213 struct usb_gadget_strings
**stringtabs
;
215 /* File system's super block, write once when file system is mounted. */
216 struct super_block
*sb
;
218 /* File permissions, written once when fs is mounted*/
219 struct ffs_file_perms
{
225 /* The endpoint files, filled by ffs_epfiles_create(),
226 * destroyed by ffs_epfiles_destroy(). */
227 struct ffs_epfile
*epfiles
;
230 /* Reference counter handling */
231 static void ffs_data_get(struct ffs_data
*ffs
);
232 static void ffs_data_put(struct ffs_data
*ffs
);
233 /* Creates new ffs_data object. */
234 static struct ffs_data
*__must_check
ffs_data_new(void) __attribute__((malloc
));
236 /* Opened counter handling. */
237 static void ffs_data_opened(struct ffs_data
*ffs
);
238 static void ffs_data_closed(struct ffs_data
*ffs
);
240 /* Called with ffs->mutex held; take over ownerrship of data. */
241 static int __must_check
242 __ffs_data_got_descs(struct ffs_data
*ffs
, char *data
, size_t len
);
243 static int __must_check
244 __ffs_data_got_strings(struct ffs_data
*ffs
, char *data
, size_t len
);
247 /* The function structure ***************************************************/
251 struct ffs_function
{
252 struct usb_configuration
*conf
;
253 struct usb_gadget
*gadget
;
254 struct ffs_data
*ffs
;
258 short *interfaces_nums
;
260 struct usb_function function
;
264 static struct ffs_function
*ffs_func_from_usb(struct usb_function
*f
)
266 return container_of(f
, struct ffs_function
, function
);
269 static void ffs_func_free(struct ffs_function
*func
);
272 static void ffs_func_eps_disable(struct ffs_function
*func
);
273 static int __must_check
ffs_func_eps_enable(struct ffs_function
*func
);
276 static int ffs_func_bind(struct usb_configuration
*,
277 struct usb_function
*);
278 static void ffs_func_unbind(struct usb_configuration
*,
279 struct usb_function
*);
280 static int ffs_func_set_alt(struct usb_function
*, unsigned, unsigned);
281 static void ffs_func_disable(struct usb_function
*);
282 static int ffs_func_setup(struct usb_function
*,
283 const struct usb_ctrlrequest
*);
284 static void ffs_func_suspend(struct usb_function
*);
285 static void ffs_func_resume(struct usb_function
*);
288 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
);
289 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
);
293 /* The endpoints structures *************************************************/
296 struct usb_ep
*ep
; /* P: ffs->eps_lock */
297 struct usb_request
*req
; /* P: epfile->mutex */
299 /* [0]: full speed, [1]: high speed */
300 struct usb_endpoint_descriptor
*descs
[2];
304 int status
; /* P: epfile->mutex */
308 /* Protects ep->ep and ep->req. */
310 wait_queue_head_t wait
;
312 struct ffs_data
*ffs
;
313 struct ffs_ep
*ep
; /* P: ffs->eps_lock */
315 struct dentry
*dentry
;
319 unsigned char in
; /* P: ffs->eps_lock */
320 unsigned char isoc
; /* P: ffs->eps_lock */
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
);
353 static int __ffs_ep0_queue_wait(struct ffs_data
*ffs
, char *data
, size_t len
)
355 struct usb_request
*req
= ffs
->ep0req
;
358 req
->zero
= len
< le16_to_cpu(ffs
->ev
.setup
.wLength
);
360 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
365 INIT_COMPLETION(ffs
->ep0req_completion
);
367 ret
= usb_ep_queue(ffs
->gadget
->ep0
, req
, GFP_ATOMIC
);
368 if (unlikely(ret
< 0))
371 ret
= wait_for_completion_interruptible(&ffs
->ep0req_completion
);
373 usb_ep_dequeue(ffs
->gadget
->ep0
, req
);
377 ffs
->setup_state
= FFS_NO_SETUP
;
378 return ffs
->ep0req_status
;
381 static int __ffs_ep0_stall(struct ffs_data
*ffs
)
383 if (ffs
->ev
.can_stall
) {
384 FVDBG("ep0 stall\n");
385 usb_ep_set_halt(ffs
->gadget
->ep0
);
386 ffs
->setup_state
= FFS_NO_SETUP
;
389 FDBG("bogus ep0 stall!\n");
395 static ssize_t
ffs_ep0_write(struct file
*file
, const char __user
*buf
,
396 size_t len
, loff_t
*ptr
)
398 struct ffs_data
*ffs
= file
->private_data
;
404 /* Fast check if setup was canceled */
405 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
409 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
410 if (unlikely(ret
< 0))
415 switch (ffs
->state
) {
416 case FFS_READ_DESCRIPTORS
:
417 case FFS_READ_STRINGS
:
419 if (unlikely(len
< 16)) {
424 data
= ffs_prepare_buffer(buf
, len
);
425 if (unlikely(IS_ERR(data
))) {
431 if (ffs
->state
== FFS_READ_DESCRIPTORS
) {
432 FINFO("read descriptors");
433 ret
= __ffs_data_got_descs(ffs
, data
, len
);
434 if (unlikely(ret
< 0))
437 ffs
->state
= FFS_READ_STRINGS
;
440 FINFO("read strings");
441 ret
= __ffs_data_got_strings(ffs
, data
, len
);
442 if (unlikely(ret
< 0))
445 ret
= ffs_epfiles_create(ffs
);
447 ffs
->state
= FFS_CLOSING
;
451 ffs
->state
= FFS_ACTIVE
;
452 mutex_unlock(&ffs
->mutex
);
454 ret
= functionfs_ready_callback(ffs
);
455 if (unlikely(ret
< 0)) {
456 ffs
->state
= FFS_CLOSING
;
460 set_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
);
468 /* We're called from user space, we can use _irq
469 * rather then _irqsave */
470 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
471 switch (FFS_SETUP_STATE(ffs
)) {
472 case FFS_SETUP_CANCELED
:
480 case FFS_SETUP_PENDING
:
484 /* FFS_SETUP_PENDING */
485 if (!(ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
)) {
486 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
487 ret
= __ffs_ep0_stall(ffs
);
491 /* FFS_SETUP_PENDING and not stall */
492 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
494 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
496 data
= ffs_prepare_buffer(buf
, len
);
497 if (unlikely(IS_ERR(data
))) {
502 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
504 /* We are guaranteed to be still in FFS_ACTIVE state
505 * but the state of setup could have changed from
506 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
507 * to check for that. If that happened we copied data
508 * from user space in vain but it's unlikely. */
509 /* For sure we are not in FFS_NO_SETUP since this is
510 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
511 * transition can be performed and it's protected by
514 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
517 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
519 /* unlocks spinlock */
520 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
532 mutex_unlock(&ffs
->mutex
);
538 static ssize_t
__ffs_ep0_read_events(struct ffs_data
*ffs
, char __user
*buf
,
541 /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need
542 * to release them. */
544 struct usb_functionfs_event events
[n
];
547 memset(events
, 0, sizeof events
);
550 events
[i
].type
= ffs
->ev
.types
[i
];
551 if (events
[i
].type
== FUNCTIONFS_SETUP
) {
552 events
[i
].u
.setup
= ffs
->ev
.setup
;
553 ffs
->setup_state
= FFS_SETUP_PENDING
;
557 if (n
< ffs
->ev
.count
) {
559 memmove(ffs
->ev
.types
, ffs
->ev
.types
+ n
,
560 ffs
->ev
.count
* sizeof *ffs
->ev
.types
);
565 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
566 mutex_unlock(&ffs
->mutex
);
568 return unlikely(__copy_to_user(buf
, events
, sizeof events
))
569 ? -EFAULT
: sizeof events
;
573 static ssize_t
ffs_ep0_read(struct file
*file
, char __user
*buf
,
574 size_t len
, loff_t
*ptr
)
576 struct ffs_data
*ffs
= file
->private_data
;
583 /* Fast check if setup was canceled */
584 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
588 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
589 if (unlikely(ret
< 0))
594 if (ffs
->state
!= FFS_ACTIVE
) {
600 /* We're called from user space, we can use _irq rather then
602 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
604 switch (FFS_SETUP_STATE(ffs
)) {
605 case FFS_SETUP_CANCELED
:
610 n
= len
/ sizeof(struct usb_functionfs_event
);
616 if ((file
->f_flags
& O_NONBLOCK
) && !ffs
->ev
.count
) {
621 if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs
->ev
.waitq
, ffs
->ev
.count
))) {
626 return __ffs_ep0_read_events(ffs
, buf
,
627 min(n
, (size_t)ffs
->ev
.count
));
630 case FFS_SETUP_PENDING
:
631 if (ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
) {
632 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
633 ret
= __ffs_ep0_stall(ffs
);
637 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
639 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
642 data
= kmalloc(len
, GFP_KERNEL
);
643 if (unlikely(!data
)) {
649 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
651 /* See ffs_ep0_write() */
652 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
657 /* unlocks spinlock */
658 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
659 if (likely(ret
> 0) && unlikely(__copy_to_user(buf
, data
, len
)))
668 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
670 mutex_unlock(&ffs
->mutex
);
677 static int ffs_ep0_open(struct inode
*inode
, struct file
*file
)
679 struct ffs_data
*ffs
= inode
->i_private
;
683 if (unlikely(ffs
->state
== FFS_CLOSING
))
686 file
->private_data
= ffs
;
687 ffs_data_opened(ffs
);
693 static int ffs_ep0_release(struct inode
*inode
, struct file
*file
)
695 struct ffs_data
*ffs
= file
->private_data
;
699 ffs_data_closed(ffs
);
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
->ops
->ioctl
) {
717 ret
= gadget
->ops
->ioctl(gadget
, code
, value
);
726 static const struct file_operations ffs_ep0_operations
= {
727 .owner
= THIS_MODULE
,
730 .open
= ffs_ep0_open
,
731 .write
= ffs_ep0_write
,
732 .read
= ffs_ep0_read
,
733 .release
= ffs_ep0_release
,
734 .unlocked_ioctl
= ffs_ep0_ioctl
,
738 /* "Normal" endpoints operations ********************************************/
741 static void ffs_epfile_io_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
744 if (likely(req
->context
)) {
745 struct ffs_ep
*ep
= _ep
->driver_data
;
746 ep
->status
= req
->status
? req
->status
: req
->actual
;
747 complete(req
->context
);
752 static ssize_t
ffs_epfile_io(struct file
*file
,
753 char __user
*buf
, size_t len
, int read
)
755 struct ffs_epfile
*epfile
= file
->private_data
;
763 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
764 mutex_unlock(&epfile
->mutex
);
767 /* Are we still active? */
768 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
)) {
773 /* Wait for endpoint to be enabled */
776 if (file
->f_flags
& O_NONBLOCK
) {
781 if (unlikely(wait_event_interruptible
782 (epfile
->wait
, (ep
= epfile
->ep
)))) {
789 halt
= !read
== !epfile
->in
;
790 if (halt
&& epfile
->isoc
) {
795 /* Allocate & copy */
796 if (!halt
&& !data
) {
797 data
= kzalloc(len
, GFP_KERNEL
);
802 unlikely(__copy_from_user(data
, buf
, len
))) {
808 /* We will be using request */
809 ret
= ffs_mutex_lock(&epfile
->mutex
,
810 file
->f_flags
& O_NONBLOCK
);
814 /* We're called from user space, we can use _irq rather then
816 spin_lock_irq(&epfile
->ffs
->eps_lock
);
818 /* While we were acquiring mutex endpoint got disabled
820 } while (unlikely(epfile
->ep
!= ep
));
823 if (unlikely(halt
)) {
824 if (likely(epfile
->ep
== ep
) && !WARN_ON(!ep
->ep
))
825 usb_ep_set_halt(ep
->ep
);
826 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
829 /* Fire the request */
830 DECLARE_COMPLETION_ONSTACK(done
);
832 struct usb_request
*req
= ep
->req
;
833 req
->context
= &done
;
834 req
->complete
= ffs_epfile_io_complete
;
838 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
840 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
842 if (unlikely(ret
< 0)) {
844 } else if (unlikely(wait_for_completion_interruptible(&done
))) {
846 usb_ep_dequeue(ep
->ep
, req
);
849 if (read
&& ret
> 0 &&
850 unlikely(copy_to_user(buf
, data
, ret
)))
855 mutex_unlock(&epfile
->mutex
);
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
);
908 static long ffs_epfile_ioctl(struct file
*file
, unsigned code
,
911 struct ffs_epfile
*epfile
= file
->private_data
;
916 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
919 spin_lock_irq(&epfile
->ffs
->eps_lock
);
920 if (likely(epfile
->ep
)) {
922 case FUNCTIONFS_FIFO_STATUS
:
923 ret
= usb_ep_fifo_status(epfile
->ep
->ep
);
925 case FUNCTIONFS_FIFO_FLUSH
:
926 usb_ep_fifo_flush(epfile
->ep
->ep
);
929 case FUNCTIONFS_CLEAR_HALT
:
930 ret
= usb_ep_clear_halt(epfile
->ep
->ep
);
932 case FUNCTIONFS_ENDPOINT_REVMAP
:
933 ret
= epfile
->ep
->num
;
941 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
947 static const struct file_operations ffs_epfile_operations
= {
948 .owner
= THIS_MODULE
,
951 .open
= ffs_epfile_open
,
952 .write
= ffs_epfile_write
,
953 .read
= ffs_epfile_read
,
954 .release
= ffs_epfile_release
,
955 .unlocked_ioctl
= ffs_epfile_ioctl
,
960 /* File system and super block operations ***********************************/
963 * Mounting the filesystem creates a controller file, used first for
964 * function configuration then later for event monitoring.
968 static struct inode
*__must_check
969 ffs_sb_make_inode(struct super_block
*sb
, void *data
,
970 const struct file_operations
*fops
,
971 const struct inode_operations
*iops
,
972 struct ffs_file_perms
*perms
)
978 inode
= new_inode(sb
);
981 struct timespec current_time
= CURRENT_TIME
;
983 inode
->i_mode
= perms
->mode
;
984 inode
->i_uid
= perms
->uid
;
985 inode
->i_gid
= perms
->gid
;
986 inode
->i_atime
= current_time
;
987 inode
->i_mtime
= current_time
;
988 inode
->i_ctime
= current_time
;
989 inode
->i_private
= data
;
1000 /* Create "regular" file */
1002 static struct inode
*ffs_sb_create_file(struct super_block
*sb
,
1003 const char *name
, void *data
,
1004 const struct file_operations
*fops
,
1005 struct dentry
**dentry_p
)
1007 struct ffs_data
*ffs
= sb
->s_fs_info
;
1008 struct dentry
*dentry
;
1009 struct inode
*inode
;
1013 dentry
= d_alloc_name(sb
->s_root
, name
);
1014 if (unlikely(!dentry
))
1017 inode
= ffs_sb_make_inode(sb
, data
, fops
, NULL
, &ffs
->file_perms
);
1018 if (unlikely(!inode
)) {
1023 d_add(dentry
, inode
);
1033 static const struct super_operations ffs_sb_operations
= {
1034 .statfs
= simple_statfs
,
1035 .drop_inode
= generic_delete_inode
,
1038 struct ffs_sb_fill_data
{
1039 struct ffs_file_perms perms
;
1041 const char *dev_name
;
1044 static int ffs_sb_fill(struct super_block
*sb
, void *_data
, int silent
)
1046 struct ffs_sb_fill_data
*data
= _data
;
1047 struct inode
*inode
;
1049 struct ffs_data
*ffs
;
1053 /* Initialize data */
1054 ffs
= ffs_data_new();
1059 ffs
->dev_name
= data
->dev_name
;
1060 ffs
->file_perms
= data
->perms
;
1062 sb
->s_fs_info
= ffs
;
1063 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1064 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1065 sb
->s_magic
= FUNCTIONFS_MAGIC
;
1066 sb
->s_op
= &ffs_sb_operations
;
1067 sb
->s_time_gran
= 1;
1070 data
->perms
.mode
= data
->root_mode
;
1071 inode
= ffs_sb_make_inode(sb
, NULL
,
1072 &simple_dir_operations
,
1073 &simple_dir_inode_operations
,
1075 if (unlikely(!inode
))
1077 d
= d_alloc_root(inode
);
1083 if (unlikely(!ffs_sb_create_file(sb
, "ep0", ffs
,
1084 &ffs_ep0_operations
, NULL
)))
1100 static int ffs_fs_parse_opts(struct ffs_sb_fill_data
*data
, char *opts
)
1104 if (!opts
|| !*opts
)
1108 char *end
, *eq
, *comma
;
1109 unsigned long value
;
1112 comma
= strchr(opts
, ',');
1117 eq
= strchr(opts
, '=');
1118 if (unlikely(!eq
)) {
1119 FERR("'=' missing in %s", opts
);
1125 value
= simple_strtoul(eq
+ 1, &end
, 0);
1126 if (unlikely(*end
!= ',' && *end
!= 0)) {
1127 FERR("%s: invalid value: %s", opts
, eq
+ 1);
1131 /* Interpret option */
1132 switch (eq
- opts
) {
1134 if (!memcmp(opts
, "rmode", 5))
1135 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1136 else if (!memcmp(opts
, "fmode", 5))
1137 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1143 if (!memcmp(opts
, "mode", 4)) {
1144 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1145 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1152 if (!memcmp(opts
, "uid", 3))
1153 data
->perms
.uid
= value
;
1154 else if (!memcmp(opts
, "gid", 3))
1155 data
->perms
.gid
= value
;
1162 FERR("%s: invalid option", opts
);
1166 /* Next iteration */
1176 /* "mount -t functionfs dev_name /dev/function" ends up here */
1179 ffs_fs_get_sb(struct file_system_type
*t
, int flags
,
1180 const char *dev_name
, void *opts
, struct vfsmount
*mnt
)
1182 struct ffs_sb_fill_data data
= {
1184 .mode
= S_IFREG
| 0600,
1188 .root_mode
= S_IFDIR
| 0500,
1194 ret
= functionfs_check_dev_callback(dev_name
);
1195 if (unlikely(ret
< 0))
1198 ret
= ffs_fs_parse_opts(&data
, opts
);
1199 if (unlikely(ret
< 0))
1202 data
.dev_name
= dev_name
;
1203 return get_sb_single(t
, flags
, &data
, ffs_sb_fill
, mnt
);
1207 ffs_fs_kill_sb(struct super_block
*sb
)
1213 kill_litter_super(sb
);
1214 ptr
= xchg(&sb
->s_fs_info
, NULL
);
1219 static struct file_system_type ffs_fs_type
= {
1220 .owner
= THIS_MODULE
,
1221 .name
= "functionfs",
1222 .get_sb
= ffs_fs_get_sb
,
1223 .kill_sb
= ffs_fs_kill_sb
,
1228 /* Driver's main init/cleanup functions *************************************/
1231 static int functionfs_init(void)
1237 ret
= register_filesystem(&ffs_fs_type
);
1239 FINFO("file system registered");
1241 FERR("failed registering file system (%d)", ret
);
1246 static void functionfs_cleanup(void)
1251 unregister_filesystem(&ffs_fs_type
);
1256 /* ffs_data and ffs_function construction and destruction code **************/
1258 static void ffs_data_clear(struct ffs_data
*ffs
);
1259 static void ffs_data_reset(struct ffs_data
*ffs
);
1262 static void ffs_data_get(struct ffs_data
*ffs
)
1266 atomic_inc(&ffs
->ref
);
1269 static void ffs_data_opened(struct ffs_data
*ffs
)
1273 atomic_inc(&ffs
->ref
);
1274 atomic_inc(&ffs
->opened
);
1277 static void ffs_data_put(struct ffs_data
*ffs
)
1281 if (unlikely(atomic_dec_and_test(&ffs
->ref
))) {
1282 FINFO("%s(): freeing", __func__
);
1283 ffs_data_clear(ffs
);
1284 BUG_ON(mutex_is_locked(&ffs
->mutex
) ||
1285 spin_is_locked(&ffs
->ev
.waitq
.lock
) ||
1286 waitqueue_active(&ffs
->ev
.waitq
) ||
1287 waitqueue_active(&ffs
->ep0req_completion
.wait
));
1294 static void ffs_data_closed(struct ffs_data
*ffs
)
1298 if (atomic_dec_and_test(&ffs
->opened
)) {
1299 ffs
->state
= FFS_CLOSING
;
1300 ffs_data_reset(ffs
);
1307 static struct ffs_data
*ffs_data_new(void)
1309 struct ffs_data
*ffs
= kzalloc(sizeof *ffs
, GFP_KERNEL
);
1315 atomic_set(&ffs
->ref
, 1);
1316 atomic_set(&ffs
->opened
, 0);
1317 ffs
->state
= FFS_READ_DESCRIPTORS
;
1318 mutex_init(&ffs
->mutex
);
1319 spin_lock_init(&ffs
->eps_lock
);
1320 init_waitqueue_head(&ffs
->ev
.waitq
);
1321 init_completion(&ffs
->ep0req_completion
);
1323 /* XXX REVISIT need to update it in some places, or do we? */
1324 ffs
->ev
.can_stall
= 1;
1330 static void ffs_data_clear(struct ffs_data
*ffs
)
1334 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
))
1335 functionfs_closed_callback(ffs
);
1337 BUG_ON(ffs
->gadget
);
1340 ffs_epfiles_destroy(ffs
->epfiles
, ffs
->eps_count
);
1342 kfree(ffs
->raw_descs
);
1343 kfree(ffs
->raw_strings
);
1344 kfree(ffs
->stringtabs
);
1348 static void ffs_data_reset(struct ffs_data
*ffs
)
1352 ffs_data_clear(ffs
);
1354 ffs
->epfiles
= NULL
;
1355 ffs
->raw_descs
= NULL
;
1356 ffs
->raw_strings
= NULL
;
1357 ffs
->stringtabs
= NULL
;
1359 ffs
->raw_descs_length
= 0;
1360 ffs
->raw_fs_descs_length
= 0;
1361 ffs
->fs_descs_count
= 0;
1362 ffs
->hs_descs_count
= 0;
1364 ffs
->strings_count
= 0;
1365 ffs
->interfaces_count
= 0;
1370 ffs
->state
= FFS_READ_DESCRIPTORS
;
1371 ffs
->setup_state
= FFS_NO_SETUP
;
1376 static int functionfs_bind(struct ffs_data
*ffs
, struct usb_composite_dev
*cdev
)
1382 if (WARN_ON(ffs
->state
!= FFS_ACTIVE
1383 || test_and_set_bit(FFS_FL_BOUND
, &ffs
->flags
)))
1388 ffs
->ep0req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
1389 if (unlikely(!ffs
->ep0req
))
1391 ffs
->ep0req
->complete
= ffs_ep0_complete
;
1392 ffs
->ep0req
->context
= ffs
;
1394 /* Get strings identifiers */
1395 for (count
= ffs
->strings_count
, i
= 0; i
< count
; ++i
) {
1396 struct usb_gadget_strings
**lang
;
1398 int id
= usb_string_id(cdev
);
1399 if (unlikely(id
< 0)) {
1400 usb_ep_free_request(cdev
->gadget
->ep0
, ffs
->ep0req
);
1405 lang
= ffs
->stringtabs
;
1407 (*lang
)->strings
[i
].id
= id
;
1412 ffs
->gadget
= cdev
->gadget
;
1417 static void functionfs_unbind(struct ffs_data
*ffs
)
1421 if (!WARN_ON(!ffs
->gadget
)) {
1422 usb_ep_free_request(ffs
->gadget
->ep0
, ffs
->ep0req
);
1430 static int ffs_epfiles_create(struct ffs_data
*ffs
)
1432 struct ffs_epfile
*epfile
, *epfiles
;
1437 count
= ffs
->eps_count
;
1438 epfiles
= kzalloc(count
* sizeof *epfiles
, GFP_KERNEL
);
1443 for (i
= 1; i
<= count
; ++i
, ++epfile
) {
1445 mutex_init(&epfile
->mutex
);
1446 init_waitqueue_head(&epfile
->wait
);
1447 sprintf(epfiles
->name
, "ep%u", i
);
1448 if (!unlikely(ffs_sb_create_file(ffs
->sb
, epfiles
->name
, epfile
,
1449 &ffs_epfile_operations
,
1450 &epfile
->dentry
))) {
1451 ffs_epfiles_destroy(epfiles
, i
- 1);
1456 ffs
->epfiles
= epfiles
;
1461 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
)
1463 struct ffs_epfile
*epfile
= epfiles
;
1467 for (; count
; --count
, ++epfile
) {
1468 BUG_ON(mutex_is_locked(&epfile
->mutex
) ||
1469 waitqueue_active(&epfile
->wait
));
1470 if (epfile
->dentry
) {
1471 d_delete(epfile
->dentry
);
1472 dput(epfile
->dentry
);
1473 epfile
->dentry
= NULL
;
1481 static int functionfs_bind_config(struct usb_composite_dev
*cdev
,
1482 struct usb_configuration
*c
,
1483 struct ffs_data
*ffs
)
1485 struct ffs_function
*func
;
1490 func
= kzalloc(sizeof *func
, GFP_KERNEL
);
1491 if (unlikely(!func
))
1494 func
->function
.name
= "Function FS Gadget";
1495 func
->function
.strings
= ffs
->stringtabs
;
1497 func
->function
.bind
= ffs_func_bind
;
1498 func
->function
.unbind
= ffs_func_unbind
;
1499 func
->function
.set_alt
= ffs_func_set_alt
;
1500 /*func->function.get_alt = ffs_func_get_alt;*/
1501 func
->function
.disable
= ffs_func_disable
;
1502 func
->function
.setup
= ffs_func_setup
;
1503 func
->function
.suspend
= ffs_func_suspend
;
1504 func
->function
.resume
= ffs_func_resume
;
1507 func
->gadget
= cdev
->gadget
;
1511 ret
= usb_add_function(c
, &func
->function
);
1513 ffs_func_free(func
);
1518 static void ffs_func_free(struct ffs_function
*func
)
1522 ffs_data_put(func
->ffs
);
1525 /* eps and interfaces_nums are allocated in the same chunk so
1526 * only one free is required. Descriptors are also allocated
1527 * in the same chunk. */
1533 static void ffs_func_eps_disable(struct ffs_function
*func
)
1535 struct ffs_ep
*ep
= func
->eps
;
1536 struct ffs_epfile
*epfile
= func
->ffs
->epfiles
;
1537 unsigned count
= func
->ffs
->eps_count
;
1538 unsigned long flags
;
1540 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1542 /* pending requests get nuked */
1544 usb_ep_disable(ep
->ep
);
1550 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1553 static int ffs_func_eps_enable(struct ffs_function
*func
)
1555 struct ffs_data
*ffs
= func
->ffs
;
1556 struct ffs_ep
*ep
= func
->eps
;
1557 struct ffs_epfile
*epfile
= ffs
->epfiles
;
1558 unsigned count
= ffs
->eps_count
;
1559 unsigned long flags
;
1562 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1564 struct usb_endpoint_descriptor
*ds
;
1565 ds
= ep
->descs
[ep
->descs
[1] ? 1 : 0];
1567 ep
->ep
->driver_data
= ep
;
1568 ret
= usb_ep_enable(ep
->ep
, ds
);
1571 epfile
->in
= usb_endpoint_dir_in(ds
);
1572 epfile
->isoc
= usb_endpoint_xfer_isoc(ds
);
1577 wake_up(&epfile
->wait
);
1582 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1588 /* Parsing and building descriptors and strings *****************************/
1591 /* This validates if data pointed by data is a valid USB descriptor as
1592 * well as record how many interfaces, endpoints and strings are
1593 * required by given configuration. Returns address afther the
1594 * descriptor or NULL if data is invalid. */
1596 enum ffs_entity_type
{
1597 FFS_DESCRIPTOR
, FFS_INTERFACE
, FFS_STRING
, FFS_ENDPOINT
1600 typedef int (*ffs_entity_callback
)(enum ffs_entity_type entity
,
1602 struct usb_descriptor_header
*desc
,
1605 static int __must_check
ffs_do_desc(char *data
, unsigned len
,
1606 ffs_entity_callback entity
, void *priv
)
1608 struct usb_descriptor_header
*_ds
= (void *)data
;
1614 /* At least two bytes are required: length and type */
1616 FVDBG("descriptor too short");
1620 /* If we have at least as many bytes as the descriptor takes? */
1621 length
= _ds
->bLength
;
1623 FVDBG("descriptor longer then available data");
1627 #define __entity_check_INTERFACE(val) 1
1628 #define __entity_check_STRING(val) (val)
1629 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1630 #define __entity(type, val) do { \
1631 FVDBG("entity " #type "(%02x)", (val)); \
1632 if (unlikely(!__entity_check_ ##type(val))) { \
1633 FVDBG("invalid entity's value"); \
1636 ret = entity(FFS_ ##type, &val, _ds, priv); \
1637 if (unlikely(ret < 0)) { \
1638 FDBG("entity " #type "(%02x); ret = %d", \
1644 /* Parse descriptor depending on type. */
1645 switch (_ds
->bDescriptorType
) {
1649 case USB_DT_DEVICE_QUALIFIER
:
1650 /* function can't have any of those */
1651 FVDBG("descriptor reserved for gadget: %d", _ds
->bDescriptorType
);
1654 case USB_DT_INTERFACE
: {
1655 struct usb_interface_descriptor
*ds
= (void *)_ds
;
1656 FVDBG("interface descriptor");
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 FVDBG("endpoint descriptor");
1669 if (length
!= USB_DT_ENDPOINT_SIZE
&&
1670 length
!= USB_DT_ENDPOINT_AUDIO_SIZE
)
1672 __entity(ENDPOINT
, ds
->bEndpointAddress
);
1677 if (length
!= sizeof(struct usb_otg_descriptor
))
1681 case USB_DT_INTERFACE_ASSOCIATION
: {
1682 struct usb_interface_assoc_descriptor
*ds
= (void *)_ds
;
1683 FVDBG("interface association descriptor");
1684 if (length
!= sizeof *ds
)
1687 __entity(STRING
, ds
->iFunction
);
1691 case USB_DT_OTHER_SPEED_CONFIG
:
1692 case USB_DT_INTERFACE_POWER
:
1694 case USB_DT_SECURITY
:
1695 case USB_DT_CS_RADIO_CONTROL
:
1697 FVDBG("unimplemented descriptor: %d", _ds
->bDescriptorType
);
1701 /* We should never be here */
1702 FVDBG("unknown descriptor: %d", _ds
->bDescriptorType
);
1706 FVDBG("invalid length: %d (descriptor %d)",
1707 _ds
->bLength
, _ds
->bDescriptorType
);
1712 #undef __entity_check_DESCRIPTOR
1713 #undef __entity_check_INTERFACE
1714 #undef __entity_check_STRING
1715 #undef __entity_check_ENDPOINT
1721 static int __must_check
ffs_do_descs(unsigned count
, char *data
, unsigned len
,
1722 ffs_entity_callback entity
, void *priv
)
1724 const unsigned _len
= len
;
1725 unsigned long num
= 0;
1735 /* Record "descriptor" entitny */
1736 ret
= entity(FFS_DESCRIPTOR
, (u8
*)num
, (void *)data
, priv
);
1737 if (unlikely(ret
< 0)) {
1738 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num
, ret
);
1745 ret
= ffs_do_desc(data
, len
, entity
, priv
);
1746 if (unlikely(ret
< 0)) {
1747 FDBG("%s returns %d", __func__
, ret
);
1758 static int __ffs_data_do_entity(enum ffs_entity_type type
,
1759 u8
*valuep
, struct usb_descriptor_header
*desc
,
1762 struct ffs_data
*ffs
= priv
;
1767 case FFS_DESCRIPTOR
:
1771 /* Interfaces are indexed from zero so if we
1772 * encountered interface "n" then there are at least
1773 * "n+1" interfaces. */
1774 if (*valuep
>= ffs
->interfaces_count
)
1775 ffs
->interfaces_count
= *valuep
+ 1;
1779 /* Strings are indexed from 1 (0 is magic ;) reserved
1780 * for languages list or some such) */
1781 if (*valuep
> ffs
->strings_count
)
1782 ffs
->strings_count
= *valuep
;
1786 /* Endpoints are indexed from 1 as well. */
1787 if ((*valuep
& USB_ENDPOINT_NUMBER_MASK
) > ffs
->eps_count
)
1788 ffs
->eps_count
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
);
1796 static int __ffs_data_got_descs(struct ffs_data
*ffs
,
1797 char *const _data
, size_t len
)
1799 unsigned fs_count
, hs_count
;
1800 int fs_len
, ret
= -EINVAL
;
1805 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_DESCRIPTORS_MAGIC
||
1806 get_unaligned_le32(data
+ 4) != len
))
1808 fs_count
= get_unaligned_le32(data
+ 8);
1809 hs_count
= get_unaligned_le32(data
+ 12);
1811 if (!fs_count
&& !hs_count
)
1817 if (likely(fs_count
)) {
1818 fs_len
= ffs_do_descs(fs_count
, data
, len
,
1819 __ffs_data_do_entity
, ffs
);
1820 if (unlikely(fs_len
< 0)) {
1831 if (likely(hs_count
)) {
1832 ret
= ffs_do_descs(hs_count
, data
, len
,
1833 __ffs_data_do_entity
, ffs
);
1834 if (unlikely(ret
< 0))
1840 if (unlikely(len
!= ret
))
1843 ffs
->raw_fs_descs_length
= fs_len
;
1844 ffs
->raw_descs_length
= fs_len
+ ret
;
1845 ffs
->raw_descs
= _data
;
1846 ffs
->fs_descs_count
= fs_count
;
1847 ffs
->hs_descs_count
= hs_count
;
1860 static int __ffs_data_got_strings(struct ffs_data
*ffs
,
1861 char *const _data
, size_t len
)
1863 u32 str_count
, needed_count
, lang_count
;
1864 struct usb_gadget_strings
**stringtabs
, *t
;
1865 struct usb_string
*strings
, *s
;
1866 const char *data
= _data
;
1870 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_STRINGS_MAGIC
||
1871 get_unaligned_le32(data
+ 4) != len
))
1873 str_count
= get_unaligned_le32(data
+ 8);
1874 lang_count
= get_unaligned_le32(data
+ 12);
1876 /* if one is zero the other must be zero */
1877 if (unlikely(!str_count
!= !lang_count
))
1880 /* Do we have at least as many strings as descriptors need? */
1881 needed_count
= ffs
->strings_count
;
1882 if (unlikely(str_count
< needed_count
))
1885 /* If we don't need any strings just return and free all
1887 if (!needed_count
) {
1894 /* Allocate everything in one chunk so there's less
1897 struct usb_gadget_strings
*stringtabs
[lang_count
+ 1];
1898 struct usb_gadget_strings stringtab
[lang_count
];
1899 struct usb_string strings
[lang_count
*(needed_count
+1)];
1903 d
= kmalloc(sizeof *d
, GFP_KERNEL
);
1909 stringtabs
= d
->stringtabs
;
1913 *stringtabs
++ = t
++;
1917 stringtabs
= d
->stringtabs
;
1923 /* For each language */
1927 do { /* lang_count > 0 so we can use do-while */
1928 unsigned needed
= needed_count
;
1930 if (unlikely(len
< 3))
1932 t
->language
= get_unaligned_le16(data
);
1939 /* For each string */
1940 do { /* str_count > 0 so we can use do-while */
1941 size_t length
= strnlen(data
, len
);
1943 if (unlikely(length
== len
))
1946 /* user may provide more strings then we need,
1947 * if that's the case we simply ingore the
1949 if (likely(needed
)) {
1950 /* s->id will be set while adding
1951 * function to configuration so for
1952 * now just leave garbage here. */
1960 } while (--str_count
);
1962 s
->id
= 0; /* terminator */
1966 } while (--lang_count
);
1968 /* Some garbage left? */
1973 ffs
->stringtabs
= stringtabs
;
1974 ffs
->raw_strings
= _data
;
1988 /* Events handling and management *******************************************/
1990 static void __ffs_event_add(struct ffs_data
*ffs
,
1991 enum usb_functionfs_event_type type
)
1993 enum usb_functionfs_event_type rem_type1
, rem_type2
= type
;
1996 /* Abort any unhandled setup */
1997 /* We do not need to worry about some cmpxchg() changing value
1998 * of ffs->setup_state without holding the lock because when
1999 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2000 * the source does nothing. */
2001 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
2002 ffs
->setup_state
= FFS_SETUP_CANCELED
;
2005 case FUNCTIONFS_RESUME
:
2006 rem_type2
= FUNCTIONFS_SUSPEND
;
2008 case FUNCTIONFS_SUSPEND
:
2009 case FUNCTIONFS_SETUP
:
2011 /* discard all similar events */
2014 case FUNCTIONFS_BIND
:
2015 case FUNCTIONFS_UNBIND
:
2016 case FUNCTIONFS_DISABLE
:
2017 case FUNCTIONFS_ENABLE
:
2018 /* discard everything other then power management. */
2019 rem_type1
= FUNCTIONFS_SUSPEND
;
2020 rem_type2
= FUNCTIONFS_RESUME
;
2029 u8
*ev
= ffs
->ev
.types
, *out
= ev
;
2030 unsigned n
= ffs
->ev
.count
;
2031 for (; n
; --n
, ++ev
)
2032 if ((*ev
== rem_type1
|| *ev
== rem_type2
) == neg
)
2035 FVDBG("purging event %d", *ev
);
2036 ffs
->ev
.count
= out
- ffs
->ev
.types
;
2039 FVDBG("adding event %d", type
);
2040 ffs
->ev
.types
[ffs
->ev
.count
++] = type
;
2041 wake_up_locked(&ffs
->ev
.waitq
);
2044 static void ffs_event_add(struct ffs_data
*ffs
,
2045 enum usb_functionfs_event_type type
)
2047 unsigned long flags
;
2048 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2049 __ffs_event_add(ffs
, type
);
2050 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2054 /* Bind/unbind USB function hooks *******************************************/
2056 static int __ffs_func_bind_do_descs(enum ffs_entity_type type
, u8
*valuep
,
2057 struct usb_descriptor_header
*desc
,
2060 struct usb_endpoint_descriptor
*ds
= (void *)desc
;
2061 struct ffs_function
*func
= priv
;
2062 struct ffs_ep
*ffs_ep
;
2064 /* If hs_descriptors is not NULL then we are reading hs
2065 * descriptors now */
2066 const int isHS
= func
->function
.hs_descriptors
!= NULL
;
2069 if (type
!= FFS_DESCRIPTOR
)
2073 func
->function
.hs_descriptors
[(long)valuep
] = desc
;
2075 func
->function
.descriptors
[(long)valuep
] = desc
;
2077 if (!desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
2080 idx
= (ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2081 ffs_ep
= func
->eps
+ idx
;
2083 if (unlikely(ffs_ep
->descs
[isHS
])) {
2084 FVDBG("two %sspeed descriptors for EP %d",
2085 isHS
? "high" : "full",
2086 ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2089 ffs_ep
->descs
[isHS
] = ds
;
2091 ffs_dump_mem(": Original ep desc", ds
, ds
->bLength
);
2093 ds
->bEndpointAddress
= ffs_ep
->descs
[0]->bEndpointAddress
;
2094 if (!ds
->wMaxPacketSize
)
2095 ds
->wMaxPacketSize
= ffs_ep
->descs
[0]->wMaxPacketSize
;
2097 struct usb_request
*req
;
2100 FVDBG("autoconfig");
2101 ep
= usb_ep_autoconfig(func
->gadget
, ds
);
2104 ep
->driver_data
= func
->eps
+ idx
;;
2106 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
2112 func
->eps_revmap
[ds
->bEndpointAddress
&
2113 USB_ENDPOINT_NUMBER_MASK
] = idx
+ 1;
2115 ffs_dump_mem(": Rewritten ep desc", ds
, ds
->bLength
);
2121 static int __ffs_func_bind_do_nums(enum ffs_entity_type type
, u8
*valuep
,
2122 struct usb_descriptor_header
*desc
,
2125 struct ffs_function
*func
= priv
;
2131 case FFS_DESCRIPTOR
:
2132 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2137 if (func
->interfaces_nums
[idx
] < 0) {
2138 int id
= usb_interface_id(func
->conf
, &func
->function
);
2139 if (unlikely(id
< 0))
2141 func
->interfaces_nums
[idx
] = id
;
2143 newValue
= func
->interfaces_nums
[idx
];
2147 /* String' IDs are allocated when fsf_data is bound to cdev */
2148 newValue
= func
->ffs
->stringtabs
[0]->strings
[*valuep
- 1].id
;
2152 /* USB_DT_ENDPOINT are handled in
2153 * __ffs_func_bind_do_descs(). */
2154 if (desc
->bDescriptorType
== USB_DT_ENDPOINT
)
2157 idx
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2158 if (unlikely(!func
->eps
[idx
].ep
))
2162 struct usb_endpoint_descriptor
**descs
;
2163 descs
= func
->eps
[idx
].descs
;
2164 newValue
= descs
[descs
[0] ? 0 : 1]->bEndpointAddress
;
2169 FVDBG("%02x -> %02x", *valuep
, newValue
);
2174 static int ffs_func_bind(struct usb_configuration
*c
,
2175 struct usb_function
*f
)
2177 struct ffs_function
*func
= ffs_func_from_usb(f
);
2178 struct ffs_data
*ffs
= func
->ffs
;
2180 const int full
= !!func
->ffs
->fs_descs_count
;
2181 const int high
= gadget_is_dualspeed(func
->gadget
) &&
2182 func
->ffs
->hs_descs_count
;
2186 /* Make it a single chunk, less management later on */
2188 struct ffs_ep eps
[ffs
->eps_count
];
2189 struct usb_descriptor_header
2190 *fs_descs
[full
? ffs
->fs_descs_count
+ 1 : 0];
2191 struct usb_descriptor_header
2192 *hs_descs
[high
? ffs
->hs_descs_count
+ 1 : 0];
2193 short inums
[ffs
->interfaces_count
];
2194 char raw_descs
[high
? ffs
->raw_descs_length
2195 : ffs
->raw_fs_descs_length
];
2200 /* Only high speed but not supported by gadget? */
2201 if (unlikely(!(full
| high
)))
2205 data
= kmalloc(sizeof *data
, GFP_KERNEL
);
2206 if (unlikely(!data
))
2210 memset(data
->eps
, 0, sizeof data
->eps
);
2211 memcpy(data
->raw_descs
, ffs
->raw_descs
+ 16, sizeof data
->raw_descs
);
2212 memset(data
->inums
, 0xff, sizeof data
->inums
);
2213 for (ret
= ffs
->eps_count
; ret
; --ret
)
2214 data
->eps
[ret
].num
= -1;
2217 func
->eps
= data
->eps
;
2218 func
->interfaces_nums
= data
->inums
;
2220 /* Go throught all the endpoint descriptors and allocate
2221 * endpoints first, so that later we can rewrite the endpoint
2222 * numbers without worying that it may be described later on. */
2224 func
->function
.descriptors
= data
->fs_descs
;
2225 ret
= ffs_do_descs(ffs
->fs_descs_count
,
2227 sizeof data
->raw_descs
,
2228 __ffs_func_bind_do_descs
, func
);
2229 if (unlikely(ret
< 0))
2236 func
->function
.hs_descriptors
= data
->hs_descs
;
2237 ret
= ffs_do_descs(ffs
->hs_descs_count
,
2238 data
->raw_descs
+ ret
,
2239 (sizeof data
->raw_descs
) - ret
,
2240 __ffs_func_bind_do_descs
, func
);
2243 /* Now handle interface numbers allocation and interface and
2244 * enpoint numbers rewritting. We can do that in one go
2246 ret
= ffs_do_descs(ffs
->fs_descs_count
+
2247 (high
? ffs
->hs_descs_count
: 0),
2248 data
->raw_descs
, sizeof data
->raw_descs
,
2249 __ffs_func_bind_do_nums
, func
);
2250 if (unlikely(ret
< 0))
2253 /* And we're done */
2254 ffs_event_add(ffs
, FUNCTIONFS_BIND
);
2258 /* XXX Do we need to release all claimed endpoints here? */
2263 /* Other USB function hooks *************************************************/
2265 static void ffs_func_unbind(struct usb_configuration
*c
,
2266 struct usb_function
*f
)
2268 struct ffs_function
*func
= ffs_func_from_usb(f
);
2269 struct ffs_data
*ffs
= func
->ffs
;
2273 if (ffs
->func
== func
) {
2274 ffs_func_eps_disable(func
);
2278 ffs_event_add(ffs
, FUNCTIONFS_UNBIND
);
2280 ffs_func_free(func
);
2284 static int ffs_func_set_alt(struct usb_function
*f
,
2285 unsigned interface
, unsigned alt
)
2287 struct ffs_function
*func
= ffs_func_from_usb(f
);
2288 struct ffs_data
*ffs
= func
->ffs
;
2291 if (alt
!= (unsigned)-1) {
2292 intf
= ffs_func_revmap_intf(func
, interface
);
2293 if (unlikely(intf
< 0))
2298 ffs_func_eps_disable(ffs
->func
);
2300 if (ffs
->state
!= FFS_ACTIVE
)
2303 if (alt
== (unsigned)-1) {
2305 ffs_event_add(ffs
, FUNCTIONFS_DISABLE
);
2310 ret
= ffs_func_eps_enable(func
);
2311 if (likely(ret
>= 0))
2312 ffs_event_add(ffs
, FUNCTIONFS_ENABLE
);
2316 static void ffs_func_disable(struct usb_function
*f
)
2318 ffs_func_set_alt(f
, 0, (unsigned)-1);
2321 static int ffs_func_setup(struct usb_function
*f
,
2322 const struct usb_ctrlrequest
*creq
)
2324 struct ffs_function
*func
= ffs_func_from_usb(f
);
2325 struct ffs_data
*ffs
= func
->ffs
;
2326 unsigned long flags
;
2331 FVDBG("creq->bRequestType = %02x", creq
->bRequestType
);
2332 FVDBG("creq->bRequest = %02x", creq
->bRequest
);
2333 FVDBG("creq->wValue = %04x", le16_to_cpu(creq
->wValue
));
2334 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq
->wIndex
));
2335 FVDBG("creq->wLength = %04x", le16_to_cpu(creq
->wLength
));
2337 /* Most requests directed to interface go throught here
2338 * (notable exceptions are set/get interface) so we need to
2339 * handle them. All other either handled by composite or
2340 * passed to usb_configuration->setup() (if one is set). No
2341 * matter, we will handle requests directed to endpoint here
2342 * as well (as it's straightforward) but what to do with any
2345 if (ffs
->state
!= FFS_ACTIVE
)
2348 switch (creq
->bRequestType
& USB_RECIP_MASK
) {
2349 case USB_RECIP_INTERFACE
:
2350 ret
= ffs_func_revmap_intf(func
, le16_to_cpu(creq
->wIndex
));
2351 if (unlikely(ret
< 0))
2355 case USB_RECIP_ENDPOINT
:
2356 ret
= ffs_func_revmap_ep(func
, le16_to_cpu(creq
->wIndex
));
2357 if (unlikely(ret
< 0))
2365 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2366 ffs
->ev
.setup
= *creq
;
2367 ffs
->ev
.setup
.wIndex
= cpu_to_le16(ret
);
2368 __ffs_event_add(ffs
, FUNCTIONFS_SETUP
);
2369 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2374 static void ffs_func_suspend(struct usb_function
*f
)
2377 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_SUSPEND
);
2380 static void ffs_func_resume(struct usb_function
*f
)
2383 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_RESUME
);
2388 /* Enpoint and interface numbers reverse mapping ****************************/
2390 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
)
2392 num
= func
->eps_revmap
[num
& USB_ENDPOINT_NUMBER_MASK
];
2393 return num
? num
: -EDOM
;
2396 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
)
2398 short *nums
= func
->interfaces_nums
;
2399 unsigned count
= func
->ffs
->interfaces_count
;
2401 for (; count
; --count
, ++nums
) {
2402 if (*nums
>= 0 && *nums
== intf
)
2403 return nums
- func
->interfaces_nums
;
2410 /* Misc helper functions ****************************************************/
2412 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
2415 ? likely(mutex_trylock(mutex
)) ? 0 : -EAGAIN
2416 : mutex_lock_interruptible(mutex
);
2420 static char *ffs_prepare_buffer(const char * __user buf
, size_t len
)
2427 data
= kmalloc(len
, GFP_KERNEL
);
2428 if (unlikely(!data
))
2429 return ERR_PTR(-ENOMEM
);
2431 if (unlikely(__copy_from_user(data
, buf
, len
))) {
2433 return ERR_PTR(-EFAULT
);
2436 FVDBG("Buffer from user space:");
2437 ffs_dump_mem("", data
, len
);