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 <linux/module.h>
26 #include <asm/unaligned.h>
28 #include <linux/usb/composite.h>
29 #include <linux/usb/functionfs.h>
34 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
36 /* Variable Length Array Macros **********************************************/
37 #define vla_group(groupname) size_t groupname##__next = 0
38 #define vla_group_size(groupname) groupname##__next
40 #define vla_item(groupname, type, name, n) \
41 size_t groupname##_##name##__offset = ({ \
42 size_t align_mask = __alignof__(type) - 1; \
43 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
44 size_t size = (n) * sizeof(type); \
45 groupname##__next = offset + size; \
49 #define vla_item_with_sz(groupname, type, name, n) \
50 size_t groupname##_##name##__sz = (n) * sizeof(type); \
51 size_t groupname##_##name##__offset = ({ \
52 size_t align_mask = __alignof__(type) - 1; \
53 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
54 size_t size = groupname##_##name##__sz; \
55 groupname##__next = offset + size; \
59 #define vla_ptr(ptr, groupname, name) \
60 ((void *) ((char *)ptr + groupname##_##name##__offset))
62 /* Reference counter handling */
63 static void ffs_data_get(struct ffs_data
*ffs
);
64 static void ffs_data_put(struct ffs_data
*ffs
);
65 /* Creates new ffs_data object. */
66 static struct ffs_data
*__must_check
ffs_data_new(void) __attribute__((malloc
));
68 /* Opened counter handling. */
69 static void ffs_data_opened(struct ffs_data
*ffs
);
70 static void ffs_data_closed(struct ffs_data
*ffs
);
72 /* Called with ffs->mutex held; take over ownership of data. */
73 static int __must_check
74 __ffs_data_got_descs(struct ffs_data
*ffs
, char *data
, size_t len
);
75 static int __must_check
76 __ffs_data_got_strings(struct ffs_data
*ffs
, char *data
, size_t len
);
79 /* The function structure ***************************************************/
84 struct usb_configuration
*conf
;
85 struct usb_gadget
*gadget
;
90 short *interfaces_nums
;
92 struct usb_function function
;
96 static struct ffs_function
*ffs_func_from_usb(struct usb_function
*f
)
98 return container_of(f
, struct ffs_function
, function
);
102 static void ffs_func_eps_disable(struct ffs_function
*func
);
103 static int __must_check
ffs_func_eps_enable(struct ffs_function
*func
);
105 static int ffs_func_bind(struct usb_configuration
*,
106 struct usb_function
*);
107 static int ffs_func_set_alt(struct usb_function
*, unsigned, unsigned);
108 static void ffs_func_disable(struct usb_function
*);
109 static int ffs_func_setup(struct usb_function
*,
110 const struct usb_ctrlrequest
*);
111 static void ffs_func_suspend(struct usb_function
*);
112 static void ffs_func_resume(struct usb_function
*);
115 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
);
116 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
);
119 /* The endpoints structures *************************************************/
122 struct usb_ep
*ep
; /* P: ffs->eps_lock */
123 struct usb_request
*req
; /* P: epfile->mutex */
125 /* [0]: full speed, [1]: high speed */
126 struct usb_endpoint_descriptor
*descs
[2];
130 int status
; /* P: epfile->mutex */
134 /* Protects ep->ep and ep->req. */
136 wait_queue_head_t wait
;
138 struct ffs_data
*ffs
;
139 struct ffs_ep
*ep
; /* P: ffs->eps_lock */
141 struct dentry
*dentry
;
145 unsigned char in
; /* P: ffs->eps_lock */
146 unsigned char isoc
; /* P: ffs->eps_lock */
151 static int __must_check
ffs_epfiles_create(struct ffs_data
*ffs
);
152 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
);
154 static struct inode
*__must_check
155 ffs_sb_create_file(struct super_block
*sb
, const char *name
, void *data
,
156 const struct file_operations
*fops
,
157 struct dentry
**dentry_p
);
159 /* Devices management *******************************************************/
161 DEFINE_MUTEX(ffs_lock
);
162 EXPORT_SYMBOL(ffs_lock
);
164 static struct ffs_dev
*ffs_find_dev(const char *name
);
165 static int _ffs_name_dev(struct ffs_dev
*dev
, const char *name
);
166 static void *ffs_acquire_dev(const char *dev_name
);
167 static void ffs_release_dev(struct ffs_data
*ffs_data
);
168 static int ffs_ready(struct ffs_data
*ffs
);
169 static void ffs_closed(struct ffs_data
*ffs
);
171 /* Misc helper functions ****************************************************/
173 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
174 __attribute__((warn_unused_result
, nonnull
));
175 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
176 __attribute__((warn_unused_result
, nonnull
));
179 /* Control file aka ep0 *****************************************************/
181 static void ffs_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
183 struct ffs_data
*ffs
= req
->context
;
185 complete_all(&ffs
->ep0req_completion
);
188 static int __ffs_ep0_queue_wait(struct ffs_data
*ffs
, char *data
, size_t len
)
190 struct usb_request
*req
= ffs
->ep0req
;
193 req
->zero
= len
< le16_to_cpu(ffs
->ev
.setup
.wLength
);
195 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
201 * UDC layer requires to provide a buffer even for ZLP, but should
202 * not use it at all. Let's provide some poisoned pointer to catch
203 * possible bug in the driver.
205 if (req
->buf
== NULL
)
206 req
->buf
= (void *)0xDEADBABE;
208 reinit_completion(&ffs
->ep0req_completion
);
210 ret
= usb_ep_queue(ffs
->gadget
->ep0
, req
, GFP_ATOMIC
);
211 if (unlikely(ret
< 0))
214 ret
= wait_for_completion_interruptible(&ffs
->ep0req_completion
);
216 usb_ep_dequeue(ffs
->gadget
->ep0
, req
);
220 ffs
->setup_state
= FFS_NO_SETUP
;
221 return ffs
->ep0req_status
;
224 static int __ffs_ep0_stall(struct ffs_data
*ffs
)
226 if (ffs
->ev
.can_stall
) {
227 pr_vdebug("ep0 stall\n");
228 usb_ep_set_halt(ffs
->gadget
->ep0
);
229 ffs
->setup_state
= FFS_NO_SETUP
;
232 pr_debug("bogus ep0 stall!\n");
237 static ssize_t
ffs_ep0_write(struct file
*file
, const char __user
*buf
,
238 size_t len
, loff_t
*ptr
)
240 struct ffs_data
*ffs
= file
->private_data
;
246 /* Fast check if setup was canceled */
247 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
251 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
252 if (unlikely(ret
< 0))
256 switch (ffs
->state
) {
257 case FFS_READ_DESCRIPTORS
:
258 case FFS_READ_STRINGS
:
260 if (unlikely(len
< 16)) {
265 data
= ffs_prepare_buffer(buf
, len
);
272 if (ffs
->state
== FFS_READ_DESCRIPTORS
) {
273 pr_info("read descriptors\n");
274 ret
= __ffs_data_got_descs(ffs
, data
, len
);
275 if (unlikely(ret
< 0))
278 ffs
->state
= FFS_READ_STRINGS
;
281 pr_info("read strings\n");
282 ret
= __ffs_data_got_strings(ffs
, data
, len
);
283 if (unlikely(ret
< 0))
286 ret
= ffs_epfiles_create(ffs
);
288 ffs
->state
= FFS_CLOSING
;
292 ffs
->state
= FFS_ACTIVE
;
293 mutex_unlock(&ffs
->mutex
);
295 ret
= ffs_ready(ffs
);
296 if (unlikely(ret
< 0)) {
297 ffs
->state
= FFS_CLOSING
;
301 set_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
);
309 * We're called from user space, we can use _irq
310 * rather then _irqsave
312 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
313 switch (FFS_SETUP_STATE(ffs
)) {
314 case FFS_SETUP_CANCELED
:
322 case FFS_SETUP_PENDING
:
326 /* FFS_SETUP_PENDING */
327 if (!(ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
)) {
328 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
329 ret
= __ffs_ep0_stall(ffs
);
333 /* FFS_SETUP_PENDING and not stall */
334 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
336 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
338 data
= ffs_prepare_buffer(buf
, len
);
344 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
347 * We are guaranteed to be still in FFS_ACTIVE state
348 * but the state of setup could have changed from
349 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
350 * to check for that. If that happened we copied data
351 * from user space in vain but it's unlikely.
353 * For sure we are not in FFS_NO_SETUP since this is
354 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
355 * transition can be performed and it's protected by
358 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
361 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
363 /* unlocks spinlock */
364 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
374 mutex_unlock(&ffs
->mutex
);
378 static ssize_t
__ffs_ep0_read_events(struct ffs_data
*ffs
, char __user
*buf
,
382 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
385 struct usb_functionfs_event events
[n
];
388 memset(events
, 0, sizeof events
);
391 events
[i
].type
= ffs
->ev
.types
[i
];
392 if (events
[i
].type
== FUNCTIONFS_SETUP
) {
393 events
[i
].u
.setup
= ffs
->ev
.setup
;
394 ffs
->setup_state
= FFS_SETUP_PENDING
;
398 if (n
< ffs
->ev
.count
) {
400 memmove(ffs
->ev
.types
, ffs
->ev
.types
+ n
,
401 ffs
->ev
.count
* sizeof *ffs
->ev
.types
);
406 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
407 mutex_unlock(&ffs
->mutex
);
409 return unlikely(__copy_to_user(buf
, events
, sizeof events
))
410 ? -EFAULT
: sizeof events
;
413 static ssize_t
ffs_ep0_read(struct file
*file
, char __user
*buf
,
414 size_t len
, loff_t
*ptr
)
416 struct ffs_data
*ffs
= file
->private_data
;
423 /* Fast check if setup was canceled */
424 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
)
428 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
429 if (unlikely(ret
< 0))
433 if (ffs
->state
!= FFS_ACTIVE
) {
439 * We're called from user space, we can use _irq rather then
442 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
444 switch (FFS_SETUP_STATE(ffs
)) {
445 case FFS_SETUP_CANCELED
:
450 n
= len
/ sizeof(struct usb_functionfs_event
);
456 if ((file
->f_flags
& O_NONBLOCK
) && !ffs
->ev
.count
) {
461 if (wait_event_interruptible_exclusive_locked_irq(ffs
->ev
.waitq
,
467 return __ffs_ep0_read_events(ffs
, buf
,
468 min(n
, (size_t)ffs
->ev
.count
));
470 case FFS_SETUP_PENDING
:
471 if (ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
) {
472 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
473 ret
= __ffs_ep0_stall(ffs
);
477 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
479 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
482 data
= kmalloc(len
, GFP_KERNEL
);
483 if (unlikely(!data
)) {
489 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
491 /* See ffs_ep0_write() */
492 if (FFS_SETUP_STATE(ffs
) == FFS_SETUP_CANCELED
) {
497 /* unlocks spinlock */
498 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
499 if (likely(ret
> 0) && unlikely(__copy_to_user(buf
, data
, len
)))
508 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
510 mutex_unlock(&ffs
->mutex
);
515 static int ffs_ep0_open(struct inode
*inode
, struct file
*file
)
517 struct ffs_data
*ffs
= inode
->i_private
;
521 if (unlikely(ffs
->state
== FFS_CLOSING
))
524 file
->private_data
= ffs
;
525 ffs_data_opened(ffs
);
530 static int ffs_ep0_release(struct inode
*inode
, struct file
*file
)
532 struct ffs_data
*ffs
= file
->private_data
;
536 ffs_data_closed(ffs
);
541 static long ffs_ep0_ioctl(struct file
*file
, unsigned code
, unsigned long value
)
543 struct ffs_data
*ffs
= file
->private_data
;
544 struct usb_gadget
*gadget
= ffs
->gadget
;
549 if (code
== FUNCTIONFS_INTERFACE_REVMAP
) {
550 struct ffs_function
*func
= ffs
->func
;
551 ret
= func
? ffs_func_revmap_intf(func
, value
) : -ENODEV
;
552 } else if (gadget
&& gadget
->ops
->ioctl
) {
553 ret
= gadget
->ops
->ioctl(gadget
, code
, value
);
561 static const struct file_operations ffs_ep0_operations
= {
564 .open
= ffs_ep0_open
,
565 .write
= ffs_ep0_write
,
566 .read
= ffs_ep0_read
,
567 .release
= ffs_ep0_release
,
568 .unlocked_ioctl
= ffs_ep0_ioctl
,
572 /* "Normal" endpoints operations ********************************************/
574 static void ffs_epfile_io_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
577 if (likely(req
->context
)) {
578 struct ffs_ep
*ep
= _ep
->driver_data
;
579 ep
->status
= req
->status
? req
->status
: req
->actual
;
580 complete(req
->context
);
584 static ssize_t
ffs_epfile_io(struct file
*file
,
585 char __user
*buf
, size_t len
, int read
)
587 struct ffs_epfile
*epfile
= file
->private_data
;
588 struct usb_gadget
*gadget
= epfile
->ffs
->gadget
;
591 ssize_t ret
, data_len
;
594 /* Are we still active? */
595 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
)) {
600 /* Wait for endpoint to be enabled */
603 if (file
->f_flags
& O_NONBLOCK
) {
608 ret
= wait_event_interruptible(epfile
->wait
, (ep
= epfile
->ep
));
616 halt
= !read
== !epfile
->in
;
617 if (halt
&& epfile
->isoc
) {
622 /* Allocate & copy */
625 * Controller may require buffer size to be aligned to
626 * maxpacketsize of an out endpoint.
628 data_len
= read
? usb_ep_align_maybe(gadget
, ep
->ep
, len
) : len
;
630 data
= kmalloc(data_len
, GFP_KERNEL
);
634 if (!read
&& unlikely(copy_from_user(data
, buf
, len
))) {
640 /* We will be using request */
641 ret
= ffs_mutex_lock(&epfile
->mutex
, file
->f_flags
& O_NONBLOCK
);
645 spin_lock_irq(&epfile
->ffs
->eps_lock
);
647 if (epfile
->ep
!= ep
) {
648 /* In the meantime, endpoint got disabled or changed. */
650 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
653 if (likely(epfile
->ep
== ep
) && !WARN_ON(!ep
->ep
))
654 usb_ep_set_halt(ep
->ep
);
655 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
658 /* Fire the request */
659 DECLARE_COMPLETION_ONSTACK(done
);
661 struct usb_request
*req
= ep
->req
;
662 req
->context
= &done
;
663 req
->complete
= ffs_epfile_io_complete
;
665 req
->length
= data_len
;
667 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
669 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
671 if (unlikely(ret
< 0)) {
673 } else if (unlikely(wait_for_completion_interruptible(&done
))) {
675 usb_ep_dequeue(ep
->ep
, req
);
678 * XXX We may end up silently droping data here.
679 * Since data_len (i.e. req->length) may be bigger
680 * than len (after being rounded up to maxpacketsize),
681 * we may end up with more data then user space has
685 if (read
&& ret
> 0 &&
686 unlikely(copy_to_user(buf
, data
,
687 min_t(size_t, ret
, len
))))
692 mutex_unlock(&epfile
->mutex
);
699 ffs_epfile_write(struct file
*file
, const char __user
*buf
, size_t len
,
704 return ffs_epfile_io(file
, (char __user
*)buf
, len
, 0);
708 ffs_epfile_read(struct file
*file
, char __user
*buf
, size_t len
, loff_t
*ptr
)
712 return ffs_epfile_io(file
, buf
, len
, 1);
716 ffs_epfile_open(struct inode
*inode
, struct file
*file
)
718 struct ffs_epfile
*epfile
= inode
->i_private
;
722 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
725 file
->private_data
= epfile
;
726 ffs_data_opened(epfile
->ffs
);
732 ffs_epfile_release(struct inode
*inode
, struct file
*file
)
734 struct ffs_epfile
*epfile
= inode
->i_private
;
738 ffs_data_closed(epfile
->ffs
);
743 static long ffs_epfile_ioctl(struct file
*file
, unsigned code
,
746 struct ffs_epfile
*epfile
= file
->private_data
;
751 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
754 spin_lock_irq(&epfile
->ffs
->eps_lock
);
755 if (likely(epfile
->ep
)) {
757 case FUNCTIONFS_FIFO_STATUS
:
758 ret
= usb_ep_fifo_status(epfile
->ep
->ep
);
760 case FUNCTIONFS_FIFO_FLUSH
:
761 usb_ep_fifo_flush(epfile
->ep
->ep
);
764 case FUNCTIONFS_CLEAR_HALT
:
765 ret
= usb_ep_clear_halt(epfile
->ep
->ep
);
767 case FUNCTIONFS_ENDPOINT_REVMAP
:
768 ret
= epfile
->ep
->num
;
776 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
781 static const struct file_operations ffs_epfile_operations
= {
784 .open
= ffs_epfile_open
,
785 .write
= ffs_epfile_write
,
786 .read
= ffs_epfile_read
,
787 .release
= ffs_epfile_release
,
788 .unlocked_ioctl
= ffs_epfile_ioctl
,
792 /* File system and super block operations ***********************************/
795 * Mounting the file system creates a controller file, used first for
796 * function configuration then later for event monitoring.
799 static struct inode
*__must_check
800 ffs_sb_make_inode(struct super_block
*sb
, void *data
,
801 const struct file_operations
*fops
,
802 const struct inode_operations
*iops
,
803 struct ffs_file_perms
*perms
)
809 inode
= new_inode(sb
);
812 struct timespec current_time
= CURRENT_TIME
;
814 inode
->i_ino
= get_next_ino();
815 inode
->i_mode
= perms
->mode
;
816 inode
->i_uid
= perms
->uid
;
817 inode
->i_gid
= perms
->gid
;
818 inode
->i_atime
= current_time
;
819 inode
->i_mtime
= current_time
;
820 inode
->i_ctime
= current_time
;
821 inode
->i_private
= data
;
831 /* Create "regular" file */
832 static struct inode
*ffs_sb_create_file(struct super_block
*sb
,
833 const char *name
, void *data
,
834 const struct file_operations
*fops
,
835 struct dentry
**dentry_p
)
837 struct ffs_data
*ffs
= sb
->s_fs_info
;
838 struct dentry
*dentry
;
843 dentry
= d_alloc_name(sb
->s_root
, name
);
844 if (unlikely(!dentry
))
847 inode
= ffs_sb_make_inode(sb
, data
, fops
, NULL
, &ffs
->file_perms
);
848 if (unlikely(!inode
)) {
853 d_add(dentry
, inode
);
861 static const struct super_operations ffs_sb_operations
= {
862 .statfs
= simple_statfs
,
863 .drop_inode
= generic_delete_inode
,
866 struct ffs_sb_fill_data
{
867 struct ffs_file_perms perms
;
869 const char *dev_name
;
870 struct ffs_data
*ffs_data
;
873 static int ffs_sb_fill(struct super_block
*sb
, void *_data
, int silent
)
875 struct ffs_sb_fill_data
*data
= _data
;
877 struct ffs_data
*ffs
= data
->ffs_data
;
882 data
->ffs_data
= NULL
;
884 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
885 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
886 sb
->s_magic
= FUNCTIONFS_MAGIC
;
887 sb
->s_op
= &ffs_sb_operations
;
891 data
->perms
.mode
= data
->root_mode
;
892 inode
= ffs_sb_make_inode(sb
, NULL
,
893 &simple_dir_operations
,
894 &simple_dir_inode_operations
,
896 sb
->s_root
= d_make_root(inode
);
897 if (unlikely(!sb
->s_root
))
901 if (unlikely(!ffs_sb_create_file(sb
, "ep0", ffs
,
902 &ffs_ep0_operations
, NULL
)))
908 static int ffs_fs_parse_opts(struct ffs_sb_fill_data
*data
, char *opts
)
920 comma
= strchr(opts
, ',');
925 eq
= strchr(opts
, '=');
927 pr_err("'=' missing in %s\n", opts
);
933 if (kstrtoul(eq
+ 1, 0, &value
)) {
934 pr_err("%s: invalid value: %s\n", opts
, eq
+ 1);
938 /* Interpret option */
941 if (!memcmp(opts
, "rmode", 5))
942 data
->root_mode
= (value
& 0555) | S_IFDIR
;
943 else if (!memcmp(opts
, "fmode", 5))
944 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
950 if (!memcmp(opts
, "mode", 4)) {
951 data
->root_mode
= (value
& 0555) | S_IFDIR
;
952 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
959 if (!memcmp(opts
, "uid", 3)) {
960 data
->perms
.uid
= make_kuid(current_user_ns(), value
);
961 if (!uid_valid(data
->perms
.uid
)) {
962 pr_err("%s: unmapped value: %lu\n", opts
, value
);
965 } else if (!memcmp(opts
, "gid", 3)) {
966 data
->perms
.gid
= make_kgid(current_user_ns(), value
);
967 if (!gid_valid(data
->perms
.gid
)) {
968 pr_err("%s: unmapped value: %lu\n", opts
, value
);
978 pr_err("%s: invalid option\n", opts
);
991 /* "mount -t functionfs dev_name /dev/function" ends up here */
993 static struct dentry
*
994 ffs_fs_mount(struct file_system_type
*t
, int flags
,
995 const char *dev_name
, void *opts
)
997 struct ffs_sb_fill_data data
= {
999 .mode
= S_IFREG
| 0600,
1000 .uid
= GLOBAL_ROOT_UID
,
1001 .gid
= GLOBAL_ROOT_GID
,
1003 .root_mode
= S_IFDIR
| 0500,
1008 struct ffs_data
*ffs
;
1012 ret
= ffs_fs_parse_opts(&data
, opts
);
1013 if (unlikely(ret
< 0))
1014 return ERR_PTR(ret
);
1016 ffs
= ffs_data_new();
1018 return ERR_PTR(-ENOMEM
);
1019 ffs
->file_perms
= data
.perms
;
1021 ffs
->dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
1022 if (unlikely(!ffs
->dev_name
)) {
1024 return ERR_PTR(-ENOMEM
);
1027 ffs_dev
= ffs_acquire_dev(dev_name
);
1028 if (IS_ERR(ffs_dev
)) {
1030 return ERR_CAST(ffs_dev
);
1032 ffs
->private_data
= ffs_dev
;
1033 data
.ffs_data
= ffs
;
1035 rv
= mount_nodev(t
, flags
, &data
, ffs_sb_fill
);
1036 if (IS_ERR(rv
) && data
.ffs_data
) {
1037 ffs_release_dev(data
.ffs_data
);
1038 ffs_data_put(data
.ffs_data
);
1044 ffs_fs_kill_sb(struct super_block
*sb
)
1048 kill_litter_super(sb
);
1049 if (sb
->s_fs_info
) {
1050 ffs_release_dev(sb
->s_fs_info
);
1051 ffs_data_put(sb
->s_fs_info
);
1055 static struct file_system_type ffs_fs_type
= {
1056 .owner
= THIS_MODULE
,
1057 .name
= "functionfs",
1058 .mount
= ffs_fs_mount
,
1059 .kill_sb
= ffs_fs_kill_sb
,
1061 MODULE_ALIAS_FS("functionfs");
1064 /* Driver's main init/cleanup functions *************************************/
1066 static int functionfs_init(void)
1072 ret
= register_filesystem(&ffs_fs_type
);
1074 pr_info("file system registered\n");
1076 pr_err("failed registering file system (%d)\n", ret
);
1081 static void functionfs_cleanup(void)
1085 pr_info("unloading\n");
1086 unregister_filesystem(&ffs_fs_type
);
1090 /* ffs_data and ffs_function construction and destruction code **************/
1092 static void ffs_data_clear(struct ffs_data
*ffs
);
1093 static void ffs_data_reset(struct ffs_data
*ffs
);
1095 static void ffs_data_get(struct ffs_data
*ffs
)
1099 atomic_inc(&ffs
->ref
);
1102 static void ffs_data_opened(struct ffs_data
*ffs
)
1106 atomic_inc(&ffs
->ref
);
1107 atomic_inc(&ffs
->opened
);
1110 static void ffs_data_put(struct ffs_data
*ffs
)
1114 if (unlikely(atomic_dec_and_test(&ffs
->ref
))) {
1115 pr_info("%s(): freeing\n", __func__
);
1116 ffs_data_clear(ffs
);
1117 BUG_ON(waitqueue_active(&ffs
->ev
.waitq
) ||
1118 waitqueue_active(&ffs
->ep0req_completion
.wait
));
1119 kfree(ffs
->dev_name
);
1124 static void ffs_data_closed(struct ffs_data
*ffs
)
1128 if (atomic_dec_and_test(&ffs
->opened
)) {
1129 ffs
->state
= FFS_CLOSING
;
1130 ffs_data_reset(ffs
);
1136 static struct ffs_data
*ffs_data_new(void)
1138 struct ffs_data
*ffs
= kzalloc(sizeof *ffs
, GFP_KERNEL
);
1144 atomic_set(&ffs
->ref
, 1);
1145 atomic_set(&ffs
->opened
, 0);
1146 ffs
->state
= FFS_READ_DESCRIPTORS
;
1147 mutex_init(&ffs
->mutex
);
1148 spin_lock_init(&ffs
->eps_lock
);
1149 init_waitqueue_head(&ffs
->ev
.waitq
);
1150 init_completion(&ffs
->ep0req_completion
);
1152 /* XXX REVISIT need to update it in some places, or do we? */
1153 ffs
->ev
.can_stall
= 1;
1158 static void ffs_data_clear(struct ffs_data
*ffs
)
1162 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
))
1165 BUG_ON(ffs
->gadget
);
1168 ffs_epfiles_destroy(ffs
->epfiles
, ffs
->eps_count
);
1170 kfree(ffs
->raw_descs
);
1171 kfree(ffs
->raw_strings
);
1172 kfree(ffs
->stringtabs
);
1175 static void ffs_data_reset(struct ffs_data
*ffs
)
1179 ffs_data_clear(ffs
);
1181 ffs
->epfiles
= NULL
;
1182 ffs
->raw_descs
= NULL
;
1183 ffs
->raw_strings
= NULL
;
1184 ffs
->stringtabs
= NULL
;
1186 ffs
->raw_descs_length
= 0;
1187 ffs
->raw_fs_descs_length
= 0;
1188 ffs
->fs_descs_count
= 0;
1189 ffs
->hs_descs_count
= 0;
1191 ffs
->strings_count
= 0;
1192 ffs
->interfaces_count
= 0;
1197 ffs
->state
= FFS_READ_DESCRIPTORS
;
1198 ffs
->setup_state
= FFS_NO_SETUP
;
1203 static int functionfs_bind(struct ffs_data
*ffs
, struct usb_composite_dev
*cdev
)
1205 struct usb_gadget_strings
**lang
;
1210 if (WARN_ON(ffs
->state
!= FFS_ACTIVE
1211 || test_and_set_bit(FFS_FL_BOUND
, &ffs
->flags
)))
1214 first_id
= usb_string_ids_n(cdev
, ffs
->strings_count
);
1215 if (unlikely(first_id
< 0))
1218 ffs
->ep0req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
1219 if (unlikely(!ffs
->ep0req
))
1221 ffs
->ep0req
->complete
= ffs_ep0_complete
;
1222 ffs
->ep0req
->context
= ffs
;
1224 lang
= ffs
->stringtabs
;
1225 for (lang
= ffs
->stringtabs
; *lang
; ++lang
) {
1226 struct usb_string
*str
= (*lang
)->strings
;
1228 for (; str
->s
; ++id
, ++str
)
1232 ffs
->gadget
= cdev
->gadget
;
1237 static void functionfs_unbind(struct ffs_data
*ffs
)
1241 if (!WARN_ON(!ffs
->gadget
)) {
1242 usb_ep_free_request(ffs
->gadget
->ep0
, ffs
->ep0req
);
1245 clear_bit(FFS_FL_BOUND
, &ffs
->flags
);
1250 static int ffs_epfiles_create(struct ffs_data
*ffs
)
1252 struct ffs_epfile
*epfile
, *epfiles
;
1257 count
= ffs
->eps_count
;
1258 epfiles
= kcalloc(count
, sizeof(*epfiles
), GFP_KERNEL
);
1263 for (i
= 1; i
<= count
; ++i
, ++epfile
) {
1265 mutex_init(&epfile
->mutex
);
1266 init_waitqueue_head(&epfile
->wait
);
1267 sprintf(epfiles
->name
, "ep%u", i
);
1268 if (!unlikely(ffs_sb_create_file(ffs
->sb
, epfiles
->name
, epfile
,
1269 &ffs_epfile_operations
,
1270 &epfile
->dentry
))) {
1271 ffs_epfiles_destroy(epfiles
, i
- 1);
1276 ffs
->epfiles
= epfiles
;
1280 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
)
1282 struct ffs_epfile
*epfile
= epfiles
;
1286 for (; count
; --count
, ++epfile
) {
1287 BUG_ON(mutex_is_locked(&epfile
->mutex
) ||
1288 waitqueue_active(&epfile
->wait
));
1289 if (epfile
->dentry
) {
1290 d_delete(epfile
->dentry
);
1291 dput(epfile
->dentry
);
1292 epfile
->dentry
= NULL
;
1300 static void ffs_func_eps_disable(struct ffs_function
*func
)
1302 struct ffs_ep
*ep
= func
->eps
;
1303 struct ffs_epfile
*epfile
= func
->ffs
->epfiles
;
1304 unsigned count
= func
->ffs
->eps_count
;
1305 unsigned long flags
;
1307 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1309 /* pending requests get nuked */
1311 usb_ep_disable(ep
->ep
);
1317 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1320 static int ffs_func_eps_enable(struct ffs_function
*func
)
1322 struct ffs_data
*ffs
= func
->ffs
;
1323 struct ffs_ep
*ep
= func
->eps
;
1324 struct ffs_epfile
*epfile
= ffs
->epfiles
;
1325 unsigned count
= ffs
->eps_count
;
1326 unsigned long flags
;
1329 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1331 struct usb_endpoint_descriptor
*ds
;
1332 ds
= ep
->descs
[ep
->descs
[1] ? 1 : 0];
1334 ep
->ep
->driver_data
= ep
;
1336 ret
= usb_ep_enable(ep
->ep
);
1339 epfile
->in
= usb_endpoint_dir_in(ds
);
1340 epfile
->isoc
= usb_endpoint_xfer_isoc(ds
);
1345 wake_up(&epfile
->wait
);
1350 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1356 /* Parsing and building descriptors and strings *****************************/
1359 * This validates if data pointed by data is a valid USB descriptor as
1360 * well as record how many interfaces, endpoints and strings are
1361 * required by given configuration. Returns address after the
1362 * descriptor or NULL if data is invalid.
1365 enum ffs_entity_type
{
1366 FFS_DESCRIPTOR
, FFS_INTERFACE
, FFS_STRING
, FFS_ENDPOINT
1369 typedef int (*ffs_entity_callback
)(enum ffs_entity_type entity
,
1371 struct usb_descriptor_header
*desc
,
1374 static int __must_check
ffs_do_desc(char *data
, unsigned len
,
1375 ffs_entity_callback entity
, void *priv
)
1377 struct usb_descriptor_header
*_ds
= (void *)data
;
1383 /* At least two bytes are required: length and type */
1385 pr_vdebug("descriptor too short\n");
1389 /* If we have at least as many bytes as the descriptor takes? */
1390 length
= _ds
->bLength
;
1392 pr_vdebug("descriptor longer then available data\n");
1396 #define __entity_check_INTERFACE(val) 1
1397 #define __entity_check_STRING(val) (val)
1398 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1399 #define __entity(type, val) do { \
1400 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1401 if (unlikely(!__entity_check_ ##type(val))) { \
1402 pr_vdebug("invalid entity's value\n"); \
1405 ret = entity(FFS_ ##type, &val, _ds, priv); \
1406 if (unlikely(ret < 0)) { \
1407 pr_debug("entity " #type "(%02x); ret = %d\n", \
1413 /* Parse descriptor depending on type. */
1414 switch (_ds
->bDescriptorType
) {
1418 case USB_DT_DEVICE_QUALIFIER
:
1419 /* function can't have any of those */
1420 pr_vdebug("descriptor reserved for gadget: %d\n",
1421 _ds
->bDescriptorType
);
1424 case USB_DT_INTERFACE
: {
1425 struct usb_interface_descriptor
*ds
= (void *)_ds
;
1426 pr_vdebug("interface descriptor\n");
1427 if (length
!= sizeof *ds
)
1430 __entity(INTERFACE
, ds
->bInterfaceNumber
);
1432 __entity(STRING
, ds
->iInterface
);
1436 case USB_DT_ENDPOINT
: {
1437 struct usb_endpoint_descriptor
*ds
= (void *)_ds
;
1438 pr_vdebug("endpoint descriptor\n");
1439 if (length
!= USB_DT_ENDPOINT_SIZE
&&
1440 length
!= USB_DT_ENDPOINT_AUDIO_SIZE
)
1442 __entity(ENDPOINT
, ds
->bEndpointAddress
);
1447 pr_vdebug("hid descriptor\n");
1448 if (length
!= sizeof(struct hid_descriptor
))
1453 if (length
!= sizeof(struct usb_otg_descriptor
))
1457 case USB_DT_INTERFACE_ASSOCIATION
: {
1458 struct usb_interface_assoc_descriptor
*ds
= (void *)_ds
;
1459 pr_vdebug("interface association descriptor\n");
1460 if (length
!= sizeof *ds
)
1463 __entity(STRING
, ds
->iFunction
);
1467 case USB_DT_OTHER_SPEED_CONFIG
:
1468 case USB_DT_INTERFACE_POWER
:
1470 case USB_DT_SECURITY
:
1471 case USB_DT_CS_RADIO_CONTROL
:
1473 pr_vdebug("unimplemented descriptor: %d\n", _ds
->bDescriptorType
);
1477 /* We should never be here */
1478 pr_vdebug("unknown descriptor: %d\n", _ds
->bDescriptorType
);
1482 pr_vdebug("invalid length: %d (descriptor %d)\n",
1483 _ds
->bLength
, _ds
->bDescriptorType
);
1488 #undef __entity_check_DESCRIPTOR
1489 #undef __entity_check_INTERFACE
1490 #undef __entity_check_STRING
1491 #undef __entity_check_ENDPOINT
1496 static int __must_check
ffs_do_descs(unsigned count
, char *data
, unsigned len
,
1497 ffs_entity_callback entity
, void *priv
)
1499 const unsigned _len
= len
;
1500 unsigned long num
= 0;
1510 /* Record "descriptor" entity */
1511 ret
= entity(FFS_DESCRIPTOR
, (u8
*)num
, (void *)data
, priv
);
1512 if (unlikely(ret
< 0)) {
1513 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1521 ret
= ffs_do_desc(data
, len
, entity
, priv
);
1522 if (unlikely(ret
< 0)) {
1523 pr_debug("%s returns %d\n", __func__
, ret
);
1533 static int __ffs_data_do_entity(enum ffs_entity_type type
,
1534 u8
*valuep
, struct usb_descriptor_header
*desc
,
1537 struct ffs_data
*ffs
= priv
;
1542 case FFS_DESCRIPTOR
:
1547 * Interfaces are indexed from zero so if we
1548 * encountered interface "n" then there are at least
1551 if (*valuep
>= ffs
->interfaces_count
)
1552 ffs
->interfaces_count
= *valuep
+ 1;
1557 * Strings are indexed from 1 (0 is magic ;) reserved
1558 * for languages list or some such)
1560 if (*valuep
> ffs
->strings_count
)
1561 ffs
->strings_count
= *valuep
;
1565 /* Endpoints are indexed from 1 as well. */
1566 if ((*valuep
& USB_ENDPOINT_NUMBER_MASK
) > ffs
->eps_count
)
1567 ffs
->eps_count
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
);
1574 static int __ffs_data_got_descs(struct ffs_data
*ffs
,
1575 char *const _data
, size_t len
)
1577 unsigned fs_count
, hs_count
;
1578 int fs_len
, ret
= -EINVAL
;
1583 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_DESCRIPTORS_MAGIC
||
1584 get_unaligned_le32(data
+ 4) != len
))
1586 fs_count
= get_unaligned_le32(data
+ 8);
1587 hs_count
= get_unaligned_le32(data
+ 12);
1589 if (!fs_count
&& !hs_count
)
1595 if (likely(fs_count
)) {
1596 fs_len
= ffs_do_descs(fs_count
, data
, len
,
1597 __ffs_data_do_entity
, ffs
);
1598 if (unlikely(fs_len
< 0)) {
1609 if (likely(hs_count
)) {
1610 ret
= ffs_do_descs(hs_count
, data
, len
,
1611 __ffs_data_do_entity
, ffs
);
1612 if (unlikely(ret
< 0))
1618 if (unlikely(len
!= ret
))
1621 ffs
->raw_fs_descs_length
= fs_len
;
1622 ffs
->raw_descs_length
= fs_len
+ ret
;
1623 ffs
->raw_descs
= _data
;
1624 ffs
->fs_descs_count
= fs_count
;
1625 ffs
->hs_descs_count
= hs_count
;
1636 static int __ffs_data_got_strings(struct ffs_data
*ffs
,
1637 char *const _data
, size_t len
)
1639 u32 str_count
, needed_count
, lang_count
;
1640 struct usb_gadget_strings
**stringtabs
, *t
;
1641 struct usb_string
*strings
, *s
;
1642 const char *data
= _data
;
1646 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_STRINGS_MAGIC
||
1647 get_unaligned_le32(data
+ 4) != len
))
1649 str_count
= get_unaligned_le32(data
+ 8);
1650 lang_count
= get_unaligned_le32(data
+ 12);
1652 /* if one is zero the other must be zero */
1653 if (unlikely(!str_count
!= !lang_count
))
1656 /* Do we have at least as many strings as descriptors need? */
1657 needed_count
= ffs
->strings_count
;
1658 if (unlikely(str_count
< needed_count
))
1662 * If we don't need any strings just return and free all
1665 if (!needed_count
) {
1670 /* Allocate everything in one chunk so there's less maintenance. */
1674 vla_item(d
, struct usb_gadget_strings
*, stringtabs
,
1676 vla_item(d
, struct usb_gadget_strings
, stringtab
, lang_count
);
1677 vla_item(d
, struct usb_string
, strings
,
1678 lang_count
*(needed_count
+1));
1680 char *vlabuf
= kmalloc(vla_group_size(d
), GFP_KERNEL
);
1682 if (unlikely(!vlabuf
)) {
1687 /* Initialize the VLA pointers */
1688 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
1689 t
= vla_ptr(vlabuf
, d
, stringtab
);
1692 *stringtabs
++ = t
++;
1696 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1697 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
1698 t
= vla_ptr(vlabuf
, d
, stringtab
);
1699 s
= vla_ptr(vlabuf
, d
, strings
);
1703 /* For each language */
1707 do { /* lang_count > 0 so we can use do-while */
1708 unsigned needed
= needed_count
;
1710 if (unlikely(len
< 3))
1712 t
->language
= get_unaligned_le16(data
);
1719 /* For each string */
1720 do { /* str_count > 0 so we can use do-while */
1721 size_t length
= strnlen(data
, len
);
1723 if (unlikely(length
== len
))
1727 * User may provide more strings then we need,
1728 * if that's the case we simply ignore the
1731 if (likely(needed
)) {
1733 * s->id will be set while adding
1734 * function to configuration so for
1735 * now just leave garbage here.
1744 } while (--str_count
);
1746 s
->id
= 0; /* terminator */
1750 } while (--lang_count
);
1752 /* Some garbage left? */
1757 ffs
->stringtabs
= stringtabs
;
1758 ffs
->raw_strings
= _data
;
1770 /* Events handling and management *******************************************/
1772 static void __ffs_event_add(struct ffs_data
*ffs
,
1773 enum usb_functionfs_event_type type
)
1775 enum usb_functionfs_event_type rem_type1
, rem_type2
= type
;
1779 * Abort any unhandled setup
1781 * We do not need to worry about some cmpxchg() changing value
1782 * of ffs->setup_state without holding the lock because when
1783 * state is FFS_SETUP_PENDING cmpxchg() in several places in
1784 * the source does nothing.
1786 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
1787 ffs
->setup_state
= FFS_SETUP_CANCELED
;
1790 case FUNCTIONFS_RESUME
:
1791 rem_type2
= FUNCTIONFS_SUSPEND
;
1793 case FUNCTIONFS_SUSPEND
:
1794 case FUNCTIONFS_SETUP
:
1796 /* Discard all similar events */
1799 case FUNCTIONFS_BIND
:
1800 case FUNCTIONFS_UNBIND
:
1801 case FUNCTIONFS_DISABLE
:
1802 case FUNCTIONFS_ENABLE
:
1803 /* Discard everything other then power management. */
1804 rem_type1
= FUNCTIONFS_SUSPEND
;
1805 rem_type2
= FUNCTIONFS_RESUME
;
1814 u8
*ev
= ffs
->ev
.types
, *out
= ev
;
1815 unsigned n
= ffs
->ev
.count
;
1816 for (; n
; --n
, ++ev
)
1817 if ((*ev
== rem_type1
|| *ev
== rem_type2
) == neg
)
1820 pr_vdebug("purging event %d\n", *ev
);
1821 ffs
->ev
.count
= out
- ffs
->ev
.types
;
1824 pr_vdebug("adding event %d\n", type
);
1825 ffs
->ev
.types
[ffs
->ev
.count
++] = type
;
1826 wake_up_locked(&ffs
->ev
.waitq
);
1829 static void ffs_event_add(struct ffs_data
*ffs
,
1830 enum usb_functionfs_event_type type
)
1832 unsigned long flags
;
1833 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
1834 __ffs_event_add(ffs
, type
);
1835 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
1839 /* Bind/unbind USB function hooks *******************************************/
1841 static int __ffs_func_bind_do_descs(enum ffs_entity_type type
, u8
*valuep
,
1842 struct usb_descriptor_header
*desc
,
1845 struct usb_endpoint_descriptor
*ds
= (void *)desc
;
1846 struct ffs_function
*func
= priv
;
1847 struct ffs_ep
*ffs_ep
;
1850 * If hs_descriptors is not NULL then we are reading hs
1853 const int isHS
= func
->function
.hs_descriptors
!= NULL
;
1856 if (type
!= FFS_DESCRIPTOR
)
1860 func
->function
.hs_descriptors
[(long)valuep
] = desc
;
1862 func
->function
.fs_descriptors
[(long)valuep
] = desc
;
1864 if (!desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1867 idx
= (ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) - 1;
1868 ffs_ep
= func
->eps
+ idx
;
1870 if (unlikely(ffs_ep
->descs
[isHS
])) {
1871 pr_vdebug("two %sspeed descriptors for EP %d\n",
1872 isHS
? "high" : "full",
1873 ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
1876 ffs_ep
->descs
[isHS
] = ds
;
1878 ffs_dump_mem(": Original ep desc", ds
, ds
->bLength
);
1880 ds
->bEndpointAddress
= ffs_ep
->descs
[0]->bEndpointAddress
;
1881 if (!ds
->wMaxPacketSize
)
1882 ds
->wMaxPacketSize
= ffs_ep
->descs
[0]->wMaxPacketSize
;
1884 struct usb_request
*req
;
1887 pr_vdebug("autoconfig\n");
1888 ep
= usb_ep_autoconfig(func
->gadget
, ds
);
1891 ep
->driver_data
= func
->eps
+ idx
;
1893 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
1899 func
->eps_revmap
[ds
->bEndpointAddress
&
1900 USB_ENDPOINT_NUMBER_MASK
] = idx
+ 1;
1902 ffs_dump_mem(": Rewritten ep desc", ds
, ds
->bLength
);
1907 static int __ffs_func_bind_do_nums(enum ffs_entity_type type
, u8
*valuep
,
1908 struct usb_descriptor_header
*desc
,
1911 struct ffs_function
*func
= priv
;
1917 case FFS_DESCRIPTOR
:
1918 /* Handled in previous pass by __ffs_func_bind_do_descs() */
1923 if (func
->interfaces_nums
[idx
] < 0) {
1924 int id
= usb_interface_id(func
->conf
, &func
->function
);
1925 if (unlikely(id
< 0))
1927 func
->interfaces_nums
[idx
] = id
;
1929 newValue
= func
->interfaces_nums
[idx
];
1933 /* String' IDs are allocated when fsf_data is bound to cdev */
1934 newValue
= func
->ffs
->stringtabs
[0]->strings
[*valuep
- 1].id
;
1939 * USB_DT_ENDPOINT are handled in
1940 * __ffs_func_bind_do_descs().
1942 if (desc
->bDescriptorType
== USB_DT_ENDPOINT
)
1945 idx
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
) - 1;
1946 if (unlikely(!func
->eps
[idx
].ep
))
1950 struct usb_endpoint_descriptor
**descs
;
1951 descs
= func
->eps
[idx
].descs
;
1952 newValue
= descs
[descs
[0] ? 0 : 1]->bEndpointAddress
;
1957 pr_vdebug("%02x -> %02x\n", *valuep
, newValue
);
1962 static inline struct f_fs_opts
*ffs_do_functionfs_bind(struct usb_function
*f
,
1963 struct usb_configuration
*c
)
1965 struct ffs_function
*func
= ffs_func_from_usb(f
);
1966 struct f_fs_opts
*ffs_opts
=
1967 container_of(f
->fi
, struct f_fs_opts
, func_inst
);
1973 * Legacy gadget triggers binding in functionfs_ready_callback,
1974 * which already uses locking; taking the same lock here would
1977 * Configfs-enabled gadgets however do need ffs_dev_lock.
1979 if (!ffs_opts
->no_configfs
)
1981 ret
= ffs_opts
->dev
->desc_ready
? 0 : -ENODEV
;
1982 func
->ffs
= ffs_opts
->dev
->ffs_data
;
1983 if (!ffs_opts
->no_configfs
)
1986 return ERR_PTR(ret
);
1989 func
->gadget
= c
->cdev
->gadget
;
1991 ffs_data_get(func
->ffs
);
1994 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1995 * configurations are bound in sequence with list_for_each_entry,
1996 * in each configuration its functions are bound in sequence
1997 * with list_for_each_entry, so we assume no race condition
1998 * with regard to ffs_opts->bound access
2000 if (!ffs_opts
->refcnt
) {
2001 ret
= functionfs_bind(func
->ffs
, c
->cdev
);
2003 return ERR_PTR(ret
);
2006 func
->function
.strings
= func
->ffs
->stringtabs
;
2011 static int _ffs_func_bind(struct usb_configuration
*c
,
2012 struct usb_function
*f
)
2014 struct ffs_function
*func
= ffs_func_from_usb(f
);
2015 struct ffs_data
*ffs
= func
->ffs
;
2017 const int full
= !!func
->ffs
->fs_descs_count
;
2018 const int high
= gadget_is_dualspeed(func
->gadget
) &&
2019 func
->ffs
->hs_descs_count
;
2023 /* Make it a single chunk, less management later on */
2025 vla_item_with_sz(d
, struct ffs_ep
, eps
, ffs
->eps_count
);
2026 vla_item_with_sz(d
, struct usb_descriptor_header
*, fs_descs
,
2027 full
? ffs
->fs_descs_count
+ 1 : 0);
2028 vla_item_with_sz(d
, struct usb_descriptor_header
*, hs_descs
,
2029 high
? ffs
->hs_descs_count
+ 1 : 0);
2030 vla_item_with_sz(d
, short, inums
, ffs
->interfaces_count
);
2031 vla_item_with_sz(d
, char, raw_descs
,
2032 high
? ffs
->raw_descs_length
: ffs
->raw_fs_descs_length
);
2037 /* Only high speed but not supported by gadget? */
2038 if (unlikely(!(full
| high
)))
2041 /* Allocate a single chunk, less management later on */
2042 vlabuf
= kmalloc(vla_group_size(d
), GFP_KERNEL
);
2043 if (unlikely(!vlabuf
))
2047 memset(vla_ptr(vlabuf
, d
, eps
), 0, d_eps__sz
);
2048 memcpy(vla_ptr(vlabuf
, d
, raw_descs
), ffs
->raw_descs
+ 16,
2050 memset(vla_ptr(vlabuf
, d
, inums
), 0xff, d_inums__sz
);
2051 for (ret
= ffs
->eps_count
; ret
; --ret
) {
2054 ptr
= vla_ptr(vlabuf
, d
, eps
);
2059 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2061 func
->eps
= vla_ptr(vlabuf
, d
, eps
);
2062 func
->interfaces_nums
= vla_ptr(vlabuf
, d
, inums
);
2065 * Go through all the endpoint descriptors and allocate
2066 * endpoints first, so that later we can rewrite the endpoint
2067 * numbers without worrying that it may be described later on.
2070 func
->function
.fs_descriptors
= vla_ptr(vlabuf
, d
, fs_descs
);
2071 ret
= ffs_do_descs(ffs
->fs_descs_count
,
2072 vla_ptr(vlabuf
, d
, raw_descs
),
2074 __ffs_func_bind_do_descs
, func
);
2075 if (unlikely(ret
< 0))
2082 func
->function
.hs_descriptors
= vla_ptr(vlabuf
, d
, hs_descs
);
2083 ret
= ffs_do_descs(ffs
->hs_descs_count
,
2084 vla_ptr(vlabuf
, d
, raw_descs
) + ret
,
2085 d_raw_descs__sz
- ret
,
2086 __ffs_func_bind_do_descs
, func
);
2087 if (unlikely(ret
< 0))
2092 * Now handle interface numbers allocation and interface and
2093 * endpoint numbers rewriting. We can do that in one go
2096 ret
= ffs_do_descs(ffs
->fs_descs_count
+
2097 (high
? ffs
->hs_descs_count
: 0),
2098 vla_ptr(vlabuf
, d
, raw_descs
), d_raw_descs__sz
,
2099 __ffs_func_bind_do_nums
, func
);
2100 if (unlikely(ret
< 0))
2103 /* And we're done */
2104 ffs_event_add(ffs
, FUNCTIONFS_BIND
);
2108 /* XXX Do we need to release all claimed endpoints here? */
2112 static int ffs_func_bind(struct usb_configuration
*c
,
2113 struct usb_function
*f
)
2115 struct f_fs_opts
*ffs_opts
= ffs_do_functionfs_bind(f
, c
);
2117 if (IS_ERR(ffs_opts
))
2118 return PTR_ERR(ffs_opts
);
2120 return _ffs_func_bind(c
, f
);
2124 /* Other USB function hooks *************************************************/
2126 static int ffs_func_set_alt(struct usb_function
*f
,
2127 unsigned interface
, unsigned alt
)
2129 struct ffs_function
*func
= ffs_func_from_usb(f
);
2130 struct ffs_data
*ffs
= func
->ffs
;
2133 if (alt
!= (unsigned)-1) {
2134 intf
= ffs_func_revmap_intf(func
, interface
);
2135 if (unlikely(intf
< 0))
2140 ffs_func_eps_disable(ffs
->func
);
2142 if (ffs
->state
!= FFS_ACTIVE
)
2145 if (alt
== (unsigned)-1) {
2147 ffs_event_add(ffs
, FUNCTIONFS_DISABLE
);
2152 ret
= ffs_func_eps_enable(func
);
2153 if (likely(ret
>= 0))
2154 ffs_event_add(ffs
, FUNCTIONFS_ENABLE
);
2158 static void ffs_func_disable(struct usb_function
*f
)
2160 ffs_func_set_alt(f
, 0, (unsigned)-1);
2163 static int ffs_func_setup(struct usb_function
*f
,
2164 const struct usb_ctrlrequest
*creq
)
2166 struct ffs_function
*func
= ffs_func_from_usb(f
);
2167 struct ffs_data
*ffs
= func
->ffs
;
2168 unsigned long flags
;
2173 pr_vdebug("creq->bRequestType = %02x\n", creq
->bRequestType
);
2174 pr_vdebug("creq->bRequest = %02x\n", creq
->bRequest
);
2175 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq
->wValue
));
2176 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq
->wIndex
));
2177 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq
->wLength
));
2180 * Most requests directed to interface go through here
2181 * (notable exceptions are set/get interface) so we need to
2182 * handle them. All other either handled by composite or
2183 * passed to usb_configuration->setup() (if one is set). No
2184 * matter, we will handle requests directed to endpoint here
2185 * as well (as it's straightforward) but what to do with any
2188 if (ffs
->state
!= FFS_ACTIVE
)
2191 switch (creq
->bRequestType
& USB_RECIP_MASK
) {
2192 case USB_RECIP_INTERFACE
:
2193 ret
= ffs_func_revmap_intf(func
, le16_to_cpu(creq
->wIndex
));
2194 if (unlikely(ret
< 0))
2198 case USB_RECIP_ENDPOINT
:
2199 ret
= ffs_func_revmap_ep(func
, le16_to_cpu(creq
->wIndex
));
2200 if (unlikely(ret
< 0))
2208 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2209 ffs
->ev
.setup
= *creq
;
2210 ffs
->ev
.setup
.wIndex
= cpu_to_le16(ret
);
2211 __ffs_event_add(ffs
, FUNCTIONFS_SETUP
);
2212 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2217 static void ffs_func_suspend(struct usb_function
*f
)
2220 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_SUSPEND
);
2223 static void ffs_func_resume(struct usb_function
*f
)
2226 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_RESUME
);
2230 /* Endpoint and interface numbers reverse mapping ***************************/
2232 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
)
2234 num
= func
->eps_revmap
[num
& USB_ENDPOINT_NUMBER_MASK
];
2235 return num
? num
: -EDOM
;
2238 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
)
2240 short *nums
= func
->interfaces_nums
;
2241 unsigned count
= func
->ffs
->interfaces_count
;
2243 for (; count
; --count
, ++nums
) {
2244 if (*nums
>= 0 && *nums
== intf
)
2245 return nums
- func
->interfaces_nums
;
2252 /* Devices management *******************************************************/
2254 static LIST_HEAD(ffs_devices
);
2256 static struct ffs_dev
*_ffs_find_dev(const char *name
)
2258 struct ffs_dev
*dev
;
2260 list_for_each_entry(dev
, &ffs_devices
, entry
) {
2261 if (!dev
->name
|| !name
)
2263 if (strcmp(dev
->name
, name
) == 0)
2271 * ffs_lock must be taken by the caller of this function
2273 static struct ffs_dev
*ffs_get_single_dev(void)
2275 struct ffs_dev
*dev
;
2277 if (list_is_singular(&ffs_devices
)) {
2278 dev
= list_first_entry(&ffs_devices
, struct ffs_dev
, entry
);
2287 * ffs_lock must be taken by the caller of this function
2289 static struct ffs_dev
*ffs_find_dev(const char *name
)
2291 struct ffs_dev
*dev
;
2293 dev
= ffs_get_single_dev();
2297 return _ffs_find_dev(name
);
2300 /* Configfs support *********************************************************/
2302 static inline struct f_fs_opts
*to_ffs_opts(struct config_item
*item
)
2304 return container_of(to_config_group(item
), struct f_fs_opts
,
2308 static void ffs_attr_release(struct config_item
*item
)
2310 struct f_fs_opts
*opts
= to_ffs_opts(item
);
2312 usb_put_function_instance(&opts
->func_inst
);
2315 static struct configfs_item_operations ffs_item_ops
= {
2316 .release
= ffs_attr_release
,
2319 static struct config_item_type ffs_func_type
= {
2320 .ct_item_ops
= &ffs_item_ops
,
2321 .ct_owner
= THIS_MODULE
,
2325 /* Function registration interface ******************************************/
2327 static void ffs_free_inst(struct usb_function_instance
*f
)
2329 struct f_fs_opts
*opts
;
2331 opts
= to_f_fs_opts(f
);
2333 ffs_free_dev(opts
->dev
);
2338 #define MAX_INST_NAME_LEN 40
2340 static int ffs_set_inst_name(struct usb_function_instance
*fi
, const char *name
)
2342 struct f_fs_opts
*opts
;
2347 name_len
= strlen(name
) + 1;
2348 if (name_len
> MAX_INST_NAME_LEN
)
2349 return -ENAMETOOLONG
;
2351 ptr
= kstrndup(name
, name_len
, GFP_KERNEL
);
2355 opts
= to_f_fs_opts(fi
);
2360 tmp
= opts
->dev
->name_allocated
? opts
->dev
->name
: NULL
;
2361 ret
= _ffs_name_dev(opts
->dev
, ptr
);
2367 opts
->dev
->name_allocated
= true;
2376 static struct usb_function_instance
*ffs_alloc_inst(void)
2378 struct f_fs_opts
*opts
;
2379 struct ffs_dev
*dev
;
2381 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
2383 return ERR_PTR(-ENOMEM
);
2385 opts
->func_inst
.set_inst_name
= ffs_set_inst_name
;
2386 opts
->func_inst
.free_func_inst
= ffs_free_inst
;
2388 dev
= ffs_alloc_dev();
2392 return ERR_CAST(dev
);
2397 config_group_init_type_name(&opts
->func_inst
.group
, "",
2399 return &opts
->func_inst
;
2402 static void ffs_free(struct usb_function
*f
)
2404 kfree(ffs_func_from_usb(f
));
2407 static void ffs_func_unbind(struct usb_configuration
*c
,
2408 struct usb_function
*f
)
2410 struct ffs_function
*func
= ffs_func_from_usb(f
);
2411 struct ffs_data
*ffs
= func
->ffs
;
2412 struct f_fs_opts
*opts
=
2413 container_of(f
->fi
, struct f_fs_opts
, func_inst
);
2414 struct ffs_ep
*ep
= func
->eps
;
2415 unsigned count
= ffs
->eps_count
;
2416 unsigned long flags
;
2419 if (ffs
->func
== func
) {
2420 ffs_func_eps_disable(func
);
2424 if (!--opts
->refcnt
)
2425 functionfs_unbind(ffs
);
2427 /* cleanup after autoconfig */
2428 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
2430 if (ep
->ep
&& ep
->req
)
2431 usb_ep_free_request(ep
->ep
, ep
->req
);
2435 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
2439 * eps, descriptors and interfaces_nums are allocated in the
2440 * same chunk so only one free is required.
2442 func
->function
.fs_descriptors
= NULL
;
2443 func
->function
.hs_descriptors
= NULL
;
2444 func
->interfaces_nums
= NULL
;
2446 ffs_event_add(ffs
, FUNCTIONFS_UNBIND
);
2449 static struct usb_function
*ffs_alloc(struct usb_function_instance
*fi
)
2451 struct ffs_function
*func
;
2455 func
= kzalloc(sizeof(*func
), GFP_KERNEL
);
2456 if (unlikely(!func
))
2457 return ERR_PTR(-ENOMEM
);
2459 func
->function
.name
= "Function FS Gadget";
2461 func
->function
.bind
= ffs_func_bind
;
2462 func
->function
.unbind
= ffs_func_unbind
;
2463 func
->function
.set_alt
= ffs_func_set_alt
;
2464 func
->function
.disable
= ffs_func_disable
;
2465 func
->function
.setup
= ffs_func_setup
;
2466 func
->function
.suspend
= ffs_func_suspend
;
2467 func
->function
.resume
= ffs_func_resume
;
2468 func
->function
.free_func
= ffs_free
;
2470 return &func
->function
;
2474 * ffs_lock must be taken by the caller of this function
2476 struct ffs_dev
*ffs_alloc_dev(void)
2478 struct ffs_dev
*dev
;
2481 if (ffs_get_single_dev())
2482 return ERR_PTR(-EBUSY
);
2484 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2486 return ERR_PTR(-ENOMEM
);
2488 if (list_empty(&ffs_devices
)) {
2489 ret
= functionfs_init();
2492 return ERR_PTR(ret
);
2496 list_add(&dev
->entry
, &ffs_devices
);
2502 * ffs_lock must be taken by the caller of this function
2503 * The caller is responsible for "name" being available whenever f_fs needs it
2505 static int _ffs_name_dev(struct ffs_dev
*dev
, const char *name
)
2507 struct ffs_dev
*existing
;
2509 existing
= _ffs_find_dev(name
);
2519 * The caller is responsible for "name" being available whenever f_fs needs it
2521 int ffs_name_dev(struct ffs_dev
*dev
, const char *name
)
2526 ret
= _ffs_name_dev(dev
, name
);
2531 EXPORT_SYMBOL(ffs_name_dev
);
2533 int ffs_single_dev(struct ffs_dev
*dev
)
2540 if (!list_is_singular(&ffs_devices
))
2548 EXPORT_SYMBOL(ffs_single_dev
);
2551 * ffs_lock must be taken by the caller of this function
2553 void ffs_free_dev(struct ffs_dev
*dev
)
2555 list_del(&dev
->entry
);
2556 if (dev
->name_allocated
)
2559 if (list_empty(&ffs_devices
))
2560 functionfs_cleanup();
2563 static void *ffs_acquire_dev(const char *dev_name
)
2565 struct ffs_dev
*ffs_dev
;
2570 ffs_dev
= ffs_find_dev(dev_name
);
2572 ffs_dev
= ERR_PTR(-ENODEV
);
2573 else if (ffs_dev
->mounted
)
2574 ffs_dev
= ERR_PTR(-EBUSY
);
2575 else if (ffs_dev
->ffs_acquire_dev_callback
&&
2576 ffs_dev
->ffs_acquire_dev_callback(ffs_dev
))
2577 ffs_dev
= ERR_PTR(-ENODEV
);
2579 ffs_dev
->mounted
= true;
2585 static void ffs_release_dev(struct ffs_data
*ffs_data
)
2587 struct ffs_dev
*ffs_dev
;
2592 ffs_dev
= ffs_data
->private_data
;
2594 ffs_dev
->mounted
= false;
2596 if (ffs_dev
->ffs_release_dev_callback
)
2597 ffs_dev
->ffs_release_dev_callback(ffs_dev
);
2602 static int ffs_ready(struct ffs_data
*ffs
)
2604 struct ffs_dev
*ffs_obj
;
2610 ffs_obj
= ffs
->private_data
;
2615 if (WARN_ON(ffs_obj
->desc_ready
)) {
2620 ffs_obj
->desc_ready
= true;
2621 ffs_obj
->ffs_data
= ffs
;
2623 if (ffs_obj
->ffs_ready_callback
)
2624 ret
= ffs_obj
->ffs_ready_callback(ffs
);
2631 static void ffs_closed(struct ffs_data
*ffs
)
2633 struct ffs_dev
*ffs_obj
;
2638 ffs_obj
= ffs
->private_data
;
2642 ffs_obj
->desc_ready
= false;
2644 if (ffs_obj
->ffs_closed_callback
)
2645 ffs_obj
->ffs_closed_callback(ffs
);
2647 if (!ffs_obj
->opts
|| ffs_obj
->opts
->no_configfs
2648 || !ffs_obj
->opts
->func_inst
.group
.cg_item
.ci_parent
)
2651 unregister_gadget_item(ffs_obj
->opts
->
2652 func_inst
.group
.cg_item
.ci_parent
->ci_parent
);
2657 /* Misc helper functions ****************************************************/
2659 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
2662 ? likely(mutex_trylock(mutex
)) ? 0 : -EAGAIN
2663 : mutex_lock_interruptible(mutex
);
2666 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
2673 data
= kmalloc(len
, GFP_KERNEL
);
2674 if (unlikely(!data
))
2675 return ERR_PTR(-ENOMEM
);
2677 if (unlikely(__copy_from_user(data
, buf
, len
))) {
2679 return ERR_PTR(-EFAULT
);
2682 pr_vdebug("Buffer from user space:\n");
2683 ffs_dump_mem("", data
, len
);
2688 DECLARE_USB_FUNCTION_INIT(ffs
, ffs_alloc_inst
, ffs_alloc
);
2689 MODULE_LICENSE("GPL");
2690 MODULE_AUTHOR("Michal Nazarewicz");