2 * f_fs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
7 * Based on inode.c (GadgetFS) which was:
8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
19 /* #define VERBOSE_DEBUG */
21 #include <linux/blkdev.h>
22 #include <linux/pagemap.h>
23 #include <linux/export.h>
24 #include <linux/hid.h>
25 #include <asm/unaligned.h>
27 #include <linux/usb/composite.h>
28 #include <linux/usb/functionfs.h>
32 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
34 /* Variable Length Array Macros **********************************************/
35 #define vla_group(groupname) size_t groupname##__next = 0
36 #define vla_group_size(groupname) groupname##__next
38 #define vla_item(groupname, type, name, n) \
39 size_t groupname##_##name##__offset = ({ \
40 size_t align_mask = __alignof__(type) - 1; \
41 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
42 size_t size = (n) * sizeof(type); \
43 groupname##__next = offset + size; \
47 #define vla_item_with_sz(groupname, type, name, n) \
48 size_t groupname##_##name##__sz = (n) * sizeof(type); \
49 size_t groupname##_##name##__offset = ({ \
50 size_t align_mask = __alignof__(type) - 1; \
51 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
52 size_t size = groupname##_##name##__sz; \
53 groupname##__next = offset + size; \
57 #define vla_ptr(ptr, groupname, name) \
58 ((void *) ((char *)ptr + groupname##_##name##__offset))
60 /* Debugging ****************************************************************/
64 # define pr_vdebug pr_debug
65 #endif /* pr_vdebug */
66 # define ffs_dump_mem(prefix, ptr, len) \
67 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
70 # define pr_vdebug(...) do { } while (0)
71 #endif /* pr_vdebug */
72 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
73 #endif /* VERBOSE_DEBUG */
75 #define ENTER() pr_vdebug("%s()\n", __func__)
78 /* The data structure and setup file ****************************************/
82 * Waiting for descriptors and strings.
84 * In this state no open(2), read(2) or write(2) on epfiles
85 * may succeed (which should not be the problem as there
86 * should be no such files opened in the first place).
92 * We've got descriptors and strings. We are or have called
93 * functionfs_ready_callback(). functionfs_bind() may have
94 * been called but we don't know.
96 * This is the only state in which operations on epfiles may
102 * All endpoints have been closed. This state is also set if
103 * we encounter an unrecoverable error. The only
104 * unrecoverable error is situation when after reading strings
105 * from user space we fail to initialise epfiles or
106 * functionfs_ready_callback() returns with error (<0).
108 * In this state no open(2), read(2) or write(2) (both on ep0
109 * as well as epfile) may succeed (at this point epfiles are
110 * unlinked and all closed so this is not a problem; ep0 is
111 * also closed but ep0 file exists and so open(2) on ep0 must
118 enum ffs_setup_state
{
119 /* There is no setup request pending. */
122 * User has read events and there was a setup request event
123 * there. The next read/write on ep0 will handle the
128 * There was event pending but before user space handled it
129 * some other event was introduced which canceled existing
130 * setup. If this state is set read/write on ep0 return
131 * -EIDRM. This state is only set when adding event.
142 struct usb_gadget
*gadget
;
145 * Protect access read/write operations, only one read/write
146 * at a time. As a consequence protects ep0req and company.
147 * While setup request is being processed (queued) this is
153 * Protect access to endpoint related structures (basically
154 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
160 * XXX REVISIT do we need our own request? Since we are not
161 * handling setup requests immediately user space may be so
162 * slow that another setup will be sent to the gadget but this
163 * time not to us but another function and then there could be
164 * a race. Is that the case? Or maybe we can use cdev->req
165 * after all, maybe we just need some spinlock for that?
167 struct usb_request
*ep0req
; /* P: mutex */
168 struct completion ep0req_completion
; /* P: mutex */
169 int ep0req_status
; /* P: mutex */
171 /* reference counter */
173 /* how many files are opened (EP0 and others) */
177 enum ffs_state state
;
180 * Possible transitions:
181 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
182 * happens only in ep0 read which is P: mutex
183 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
184 * happens only in ep0 i/o which is P: mutex
185 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
186 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
188 enum ffs_setup_state setup_state
;
190 #define FFS_SETUP_STATE(ffs) \
191 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
192 FFS_SETUP_CANCELED, FFS_NO_SETUP))
197 unsigned short count
;
198 /* XXX REVISIT need to update it in some places, or do we? */
199 unsigned short can_stall
;
200 struct usb_ctrlrequest setup
;
202 wait_queue_head_t waitq
;
203 } ev
; /* the whole structure, P: ev.waitq.lock */
207 #define FFS_FL_CALL_CLOSED_CALLBACK 0
208 #define FFS_FL_BOUND 1
210 /* Active function */
211 struct ffs_function
*func
;
214 * Device name, write once when file system is mounted.
215 * Intended for user to read if she wants.
217 const char *dev_name
;
218 /* Private data for our user (ie. gadget). Managed by user. */
221 /* filled by __ffs_data_got_descs() */
223 * Real descriptors are 16 bytes after raw_descs (so you need
224 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
225 * first full speed descriptor). raw_descs_length and
226 * raw_fs_descs_length do not have those 16 bytes added.
228 const void *raw_descs
;
229 unsigned raw_descs_length
;
230 unsigned raw_fs_descs_length
;
231 unsigned fs_descs_count
;
232 unsigned hs_descs_count
;
234 unsigned short strings_count
;
235 unsigned short interfaces_count
;
236 unsigned short eps_count
;
237 unsigned short _pad1
;
239 /* filled by __ffs_data_got_strings() */
240 /* ids in stringtabs are set in functionfs_bind() */
241 const void *raw_strings
;
242 struct usb_gadget_strings
**stringtabs
;
245 * File system's super block, write once when file system is
248 struct super_block
*sb
;
250 /* File permissions, written once when fs is mounted */
251 struct ffs_file_perms
{
258 * The endpoint files, filled by ffs_epfiles_create(),
259 * destroyed by ffs_epfiles_destroy().
261 struct ffs_epfile
*epfiles
;
264 /* Reference counter handling */
265 static void ffs_data_get(struct ffs_data
*ffs
);
266 static void ffs_data_put(struct ffs_data
*ffs
);
267 /* Creates new ffs_data object. */
268 static struct ffs_data
*__must_check
ffs_data_new(void) __attribute__((malloc
));
270 /* Opened counter handling. */
271 static void ffs_data_opened(struct ffs_data
*ffs
);
272 static void ffs_data_closed(struct ffs_data
*ffs
);
274 /* Called with ffs->mutex held; take over ownership of data. */
275 static int __must_check
276 __ffs_data_got_descs(struct ffs_data
*ffs
, char *data
, size_t len
);
277 static int __must_check
278 __ffs_data_got_strings(struct ffs_data
*ffs
, char *data
, size_t len
);
281 /* The function structure ***************************************************/
285 struct ffs_function
{
286 struct usb_configuration
*conf
;
287 struct usb_gadget
*gadget
;
288 struct ffs_data
*ffs
;
292 short *interfaces_nums
;
294 struct usb_function function
;
298 static struct ffs_function
*ffs_func_from_usb(struct usb_function
*f
)
300 return container_of(f
, struct ffs_function
, function
);
303 static void ffs_func_free(struct ffs_function
*func
);
305 static void ffs_func_eps_disable(struct ffs_function
*func
);
306 static int __must_check
ffs_func_eps_enable(struct ffs_function
*func
);
308 static int ffs_func_bind(struct usb_configuration
*,
309 struct usb_function
*);
310 static void ffs_func_unbind(struct usb_configuration
*,
311 struct usb_function
*);
312 static int ffs_func_set_alt(struct usb_function
*, unsigned, unsigned);
313 static void ffs_func_disable(struct usb_function
*);
314 static int ffs_func_setup(struct usb_function
*,
315 const struct usb_ctrlrequest
*);
316 static void ffs_func_suspend(struct usb_function
*);
317 static void ffs_func_resume(struct usb_function
*);
320 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
);
321 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
);
324 /* The endpoints structures *************************************************/
327 struct usb_ep
*ep
; /* P: ffs->eps_lock */
328 struct usb_request
*req
; /* P: epfile->mutex */
330 /* [0]: full speed, [1]: high speed */
331 struct usb_endpoint_descriptor
*descs
[2];
335 int status
; /* P: epfile->mutex */
339 /* Protects ep->ep and ep->req. */
341 wait_queue_head_t wait
;
343 struct ffs_data
*ffs
;
344 struct ffs_ep
*ep
; /* P: ffs->eps_lock */
346 struct dentry
*dentry
;
350 unsigned char in
; /* P: ffs->eps_lock */
351 unsigned char isoc
; /* P: ffs->eps_lock */
356 static int __must_check
ffs_epfiles_create(struct ffs_data
*ffs
);
357 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
);
359 static struct inode
*__must_check
360 ffs_sb_create_file(struct super_block
*sb
, const char *name
, void *data
,
361 const struct file_operations
*fops
,
362 struct dentry
**dentry_p
);
365 /* Misc helper functions ****************************************************/
367 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
368 __attribute__((warn_unused_result
, nonnull
));
369 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
370 __attribute__((warn_unused_result
, nonnull
));
373 /* Control file aka ep0 *****************************************************/
375 static void ffs_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
377 struct ffs_data
*ffs
= req
->context
;
379 complete_all(&ffs
->ep0req_completion
);
382 static int __ffs_ep0_queue_wait(struct ffs_data
*ffs
, char *data
, size_t len
)
384 struct usb_request
*req
= ffs
->ep0req
;
387 req
->zero
= len
< le16_to_cpu(ffs
->ev
.setup
.wLength
);
389 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
395 * UDC layer requires to provide a buffer even for ZLP, but should
396 * not use it at all. Let's provide some poisoned pointer to catch
397 * possible bug in the driver.
399 if (req
->buf
== NULL
)
400 req
->buf
= (void *)0xDEADBABE;
402 reinit_completion(&ffs
->ep0req_completion
);
404 ret
= usb_ep_queue(ffs
->gadget
->ep0
, req
, GFP_ATOMIC
);
405 if (unlikely(ret
< 0))
408 ret
= wait_for_completion_interruptible(&ffs
->ep0req_completion
);
410 usb_ep_dequeue(ffs
->gadget
->ep0
, req
);
414 ffs
->setup_state
= FFS_NO_SETUP
;
415 return ffs
->ep0req_status
;
418 static int __ffs_ep0_stall(struct ffs_data
*ffs
)
420 if (ffs
->ev
.can_stall
) {
421 pr_vdebug("ep0 stall\n");
422 usb_ep_set_halt(ffs
->gadget
->ep0
);
423 ffs
->setup_state
= FFS_NO_SETUP
;
426 pr_debug("bogus ep0 stall!\n");
431 static ssize_t
ffs_ep0_write(struct file
*file
, const char __user
*buf
,
432 size_t len
, loff_t
*ptr
)
434 struct ffs_data
*ffs
= file
->private_data
;
440 /* Fast check if setup was canceled */
441 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
445 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
446 if (unlikely(ret
< 0))
450 switch (ffs
->state
) {
451 case FFS_READ_DESCRIPTORS
:
452 case FFS_READ_STRINGS
:
454 if (unlikely(len
< 16)) {
459 data
= ffs_prepare_buffer(buf
, len
);
466 if (ffs
->state
== FFS_READ_DESCRIPTORS
) {
467 pr_info("read descriptors\n");
468 ret
= __ffs_data_got_descs(ffs
, data
, len
);
469 if (unlikely(ret
< 0))
472 ffs
->state
= FFS_READ_STRINGS
;
475 pr_info("read strings\n");
476 ret
= __ffs_data_got_strings(ffs
, data
, len
);
477 if (unlikely(ret
< 0))
480 ret
= ffs_epfiles_create(ffs
);
482 ffs
->state
= FFS_CLOSING
;
486 ffs
->state
= FFS_ACTIVE
;
487 mutex_unlock(&ffs
->mutex
);
489 ret
= functionfs_ready_callback(ffs
);
490 if (unlikely(ret
< 0)) {
491 ffs
->state
= FFS_CLOSING
;
495 set_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
);
503 * We're called from user space, we can use _irq
504 * rather then _irqsave
506 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
507 switch (FFS_SETUP_STATE(ffs
)) {
508 case FFS_SETUP_CANCELED
:
516 case FFS_SETUP_PENDING
:
520 /* FFS_SETUP_PENDING */
521 if (!(ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
)) {
522 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
523 ret
= __ffs_ep0_stall(ffs
);
527 /* FFS_SETUP_PENDING and not stall */
528 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
530 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
532 data
= ffs_prepare_buffer(buf
, len
);
538 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
541 * We are guaranteed to be still in FFS_ACTIVE state
542 * but the state of setup could have changed from
543 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
544 * to check for that. If that happened we copied data
545 * from user space in vain but it's unlikely.
547 * For sure we are not in FFS_NO_SETUP since this is
548 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
549 * transition can be performed and it's protected by
552 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
555 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
557 /* unlocks spinlock */
558 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
568 mutex_unlock(&ffs
->mutex
);
572 static ssize_t
__ffs_ep0_read_events(struct ffs_data
*ffs
, char __user
*buf
,
576 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
579 struct usb_functionfs_event events
[n
];
582 memset(events
, 0, sizeof events
);
585 events
[i
].type
= ffs
->ev
.types
[i
];
586 if (events
[i
].type
== FUNCTIONFS_SETUP
) {
587 events
[i
].u
.setup
= ffs
->ev
.setup
;
588 ffs
->setup_state
= FFS_SETUP_PENDING
;
592 if (n
< ffs
->ev
.count
) {
594 memmove(ffs
->ev
.types
, ffs
->ev
.types
+ n
,
595 ffs
->ev
.count
* sizeof *ffs
->ev
.types
);
600 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
601 mutex_unlock(&ffs
->mutex
);
603 return unlikely(__copy_to_user(buf
, events
, sizeof events
))
604 ? -EFAULT
: sizeof events
;
607 static ssize_t
ffs_ep0_read(struct file
*file
, char __user
*buf
,
608 size_t len
, loff_t
*ptr
)
610 struct ffs_data
*ffs
= file
->private_data
;
617 /* Fast check if setup was canceled */
618 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
622 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
623 if (unlikely(ret
< 0))
627 if (ffs
->state
!= FFS_ACTIVE
) {
633 * We're called from user space, we can use _irq rather then
636 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
638 switch (FFS_SETUP_STATE(ffs
)) {
639 case FFS_SETUP_CANCELED
:
644 n
= len
/ sizeof(struct usb_functionfs_event
);
650 if ((file
->f_flags
& O_NONBLOCK
) && !ffs
->ev
.count
) {
655 if (wait_event_interruptible_exclusive_locked_irq(ffs
->ev
.waitq
,
661 return __ffs_ep0_read_events(ffs
, buf
,
662 min(n
, (size_t)ffs
->ev
.count
));
664 case FFS_SETUP_PENDING
:
665 if (ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
) {
666 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
667 ret
= __ffs_ep0_stall(ffs
);
671 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
673 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
676 data
= kmalloc(len
, GFP_KERNEL
);
677 if (unlikely(!data
)) {
683 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
685 /* See ffs_ep0_write() */
686 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
691 /* unlocks spinlock */
692 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
693 if (likely(ret
> 0) && unlikely(__copy_to_user(buf
, data
, len
)))
702 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
704 mutex_unlock(&ffs
->mutex
);
709 static int ffs_ep0_open(struct inode
*inode
, struct file
*file
)
711 struct ffs_data
*ffs
= inode
->i_private
;
715 if (unlikely(ffs
->state
== FFS_CLOSING
))
718 file
->private_data
= ffs
;
719 ffs_data_opened(ffs
);
724 static int ffs_ep0_release(struct inode
*inode
, struct file
*file
)
726 struct ffs_data
*ffs
= file
->private_data
;
730 ffs_data_closed(ffs
);
735 static long ffs_ep0_ioctl(struct file
*file
, unsigned code
, unsigned long value
)
737 struct ffs_data
*ffs
= file
->private_data
;
738 struct usb_gadget
*gadget
= ffs
->gadget
;
743 if (code
== FUNCTIONFS_INTERFACE_REVMAP
) {
744 struct ffs_function
*func
= ffs
->func
;
745 ret
= func
? ffs_func_revmap_intf(func
, value
) : -ENODEV
;
746 } else if (gadget
&& gadget
->ops
->ioctl
) {
747 ret
= gadget
->ops
->ioctl(gadget
, code
, value
);
755 static const struct file_operations ffs_ep0_operations
= {
758 .open
= ffs_ep0_open
,
759 .write
= ffs_ep0_write
,
760 .read
= ffs_ep0_read
,
761 .release
= ffs_ep0_release
,
762 .unlocked_ioctl
= ffs_ep0_ioctl
,
766 /* "Normal" endpoints operations ********************************************/
768 static void ffs_epfile_io_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
771 if (likely(req
->context
)) {
772 struct ffs_ep
*ep
= _ep
->driver_data
;
773 ep
->status
= req
->status
? req
->status
: req
->actual
;
774 complete(req
->context
);
778 static ssize_t
ffs_epfile_io(struct file
*file
,
779 char __user
*buf
, size_t len
, int read
)
781 struct ffs_epfile
*epfile
= file
->private_data
;
782 struct usb_gadget
*gadget
= epfile
->ffs
->gadget
;
785 ssize_t ret
, data_len
;
788 /* Are we still active? */
789 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
)) {
794 /* Wait for endpoint to be enabled */
797 if (file
->f_flags
& O_NONBLOCK
) {
802 ret
= wait_event_interruptible(epfile
->wait
, (ep
= epfile
->ep
));
810 halt
= !read
== !epfile
->in
;
811 if (halt
&& epfile
->isoc
) {
816 /* Allocate & copy */
819 * Controller may require buffer size to be aligned to
820 * maxpacketsize of an out endpoint.
822 data_len
= read
? usb_ep_align_maybe(gadget
, ep
->ep
, len
) : len
;
824 data
= kmalloc(data_len
, GFP_KERNEL
);
828 if (!read
&& unlikely(copy_from_user(data
, buf
, len
))) {
834 /* We will be using request */
835 ret
= ffs_mutex_lock(&epfile
->mutex
, file
->f_flags
& O_NONBLOCK
);
839 spin_lock_irq(&epfile
->ffs
->eps_lock
);
841 if (epfile
->ep
!= ep
) {
842 /* In the meantime, endpoint got disabled or changed. */
844 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
847 if (likely(epfile
->ep
== ep
) && !WARN_ON(!ep
->ep
))
848 usb_ep_set_halt(ep
->ep
);
849 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
852 /* Fire the request */
853 DECLARE_COMPLETION_ONSTACK(done
);
855 struct usb_request
*req
= ep
->req
;
856 req
->context
= &done
;
857 req
->complete
= ffs_epfile_io_complete
;
859 req
->length
= data_len
;
861 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
863 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
865 if (unlikely(ret
< 0)) {
867 } else if (unlikely(wait_for_completion_interruptible(&done
))) {
869 usb_ep_dequeue(ep
->ep
, req
);
872 * XXX We may end up silently droping data here.
873 * Since data_len (i.e. req->length) may be bigger
874 * than len (after being rounded up to maxpacketsize),
875 * we may end up with more data then user space has
879 if (read
&& ret
> 0 &&
880 unlikely(copy_to_user(buf
, data
,
881 min_t(size_t, ret
, len
))))
886 mutex_unlock(&epfile
->mutex
);
893 ffs_epfile_write(struct file
*file
, const char __user
*buf
, size_t len
,
898 return ffs_epfile_io(file
, (char __user
*)buf
, len
, 0);
902 ffs_epfile_read(struct file
*file
, char __user
*buf
, size_t len
, loff_t
*ptr
)
906 return ffs_epfile_io(file
, buf
, len
, 1);
910 ffs_epfile_open(struct inode
*inode
, struct file
*file
)
912 struct ffs_epfile
*epfile
= inode
->i_private
;
916 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
919 file
->private_data
= epfile
;
920 ffs_data_opened(epfile
->ffs
);
926 ffs_epfile_release(struct inode
*inode
, struct file
*file
)
928 struct ffs_epfile
*epfile
= inode
->i_private
;
932 ffs_data_closed(epfile
->ffs
);
937 static long ffs_epfile_ioctl(struct file
*file
, unsigned code
,
940 struct ffs_epfile
*epfile
= file
->private_data
;
945 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
948 spin_lock_irq(&epfile
->ffs
->eps_lock
);
949 if (likely(epfile
->ep
)) {
951 case FUNCTIONFS_FIFO_STATUS
:
952 ret
= usb_ep_fifo_status(epfile
->ep
->ep
);
954 case FUNCTIONFS_FIFO_FLUSH
:
955 usb_ep_fifo_flush(epfile
->ep
->ep
);
958 case FUNCTIONFS_CLEAR_HALT
:
959 ret
= usb_ep_clear_halt(epfile
->ep
->ep
);
961 case FUNCTIONFS_ENDPOINT_REVMAP
:
962 ret
= epfile
->ep
->num
;
970 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
975 static const struct file_operations ffs_epfile_operations
= {
978 .open
= ffs_epfile_open
,
979 .write
= ffs_epfile_write
,
980 .read
= ffs_epfile_read
,
981 .release
= ffs_epfile_release
,
982 .unlocked_ioctl
= ffs_epfile_ioctl
,
986 /* File system and super block operations ***********************************/
989 * Mounting the file system creates a controller file, used first for
990 * function configuration then later for event monitoring.
993 static struct inode
*__must_check
994 ffs_sb_make_inode(struct super_block
*sb
, void *data
,
995 const struct file_operations
*fops
,
996 const struct inode_operations
*iops
,
997 struct ffs_file_perms
*perms
)
1003 inode
= new_inode(sb
);
1005 if (likely(inode
)) {
1006 struct timespec current_time
= CURRENT_TIME
;
1008 inode
->i_ino
= get_next_ino();
1009 inode
->i_mode
= perms
->mode
;
1010 inode
->i_uid
= perms
->uid
;
1011 inode
->i_gid
= perms
->gid
;
1012 inode
->i_atime
= current_time
;
1013 inode
->i_mtime
= current_time
;
1014 inode
->i_ctime
= current_time
;
1015 inode
->i_private
= data
;
1017 inode
->i_fop
= fops
;
1025 /* Create "regular" file */
1026 static struct inode
*ffs_sb_create_file(struct super_block
*sb
,
1027 const char *name
, void *data
,
1028 const struct file_operations
*fops
,
1029 struct dentry
**dentry_p
)
1031 struct ffs_data
*ffs
= sb
->s_fs_info
;
1032 struct dentry
*dentry
;
1033 struct inode
*inode
;
1037 dentry
= d_alloc_name(sb
->s_root
, name
);
1038 if (unlikely(!dentry
))
1041 inode
= ffs_sb_make_inode(sb
, data
, fops
, NULL
, &ffs
->file_perms
);
1042 if (unlikely(!inode
)) {
1047 d_add(dentry
, inode
);
1055 static const struct super_operations ffs_sb_operations
= {
1056 .statfs
= simple_statfs
,
1057 .drop_inode
= generic_delete_inode
,
1060 struct ffs_sb_fill_data
{
1061 struct ffs_file_perms perms
;
1063 const char *dev_name
;
1064 struct ffs_data
*ffs_data
;
1067 static int ffs_sb_fill(struct super_block
*sb
, void *_data
, int silent
)
1069 struct ffs_sb_fill_data
*data
= _data
;
1070 struct inode
*inode
;
1071 struct ffs_data
*ffs
= data
->ffs_data
;
1076 data
->ffs_data
= NULL
;
1077 sb
->s_fs_info
= ffs
;
1078 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1079 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1080 sb
->s_magic
= FUNCTIONFS_MAGIC
;
1081 sb
->s_op
= &ffs_sb_operations
;
1082 sb
->s_time_gran
= 1;
1085 data
->perms
.mode
= data
->root_mode
;
1086 inode
= ffs_sb_make_inode(sb
, NULL
,
1087 &simple_dir_operations
,
1088 &simple_dir_inode_operations
,
1090 sb
->s_root
= d_make_root(inode
);
1091 if (unlikely(!sb
->s_root
))
1095 if (unlikely(!ffs_sb_create_file(sb
, "ep0", ffs
,
1096 &ffs_ep0_operations
, NULL
)))
1102 static int ffs_fs_parse_opts(struct ffs_sb_fill_data
*data
, char *opts
)
1106 if (!opts
|| !*opts
)
1110 unsigned long value
;
1114 comma
= strchr(opts
, ',');
1119 eq
= strchr(opts
, '=');
1120 if (unlikely(!eq
)) {
1121 pr_err("'=' missing in %s\n", opts
);
1127 if (kstrtoul(eq
+ 1, 0, &value
)) {
1128 pr_err("%s: invalid value: %s\n", opts
, eq
+ 1);
1132 /* Interpret option */
1133 switch (eq
- opts
) {
1135 if (!memcmp(opts
, "rmode", 5))
1136 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1137 else if (!memcmp(opts
, "fmode", 5))
1138 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1144 if (!memcmp(opts
, "mode", 4)) {
1145 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1146 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1153 if (!memcmp(opts
, "uid", 3)) {
1154 data
->perms
.uid
= make_kuid(current_user_ns(), value
);
1155 if (!uid_valid(data
->perms
.uid
)) {
1156 pr_err("%s: unmapped value: %lu\n", opts
, value
);
1159 } else if (!memcmp(opts
, "gid", 3)) {
1160 data
->perms
.gid
= make_kgid(current_user_ns(), value
);
1161 if (!gid_valid(data
->perms
.gid
)) {
1162 pr_err("%s: unmapped value: %lu\n", opts
, value
);
1172 pr_err("%s: invalid option\n", opts
);
1176 /* Next iteration */
1185 /* "mount -t functionfs dev_name /dev/function" ends up here */
1187 static struct dentry
*
1188 ffs_fs_mount(struct file_system_type
*t
, int flags
,
1189 const char *dev_name
, void *opts
)
1191 struct ffs_sb_fill_data data
= {
1193 .mode
= S_IFREG
| 0600,
1194 .uid
= GLOBAL_ROOT_UID
,
1195 .gid
= GLOBAL_ROOT_GID
,
1197 .root_mode
= S_IFDIR
| 0500,
1202 struct ffs_data
*ffs
;
1206 ret
= ffs_fs_parse_opts(&data
, opts
);
1207 if (unlikely(ret
< 0))
1208 return ERR_PTR(ret
);
1210 ffs
= ffs_data_new();
1212 return ERR_PTR(-ENOMEM
);
1213 ffs
->file_perms
= data
.perms
;
1215 ffs
->dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
1216 if (unlikely(!ffs
->dev_name
)) {
1218 return ERR_PTR(-ENOMEM
);
1221 ffs_dev
= functionfs_acquire_dev_callback(dev_name
);
1222 if (IS_ERR(ffs_dev
)) {
1224 return ERR_CAST(ffs_dev
);
1226 ffs
->private_data
= ffs_dev
;
1227 data
.ffs_data
= ffs
;
1229 rv
= mount_nodev(t
, flags
, &data
, ffs_sb_fill
);
1230 if (IS_ERR(rv
) && data
.ffs_data
) {
1231 functionfs_release_dev_callback(data
.ffs_data
);
1232 ffs_data_put(data
.ffs_data
);
1238 ffs_fs_kill_sb(struct super_block
*sb
)
1242 kill_litter_super(sb
);
1243 if (sb
->s_fs_info
) {
1244 functionfs_release_dev_callback(sb
->s_fs_info
);
1245 ffs_data_put(sb
->s_fs_info
);
1249 static struct file_system_type ffs_fs_type
= {
1250 .owner
= THIS_MODULE
,
1251 .name
= "functionfs",
1252 .mount
= ffs_fs_mount
,
1253 .kill_sb
= ffs_fs_kill_sb
,
1255 MODULE_ALIAS_FS("functionfs");
1258 /* Driver's main init/cleanup functions *************************************/
1260 static int functionfs_init(void)
1266 ret
= register_filesystem(&ffs_fs_type
);
1268 pr_info("file system registered\n");
1270 pr_err("failed registering file system (%d)\n", ret
);
1275 static void functionfs_cleanup(void)
1279 pr_info("unloading\n");
1280 unregister_filesystem(&ffs_fs_type
);
1284 /* ffs_data and ffs_function construction and destruction code **************/
1286 static void ffs_data_clear(struct ffs_data
*ffs
);
1287 static void ffs_data_reset(struct ffs_data
*ffs
);
1289 static void ffs_data_get(struct ffs_data
*ffs
)
1293 atomic_inc(&ffs
->ref
);
1296 static void ffs_data_opened(struct ffs_data
*ffs
)
1300 atomic_inc(&ffs
->ref
);
1301 atomic_inc(&ffs
->opened
);
1304 static void ffs_data_put(struct ffs_data
*ffs
)
1308 if (unlikely(atomic_dec_and_test(&ffs
->ref
))) {
1309 pr_info("%s(): freeing\n", __func__
);
1310 ffs_data_clear(ffs
);
1311 BUG_ON(waitqueue_active(&ffs
->ev
.waitq
) ||
1312 waitqueue_active(&ffs
->ep0req_completion
.wait
));
1313 kfree(ffs
->dev_name
);
1318 static void ffs_data_closed(struct ffs_data
*ffs
)
1322 if (atomic_dec_and_test(&ffs
->opened
)) {
1323 ffs
->state
= FFS_CLOSING
;
1324 ffs_data_reset(ffs
);
1330 static struct ffs_data
*ffs_data_new(void)
1332 struct ffs_data
*ffs
= kzalloc(sizeof *ffs
, GFP_KERNEL
);
1338 atomic_set(&ffs
->ref
, 1);
1339 atomic_set(&ffs
->opened
, 0);
1340 ffs
->state
= FFS_READ_DESCRIPTORS
;
1341 mutex_init(&ffs
->mutex
);
1342 spin_lock_init(&ffs
->eps_lock
);
1343 init_waitqueue_head(&ffs
->ev
.waitq
);
1344 init_completion(&ffs
->ep0req_completion
);
1346 /* XXX REVISIT need to update it in some places, or do we? */
1347 ffs
->ev
.can_stall
= 1;
1352 static void ffs_data_clear(struct ffs_data
*ffs
)
1356 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
))
1357 functionfs_closed_callback(ffs
);
1359 BUG_ON(ffs
->gadget
);
1362 ffs_epfiles_destroy(ffs
->epfiles
, ffs
->eps_count
);
1364 kfree(ffs
->raw_descs
);
1365 kfree(ffs
->raw_strings
);
1366 kfree(ffs
->stringtabs
);
1369 static void ffs_data_reset(struct ffs_data
*ffs
)
1373 ffs_data_clear(ffs
);
1375 ffs
->epfiles
= NULL
;
1376 ffs
->raw_descs
= NULL
;
1377 ffs
->raw_strings
= NULL
;
1378 ffs
->stringtabs
= NULL
;
1380 ffs
->raw_descs_length
= 0;
1381 ffs
->raw_fs_descs_length
= 0;
1382 ffs
->fs_descs_count
= 0;
1383 ffs
->hs_descs_count
= 0;
1385 ffs
->strings_count
= 0;
1386 ffs
->interfaces_count
= 0;
1391 ffs
->state
= FFS_READ_DESCRIPTORS
;
1392 ffs
->setup_state
= FFS_NO_SETUP
;
1397 static int functionfs_bind(struct ffs_data
*ffs
, struct usb_composite_dev
*cdev
)
1399 struct usb_gadget_strings
**lang
;
1404 if (WARN_ON(ffs
->state
!= FFS_ACTIVE
1405 || test_and_set_bit(FFS_FL_BOUND
, &ffs
->flags
)))
1408 first_id
= usb_string_ids_n(cdev
, ffs
->strings_count
);
1409 if (unlikely(first_id
< 0))
1412 ffs
->ep0req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
1413 if (unlikely(!ffs
->ep0req
))
1415 ffs
->ep0req
->complete
= ffs_ep0_complete
;
1416 ffs
->ep0req
->context
= ffs
;
1418 lang
= ffs
->stringtabs
;
1419 for (lang
= ffs
->stringtabs
; *lang
; ++lang
) {
1420 struct usb_string
*str
= (*lang
)->strings
;
1422 for (; str
->s
; ++id
, ++str
)
1426 ffs
->gadget
= cdev
->gadget
;
1431 static void functionfs_unbind(struct ffs_data
*ffs
)
1435 if (!WARN_ON(!ffs
->gadget
)) {
1436 usb_ep_free_request(ffs
->gadget
->ep0
, ffs
->ep0req
);
1439 clear_bit(FFS_FL_BOUND
, &ffs
->flags
);
1444 static int ffs_epfiles_create(struct ffs_data
*ffs
)
1446 struct ffs_epfile
*epfile
, *epfiles
;
1451 count
= ffs
->eps_count
;
1452 epfiles
= kcalloc(count
, sizeof(*epfiles
), GFP_KERNEL
);
1457 for (i
= 1; i
<= count
; ++i
, ++epfile
) {
1459 mutex_init(&epfile
->mutex
);
1460 init_waitqueue_head(&epfile
->wait
);
1461 sprintf(epfiles
->name
, "ep%u", i
);
1462 if (!unlikely(ffs_sb_create_file(ffs
->sb
, epfiles
->name
, epfile
,
1463 &ffs_epfile_operations
,
1464 &epfile
->dentry
))) {
1465 ffs_epfiles_destroy(epfiles
, i
- 1);
1470 ffs
->epfiles
= epfiles
;
1474 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
)
1476 struct ffs_epfile
*epfile
= epfiles
;
1480 for (; count
; --count
, ++epfile
) {
1481 BUG_ON(mutex_is_locked(&epfile
->mutex
) ||
1482 waitqueue_active(&epfile
->wait
));
1483 if (epfile
->dentry
) {
1484 d_delete(epfile
->dentry
);
1485 dput(epfile
->dentry
);
1486 epfile
->dentry
= NULL
;
1493 static int functionfs_bind_config(struct usb_composite_dev
*cdev
,
1494 struct usb_configuration
*c
,
1495 struct ffs_data
*ffs
)
1497 struct ffs_function
*func
;
1502 func
= kzalloc(sizeof *func
, GFP_KERNEL
);
1503 if (unlikely(!func
))
1506 func
->function
.name
= "Function FS Gadget";
1507 func
->function
.strings
= ffs
->stringtabs
;
1509 func
->function
.bind
= ffs_func_bind
;
1510 func
->function
.unbind
= ffs_func_unbind
;
1511 func
->function
.set_alt
= ffs_func_set_alt
;
1512 func
->function
.disable
= ffs_func_disable
;
1513 func
->function
.setup
= ffs_func_setup
;
1514 func
->function
.suspend
= ffs_func_suspend
;
1515 func
->function
.resume
= ffs_func_resume
;
1518 func
->gadget
= cdev
->gadget
;
1522 ret
= usb_add_function(c
, &func
->function
);
1524 ffs_func_free(func
);
1529 static void ffs_func_free(struct ffs_function
*func
)
1531 struct ffs_ep
*ep
= func
->eps
;
1532 unsigned count
= func
->ffs
->eps_count
;
1533 unsigned long flags
;
1537 /* cleanup after autoconfig */
1538 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1540 if (ep
->ep
&& ep
->req
)
1541 usb_ep_free_request(ep
->ep
, ep
->req
);
1545 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1547 ffs_data_put(func
->ffs
);
1551 * eps and interfaces_nums are allocated in the same chunk so
1552 * only one free is required. Descriptors are also allocated
1553 * in the same chunk.
1559 static void ffs_func_eps_disable(struct ffs_function
*func
)
1561 struct ffs_ep
*ep
= func
->eps
;
1562 struct ffs_epfile
*epfile
= func
->ffs
->epfiles
;
1563 unsigned count
= func
->ffs
->eps_count
;
1564 unsigned long flags
;
1566 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1568 /* pending requests get nuked */
1570 usb_ep_disable(ep
->ep
);
1576 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1579 static int ffs_func_eps_enable(struct ffs_function
*func
)
1581 struct ffs_data
*ffs
= func
->ffs
;
1582 struct ffs_ep
*ep
= func
->eps
;
1583 struct ffs_epfile
*epfile
= ffs
->epfiles
;
1584 unsigned count
= ffs
->eps_count
;
1585 unsigned long flags
;
1588 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1590 struct usb_endpoint_descriptor
*ds
;
1591 ds
= ep
->descs
[ep
->descs
[1] ? 1 : 0];
1593 ep
->ep
->driver_data
= ep
;
1595 ret
= usb_ep_enable(ep
->ep
);
1598 epfile
->in
= usb_endpoint_dir_in(ds
);
1599 epfile
->isoc
= usb_endpoint_xfer_isoc(ds
);
1604 wake_up(&epfile
->wait
);
1609 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1615 /* Parsing and building descriptors and strings *****************************/
1618 * This validates if data pointed by data is a valid USB descriptor as
1619 * well as record how many interfaces, endpoints and strings are
1620 * required by given configuration. Returns address after the
1621 * descriptor or NULL if data is invalid.
1624 enum ffs_entity_type
{
1625 FFS_DESCRIPTOR
, FFS_INTERFACE
, FFS_STRING
, FFS_ENDPOINT
1628 typedef int (*ffs_entity_callback
)(enum ffs_entity_type entity
,
1630 struct usb_descriptor_header
*desc
,
1633 static int __must_check
ffs_do_desc(char *data
, unsigned len
,
1634 ffs_entity_callback entity
, void *priv
)
1636 struct usb_descriptor_header
*_ds
= (void *)data
;
1642 /* At least two bytes are required: length and type */
1644 pr_vdebug("descriptor too short\n");
1648 /* If we have at least as many bytes as the descriptor takes? */
1649 length
= _ds
->bLength
;
1651 pr_vdebug("descriptor longer then available data\n");
1655 #define __entity_check_INTERFACE(val) 1
1656 #define __entity_check_STRING(val) (val)
1657 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1658 #define __entity(type, val) do { \
1659 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1660 if (unlikely(!__entity_check_ ##type(val))) { \
1661 pr_vdebug("invalid entity's value\n"); \
1664 ret = entity(FFS_ ##type, &val, _ds, priv); \
1665 if (unlikely(ret < 0)) { \
1666 pr_debug("entity " #type "(%02x); ret = %d\n", \
1672 /* Parse descriptor depending on type. */
1673 switch (_ds
->bDescriptorType
) {
1677 case USB_DT_DEVICE_QUALIFIER
:
1678 /* function can't have any of those */
1679 pr_vdebug("descriptor reserved for gadget: %d\n",
1680 _ds
->bDescriptorType
);
1683 case USB_DT_INTERFACE
: {
1684 struct usb_interface_descriptor
*ds
= (void *)_ds
;
1685 pr_vdebug("interface descriptor\n");
1686 if (length
!= sizeof *ds
)
1689 __entity(INTERFACE
, ds
->bInterfaceNumber
);
1691 __entity(STRING
, ds
->iInterface
);
1695 case USB_DT_ENDPOINT
: {
1696 struct usb_endpoint_descriptor
*ds
= (void *)_ds
;
1697 pr_vdebug("endpoint descriptor\n");
1698 if (length
!= USB_DT_ENDPOINT_SIZE
&&
1699 length
!= USB_DT_ENDPOINT_AUDIO_SIZE
)
1701 __entity(ENDPOINT
, ds
->bEndpointAddress
);
1706 pr_vdebug("hid descriptor\n");
1707 if (length
!= sizeof(struct hid_descriptor
))
1712 if (length
!= sizeof(struct usb_otg_descriptor
))
1716 case USB_DT_INTERFACE_ASSOCIATION
: {
1717 struct usb_interface_assoc_descriptor
*ds
= (void *)_ds
;
1718 pr_vdebug("interface association descriptor\n");
1719 if (length
!= sizeof *ds
)
1722 __entity(STRING
, ds
->iFunction
);
1726 case USB_DT_OTHER_SPEED_CONFIG
:
1727 case USB_DT_INTERFACE_POWER
:
1729 case USB_DT_SECURITY
:
1730 case USB_DT_CS_RADIO_CONTROL
:
1732 pr_vdebug("unimplemented descriptor: %d\n", _ds
->bDescriptorType
);
1736 /* We should never be here */
1737 pr_vdebug("unknown descriptor: %d\n", _ds
->bDescriptorType
);
1741 pr_vdebug("invalid length: %d (descriptor %d)\n",
1742 _ds
->bLength
, _ds
->bDescriptorType
);
1747 #undef __entity_check_DESCRIPTOR
1748 #undef __entity_check_INTERFACE
1749 #undef __entity_check_STRING
1750 #undef __entity_check_ENDPOINT
1755 static int __must_check
ffs_do_descs(unsigned count
, char *data
, unsigned len
,
1756 ffs_entity_callback entity
, void *priv
)
1758 const unsigned _len
= len
;
1759 unsigned long num
= 0;
1769 /* Record "descriptor" entity */
1770 ret
= entity(FFS_DESCRIPTOR
, (u8
*)num
, (void *)data
, priv
);
1771 if (unlikely(ret
< 0)) {
1772 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1780 ret
= ffs_do_desc(data
, len
, entity
, priv
);
1781 if (unlikely(ret
< 0)) {
1782 pr_debug("%s returns %d\n", __func__
, ret
);
1792 static int __ffs_data_do_entity(enum ffs_entity_type type
,
1793 u8
*valuep
, struct usb_descriptor_header
*desc
,
1796 struct ffs_data
*ffs
= priv
;
1801 case FFS_DESCRIPTOR
:
1806 * Interfaces are indexed from zero so if we
1807 * encountered interface "n" then there are at least
1810 if (*valuep
>= ffs
->interfaces_count
)
1811 ffs
->interfaces_count
= *valuep
+ 1;
1816 * Strings are indexed from 1 (0 is magic ;) reserved
1817 * for languages list or some such)
1819 if (*valuep
> ffs
->strings_count
)
1820 ffs
->strings_count
= *valuep
;
1824 /* Endpoints are indexed from 1 as well. */
1825 if ((*valuep
& USB_ENDPOINT_NUMBER_MASK
) > ffs
->eps_count
)
1826 ffs
->eps_count
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
);
1833 static int __ffs_data_got_descs(struct ffs_data
*ffs
,
1834 char *const _data
, size_t len
)
1836 unsigned fs_count
, hs_count
;
1837 int fs_len
, ret
= -EINVAL
;
1842 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_DESCRIPTORS_MAGIC
||
1843 get_unaligned_le32(data
+ 4) != len
))
1845 fs_count
= get_unaligned_le32(data
+ 8);
1846 hs_count
= get_unaligned_le32(data
+ 12);
1848 if (!fs_count
&& !hs_count
)
1854 if (likely(fs_count
)) {
1855 fs_len
= ffs_do_descs(fs_count
, data
, len
,
1856 __ffs_data_do_entity
, ffs
);
1857 if (unlikely(fs_len
< 0)) {
1868 if (likely(hs_count
)) {
1869 ret
= ffs_do_descs(hs_count
, data
, len
,
1870 __ffs_data_do_entity
, ffs
);
1871 if (unlikely(ret
< 0))
1877 if (unlikely(len
!= ret
))
1880 ffs
->raw_fs_descs_length
= fs_len
;
1881 ffs
->raw_descs_length
= fs_len
+ ret
;
1882 ffs
->raw_descs
= _data
;
1883 ffs
->fs_descs_count
= fs_count
;
1884 ffs
->hs_descs_count
= hs_count
;
1895 static int __ffs_data_got_strings(struct ffs_data
*ffs
,
1896 char *const _data
, size_t len
)
1898 u32 str_count
, needed_count
, lang_count
;
1899 struct usb_gadget_strings
**stringtabs
, *t
;
1900 struct usb_string
*strings
, *s
;
1901 const char *data
= _data
;
1905 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_STRINGS_MAGIC
||
1906 get_unaligned_le32(data
+ 4) != len
))
1908 str_count
= get_unaligned_le32(data
+ 8);
1909 lang_count
= get_unaligned_le32(data
+ 12);
1911 /* if one is zero the other must be zero */
1912 if (unlikely(!str_count
!= !lang_count
))
1915 /* Do we have at least as many strings as descriptors need? */
1916 needed_count
= ffs
->strings_count
;
1917 if (unlikely(str_count
< needed_count
))
1921 * If we don't need any strings just return and free all
1924 if (!needed_count
) {
1929 /* Allocate everything in one chunk so there's less maintenance. */
1933 vla_item(d
, struct usb_gadget_strings
*, stringtabs
,
1935 vla_item(d
, struct usb_gadget_strings
, stringtab
, lang_count
);
1936 vla_item(d
, struct usb_string
, strings
,
1937 lang_count
*(needed_count
+1));
1939 char *vlabuf
= kmalloc(vla_group_size(d
), GFP_KERNEL
);
1941 if (unlikely(!vlabuf
)) {
1946 /* Initialize the VLA pointers */
1947 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
1948 t
= vla_ptr(vlabuf
, d
, stringtab
);
1951 *stringtabs
++ = t
++;
1955 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1956 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
1957 t
= vla_ptr(vlabuf
, d
, stringtab
);
1958 s
= vla_ptr(vlabuf
, d
, strings
);
1962 /* For each language */
1966 do { /* lang_count > 0 so we can use do-while */
1967 unsigned needed
= needed_count
;
1969 if (unlikely(len
< 3))
1971 t
->language
= get_unaligned_le16(data
);
1978 /* For each string */
1979 do { /* str_count > 0 so we can use do-while */
1980 size_t length
= strnlen(data
, len
);
1982 if (unlikely(length
== len
))
1986 * User may provide more strings then we need,
1987 * if that's the case we simply ignore the
1990 if (likely(needed
)) {
1992 * s->id will be set while adding
1993 * function to configuration so for
1994 * now just leave garbage here.
2003 } while (--str_count
);
2005 s
->id
= 0; /* terminator */
2009 } while (--lang_count
);
2011 /* Some garbage left? */
2016 ffs
->stringtabs
= stringtabs
;
2017 ffs
->raw_strings
= _data
;
2029 /* Events handling and management *******************************************/
2031 static void __ffs_event_add(struct ffs_data
*ffs
,
2032 enum usb_functionfs_event_type type
)
2034 enum usb_functionfs_event_type rem_type1
, rem_type2
= type
;
2038 * Abort any unhandled setup
2040 * We do not need to worry about some cmpxchg() changing value
2041 * of ffs->setup_state without holding the lock because when
2042 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2043 * the source does nothing.
2045 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
2046 ffs
->setup_state
= FFS_SETUP_CANCELED
;
2049 case FUNCTIONFS_RESUME
:
2050 rem_type2
= FUNCTIONFS_SUSPEND
;
2052 case FUNCTIONFS_SUSPEND
:
2053 case FUNCTIONFS_SETUP
:
2055 /* Discard all similar events */
2058 case FUNCTIONFS_BIND
:
2059 case FUNCTIONFS_UNBIND
:
2060 case FUNCTIONFS_DISABLE
:
2061 case FUNCTIONFS_ENABLE
:
2062 /* Discard everything other then power management. */
2063 rem_type1
= FUNCTIONFS_SUSPEND
;
2064 rem_type2
= FUNCTIONFS_RESUME
;
2073 u8
*ev
= ffs
->ev
.types
, *out
= ev
;
2074 unsigned n
= ffs
->ev
.count
;
2075 for (; n
; --n
, ++ev
)
2076 if ((*ev
== rem_type1
|| *ev
== rem_type2
) == neg
)
2079 pr_vdebug("purging event %d\n", *ev
);
2080 ffs
->ev
.count
= out
- ffs
->ev
.types
;
2083 pr_vdebug("adding event %d\n", type
);
2084 ffs
->ev
.types
[ffs
->ev
.count
++] = type
;
2085 wake_up_locked(&ffs
->ev
.waitq
);
2088 static void ffs_event_add(struct ffs_data
*ffs
,
2089 enum usb_functionfs_event_type type
)
2091 unsigned long flags
;
2092 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2093 __ffs_event_add(ffs
, type
);
2094 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2098 /* Bind/unbind USB function hooks *******************************************/
2100 static int __ffs_func_bind_do_descs(enum ffs_entity_type type
, u8
*valuep
,
2101 struct usb_descriptor_header
*desc
,
2104 struct usb_endpoint_descriptor
*ds
= (void *)desc
;
2105 struct ffs_function
*func
= priv
;
2106 struct ffs_ep
*ffs_ep
;
2109 * If hs_descriptors is not NULL then we are reading hs
2112 const int isHS
= func
->function
.hs_descriptors
!= NULL
;
2115 if (type
!= FFS_DESCRIPTOR
)
2119 func
->function
.hs_descriptors
[(long)valuep
] = desc
;
2121 func
->function
.fs_descriptors
[(long)valuep
] = desc
;
2123 if (!desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
2126 idx
= (ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2127 ffs_ep
= func
->eps
+ idx
;
2129 if (unlikely(ffs_ep
->descs
[isHS
])) {
2130 pr_vdebug("two %sspeed descriptors for EP %d\n",
2131 isHS
? "high" : "full",
2132 ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2135 ffs_ep
->descs
[isHS
] = ds
;
2137 ffs_dump_mem(": Original ep desc", ds
, ds
->bLength
);
2139 ds
->bEndpointAddress
= ffs_ep
->descs
[0]->bEndpointAddress
;
2140 if (!ds
->wMaxPacketSize
)
2141 ds
->wMaxPacketSize
= ffs_ep
->descs
[0]->wMaxPacketSize
;
2143 struct usb_request
*req
;
2146 pr_vdebug("autoconfig\n");
2147 ep
= usb_ep_autoconfig(func
->gadget
, ds
);
2150 ep
->driver_data
= func
->eps
+ idx
;
2152 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
2158 func
->eps_revmap
[ds
->bEndpointAddress
&
2159 USB_ENDPOINT_NUMBER_MASK
] = idx
+ 1;
2161 ffs_dump_mem(": Rewritten ep desc", ds
, ds
->bLength
);
2166 static int __ffs_func_bind_do_nums(enum ffs_entity_type type
, u8
*valuep
,
2167 struct usb_descriptor_header
*desc
,
2170 struct ffs_function
*func
= priv
;
2176 case FFS_DESCRIPTOR
:
2177 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2182 if (func
->interfaces_nums
[idx
] < 0) {
2183 int id
= usb_interface_id(func
->conf
, &func
->function
);
2184 if (unlikely(id
< 0))
2186 func
->interfaces_nums
[idx
] = id
;
2188 newValue
= func
->interfaces_nums
[idx
];
2192 /* String' IDs are allocated when fsf_data is bound to cdev */
2193 newValue
= func
->ffs
->stringtabs
[0]->strings
[*valuep
- 1].id
;
2198 * USB_DT_ENDPOINT are handled in
2199 * __ffs_func_bind_do_descs().
2201 if (desc
->bDescriptorType
== USB_DT_ENDPOINT
)
2204 idx
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2205 if (unlikely(!func
->eps
[idx
].ep
))
2209 struct usb_endpoint_descriptor
**descs
;
2210 descs
= func
->eps
[idx
].descs
;
2211 newValue
= descs
[descs
[0] ? 0 : 1]->bEndpointAddress
;
2216 pr_vdebug("%02x -> %02x\n", *valuep
, newValue
);
2221 static int ffs_func_bind(struct usb_configuration
*c
,
2222 struct usb_function
*f
)
2224 struct ffs_function
*func
= ffs_func_from_usb(f
);
2225 struct ffs_data
*ffs
= func
->ffs
;
2227 const int full
= !!func
->ffs
->fs_descs_count
;
2228 const int high
= gadget_is_dualspeed(func
->gadget
) &&
2229 func
->ffs
->hs_descs_count
;
2233 /* Make it a single chunk, less management later on */
2235 vla_item_with_sz(d
, struct ffs_ep
, eps
, ffs
->eps_count
);
2236 vla_item_with_sz(d
, struct usb_descriptor_header
*, fs_descs
,
2237 full
? ffs
->fs_descs_count
+ 1 : 0);
2238 vla_item_with_sz(d
, struct usb_descriptor_header
*, hs_descs
,
2239 high
? ffs
->hs_descs_count
+ 1 : 0);
2240 vla_item_with_sz(d
, short, inums
, ffs
->interfaces_count
);
2241 vla_item_with_sz(d
, char, raw_descs
,
2242 high
? ffs
->raw_descs_length
: ffs
->raw_fs_descs_length
);
2247 /* Only high speed but not supported by gadget? */
2248 if (unlikely(!(full
| high
)))
2251 /* Allocate a single chunk, less management later on */
2252 vlabuf
= kmalloc(vla_group_size(d
), GFP_KERNEL
);
2253 if (unlikely(!vlabuf
))
2257 memset(vla_ptr(vlabuf
, d
, eps
), 0, d_eps__sz
);
2258 memcpy(vla_ptr(vlabuf
, d
, raw_descs
), ffs
->raw_descs
+ 16,
2260 memset(vla_ptr(vlabuf
, d
, inums
), 0xff, d_inums__sz
);
2261 for (ret
= ffs
->eps_count
; ret
; --ret
) {
2264 ptr
= vla_ptr(vlabuf
, d
, eps
);
2269 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2271 func
->eps
= vla_ptr(vlabuf
, d
, eps
);
2272 func
->interfaces_nums
= vla_ptr(vlabuf
, d
, inums
);
2275 * Go through all the endpoint descriptors and allocate
2276 * endpoints first, so that later we can rewrite the endpoint
2277 * numbers without worrying that it may be described later on.
2280 func
->function
.fs_descriptors
= vla_ptr(vlabuf
, d
, fs_descs
);
2281 ret
= ffs_do_descs(ffs
->fs_descs_count
,
2282 vla_ptr(vlabuf
, d
, raw_descs
),
2284 __ffs_func_bind_do_descs
, func
);
2285 if (unlikely(ret
< 0))
2292 func
->function
.hs_descriptors
= vla_ptr(vlabuf
, d
, hs_descs
);
2293 ret
= ffs_do_descs(ffs
->hs_descs_count
,
2294 vla_ptr(vlabuf
, d
, raw_descs
) + ret
,
2295 d_raw_descs__sz
- ret
,
2296 __ffs_func_bind_do_descs
, func
);
2297 if (unlikely(ret
< 0))
2302 * Now handle interface numbers allocation and interface and
2303 * endpoint numbers rewriting. We can do that in one go
2306 ret
= ffs_do_descs(ffs
->fs_descs_count
+
2307 (high
? ffs
->hs_descs_count
: 0),
2308 vla_ptr(vlabuf
, d
, raw_descs
), d_raw_descs__sz
,
2309 __ffs_func_bind_do_nums
, func
);
2310 if (unlikely(ret
< 0))
2313 /* And we're done */
2314 ffs_event_add(ffs
, FUNCTIONFS_BIND
);
2318 /* XXX Do we need to release all claimed endpoints here? */
2323 /* Other USB function hooks *************************************************/
2325 static void ffs_func_unbind(struct usb_configuration
*c
,
2326 struct usb_function
*f
)
2328 struct ffs_function
*func
= ffs_func_from_usb(f
);
2329 struct ffs_data
*ffs
= func
->ffs
;
2333 if (ffs
->func
== func
) {
2334 ffs_func_eps_disable(func
);
2338 ffs_event_add(ffs
, FUNCTIONFS_UNBIND
);
2340 ffs_func_free(func
);
2343 static int ffs_func_set_alt(struct usb_function
*f
,
2344 unsigned interface
, unsigned alt
)
2346 struct ffs_function
*func
= ffs_func_from_usb(f
);
2347 struct ffs_data
*ffs
= func
->ffs
;
2350 if (alt
!= (unsigned)-1) {
2351 intf
= ffs_func_revmap_intf(func
, interface
);
2352 if (unlikely(intf
< 0))
2357 ffs_func_eps_disable(ffs
->func
);
2359 if (ffs
->state
!= FFS_ACTIVE
)
2362 if (alt
== (unsigned)-1) {
2364 ffs_event_add(ffs
, FUNCTIONFS_DISABLE
);
2369 ret
= ffs_func_eps_enable(func
);
2370 if (likely(ret
>= 0))
2371 ffs_event_add(ffs
, FUNCTIONFS_ENABLE
);
2375 static void ffs_func_disable(struct usb_function
*f
)
2377 ffs_func_set_alt(f
, 0, (unsigned)-1);
2380 static int ffs_func_setup(struct usb_function
*f
,
2381 const struct usb_ctrlrequest
*creq
)
2383 struct ffs_function
*func
= ffs_func_from_usb(f
);
2384 struct ffs_data
*ffs
= func
->ffs
;
2385 unsigned long flags
;
2390 pr_vdebug("creq->bRequestType = %02x\n", creq
->bRequestType
);
2391 pr_vdebug("creq->bRequest = %02x\n", creq
->bRequest
);
2392 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq
->wValue
));
2393 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq
->wIndex
));
2394 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq
->wLength
));
2397 * Most requests directed to interface go through here
2398 * (notable exceptions are set/get interface) so we need to
2399 * handle them. All other either handled by composite or
2400 * passed to usb_configuration->setup() (if one is set). No
2401 * matter, we will handle requests directed to endpoint here
2402 * as well (as it's straightforward) but what to do with any
2405 if (ffs
->state
!= FFS_ACTIVE
)
2408 switch (creq
->bRequestType
& USB_RECIP_MASK
) {
2409 case USB_RECIP_INTERFACE
:
2410 ret
= ffs_func_revmap_intf(func
, le16_to_cpu(creq
->wIndex
));
2411 if (unlikely(ret
< 0))
2415 case USB_RECIP_ENDPOINT
:
2416 ret
= ffs_func_revmap_ep(func
, le16_to_cpu(creq
->wIndex
));
2417 if (unlikely(ret
< 0))
2425 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2426 ffs
->ev
.setup
= *creq
;
2427 ffs
->ev
.setup
.wIndex
= cpu_to_le16(ret
);
2428 __ffs_event_add(ffs
, FUNCTIONFS_SETUP
);
2429 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2434 static void ffs_func_suspend(struct usb_function
*f
)
2437 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_SUSPEND
);
2440 static void ffs_func_resume(struct usb_function
*f
)
2443 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_RESUME
);
2447 /* Endpoint and interface numbers reverse mapping ***************************/
2449 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
)
2451 num
= func
->eps_revmap
[num
& USB_ENDPOINT_NUMBER_MASK
];
2452 return num
? num
: -EDOM
;
2455 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
)
2457 short *nums
= func
->interfaces_nums
;
2458 unsigned count
= func
->ffs
->interfaces_count
;
2460 for (; count
; --count
, ++nums
) {
2461 if (*nums
>= 0 && *nums
== intf
)
2462 return nums
- func
->interfaces_nums
;
2469 /* Misc helper functions ****************************************************/
2471 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
2474 ? likely(mutex_trylock(mutex
)) ? 0 : -EAGAIN
2475 : mutex_lock_interruptible(mutex
);
2478 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
2485 data
= kmalloc(len
, GFP_KERNEL
);
2486 if (unlikely(!data
))
2487 return ERR_PTR(-ENOMEM
);
2489 if (unlikely(__copy_from_user(data
, buf
, len
))) {
2491 return ERR_PTR(-EFAULT
);
2494 pr_vdebug("Buffer from user space:\n");
2495 ffs_dump_mem("", data
, len
);