2 * f_fs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.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.
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 <linux/export.h>
33 #include <asm/unaligned.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 /* Debugging ****************************************************************/
45 # define pr_vdebug pr_debug
46 # define ffs_dump_mem(prefix, ptr, len) \
47 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
49 # define pr_vdebug(...) do { } while (0)
50 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
51 #endif /* VERBOSE_DEBUG */
53 #define ENTER() pr_vdebug("%s()\n", __func__)
56 /* The data structure and setup file ****************************************/
60 * Waiting for descriptors and strings.
62 * In this state no open(2), read(2) or write(2) on epfiles
63 * may succeed (which should not be the problem as there
64 * should be no such files opened in the first place).
70 * We've got descriptors and strings. We are or have called
71 * functionfs_ready_callback(). functionfs_bind() may have
72 * been called but we don't know.
74 * This is the only state in which operations on epfiles may
80 * All endpoints have been closed. This state is also set if
81 * we encounter an unrecoverable error. The only
82 * unrecoverable error is situation when after reading strings
83 * from user space we fail to initialise epfiles or
84 * functionfs_ready_callback() returns with error (<0).
86 * In this state no open(2), read(2) or write(2) (both on ep0
87 * as well as epfile) may succeed (at this point epfiles are
88 * unlinked and all closed so this is not a problem; ep0 is
89 * also closed but ep0 file exists and so open(2) on ep0 must
96 enum ffs_setup_state
{
97 /* There is no setup request pending. */
100 * User has read events and there was a setup request event
101 * there. The next read/write on ep0 will handle the
106 * There was event pending but before user space handled it
107 * some other event was introduced which canceled existing
108 * setup. If this state is set read/write on ep0 return
109 * -EIDRM. This state is only set when adding event.
120 struct usb_gadget
*gadget
;
123 * Protect access read/write operations, only one read/write
124 * at a time. As a consequence protects ep0req and company.
125 * While setup request is being processed (queued) this is
131 * Protect access to endpoint related structures (basically
132 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
138 * XXX REVISIT do we need our own request? Since we are not
139 * handling setup requests immediately user space may be so
140 * slow that another setup will be sent to the gadget but this
141 * time not to us but another function and then there could be
142 * a race. Is that the case? Or maybe we can use cdev->req
143 * after all, maybe we just need some spinlock for that?
145 struct usb_request
*ep0req
; /* P: mutex */
146 struct completion ep0req_completion
; /* P: mutex */
147 int ep0req_status
; /* P: mutex */
149 /* reference counter */
151 /* how many files are opened (EP0 and others) */
155 enum ffs_state state
;
158 * Possible transitions:
159 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
160 * happens only in ep0 read which is P: mutex
161 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
162 * happens only in ep0 i/o which is P: mutex
163 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
164 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
166 enum ffs_setup_state setup_state
;
168 #define FFS_SETUP_STATE(ffs) \
169 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
170 FFS_SETUP_CANCELED, FFS_NO_SETUP))
175 unsigned short count
;
176 /* XXX REVISIT need to update it in some places, or do we? */
177 unsigned short can_stall
;
178 struct usb_ctrlrequest setup
;
180 wait_queue_head_t waitq
;
181 } ev
; /* the whole structure, P: ev.waitq.lock */
185 #define FFS_FL_CALL_CLOSED_CALLBACK 0
186 #define FFS_FL_BOUND 1
188 /* Active function */
189 struct ffs_function
*func
;
192 * Device name, write once when file system is mounted.
193 * Intended for user to read if she wants.
195 const char *dev_name
;
196 /* Private data for our user (ie. gadget). Managed by user. */
199 /* filled by __ffs_data_got_descs() */
201 * Real descriptors are 16 bytes after raw_descs (so you need
202 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
203 * first full speed descriptor). raw_descs_length and
204 * raw_fs_descs_length do not have those 16 bytes added.
206 const void *raw_descs
;
207 unsigned raw_descs_length
;
208 unsigned raw_fs_descs_length
;
209 unsigned fs_descs_count
;
210 unsigned hs_descs_count
;
212 unsigned short strings_count
;
213 unsigned short interfaces_count
;
214 unsigned short eps_count
;
215 unsigned short _pad1
;
217 /* filled by __ffs_data_got_strings() */
218 /* ids in stringtabs are set in functionfs_bind() */
219 const void *raw_strings
;
220 struct usb_gadget_strings
**stringtabs
;
223 * File system's super block, write once when file system is
226 struct super_block
*sb
;
228 /* File permissions, written once when fs is mounted */
229 struct ffs_file_perms
{
236 * The endpoint files, filled by ffs_epfiles_create(),
237 * destroyed by ffs_epfiles_destroy().
239 struct ffs_epfile
*epfiles
;
242 /* Reference counter handling */
243 static void ffs_data_get(struct ffs_data
*ffs
);
244 static void ffs_data_put(struct ffs_data
*ffs
);
245 /* Creates new ffs_data object. */
246 static struct ffs_data
*__must_check
ffs_data_new(void) __attribute__((malloc
));
248 /* Opened counter handling. */
249 static void ffs_data_opened(struct ffs_data
*ffs
);
250 static void ffs_data_closed(struct ffs_data
*ffs
);
252 /* Called with ffs->mutex held; take over ownership of data. */
253 static int __must_check
254 __ffs_data_got_descs(struct ffs_data
*ffs
, char *data
, size_t len
);
255 static int __must_check
256 __ffs_data_got_strings(struct ffs_data
*ffs
, char *data
, size_t len
);
259 /* The function structure ***************************************************/
263 struct ffs_function
{
264 struct usb_configuration
*conf
;
265 struct usb_gadget
*gadget
;
266 struct ffs_data
*ffs
;
270 short *interfaces_nums
;
272 struct usb_function function
;
276 static struct ffs_function
*ffs_func_from_usb(struct usb_function
*f
)
278 return container_of(f
, struct ffs_function
, function
);
281 static void ffs_func_free(struct ffs_function
*func
);
283 static void ffs_func_eps_disable(struct ffs_function
*func
);
284 static int __must_check
ffs_func_eps_enable(struct ffs_function
*func
);
286 static int ffs_func_bind(struct usb_configuration
*,
287 struct usb_function
*);
288 static void ffs_func_unbind(struct usb_configuration
*,
289 struct usb_function
*);
290 static int ffs_func_set_alt(struct usb_function
*, unsigned, unsigned);
291 static void ffs_func_disable(struct usb_function
*);
292 static int ffs_func_setup(struct usb_function
*,
293 const struct usb_ctrlrequest
*);
294 static void ffs_func_suspend(struct usb_function
*);
295 static void ffs_func_resume(struct usb_function
*);
298 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
);
299 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
);
302 /* The endpoints structures *************************************************/
305 struct usb_ep
*ep
; /* P: ffs->eps_lock */
306 struct usb_request
*req
; /* P: epfile->mutex */
308 /* [0]: full speed, [1]: high speed */
309 struct usb_endpoint_descriptor
*descs
[2];
313 int status
; /* P: epfile->mutex */
317 /* Protects ep->ep and ep->req. */
319 wait_queue_head_t wait
;
321 struct ffs_data
*ffs
;
322 struct ffs_ep
*ep
; /* P: ffs->eps_lock */
324 struct dentry
*dentry
;
328 unsigned char in
; /* P: ffs->eps_lock */
329 unsigned char isoc
; /* P: ffs->eps_lock */
334 static int __must_check
ffs_epfiles_create(struct ffs_data
*ffs
);
335 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
);
337 static struct inode
*__must_check
338 ffs_sb_create_file(struct super_block
*sb
, const char *name
, void *data
,
339 const struct file_operations
*fops
,
340 struct dentry
**dentry_p
);
343 /* Misc helper functions ****************************************************/
345 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
346 __attribute__((warn_unused_result
, nonnull
));
347 static char *ffs_prepare_buffer(const char * __user buf
, size_t len
)
348 __attribute__((warn_unused_result
, nonnull
));
351 /* Control file aka ep0 *****************************************************/
353 static void ffs_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
355 struct ffs_data
*ffs
= req
->context
;
357 complete_all(&ffs
->ep0req_completion
);
360 static int __ffs_ep0_queue_wait(struct ffs_data
*ffs
, char *data
, size_t len
)
362 struct usb_request
*req
= ffs
->ep0req
;
365 req
->zero
= len
< le16_to_cpu(ffs
->ev
.setup
.wLength
);
367 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
373 * UDC layer requires to provide a buffer even for ZLP, but should
374 * not use it at all. Let's provide some poisoned pointer to catch
375 * possible bug in the driver.
377 if (req
->buf
== NULL
)
378 req
->buf
= (void *)0xDEADBABE;
380 INIT_COMPLETION(ffs
->ep0req_completion
);
382 ret
= usb_ep_queue(ffs
->gadget
->ep0
, req
, GFP_ATOMIC
);
383 if (unlikely(ret
< 0))
386 ret
= wait_for_completion_interruptible(&ffs
->ep0req_completion
);
388 usb_ep_dequeue(ffs
->gadget
->ep0
, req
);
392 ffs
->setup_state
= FFS_NO_SETUP
;
393 return ffs
->ep0req_status
;
396 static int __ffs_ep0_stall(struct ffs_data
*ffs
)
398 if (ffs
->ev
.can_stall
) {
399 pr_vdebug("ep0 stall\n");
400 usb_ep_set_halt(ffs
->gadget
->ep0
);
401 ffs
->setup_state
= FFS_NO_SETUP
;
404 pr_debug("bogus ep0 stall!\n");
409 static ssize_t
ffs_ep0_write(struct file
*file
, const char __user
*buf
,
410 size_t len
, loff_t
*ptr
)
412 struct ffs_data
*ffs
= file
->private_data
;
418 /* Fast check if setup was canceled */
419 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
423 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
424 if (unlikely(ret
< 0))
428 switch (ffs
->state
) {
429 case FFS_READ_DESCRIPTORS
:
430 case FFS_READ_STRINGS
:
432 if (unlikely(len
< 16)) {
437 data
= ffs_prepare_buffer(buf
, len
);
444 if (ffs
->state
== FFS_READ_DESCRIPTORS
) {
445 pr_info("read descriptors\n");
446 ret
= __ffs_data_got_descs(ffs
, data
, len
);
447 if (unlikely(ret
< 0))
450 ffs
->state
= FFS_READ_STRINGS
;
453 pr_info("read strings\n");
454 ret
= __ffs_data_got_strings(ffs
, data
, len
);
455 if (unlikely(ret
< 0))
458 ret
= ffs_epfiles_create(ffs
);
460 ffs
->state
= FFS_CLOSING
;
464 ffs
->state
= FFS_ACTIVE
;
465 mutex_unlock(&ffs
->mutex
);
467 ret
= functionfs_ready_callback(ffs
);
468 if (unlikely(ret
< 0)) {
469 ffs
->state
= FFS_CLOSING
;
473 set_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
);
481 * We're called from user space, we can use _irq
482 * rather then _irqsave
484 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
485 switch (FFS_SETUP_STATE(ffs
)) {
486 case FFS_SETUP_CANCELED
:
494 case FFS_SETUP_PENDING
:
498 /* FFS_SETUP_PENDING */
499 if (!(ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
)) {
500 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
501 ret
= __ffs_ep0_stall(ffs
);
505 /* FFS_SETUP_PENDING and not stall */
506 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
508 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
510 data
= ffs_prepare_buffer(buf
, len
);
516 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
519 * We are guaranteed to be still in FFS_ACTIVE state
520 * but the state of setup could have changed from
521 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
522 * to check for that. If that happened we copied data
523 * from user space in vain but it's unlikely.
525 * For sure we are not in FFS_NO_SETUP since this is
526 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
527 * transition can be performed and it's protected by
530 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
533 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
535 /* unlocks spinlock */
536 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
546 mutex_unlock(&ffs
->mutex
);
550 static ssize_t
__ffs_ep0_read_events(struct ffs_data
*ffs
, char __user
*buf
,
554 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
557 struct usb_functionfs_event events
[n
];
560 memset(events
, 0, sizeof events
);
563 events
[i
].type
= ffs
->ev
.types
[i
];
564 if (events
[i
].type
== FUNCTIONFS_SETUP
) {
565 events
[i
].u
.setup
= ffs
->ev
.setup
;
566 ffs
->setup_state
= FFS_SETUP_PENDING
;
570 if (n
< ffs
->ev
.count
) {
572 memmove(ffs
->ev
.types
, ffs
->ev
.types
+ n
,
573 ffs
->ev
.count
* sizeof *ffs
->ev
.types
);
578 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
579 mutex_unlock(&ffs
->mutex
);
581 return unlikely(__copy_to_user(buf
, events
, sizeof events
))
582 ? -EFAULT
: sizeof events
;
585 static ssize_t
ffs_ep0_read(struct file
*file
, char __user
*buf
,
586 size_t len
, loff_t
*ptr
)
588 struct ffs_data
*ffs
= file
->private_data
;
595 /* Fast check if setup was canceled */
596 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
600 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
601 if (unlikely(ret
< 0))
605 if (ffs
->state
!= FFS_ACTIVE
) {
611 * We're called from user space, we can use _irq rather then
614 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
616 switch (FFS_SETUP_STATE(ffs
)) {
617 case FFS_SETUP_CANCELED
:
622 n
= len
/ sizeof(struct usb_functionfs_event
);
628 if ((file
->f_flags
& O_NONBLOCK
) && !ffs
->ev
.count
) {
633 if (wait_event_interruptible_exclusive_locked_irq(ffs
->ev
.waitq
,
639 return __ffs_ep0_read_events(ffs
, buf
,
640 min(n
, (size_t)ffs
->ev
.count
));
642 case FFS_SETUP_PENDING
:
643 if (ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
) {
644 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
645 ret
= __ffs_ep0_stall(ffs
);
649 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
651 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
654 data
= kmalloc(len
, GFP_KERNEL
);
655 if (unlikely(!data
)) {
661 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
663 /* See ffs_ep0_write() */
664 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
669 /* unlocks spinlock */
670 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
671 if (likely(ret
> 0) && unlikely(__copy_to_user(buf
, data
, len
)))
680 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
682 mutex_unlock(&ffs
->mutex
);
687 static int ffs_ep0_open(struct inode
*inode
, struct file
*file
)
689 struct ffs_data
*ffs
= inode
->i_private
;
693 if (unlikely(ffs
->state
== FFS_CLOSING
))
696 file
->private_data
= ffs
;
697 ffs_data_opened(ffs
);
702 static int ffs_ep0_release(struct inode
*inode
, struct file
*file
)
704 struct ffs_data
*ffs
= file
->private_data
;
708 ffs_data_closed(ffs
);
713 static long ffs_ep0_ioctl(struct file
*file
, unsigned code
, unsigned long value
)
715 struct ffs_data
*ffs
= file
->private_data
;
716 struct usb_gadget
*gadget
= ffs
->gadget
;
721 if (code
== FUNCTIONFS_INTERFACE_REVMAP
) {
722 struct ffs_function
*func
= ffs
->func
;
723 ret
= func
? ffs_func_revmap_intf(func
, value
) : -ENODEV
;
724 } else if (gadget
->ops
->ioctl
) {
725 ret
= gadget
->ops
->ioctl(gadget
, code
, value
);
733 static const struct file_operations ffs_ep0_operations
= {
734 .owner
= THIS_MODULE
,
737 .open
= ffs_ep0_open
,
738 .write
= ffs_ep0_write
,
739 .read
= ffs_ep0_read
,
740 .release
= ffs_ep0_release
,
741 .unlocked_ioctl
= ffs_ep0_ioctl
,
745 /* "Normal" endpoints operations ********************************************/
747 static void ffs_epfile_io_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
750 if (likely(req
->context
)) {
751 struct ffs_ep
*ep
= _ep
->driver_data
;
752 ep
->status
= req
->status
? req
->status
: req
->actual
;
753 complete(req
->context
);
757 static ssize_t
ffs_epfile_io(struct file
*file
,
758 char __user
*buf
, size_t len
, int read
)
760 struct ffs_epfile
*epfile
= file
->private_data
;
768 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
769 mutex_unlock(&epfile
->mutex
);
772 /* Are we still active? */
773 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
)) {
778 /* Wait for endpoint to be enabled */
781 if (file
->f_flags
& O_NONBLOCK
) {
786 if (wait_event_interruptible(epfile
->wait
,
787 (ep
= epfile
->ep
))) {
794 halt
= !read
== !epfile
->in
;
795 if (halt
&& epfile
->isoc
) {
800 /* Allocate & copy */
801 if (!halt
&& !data
) {
802 data
= kzalloc(len
, GFP_KERNEL
);
807 unlikely(__copy_from_user(data
, buf
, len
))) {
813 /* We will be using request */
814 ret
= ffs_mutex_lock(&epfile
->mutex
,
815 file
->f_flags
& O_NONBLOCK
);
820 * We're called from user space, we can use _irq rather then
823 spin_lock_irq(&epfile
->ffs
->eps_lock
);
826 * While we were acquiring mutex endpoint got disabled
829 } while (unlikely(epfile
->ep
!= ep
));
832 if (unlikely(halt
)) {
833 if (likely(epfile
->ep
== ep
) && !WARN_ON(!ep
->ep
))
834 usb_ep_set_halt(ep
->ep
);
835 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
838 /* Fire the request */
839 DECLARE_COMPLETION_ONSTACK(done
);
841 struct usb_request
*req
= ep
->req
;
842 req
->context
= &done
;
843 req
->complete
= ffs_epfile_io_complete
;
847 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
849 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
851 if (unlikely(ret
< 0)) {
853 } else if (unlikely(wait_for_completion_interruptible(&done
))) {
855 usb_ep_dequeue(ep
->ep
, req
);
858 if (read
&& ret
> 0 &&
859 unlikely(copy_to_user(buf
, data
, ret
)))
864 mutex_unlock(&epfile
->mutex
);
871 ffs_epfile_write(struct file
*file
, const char __user
*buf
, size_t len
,
876 return ffs_epfile_io(file
, (char __user
*)buf
, len
, 0);
880 ffs_epfile_read(struct file
*file
, char __user
*buf
, size_t len
, loff_t
*ptr
)
884 return ffs_epfile_io(file
, buf
, len
, 1);
888 ffs_epfile_open(struct inode
*inode
, struct file
*file
)
890 struct ffs_epfile
*epfile
= inode
->i_private
;
894 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
897 file
->private_data
= epfile
;
898 ffs_data_opened(epfile
->ffs
);
904 ffs_epfile_release(struct inode
*inode
, struct file
*file
)
906 struct ffs_epfile
*epfile
= inode
->i_private
;
910 ffs_data_closed(epfile
->ffs
);
915 static long ffs_epfile_ioctl(struct file
*file
, unsigned code
,
918 struct ffs_epfile
*epfile
= file
->private_data
;
923 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
926 spin_lock_irq(&epfile
->ffs
->eps_lock
);
927 if (likely(epfile
->ep
)) {
929 case FUNCTIONFS_FIFO_STATUS
:
930 ret
= usb_ep_fifo_status(epfile
->ep
->ep
);
932 case FUNCTIONFS_FIFO_FLUSH
:
933 usb_ep_fifo_flush(epfile
->ep
->ep
);
936 case FUNCTIONFS_CLEAR_HALT
:
937 ret
= usb_ep_clear_halt(epfile
->ep
->ep
);
939 case FUNCTIONFS_ENDPOINT_REVMAP
:
940 ret
= epfile
->ep
->num
;
948 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
953 static const struct file_operations ffs_epfile_operations
= {
954 .owner
= THIS_MODULE
,
957 .open
= ffs_epfile_open
,
958 .write
= ffs_epfile_write
,
959 .read
= ffs_epfile_read
,
960 .release
= ffs_epfile_release
,
961 .unlocked_ioctl
= ffs_epfile_ioctl
,
965 /* File system and super block operations ***********************************/
968 * Mounting the file system creates a controller file, used first for
969 * function configuration then later for event monitoring.
972 static struct inode
*__must_check
973 ffs_sb_make_inode(struct super_block
*sb
, void *data
,
974 const struct file_operations
*fops
,
975 const struct inode_operations
*iops
,
976 struct ffs_file_perms
*perms
)
982 inode
= new_inode(sb
);
985 struct timespec current_time
= CURRENT_TIME
;
987 inode
->i_ino
= get_next_ino();
988 inode
->i_mode
= perms
->mode
;
989 inode
->i_uid
= perms
->uid
;
990 inode
->i_gid
= perms
->gid
;
991 inode
->i_atime
= current_time
;
992 inode
->i_mtime
= current_time
;
993 inode
->i_ctime
= current_time
;
994 inode
->i_private
= data
;
1004 /* Create "regular" file */
1005 static struct inode
*ffs_sb_create_file(struct super_block
*sb
,
1006 const char *name
, void *data
,
1007 const struct file_operations
*fops
,
1008 struct dentry
**dentry_p
)
1010 struct ffs_data
*ffs
= sb
->s_fs_info
;
1011 struct dentry
*dentry
;
1012 struct inode
*inode
;
1016 dentry
= d_alloc_name(sb
->s_root
, name
);
1017 if (unlikely(!dentry
))
1020 inode
= ffs_sb_make_inode(sb
, data
, fops
, NULL
, &ffs
->file_perms
);
1021 if (unlikely(!inode
)) {
1026 d_add(dentry
, inode
);
1034 static const struct super_operations ffs_sb_operations
= {
1035 .statfs
= simple_statfs
,
1036 .drop_inode
= generic_delete_inode
,
1039 struct ffs_sb_fill_data
{
1040 struct ffs_file_perms perms
;
1042 const char *dev_name
;
1045 static int ffs_sb_fill(struct super_block
*sb
, void *_data
, int silent
)
1047 struct ffs_sb_fill_data
*data
= _data
;
1048 struct inode
*inode
;
1050 struct ffs_data
*ffs
;
1054 /* Initialise data */
1055 ffs
= ffs_data_new();
1060 ffs
->dev_name
= data
->dev_name
;
1061 ffs
->file_perms
= data
->perms
;
1063 sb
->s_fs_info
= ffs
;
1064 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1065 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1066 sb
->s_magic
= FUNCTIONFS_MAGIC
;
1067 sb
->s_op
= &ffs_sb_operations
;
1068 sb
->s_time_gran
= 1;
1071 data
->perms
.mode
= data
->root_mode
;
1072 inode
= ffs_sb_make_inode(sb
, NULL
,
1073 &simple_dir_operations
,
1074 &simple_dir_inode_operations
,
1076 if (unlikely(!inode
))
1078 d
= d_alloc_root(inode
);
1084 if (unlikely(!ffs_sb_create_file(sb
, "ep0", ffs
,
1085 &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 pr_err("'=' missing in %s\n", opts
);
1125 value
= simple_strtoul(eq
+ 1, &end
, 0);
1126 if (unlikely(*end
!= ',' && *end
!= 0)) {
1127 pr_err("%s: invalid value: %s\n", 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 pr_err("%s: invalid option\n", opts
);
1166 /* Next iteration */
1175 /* "mount -t functionfs dev_name /dev/function" ends up here */
1177 static struct dentry
*
1178 ffs_fs_mount(struct file_system_type
*t
, int flags
,
1179 const char *dev_name
, void *opts
)
1181 struct ffs_sb_fill_data data
= {
1183 .mode
= S_IFREG
| 0600,
1187 .root_mode
= S_IFDIR
| 0500,
1193 ret
= functionfs_check_dev_callback(dev_name
);
1194 if (unlikely(ret
< 0))
1195 return ERR_PTR(ret
);
1197 ret
= ffs_fs_parse_opts(&data
, opts
);
1198 if (unlikely(ret
< 0))
1199 return ERR_PTR(ret
);
1201 data
.dev_name
= dev_name
;
1202 return mount_single(t
, flags
, &data
, ffs_sb_fill
);
1206 ffs_fs_kill_sb(struct super_block
*sb
)
1212 kill_litter_super(sb
);
1213 ptr
= xchg(&sb
->s_fs_info
, NULL
);
1218 static struct file_system_type ffs_fs_type
= {
1219 .owner
= THIS_MODULE
,
1220 .name
= "functionfs",
1221 .mount
= ffs_fs_mount
,
1222 .kill_sb
= ffs_fs_kill_sb
,
1226 /* Driver's main init/cleanup functions *************************************/
1228 static int functionfs_init(void)
1234 ret
= register_filesystem(&ffs_fs_type
);
1236 pr_info("file system registered\n");
1238 pr_err("failed registering file system (%d)\n", ret
);
1243 static void functionfs_cleanup(void)
1247 pr_info("unloading\n");
1248 unregister_filesystem(&ffs_fs_type
);
1252 /* ffs_data and ffs_function construction and destruction code **************/
1254 static void ffs_data_clear(struct ffs_data
*ffs
);
1255 static void ffs_data_reset(struct ffs_data
*ffs
);
1257 static void ffs_data_get(struct ffs_data
*ffs
)
1261 atomic_inc(&ffs
->ref
);
1264 static void ffs_data_opened(struct ffs_data
*ffs
)
1268 atomic_inc(&ffs
->ref
);
1269 atomic_inc(&ffs
->opened
);
1272 static void ffs_data_put(struct ffs_data
*ffs
)
1276 if (unlikely(atomic_dec_and_test(&ffs
->ref
))) {
1277 pr_info("%s(): freeing\n", __func__
);
1278 ffs_data_clear(ffs
);
1279 BUG_ON(mutex_is_locked(&ffs
->mutex
) ||
1280 spin_is_locked(&ffs
->ev
.waitq
.lock
) ||
1281 waitqueue_active(&ffs
->ev
.waitq
) ||
1282 waitqueue_active(&ffs
->ep0req_completion
.wait
));
1287 static void ffs_data_closed(struct ffs_data
*ffs
)
1291 if (atomic_dec_and_test(&ffs
->opened
)) {
1292 ffs
->state
= FFS_CLOSING
;
1293 ffs_data_reset(ffs
);
1299 static struct ffs_data
*ffs_data_new(void)
1301 struct ffs_data
*ffs
= kzalloc(sizeof *ffs
, GFP_KERNEL
);
1307 atomic_set(&ffs
->ref
, 1);
1308 atomic_set(&ffs
->opened
, 0);
1309 ffs
->state
= FFS_READ_DESCRIPTORS
;
1310 mutex_init(&ffs
->mutex
);
1311 spin_lock_init(&ffs
->eps_lock
);
1312 init_waitqueue_head(&ffs
->ev
.waitq
);
1313 init_completion(&ffs
->ep0req_completion
);
1315 /* XXX REVISIT need to update it in some places, or do we? */
1316 ffs
->ev
.can_stall
= 1;
1321 static void ffs_data_clear(struct ffs_data
*ffs
)
1325 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
))
1326 functionfs_closed_callback(ffs
);
1328 BUG_ON(ffs
->gadget
);
1331 ffs_epfiles_destroy(ffs
->epfiles
, ffs
->eps_count
);
1333 kfree(ffs
->raw_descs
);
1334 kfree(ffs
->raw_strings
);
1335 kfree(ffs
->stringtabs
);
1338 static void ffs_data_reset(struct ffs_data
*ffs
)
1342 ffs_data_clear(ffs
);
1344 ffs
->epfiles
= NULL
;
1345 ffs
->raw_descs
= NULL
;
1346 ffs
->raw_strings
= NULL
;
1347 ffs
->stringtabs
= NULL
;
1349 ffs
->raw_descs_length
= 0;
1350 ffs
->raw_fs_descs_length
= 0;
1351 ffs
->fs_descs_count
= 0;
1352 ffs
->hs_descs_count
= 0;
1354 ffs
->strings_count
= 0;
1355 ffs
->interfaces_count
= 0;
1360 ffs
->state
= FFS_READ_DESCRIPTORS
;
1361 ffs
->setup_state
= FFS_NO_SETUP
;
1366 static int functionfs_bind(struct ffs_data
*ffs
, struct usb_composite_dev
*cdev
)
1368 struct usb_gadget_strings
**lang
;
1373 if (WARN_ON(ffs
->state
!= FFS_ACTIVE
1374 || test_and_set_bit(FFS_FL_BOUND
, &ffs
->flags
)))
1377 first_id
= usb_string_ids_n(cdev
, ffs
->strings_count
);
1378 if (unlikely(first_id
< 0))
1381 ffs
->ep0req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
1382 if (unlikely(!ffs
->ep0req
))
1384 ffs
->ep0req
->complete
= ffs_ep0_complete
;
1385 ffs
->ep0req
->context
= ffs
;
1387 lang
= ffs
->stringtabs
;
1388 for (lang
= ffs
->stringtabs
; *lang
; ++lang
) {
1389 struct usb_string
*str
= (*lang
)->strings
;
1391 for (; str
->s
; ++id
, ++str
)
1395 ffs
->gadget
= cdev
->gadget
;
1400 static void functionfs_unbind(struct ffs_data
*ffs
)
1404 if (!WARN_ON(!ffs
->gadget
)) {
1405 usb_ep_free_request(ffs
->gadget
->ep0
, ffs
->ep0req
);
1412 static int ffs_epfiles_create(struct ffs_data
*ffs
)
1414 struct ffs_epfile
*epfile
, *epfiles
;
1419 count
= ffs
->eps_count
;
1420 epfiles
= kzalloc(count
* sizeof *epfiles
, GFP_KERNEL
);
1425 for (i
= 1; i
<= count
; ++i
, ++epfile
) {
1427 mutex_init(&epfile
->mutex
);
1428 init_waitqueue_head(&epfile
->wait
);
1429 sprintf(epfiles
->name
, "ep%u", i
);
1430 if (!unlikely(ffs_sb_create_file(ffs
->sb
, epfiles
->name
, epfile
,
1431 &ffs_epfile_operations
,
1432 &epfile
->dentry
))) {
1433 ffs_epfiles_destroy(epfiles
, i
- 1);
1438 ffs
->epfiles
= epfiles
;
1442 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
)
1444 struct ffs_epfile
*epfile
= epfiles
;
1448 for (; count
; --count
, ++epfile
) {
1449 BUG_ON(mutex_is_locked(&epfile
->mutex
) ||
1450 waitqueue_active(&epfile
->wait
));
1451 if (epfile
->dentry
) {
1452 d_delete(epfile
->dentry
);
1453 dput(epfile
->dentry
);
1454 epfile
->dentry
= NULL
;
1461 static int functionfs_bind_config(struct usb_composite_dev
*cdev
,
1462 struct usb_configuration
*c
,
1463 struct ffs_data
*ffs
)
1465 struct ffs_function
*func
;
1470 func
= kzalloc(sizeof *func
, GFP_KERNEL
);
1471 if (unlikely(!func
))
1474 func
->function
.name
= "Function FS Gadget";
1475 func
->function
.strings
= ffs
->stringtabs
;
1477 func
->function
.bind
= ffs_func_bind
;
1478 func
->function
.unbind
= ffs_func_unbind
;
1479 func
->function
.set_alt
= ffs_func_set_alt
;
1480 func
->function
.disable
= ffs_func_disable
;
1481 func
->function
.setup
= ffs_func_setup
;
1482 func
->function
.suspend
= ffs_func_suspend
;
1483 func
->function
.resume
= ffs_func_resume
;
1486 func
->gadget
= cdev
->gadget
;
1490 ret
= usb_add_function(c
, &func
->function
);
1492 ffs_func_free(func
);
1497 static void ffs_func_free(struct ffs_function
*func
)
1501 ffs_data_put(func
->ffs
);
1505 * eps and interfaces_nums are allocated in the same chunk so
1506 * only one free is required. Descriptors are also allocated
1507 * in the same chunk.
1513 static void ffs_func_eps_disable(struct ffs_function
*func
)
1515 struct ffs_ep
*ep
= func
->eps
;
1516 struct ffs_epfile
*epfile
= func
->ffs
->epfiles
;
1517 unsigned count
= func
->ffs
->eps_count
;
1518 unsigned long flags
;
1520 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1522 /* pending requests get nuked */
1524 usb_ep_disable(ep
->ep
);
1530 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1533 static int ffs_func_eps_enable(struct ffs_function
*func
)
1535 struct ffs_data
*ffs
= func
->ffs
;
1536 struct ffs_ep
*ep
= func
->eps
;
1537 struct ffs_epfile
*epfile
= ffs
->epfiles
;
1538 unsigned count
= ffs
->eps_count
;
1539 unsigned long flags
;
1542 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1544 struct usb_endpoint_descriptor
*ds
;
1545 ds
= ep
->descs
[ep
->descs
[1] ? 1 : 0];
1547 ep
->ep
->driver_data
= ep
;
1549 ret
= usb_ep_enable(ep
->ep
);
1552 epfile
->in
= usb_endpoint_dir_in(ds
);
1553 epfile
->isoc
= usb_endpoint_xfer_isoc(ds
);
1558 wake_up(&epfile
->wait
);
1563 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1569 /* Parsing and building descriptors and strings *****************************/
1572 * This validates if data pointed by data is a valid USB descriptor as
1573 * well as record how many interfaces, endpoints and strings are
1574 * required by given configuration. Returns address after the
1575 * descriptor or NULL if data is invalid.
1578 enum ffs_entity_type
{
1579 FFS_DESCRIPTOR
, FFS_INTERFACE
, FFS_STRING
, FFS_ENDPOINT
1582 typedef int (*ffs_entity_callback
)(enum ffs_entity_type entity
,
1584 struct usb_descriptor_header
*desc
,
1587 static int __must_check
ffs_do_desc(char *data
, unsigned len
,
1588 ffs_entity_callback entity
, void *priv
)
1590 struct usb_descriptor_header
*_ds
= (void *)data
;
1596 /* At least two bytes are required: length and type */
1598 pr_vdebug("descriptor too short\n");
1602 /* If we have at least as many bytes as the descriptor takes? */
1603 length
= _ds
->bLength
;
1605 pr_vdebug("descriptor longer then available data\n");
1609 #define __entity_check_INTERFACE(val) 1
1610 #define __entity_check_STRING(val) (val)
1611 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1612 #define __entity(type, val) do { \
1613 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1614 if (unlikely(!__entity_check_ ##type(val))) { \
1615 pr_vdebug("invalid entity's value\n"); \
1618 ret = entity(FFS_ ##type, &val, _ds, priv); \
1619 if (unlikely(ret < 0)) { \
1620 pr_debug("entity " #type "(%02x); ret = %d\n", \
1626 /* Parse descriptor depending on type. */
1627 switch (_ds
->bDescriptorType
) {
1631 case USB_DT_DEVICE_QUALIFIER
:
1632 /* function can't have any of those */
1633 pr_vdebug("descriptor reserved for gadget: %d\n",
1634 _ds
->bDescriptorType
);
1637 case USB_DT_INTERFACE
: {
1638 struct usb_interface_descriptor
*ds
= (void *)_ds
;
1639 pr_vdebug("interface descriptor\n");
1640 if (length
!= sizeof *ds
)
1643 __entity(INTERFACE
, ds
->bInterfaceNumber
);
1645 __entity(STRING
, ds
->iInterface
);
1649 case USB_DT_ENDPOINT
: {
1650 struct usb_endpoint_descriptor
*ds
= (void *)_ds
;
1651 pr_vdebug("endpoint descriptor\n");
1652 if (length
!= USB_DT_ENDPOINT_SIZE
&&
1653 length
!= USB_DT_ENDPOINT_AUDIO_SIZE
)
1655 __entity(ENDPOINT
, ds
->bEndpointAddress
);
1660 if (length
!= sizeof(struct usb_otg_descriptor
))
1664 case USB_DT_INTERFACE_ASSOCIATION
: {
1665 struct usb_interface_assoc_descriptor
*ds
= (void *)_ds
;
1666 pr_vdebug("interface association descriptor\n");
1667 if (length
!= sizeof *ds
)
1670 __entity(STRING
, ds
->iFunction
);
1674 case USB_DT_OTHER_SPEED_CONFIG
:
1675 case USB_DT_INTERFACE_POWER
:
1677 case USB_DT_SECURITY
:
1678 case USB_DT_CS_RADIO_CONTROL
:
1680 pr_vdebug("unimplemented descriptor: %d\n", _ds
->bDescriptorType
);
1684 /* We should never be here */
1685 pr_vdebug("unknown descriptor: %d\n", _ds
->bDescriptorType
);
1689 pr_vdebug("invalid length: %d (descriptor %d)\n",
1690 _ds
->bLength
, _ds
->bDescriptorType
);
1695 #undef __entity_check_DESCRIPTOR
1696 #undef __entity_check_INTERFACE
1697 #undef __entity_check_STRING
1698 #undef __entity_check_ENDPOINT
1703 static int __must_check
ffs_do_descs(unsigned count
, char *data
, unsigned len
,
1704 ffs_entity_callback entity
, void *priv
)
1706 const unsigned _len
= len
;
1707 unsigned long num
= 0;
1717 /* Record "descriptor" entity */
1718 ret
= entity(FFS_DESCRIPTOR
, (u8
*)num
, (void *)data
, priv
);
1719 if (unlikely(ret
< 0)) {
1720 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1728 ret
= ffs_do_desc(data
, len
, entity
, priv
);
1729 if (unlikely(ret
< 0)) {
1730 pr_debug("%s returns %d\n", __func__
, ret
);
1740 static int __ffs_data_do_entity(enum ffs_entity_type type
,
1741 u8
*valuep
, struct usb_descriptor_header
*desc
,
1744 struct ffs_data
*ffs
= priv
;
1749 case FFS_DESCRIPTOR
:
1754 * Interfaces are indexed from zero so if we
1755 * encountered interface "n" then there are at least
1758 if (*valuep
>= ffs
->interfaces_count
)
1759 ffs
->interfaces_count
= *valuep
+ 1;
1764 * Strings are indexed from 1 (0 is magic ;) reserved
1765 * for languages list or some such)
1767 if (*valuep
> ffs
->strings_count
)
1768 ffs
->strings_count
= *valuep
;
1772 /* Endpoints are indexed from 1 as well. */
1773 if ((*valuep
& USB_ENDPOINT_NUMBER_MASK
) > ffs
->eps_count
)
1774 ffs
->eps_count
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
);
1781 static int __ffs_data_got_descs(struct ffs_data
*ffs
,
1782 char *const _data
, size_t len
)
1784 unsigned fs_count
, hs_count
;
1785 int fs_len
, ret
= -EINVAL
;
1790 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_DESCRIPTORS_MAGIC
||
1791 get_unaligned_le32(data
+ 4) != len
))
1793 fs_count
= get_unaligned_le32(data
+ 8);
1794 hs_count
= get_unaligned_le32(data
+ 12);
1796 if (!fs_count
&& !hs_count
)
1802 if (likely(fs_count
)) {
1803 fs_len
= ffs_do_descs(fs_count
, data
, len
,
1804 __ffs_data_do_entity
, ffs
);
1805 if (unlikely(fs_len
< 0)) {
1816 if (likely(hs_count
)) {
1817 ret
= ffs_do_descs(hs_count
, data
, len
,
1818 __ffs_data_do_entity
, ffs
);
1819 if (unlikely(ret
< 0))
1825 if (unlikely(len
!= ret
))
1828 ffs
->raw_fs_descs_length
= fs_len
;
1829 ffs
->raw_descs_length
= fs_len
+ ret
;
1830 ffs
->raw_descs
= _data
;
1831 ffs
->fs_descs_count
= fs_count
;
1832 ffs
->hs_descs_count
= hs_count
;
1843 static int __ffs_data_got_strings(struct ffs_data
*ffs
,
1844 char *const _data
, size_t len
)
1846 u32 str_count
, needed_count
, lang_count
;
1847 struct usb_gadget_strings
**stringtabs
, *t
;
1848 struct usb_string
*strings
, *s
;
1849 const char *data
= _data
;
1853 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_STRINGS_MAGIC
||
1854 get_unaligned_le32(data
+ 4) != len
))
1856 str_count
= get_unaligned_le32(data
+ 8);
1857 lang_count
= get_unaligned_le32(data
+ 12);
1859 /* if one is zero the other must be zero */
1860 if (unlikely(!str_count
!= !lang_count
))
1863 /* Do we have at least as many strings as descriptors need? */
1864 needed_count
= ffs
->strings_count
;
1865 if (unlikely(str_count
< needed_count
))
1869 * If we don't need any strings just return and free all
1872 if (!needed_count
) {
1877 /* Allocate everything in one chunk so there's less maintenance. */
1880 struct usb_gadget_strings
*stringtabs
[lang_count
+ 1];
1881 struct usb_gadget_strings stringtab
[lang_count
];
1882 struct usb_string strings
[lang_count
*(needed_count
+1)];
1886 d
= kmalloc(sizeof *d
, GFP_KERNEL
);
1892 stringtabs
= d
->stringtabs
;
1896 *stringtabs
++ = t
++;
1900 stringtabs
= d
->stringtabs
;
1906 /* For each language */
1910 do { /* lang_count > 0 so we can use do-while */
1911 unsigned needed
= needed_count
;
1913 if (unlikely(len
< 3))
1915 t
->language
= get_unaligned_le16(data
);
1922 /* For each string */
1923 do { /* str_count > 0 so we can use do-while */
1924 size_t length
= strnlen(data
, len
);
1926 if (unlikely(length
== len
))
1930 * User may provide more strings then we need,
1931 * if that's the case we simply ignore the
1934 if (likely(needed
)) {
1936 * s->id will be set while adding
1937 * function to configuration so for
1938 * now just leave garbage here.
1947 } while (--str_count
);
1949 s
->id
= 0; /* terminator */
1953 } while (--lang_count
);
1955 /* Some garbage left? */
1960 ffs
->stringtabs
= stringtabs
;
1961 ffs
->raw_strings
= _data
;
1973 /* Events handling and management *******************************************/
1975 static void __ffs_event_add(struct ffs_data
*ffs
,
1976 enum usb_functionfs_event_type type
)
1978 enum usb_functionfs_event_type rem_type1
, rem_type2
= type
;
1982 * Abort any unhandled setup
1984 * We do not need to worry about some cmpxchg() changing value
1985 * of ffs->setup_state without holding the lock because when
1986 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1987 * the source does nothing.
1989 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
1990 ffs
->setup_state
= FFS_SETUP_CANCELED
;
1993 case FUNCTIONFS_RESUME
:
1994 rem_type2
= FUNCTIONFS_SUSPEND
;
1996 case FUNCTIONFS_SUSPEND
:
1997 case FUNCTIONFS_SETUP
:
1999 /* Discard all similar events */
2002 case FUNCTIONFS_BIND
:
2003 case FUNCTIONFS_UNBIND
:
2004 case FUNCTIONFS_DISABLE
:
2005 case FUNCTIONFS_ENABLE
:
2006 /* Discard everything other then power management. */
2007 rem_type1
= FUNCTIONFS_SUSPEND
;
2008 rem_type2
= FUNCTIONFS_RESUME
;
2017 u8
*ev
= ffs
->ev
.types
, *out
= ev
;
2018 unsigned n
= ffs
->ev
.count
;
2019 for (; n
; --n
, ++ev
)
2020 if ((*ev
== rem_type1
|| *ev
== rem_type2
) == neg
)
2023 pr_vdebug("purging event %d\n", *ev
);
2024 ffs
->ev
.count
= out
- ffs
->ev
.types
;
2027 pr_vdebug("adding event %d\n", type
);
2028 ffs
->ev
.types
[ffs
->ev
.count
++] = type
;
2029 wake_up_locked(&ffs
->ev
.waitq
);
2032 static void ffs_event_add(struct ffs_data
*ffs
,
2033 enum usb_functionfs_event_type type
)
2035 unsigned long flags
;
2036 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2037 __ffs_event_add(ffs
, type
);
2038 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2042 /* Bind/unbind USB function hooks *******************************************/
2044 static int __ffs_func_bind_do_descs(enum ffs_entity_type type
, u8
*valuep
,
2045 struct usb_descriptor_header
*desc
,
2048 struct usb_endpoint_descriptor
*ds
= (void *)desc
;
2049 struct ffs_function
*func
= priv
;
2050 struct ffs_ep
*ffs_ep
;
2053 * If hs_descriptors is not NULL then we are reading hs
2056 const int isHS
= func
->function
.hs_descriptors
!= NULL
;
2059 if (type
!= FFS_DESCRIPTOR
)
2063 func
->function
.hs_descriptors
[(long)valuep
] = desc
;
2065 func
->function
.descriptors
[(long)valuep
] = desc
;
2067 if (!desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
2070 idx
= (ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2071 ffs_ep
= func
->eps
+ idx
;
2073 if (unlikely(ffs_ep
->descs
[isHS
])) {
2074 pr_vdebug("two %sspeed descriptors for EP %d\n",
2075 isHS
? "high" : "full",
2076 ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2079 ffs_ep
->descs
[isHS
] = ds
;
2081 ffs_dump_mem(": Original ep desc", ds
, ds
->bLength
);
2083 ds
->bEndpointAddress
= ffs_ep
->descs
[0]->bEndpointAddress
;
2084 if (!ds
->wMaxPacketSize
)
2085 ds
->wMaxPacketSize
= ffs_ep
->descs
[0]->wMaxPacketSize
;
2087 struct usb_request
*req
;
2090 pr_vdebug("autoconfig\n");
2091 ep
= usb_ep_autoconfig(func
->gadget
, ds
);
2094 ep
->driver_data
= func
->eps
+ idx
;
2096 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
2102 func
->eps_revmap
[ds
->bEndpointAddress
&
2103 USB_ENDPOINT_NUMBER_MASK
] = idx
+ 1;
2105 ffs_dump_mem(": Rewritten ep desc", ds
, ds
->bLength
);
2110 static int __ffs_func_bind_do_nums(enum ffs_entity_type type
, u8
*valuep
,
2111 struct usb_descriptor_header
*desc
,
2114 struct ffs_function
*func
= priv
;
2120 case FFS_DESCRIPTOR
:
2121 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2126 if (func
->interfaces_nums
[idx
] < 0) {
2127 int id
= usb_interface_id(func
->conf
, &func
->function
);
2128 if (unlikely(id
< 0))
2130 func
->interfaces_nums
[idx
] = id
;
2132 newValue
= func
->interfaces_nums
[idx
];
2136 /* String' IDs are allocated when fsf_data is bound to cdev */
2137 newValue
= func
->ffs
->stringtabs
[0]->strings
[*valuep
- 1].id
;
2142 * USB_DT_ENDPOINT are handled in
2143 * __ffs_func_bind_do_descs().
2145 if (desc
->bDescriptorType
== USB_DT_ENDPOINT
)
2148 idx
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2149 if (unlikely(!func
->eps
[idx
].ep
))
2153 struct usb_endpoint_descriptor
**descs
;
2154 descs
= func
->eps
[idx
].descs
;
2155 newValue
= descs
[descs
[0] ? 0 : 1]->bEndpointAddress
;
2160 pr_vdebug("%02x -> %02x\n", *valuep
, newValue
);
2165 static int ffs_func_bind(struct usb_configuration
*c
,
2166 struct usb_function
*f
)
2168 struct ffs_function
*func
= ffs_func_from_usb(f
);
2169 struct ffs_data
*ffs
= func
->ffs
;
2171 const int full
= !!func
->ffs
->fs_descs_count
;
2172 const int high
= gadget_is_dualspeed(func
->gadget
) &&
2173 func
->ffs
->hs_descs_count
;
2177 /* Make it a single chunk, less management later on */
2179 struct ffs_ep eps
[ffs
->eps_count
];
2180 struct usb_descriptor_header
2181 *fs_descs
[full
? ffs
->fs_descs_count
+ 1 : 0];
2182 struct usb_descriptor_header
2183 *hs_descs
[high
? ffs
->hs_descs_count
+ 1 : 0];
2184 short inums
[ffs
->interfaces_count
];
2185 char raw_descs
[high
? ffs
->raw_descs_length
2186 : ffs
->raw_fs_descs_length
];
2191 /* Only high speed but not supported by gadget? */
2192 if (unlikely(!(full
| high
)))
2196 data
= kmalloc(sizeof *data
, GFP_KERNEL
);
2197 if (unlikely(!data
))
2201 memset(data
->eps
, 0, sizeof data
->eps
);
2202 memcpy(data
->raw_descs
, ffs
->raw_descs
+ 16, sizeof data
->raw_descs
);
2203 memset(data
->inums
, 0xff, sizeof data
->inums
);
2204 for (ret
= ffs
->eps_count
; ret
; --ret
)
2205 data
->eps
[ret
].num
= -1;
2208 func
->eps
= data
->eps
;
2209 func
->interfaces_nums
= data
->inums
;
2212 * Go through all the endpoint descriptors and allocate
2213 * endpoints first, so that later we can rewrite the endpoint
2214 * numbers without worrying that it may be described later on.
2217 func
->function
.descriptors
= data
->fs_descs
;
2218 ret
= ffs_do_descs(ffs
->fs_descs_count
,
2220 sizeof data
->raw_descs
,
2221 __ffs_func_bind_do_descs
, func
);
2222 if (unlikely(ret
< 0))
2229 func
->function
.hs_descriptors
= data
->hs_descs
;
2230 ret
= ffs_do_descs(ffs
->hs_descs_count
,
2231 data
->raw_descs
+ ret
,
2232 (sizeof data
->raw_descs
) - ret
,
2233 __ffs_func_bind_do_descs
, func
);
2237 * Now handle interface numbers allocation and interface and
2238 * endpoint numbers rewriting. We can do that in one go
2241 ret
= ffs_do_descs(ffs
->fs_descs_count
+
2242 (high
? ffs
->hs_descs_count
: 0),
2243 data
->raw_descs
, sizeof data
->raw_descs
,
2244 __ffs_func_bind_do_nums
, func
);
2245 if (unlikely(ret
< 0))
2248 /* And we're done */
2249 ffs_event_add(ffs
, FUNCTIONFS_BIND
);
2253 /* XXX Do we need to release all claimed endpoints here? */
2258 /* Other USB function hooks *************************************************/
2260 static void ffs_func_unbind(struct usb_configuration
*c
,
2261 struct usb_function
*f
)
2263 struct ffs_function
*func
= ffs_func_from_usb(f
);
2264 struct ffs_data
*ffs
= func
->ffs
;
2268 if (ffs
->func
== func
) {
2269 ffs_func_eps_disable(func
);
2273 ffs_event_add(ffs
, FUNCTIONFS_UNBIND
);
2275 ffs_func_free(func
);
2278 static int ffs_func_set_alt(struct usb_function
*f
,
2279 unsigned interface
, unsigned alt
)
2281 struct ffs_function
*func
= ffs_func_from_usb(f
);
2282 struct ffs_data
*ffs
= func
->ffs
;
2285 if (alt
!= (unsigned)-1) {
2286 intf
= ffs_func_revmap_intf(func
, interface
);
2287 if (unlikely(intf
< 0))
2292 ffs_func_eps_disable(ffs
->func
);
2294 if (ffs
->state
!= FFS_ACTIVE
)
2297 if (alt
== (unsigned)-1) {
2299 ffs_event_add(ffs
, FUNCTIONFS_DISABLE
);
2304 ret
= ffs_func_eps_enable(func
);
2305 if (likely(ret
>= 0))
2306 ffs_event_add(ffs
, FUNCTIONFS_ENABLE
);
2310 static void ffs_func_disable(struct usb_function
*f
)
2312 ffs_func_set_alt(f
, 0, (unsigned)-1);
2315 static int ffs_func_setup(struct usb_function
*f
,
2316 const struct usb_ctrlrequest
*creq
)
2318 struct ffs_function
*func
= ffs_func_from_usb(f
);
2319 struct ffs_data
*ffs
= func
->ffs
;
2320 unsigned long flags
;
2325 pr_vdebug("creq->bRequestType = %02x\n", creq
->bRequestType
);
2326 pr_vdebug("creq->bRequest = %02x\n", creq
->bRequest
);
2327 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq
->wValue
));
2328 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq
->wIndex
));
2329 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq
->wLength
));
2332 * Most requests directed to interface go through here
2333 * (notable exceptions are set/get interface) so we need to
2334 * handle them. All other either handled by composite or
2335 * passed to usb_configuration->setup() (if one is set). No
2336 * matter, we will handle requests directed to endpoint here
2337 * as well (as it's straightforward) but what to do with any
2340 if (ffs
->state
!= FFS_ACTIVE
)
2343 switch (creq
->bRequestType
& USB_RECIP_MASK
) {
2344 case USB_RECIP_INTERFACE
:
2345 ret
= ffs_func_revmap_intf(func
, le16_to_cpu(creq
->wIndex
));
2346 if (unlikely(ret
< 0))
2350 case USB_RECIP_ENDPOINT
:
2351 ret
= ffs_func_revmap_ep(func
, le16_to_cpu(creq
->wIndex
));
2352 if (unlikely(ret
< 0))
2360 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2361 ffs
->ev
.setup
= *creq
;
2362 ffs
->ev
.setup
.wIndex
= cpu_to_le16(ret
);
2363 __ffs_event_add(ffs
, FUNCTIONFS_SETUP
);
2364 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2369 static void ffs_func_suspend(struct usb_function
*f
)
2372 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_SUSPEND
);
2375 static void ffs_func_resume(struct usb_function
*f
)
2378 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_RESUME
);
2382 /* Endpoint and interface numbers reverse mapping ***************************/
2384 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
)
2386 num
= func
->eps_revmap
[num
& USB_ENDPOINT_NUMBER_MASK
];
2387 return num
? num
: -EDOM
;
2390 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
)
2392 short *nums
= func
->interfaces_nums
;
2393 unsigned count
= func
->ffs
->interfaces_count
;
2395 for (; count
; --count
, ++nums
) {
2396 if (*nums
>= 0 && *nums
== intf
)
2397 return nums
- func
->interfaces_nums
;
2404 /* Misc helper functions ****************************************************/
2406 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
2409 ? likely(mutex_trylock(mutex
)) ? 0 : -EAGAIN
2410 : mutex_lock_interruptible(mutex
);
2413 static char *ffs_prepare_buffer(const char * __user buf
, size_t len
)
2420 data
= kmalloc(len
, GFP_KERNEL
);
2421 if (unlikely(!data
))
2422 return ERR_PTR(-ENOMEM
);
2424 if (unlikely(__copy_from_user(data
, buf
, len
))) {
2426 return ERR_PTR(-EFAULT
);
2429 pr_vdebug("Buffer from user space:\n");
2430 ffs_dump_mem("", data
, len
);