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 <linux/uio.h>
27 #include <asm/unaligned.h>
29 #include <linux/usb/composite.h>
30 #include <linux/usb/functionfs.h>
32 #include <linux/aio.h>
33 #include <linux/mmu_context.h>
34 #include <linux/poll.h>
35 #include <linux/eventfd.h>
39 #include "u_os_desc.h"
42 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
44 /* Reference counter handling */
45 static void ffs_data_get(struct ffs_data
*ffs
);
46 static void ffs_data_put(struct ffs_data
*ffs
);
47 /* Creates new ffs_data object. */
48 static struct ffs_data
*__must_check
ffs_data_new(void) __attribute__((malloc
));
50 /* Opened counter handling. */
51 static void ffs_data_opened(struct ffs_data
*ffs
);
52 static void ffs_data_closed(struct ffs_data
*ffs
);
54 /* Called with ffs->mutex held; take over ownership of data. */
55 static int __must_check
56 __ffs_data_got_descs(struct ffs_data
*ffs
, char *data
, size_t len
);
57 static int __must_check
58 __ffs_data_got_strings(struct ffs_data
*ffs
, char *data
, size_t len
);
61 /* The function structure ***************************************************/
66 struct usb_configuration
*conf
;
67 struct usb_gadget
*gadget
;
72 short *interfaces_nums
;
74 struct usb_function function
;
78 static struct ffs_function
*ffs_func_from_usb(struct usb_function
*f
)
80 return container_of(f
, struct ffs_function
, function
);
84 static inline enum ffs_setup_state
85 ffs_setup_state_clear_cancelled(struct ffs_data
*ffs
)
87 return (enum ffs_setup_state
)
88 cmpxchg(&ffs
->setup_state
, FFS_SETUP_CANCELLED
, FFS_NO_SETUP
);
92 static void ffs_func_eps_disable(struct ffs_function
*func
);
93 static int __must_check
ffs_func_eps_enable(struct ffs_function
*func
);
95 static int ffs_func_bind(struct usb_configuration
*,
96 struct usb_function
*);
97 static int ffs_func_set_alt(struct usb_function
*, unsigned, unsigned);
98 static void ffs_func_disable(struct usb_function
*);
99 static int ffs_func_setup(struct usb_function
*,
100 const struct usb_ctrlrequest
*);
101 static void ffs_func_suspend(struct usb_function
*);
102 static void ffs_func_resume(struct usb_function
*);
105 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
);
106 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
);
109 /* The endpoints structures *************************************************/
112 struct usb_ep
*ep
; /* P: ffs->eps_lock */
113 struct usb_request
*req
; /* P: epfile->mutex */
115 /* [0]: full speed, [1]: high speed, [2]: super speed */
116 struct usb_endpoint_descriptor
*descs
[3];
120 int status
; /* P: epfile->mutex */
124 /* Protects ep->ep and ep->req. */
126 wait_queue_head_t wait
;
128 struct ffs_data
*ffs
;
129 struct ffs_ep
*ep
; /* P: ffs->eps_lock */
131 struct dentry
*dentry
;
134 * Buffer for holding data from partial reads which may happen since
135 * we’re rounding user read requests to a multiple of a max packet size.
137 struct ffs_buffer
*read_buffer
; /* P: epfile->mutex */
141 unsigned char in
; /* P: ffs->eps_lock */
142 unsigned char isoc
; /* P: ffs->eps_lock */
153 /* ffs_io_data structure ***************************************************/
160 struct iov_iter data
;
164 struct mm_struct
*mm
;
165 struct work_struct work
;
168 struct usb_request
*req
;
170 struct ffs_data
*ffs
;
173 struct ffs_desc_helper
{
174 struct ffs_data
*ffs
;
175 unsigned interfaces_count
;
179 static int __must_check
ffs_epfiles_create(struct ffs_data
*ffs
);
180 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
);
182 static struct dentry
*
183 ffs_sb_create_file(struct super_block
*sb
, const char *name
, void *data
,
184 const struct file_operations
*fops
);
186 /* Devices management *******************************************************/
188 DEFINE_MUTEX(ffs_lock
);
189 EXPORT_SYMBOL_GPL(ffs_lock
);
191 static struct ffs_dev
*_ffs_find_dev(const char *name
);
192 static struct ffs_dev
*_ffs_alloc_dev(void);
193 static int _ffs_name_dev(struct ffs_dev
*dev
, const char *name
);
194 static void _ffs_free_dev(struct ffs_dev
*dev
);
195 static void *ffs_acquire_dev(const char *dev_name
);
196 static void ffs_release_dev(struct ffs_data
*ffs_data
);
197 static int ffs_ready(struct ffs_data
*ffs
);
198 static void ffs_closed(struct ffs_data
*ffs
);
200 /* Misc helper functions ****************************************************/
202 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
203 __attribute__((warn_unused_result
, nonnull
));
204 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
205 __attribute__((warn_unused_result
, nonnull
));
208 /* Control file aka ep0 *****************************************************/
210 static void ffs_ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
212 struct ffs_data
*ffs
= req
->context
;
214 complete_all(&ffs
->ep0req_completion
);
217 static int __ffs_ep0_queue_wait(struct ffs_data
*ffs
, char *data
, size_t len
)
219 struct usb_request
*req
= ffs
->ep0req
;
222 req
->zero
= len
< le16_to_cpu(ffs
->ev
.setup
.wLength
);
224 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
230 * UDC layer requires to provide a buffer even for ZLP, but should
231 * not use it at all. Let's provide some poisoned pointer to catch
232 * possible bug in the driver.
234 if (req
->buf
== NULL
)
235 req
->buf
= (void *)0xDEADBABE;
237 reinit_completion(&ffs
->ep0req_completion
);
239 ret
= usb_ep_queue(ffs
->gadget
->ep0
, req
, GFP_ATOMIC
);
240 if (unlikely(ret
< 0))
243 ret
= wait_for_completion_interruptible(&ffs
->ep0req_completion
);
245 usb_ep_dequeue(ffs
->gadget
->ep0
, req
);
249 ffs
->setup_state
= FFS_NO_SETUP
;
250 return req
->status
? req
->status
: req
->actual
;
253 static int __ffs_ep0_stall(struct ffs_data
*ffs
)
255 if (ffs
->ev
.can_stall
) {
256 pr_vdebug("ep0 stall\n");
257 usb_ep_set_halt(ffs
->gadget
->ep0
);
258 ffs
->setup_state
= FFS_NO_SETUP
;
261 pr_debug("bogus ep0 stall!\n");
266 static ssize_t
ffs_ep0_write(struct file
*file
, const char __user
*buf
,
267 size_t len
, loff_t
*ptr
)
269 struct ffs_data
*ffs
= file
->private_data
;
275 /* Fast check if setup was canceled */
276 if (ffs_setup_state_clear_cancelled(ffs
) == FFS_SETUP_CANCELLED
)
280 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
281 if (unlikely(ret
< 0))
285 switch (ffs
->state
) {
286 case FFS_READ_DESCRIPTORS
:
287 case FFS_READ_STRINGS
:
289 if (unlikely(len
< 16)) {
294 data
= ffs_prepare_buffer(buf
, len
);
301 if (ffs
->state
== FFS_READ_DESCRIPTORS
) {
302 pr_info("read descriptors\n");
303 ret
= __ffs_data_got_descs(ffs
, data
, len
);
304 if (unlikely(ret
< 0))
307 ffs
->state
= FFS_READ_STRINGS
;
310 pr_info("read strings\n");
311 ret
= __ffs_data_got_strings(ffs
, data
, len
);
312 if (unlikely(ret
< 0))
315 ret
= ffs_epfiles_create(ffs
);
317 ffs
->state
= FFS_CLOSING
;
321 ffs
->state
= FFS_ACTIVE
;
322 mutex_unlock(&ffs
->mutex
);
324 ret
= ffs_ready(ffs
);
325 if (unlikely(ret
< 0)) {
326 ffs
->state
= FFS_CLOSING
;
337 * We're called from user space, we can use _irq
338 * rather then _irqsave
340 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
341 switch (ffs_setup_state_clear_cancelled(ffs
)) {
342 case FFS_SETUP_CANCELLED
:
350 case FFS_SETUP_PENDING
:
354 /* FFS_SETUP_PENDING */
355 if (!(ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
)) {
356 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
357 ret
= __ffs_ep0_stall(ffs
);
361 /* FFS_SETUP_PENDING and not stall */
362 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
364 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
366 data
= ffs_prepare_buffer(buf
, len
);
372 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
375 * We are guaranteed to be still in FFS_ACTIVE state
376 * but the state of setup could have changed from
377 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
378 * to check for that. If that happened we copied data
379 * from user space in vain but it's unlikely.
381 * For sure we are not in FFS_NO_SETUP since this is
382 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
383 * transition can be performed and it's protected by
386 if (ffs_setup_state_clear_cancelled(ffs
) ==
387 FFS_SETUP_CANCELLED
) {
390 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
392 /* unlocks spinlock */
393 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
403 mutex_unlock(&ffs
->mutex
);
407 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
408 static ssize_t
__ffs_ep0_read_events(struct ffs_data
*ffs
, char __user
*buf
,
412 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
413 * size of ffs->ev.types array (which is four) so that's how much space
416 struct usb_functionfs_event events
[ARRAY_SIZE(ffs
->ev
.types
)];
417 const size_t size
= n
* sizeof *events
;
420 memset(events
, 0, size
);
423 events
[i
].type
= ffs
->ev
.types
[i
];
424 if (events
[i
].type
== FUNCTIONFS_SETUP
) {
425 events
[i
].u
.setup
= ffs
->ev
.setup
;
426 ffs
->setup_state
= FFS_SETUP_PENDING
;
432 memmove(ffs
->ev
.types
, ffs
->ev
.types
+ n
,
433 ffs
->ev
.count
* sizeof *ffs
->ev
.types
);
435 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
436 mutex_unlock(&ffs
->mutex
);
438 return unlikely(copy_to_user(buf
, events
, size
)) ? -EFAULT
: size
;
441 static ssize_t
ffs_ep0_read(struct file
*file
, char __user
*buf
,
442 size_t len
, loff_t
*ptr
)
444 struct ffs_data
*ffs
= file
->private_data
;
451 /* Fast check if setup was canceled */
452 if (ffs_setup_state_clear_cancelled(ffs
) == FFS_SETUP_CANCELLED
)
456 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
457 if (unlikely(ret
< 0))
461 if (ffs
->state
!= FFS_ACTIVE
) {
467 * We're called from user space, we can use _irq rather then
470 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
472 switch (ffs_setup_state_clear_cancelled(ffs
)) {
473 case FFS_SETUP_CANCELLED
:
478 n
= len
/ sizeof(struct usb_functionfs_event
);
484 if ((file
->f_flags
& O_NONBLOCK
) && !ffs
->ev
.count
) {
489 if (wait_event_interruptible_exclusive_locked_irq(ffs
->ev
.waitq
,
495 return __ffs_ep0_read_events(ffs
, buf
,
496 min(n
, (size_t)ffs
->ev
.count
));
498 case FFS_SETUP_PENDING
:
499 if (ffs
->ev
.setup
.bRequestType
& USB_DIR_IN
) {
500 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
501 ret
= __ffs_ep0_stall(ffs
);
505 len
= min(len
, (size_t)le16_to_cpu(ffs
->ev
.setup
.wLength
));
507 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
510 data
= kmalloc(len
, GFP_KERNEL
);
511 if (unlikely(!data
)) {
517 spin_lock_irq(&ffs
->ev
.waitq
.lock
);
519 /* See ffs_ep0_write() */
520 if (ffs_setup_state_clear_cancelled(ffs
) ==
521 FFS_SETUP_CANCELLED
) {
526 /* unlocks spinlock */
527 ret
= __ffs_ep0_queue_wait(ffs
, data
, len
);
528 if (likely(ret
> 0) && unlikely(copy_to_user(buf
, data
, len
)))
537 spin_unlock_irq(&ffs
->ev
.waitq
.lock
);
539 mutex_unlock(&ffs
->mutex
);
544 static int ffs_ep0_open(struct inode
*inode
, struct file
*file
)
546 struct ffs_data
*ffs
= inode
->i_private
;
550 if (unlikely(ffs
->state
== FFS_CLOSING
))
553 file
->private_data
= ffs
;
554 ffs_data_opened(ffs
);
559 static int ffs_ep0_release(struct inode
*inode
, struct file
*file
)
561 struct ffs_data
*ffs
= file
->private_data
;
565 ffs_data_closed(ffs
);
570 static long ffs_ep0_ioctl(struct file
*file
, unsigned code
, unsigned long value
)
572 struct ffs_data
*ffs
= file
->private_data
;
573 struct usb_gadget
*gadget
= ffs
->gadget
;
578 if (code
== FUNCTIONFS_INTERFACE_REVMAP
) {
579 struct ffs_function
*func
= ffs
->func
;
580 ret
= func
? ffs_func_revmap_intf(func
, value
) : -ENODEV
;
581 } else if (gadget
&& gadget
->ops
->ioctl
) {
582 ret
= gadget
->ops
->ioctl(gadget
, code
, value
);
590 static unsigned int ffs_ep0_poll(struct file
*file
, poll_table
*wait
)
592 struct ffs_data
*ffs
= file
->private_data
;
593 unsigned int mask
= POLLWRNORM
;
596 poll_wait(file
, &ffs
->ev
.waitq
, wait
);
598 ret
= ffs_mutex_lock(&ffs
->mutex
, file
->f_flags
& O_NONBLOCK
);
599 if (unlikely(ret
< 0))
602 switch (ffs
->state
) {
603 case FFS_READ_DESCRIPTORS
:
604 case FFS_READ_STRINGS
:
609 switch (ffs
->setup_state
) {
615 case FFS_SETUP_PENDING
:
616 case FFS_SETUP_CANCELLED
:
617 mask
|= (POLLIN
| POLLOUT
);
622 case FFS_DEACTIVATED
:
626 mutex_unlock(&ffs
->mutex
);
631 static const struct file_operations ffs_ep0_operations
= {
634 .open
= ffs_ep0_open
,
635 .write
= ffs_ep0_write
,
636 .read
= ffs_ep0_read
,
637 .release
= ffs_ep0_release
,
638 .unlocked_ioctl
= ffs_ep0_ioctl
,
639 .poll
= ffs_ep0_poll
,
643 /* "Normal" endpoints operations ********************************************/
645 static void ffs_epfile_io_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
648 if (likely(req
->context
)) {
649 struct ffs_ep
*ep
= _ep
->driver_data
;
650 ep
->status
= req
->status
? req
->status
: req
->actual
;
651 complete(req
->context
);
655 static ssize_t
ffs_copy_to_iter(void *data
, int data_len
, struct iov_iter
*iter
)
657 ssize_t ret
= copy_to_iter(data
, data_len
, iter
);
658 if (likely(ret
== data_len
))
661 if (unlikely(iov_iter_count(iter
)))
665 * Dear user space developer!
667 * TL;DR: To stop getting below error message in your kernel log, change
668 * user space code using functionfs to align read buffers to a max
671 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
672 * packet size. When unaligned buffer is passed to functionfs, it
673 * internally uses a larger, aligned buffer so that such UDCs are happy.
675 * Unfortunately, this means that host may send more data than was
676 * requested in read(2) system call. f_fs doesn’t know what to do with
677 * that excess data so it simply drops it.
679 * Was the buffer aligned in the first place, no such problem would
682 * Data may be dropped only in AIO reads. Synchronous reads are handled
683 * by splitting a request into multiple parts. This splitting may still
684 * be a problem though so it’s likely best to align the buffer
685 * regardless of it being AIO or not..
687 * This only affects OUT endpoints, i.e. reading data with a read(2),
688 * aio_read(2) etc. system calls. Writing data to an IN endpoint is not
691 pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
692 "Align read buffer size to max packet size to avoid the problem.\n",
698 static void ffs_user_copy_worker(struct work_struct
*work
)
700 struct ffs_io_data
*io_data
= container_of(work
, struct ffs_io_data
,
702 int ret
= io_data
->req
->status
? io_data
->req
->status
:
703 io_data
->req
->actual
;
704 bool kiocb_has_eventfd
= io_data
->kiocb
->ki_flags
& IOCB_EVENTFD
;
706 if (io_data
->read
&& ret
> 0) {
708 ret
= ffs_copy_to_iter(io_data
->buf
, ret
, &io_data
->data
);
709 unuse_mm(io_data
->mm
);
712 io_data
->kiocb
->ki_complete(io_data
->kiocb
, ret
, ret
);
714 if (io_data
->ffs
->ffs_eventfd
&& !kiocb_has_eventfd
)
715 eventfd_signal(io_data
->ffs
->ffs_eventfd
, 1);
717 usb_ep_free_request(io_data
->ep
, io_data
->req
);
720 kfree(io_data
->to_free
);
725 static void ffs_epfile_async_io_complete(struct usb_ep
*_ep
,
726 struct usb_request
*req
)
728 struct ffs_io_data
*io_data
= req
->context
;
732 INIT_WORK(&io_data
->work
, ffs_user_copy_worker
);
733 schedule_work(&io_data
->work
);
736 /* Assumes epfile->mutex is held. */
737 static ssize_t
__ffs_epfile_read_buffered(struct ffs_epfile
*epfile
,
738 struct iov_iter
*iter
)
740 struct ffs_buffer
*buf
= epfile
->read_buffer
;
745 ret
= copy_to_iter(buf
->data
, buf
->length
, iter
);
746 if (buf
->length
== ret
) {
748 epfile
->read_buffer
= NULL
;
749 } else if (unlikely(iov_iter_count(iter
))) {
758 /* Assumes epfile->mutex is held. */
759 static ssize_t
__ffs_epfile_read_data(struct ffs_epfile
*epfile
,
760 void *data
, int data_len
,
761 struct iov_iter
*iter
)
763 struct ffs_buffer
*buf
;
765 ssize_t ret
= copy_to_iter(data
, data_len
, iter
);
766 if (likely(data_len
== ret
))
769 if (unlikely(iov_iter_count(iter
)))
772 /* See ffs_copy_to_iter for more context. */
773 pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
777 buf
= kmalloc(sizeof(*buf
) + data_len
, GFP_KERNEL
);
778 buf
->length
= data_len
;
779 buf
->data
= buf
->storage
;
780 memcpy(buf
->storage
, data
+ ret
, data_len
);
781 epfile
->read_buffer
= buf
;
786 static ssize_t
ffs_epfile_io(struct file
*file
, struct ffs_io_data
*io_data
)
788 struct ffs_epfile
*epfile
= file
->private_data
;
789 struct usb_request
*req
;
792 ssize_t ret
, data_len
= -EINVAL
;
795 /* Are we still active? */
796 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
799 /* Wait for endpoint to be enabled */
802 if (file
->f_flags
& O_NONBLOCK
)
805 ret
= wait_event_interruptible(epfile
->wait
, (ep
= epfile
->ep
));
811 halt
= (!io_data
->read
== !epfile
->in
);
812 if (halt
&& epfile
->isoc
)
815 /* We will be using request and read_buffer */
816 ret
= ffs_mutex_lock(&epfile
->mutex
, file
->f_flags
& O_NONBLOCK
);
820 /* Allocate & copy */
822 struct usb_gadget
*gadget
;
825 * Do we have buffered data from previous partial read? Check
826 * that for synchronous case only because we do not have
827 * facility to ‘wake up’ a pending asynchronous read and push
828 * buffered data to it which we would need to make things behave
831 if (!io_data
->aio
&& io_data
->read
) {
832 ret
= __ffs_epfile_read_buffered(epfile
, &io_data
->data
);
838 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
839 * before the waiting completes, so do not assign to 'gadget'
842 gadget
= epfile
->ffs
->gadget
;
844 spin_lock_irq(&epfile
->ffs
->eps_lock
);
845 /* In the meantime, endpoint got disabled or changed. */
846 if (epfile
->ep
!= ep
) {
850 data_len
= iov_iter_count(&io_data
->data
);
852 * Controller may require buffer size to be aligned to
853 * maxpacketsize of an out endpoint.
856 data_len
= usb_ep_align_maybe(gadget
, ep
->ep
, data_len
);
857 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
859 data
= kmalloc(data_len
, GFP_KERNEL
);
860 if (unlikely(!data
)) {
864 if (!io_data
->read
&&
865 copy_from_iter(data
, data_len
, &io_data
->data
) != data_len
) {
871 spin_lock_irq(&epfile
->ffs
->eps_lock
);
873 if (epfile
->ep
!= ep
) {
874 /* In the meantime, endpoint got disabled or changed. */
878 if (likely(epfile
->ep
== ep
) && !WARN_ON(!ep
->ep
))
879 usb_ep_set_halt(ep
->ep
);
881 } else if (unlikely(data_len
== -EINVAL
)) {
883 * Sanity Check: even though data_len can't be used
884 * uninitialized at the time I write this comment, some
885 * compilers complain about this situation.
886 * In order to keep the code clean from warnings, data_len is
887 * being initialized to -EINVAL during its declaration, which
888 * means we can't rely on compiler anymore to warn no future
889 * changes won't result in data_len being used uninitialized.
890 * For such reason, we're adding this redundant sanity check
893 WARN(1, "%s: data_len == -EINVAL\n", __func__
);
895 } else if (!io_data
->aio
) {
896 DECLARE_COMPLETION_ONSTACK(done
);
897 bool interrupted
= false;
901 req
->length
= data_len
;
903 req
->context
= &done
;
904 req
->complete
= ffs_epfile_io_complete
;
906 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
907 if (unlikely(ret
< 0))
910 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
912 if (unlikely(wait_for_completion_interruptible(&done
))) {
914 * To avoid race condition with ffs_epfile_io_complete,
915 * dequeue the request first then check
916 * status. usb_ep_dequeue API should guarantee no race
917 * condition with req->complete callback.
919 usb_ep_dequeue(ep
->ep
, req
);
920 interrupted
= ep
->status
< 0;
925 else if (io_data
->read
&& ep
->status
> 0)
926 ret
= __ffs_epfile_read_data(epfile
, data
, ep
->status
,
931 } else if (!(req
= usb_ep_alloc_request(ep
->ep
, GFP_KERNEL
))) {
935 req
->length
= data_len
;
938 io_data
->ep
= ep
->ep
;
940 io_data
->ffs
= epfile
->ffs
;
942 req
->context
= io_data
;
943 req
->complete
= ffs_epfile_async_io_complete
;
945 ret
= usb_ep_queue(ep
->ep
, req
, GFP_ATOMIC
);
947 usb_ep_free_request(ep
->ep
, req
);
953 * Do not kfree the buffer in this function. It will be freed
954 * by ffs_user_copy_worker.
960 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
962 mutex_unlock(&epfile
->mutex
);
969 ffs_epfile_open(struct inode
*inode
, struct file
*file
)
971 struct ffs_epfile
*epfile
= inode
->i_private
;
975 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
978 file
->private_data
= epfile
;
979 ffs_data_opened(epfile
->ffs
);
984 static int ffs_aio_cancel(struct kiocb
*kiocb
)
986 struct ffs_io_data
*io_data
= kiocb
->private;
987 struct ffs_epfile
*epfile
= kiocb
->ki_filp
->private_data
;
992 spin_lock_irq(&epfile
->ffs
->eps_lock
);
994 if (likely(io_data
&& io_data
->ep
&& io_data
->req
))
995 value
= usb_ep_dequeue(io_data
->ep
, io_data
->req
);
999 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
1004 static ssize_t
ffs_epfile_write_iter(struct kiocb
*kiocb
, struct iov_iter
*from
)
1006 struct ffs_io_data io_data
, *p
= &io_data
;
1011 if (!is_sync_kiocb(kiocb
)) {
1012 p
= kmalloc(sizeof(io_data
), GFP_KERNEL
);
1023 p
->mm
= current
->mm
;
1028 kiocb_set_cancel_fn(kiocb
, ffs_aio_cancel
);
1030 res
= ffs_epfile_io(kiocb
->ki_filp
, p
);
1031 if (res
== -EIOCBQUEUED
)
1040 static ssize_t
ffs_epfile_read_iter(struct kiocb
*kiocb
, struct iov_iter
*to
)
1042 struct ffs_io_data io_data
, *p
= &io_data
;
1047 if (!is_sync_kiocb(kiocb
)) {
1048 p
= kmalloc(sizeof(io_data
), GFP_KERNEL
);
1059 p
->to_free
= dup_iter(&p
->data
, to
, GFP_KERNEL
);
1068 p
->mm
= current
->mm
;
1073 kiocb_set_cancel_fn(kiocb
, ffs_aio_cancel
);
1075 res
= ffs_epfile_io(kiocb
->ki_filp
, p
);
1076 if (res
== -EIOCBQUEUED
)
1089 ffs_epfile_release(struct inode
*inode
, struct file
*file
)
1091 struct ffs_epfile
*epfile
= inode
->i_private
;
1095 kfree(epfile
->read_buffer
);
1096 epfile
->read_buffer
= NULL
;
1097 ffs_data_closed(epfile
->ffs
);
1102 static long ffs_epfile_ioctl(struct file
*file
, unsigned code
,
1103 unsigned long value
)
1105 struct ffs_epfile
*epfile
= file
->private_data
;
1110 if (WARN_ON(epfile
->ffs
->state
!= FFS_ACTIVE
))
1113 spin_lock_irq(&epfile
->ffs
->eps_lock
);
1114 if (likely(epfile
->ep
)) {
1116 case FUNCTIONFS_FIFO_STATUS
:
1117 ret
= usb_ep_fifo_status(epfile
->ep
->ep
);
1119 case FUNCTIONFS_FIFO_FLUSH
:
1120 usb_ep_fifo_flush(epfile
->ep
->ep
);
1123 case FUNCTIONFS_CLEAR_HALT
:
1124 ret
= usb_ep_clear_halt(epfile
->ep
->ep
);
1126 case FUNCTIONFS_ENDPOINT_REVMAP
:
1127 ret
= epfile
->ep
->num
;
1129 case FUNCTIONFS_ENDPOINT_DESC
:
1132 struct usb_endpoint_descriptor
*desc
;
1134 switch (epfile
->ffs
->gadget
->speed
) {
1135 case USB_SPEED_SUPER
:
1138 case USB_SPEED_HIGH
:
1144 desc
= epfile
->ep
->descs
[desc_idx
];
1146 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
1147 ret
= copy_to_user((void *)value
, desc
, sizeof(*desc
));
1158 spin_unlock_irq(&epfile
->ffs
->eps_lock
);
1163 static const struct file_operations ffs_epfile_operations
= {
1164 .llseek
= no_llseek
,
1166 .open
= ffs_epfile_open
,
1167 .write_iter
= ffs_epfile_write_iter
,
1168 .read_iter
= ffs_epfile_read_iter
,
1169 .release
= ffs_epfile_release
,
1170 .unlocked_ioctl
= ffs_epfile_ioctl
,
1174 /* File system and super block operations ***********************************/
1177 * Mounting the file system creates a controller file, used first for
1178 * function configuration then later for event monitoring.
1181 static struct inode
*__must_check
1182 ffs_sb_make_inode(struct super_block
*sb
, void *data
,
1183 const struct file_operations
*fops
,
1184 const struct inode_operations
*iops
,
1185 struct ffs_file_perms
*perms
)
1187 struct inode
*inode
;
1191 inode
= new_inode(sb
);
1193 if (likely(inode
)) {
1194 struct timespec current_time
= CURRENT_TIME
;
1196 inode
->i_ino
= get_next_ino();
1197 inode
->i_mode
= perms
->mode
;
1198 inode
->i_uid
= perms
->uid
;
1199 inode
->i_gid
= perms
->gid
;
1200 inode
->i_atime
= current_time
;
1201 inode
->i_mtime
= current_time
;
1202 inode
->i_ctime
= current_time
;
1203 inode
->i_private
= data
;
1205 inode
->i_fop
= fops
;
1213 /* Create "regular" file */
1214 static struct dentry
*ffs_sb_create_file(struct super_block
*sb
,
1215 const char *name
, void *data
,
1216 const struct file_operations
*fops
)
1218 struct ffs_data
*ffs
= sb
->s_fs_info
;
1219 struct dentry
*dentry
;
1220 struct inode
*inode
;
1224 dentry
= d_alloc_name(sb
->s_root
, name
);
1225 if (unlikely(!dentry
))
1228 inode
= ffs_sb_make_inode(sb
, data
, fops
, NULL
, &ffs
->file_perms
);
1229 if (unlikely(!inode
)) {
1234 d_add(dentry
, inode
);
1239 static const struct super_operations ffs_sb_operations
= {
1240 .statfs
= simple_statfs
,
1241 .drop_inode
= generic_delete_inode
,
1244 struct ffs_sb_fill_data
{
1245 struct ffs_file_perms perms
;
1247 const char *dev_name
;
1249 struct ffs_data
*ffs_data
;
1252 static int ffs_sb_fill(struct super_block
*sb
, void *_data
, int silent
)
1254 struct ffs_sb_fill_data
*data
= _data
;
1255 struct inode
*inode
;
1256 struct ffs_data
*ffs
= data
->ffs_data
;
1261 data
->ffs_data
= NULL
;
1262 sb
->s_fs_info
= ffs
;
1263 sb
->s_blocksize
= PAGE_SIZE
;
1264 sb
->s_blocksize_bits
= PAGE_SHIFT
;
1265 sb
->s_magic
= FUNCTIONFS_MAGIC
;
1266 sb
->s_op
= &ffs_sb_operations
;
1267 sb
->s_time_gran
= 1;
1270 data
->perms
.mode
= data
->root_mode
;
1271 inode
= ffs_sb_make_inode(sb
, NULL
,
1272 &simple_dir_operations
,
1273 &simple_dir_inode_operations
,
1275 sb
->s_root
= d_make_root(inode
);
1276 if (unlikely(!sb
->s_root
))
1280 if (unlikely(!ffs_sb_create_file(sb
, "ep0", ffs
,
1281 &ffs_ep0_operations
)))
1287 static int ffs_fs_parse_opts(struct ffs_sb_fill_data
*data
, char *opts
)
1291 if (!opts
|| !*opts
)
1295 unsigned long value
;
1299 comma
= strchr(opts
, ',');
1304 eq
= strchr(opts
, '=');
1305 if (unlikely(!eq
)) {
1306 pr_err("'=' missing in %s\n", opts
);
1312 if (kstrtoul(eq
+ 1, 0, &value
)) {
1313 pr_err("%s: invalid value: %s\n", opts
, eq
+ 1);
1317 /* Interpret option */
1318 switch (eq
- opts
) {
1320 if (!memcmp(opts
, "no_disconnect", 13))
1321 data
->no_disconnect
= !!value
;
1326 if (!memcmp(opts
, "rmode", 5))
1327 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1328 else if (!memcmp(opts
, "fmode", 5))
1329 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1335 if (!memcmp(opts
, "mode", 4)) {
1336 data
->root_mode
= (value
& 0555) | S_IFDIR
;
1337 data
->perms
.mode
= (value
& 0666) | S_IFREG
;
1344 if (!memcmp(opts
, "uid", 3)) {
1345 data
->perms
.uid
= make_kuid(current_user_ns(), value
);
1346 if (!uid_valid(data
->perms
.uid
)) {
1347 pr_err("%s: unmapped value: %lu\n", opts
, value
);
1350 } else if (!memcmp(opts
, "gid", 3)) {
1351 data
->perms
.gid
= make_kgid(current_user_ns(), value
);
1352 if (!gid_valid(data
->perms
.gid
)) {
1353 pr_err("%s: unmapped value: %lu\n", opts
, value
);
1363 pr_err("%s: invalid option\n", opts
);
1367 /* Next iteration */
1376 /* "mount -t functionfs dev_name /dev/function" ends up here */
1378 static struct dentry
*
1379 ffs_fs_mount(struct file_system_type
*t
, int flags
,
1380 const char *dev_name
, void *opts
)
1382 struct ffs_sb_fill_data data
= {
1384 .mode
= S_IFREG
| 0600,
1385 .uid
= GLOBAL_ROOT_UID
,
1386 .gid
= GLOBAL_ROOT_GID
,
1388 .root_mode
= S_IFDIR
| 0500,
1389 .no_disconnect
= false,
1394 struct ffs_data
*ffs
;
1398 ret
= ffs_fs_parse_opts(&data
, opts
);
1399 if (unlikely(ret
< 0))
1400 return ERR_PTR(ret
);
1402 ffs
= ffs_data_new();
1404 return ERR_PTR(-ENOMEM
);
1405 ffs
->file_perms
= data
.perms
;
1406 ffs
->no_disconnect
= data
.no_disconnect
;
1408 ffs
->dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
1409 if (unlikely(!ffs
->dev_name
)) {
1411 return ERR_PTR(-ENOMEM
);
1414 ffs_dev
= ffs_acquire_dev(dev_name
);
1415 if (IS_ERR(ffs_dev
)) {
1417 return ERR_CAST(ffs_dev
);
1419 ffs
->private_data
= ffs_dev
;
1420 data
.ffs_data
= ffs
;
1422 rv
= mount_nodev(t
, flags
, &data
, ffs_sb_fill
);
1423 if (IS_ERR(rv
) && data
.ffs_data
) {
1424 ffs_release_dev(data
.ffs_data
);
1425 ffs_data_put(data
.ffs_data
);
1431 ffs_fs_kill_sb(struct super_block
*sb
)
1435 kill_litter_super(sb
);
1436 if (sb
->s_fs_info
) {
1437 ffs_release_dev(sb
->s_fs_info
);
1438 ffs_data_closed(sb
->s_fs_info
);
1439 ffs_data_put(sb
->s_fs_info
);
1443 static struct file_system_type ffs_fs_type
= {
1444 .owner
= THIS_MODULE
,
1445 .name
= "functionfs",
1446 .mount
= ffs_fs_mount
,
1447 .kill_sb
= ffs_fs_kill_sb
,
1449 MODULE_ALIAS_FS("functionfs");
1452 /* Driver's main init/cleanup functions *************************************/
1454 static int functionfs_init(void)
1460 ret
= register_filesystem(&ffs_fs_type
);
1462 pr_info("file system registered\n");
1464 pr_err("failed registering file system (%d)\n", ret
);
1469 static void functionfs_cleanup(void)
1473 pr_info("unloading\n");
1474 unregister_filesystem(&ffs_fs_type
);
1478 /* ffs_data and ffs_function construction and destruction code **************/
1480 static void ffs_data_clear(struct ffs_data
*ffs
);
1481 static void ffs_data_reset(struct ffs_data
*ffs
);
1483 static void ffs_data_get(struct ffs_data
*ffs
)
1487 atomic_inc(&ffs
->ref
);
1490 static void ffs_data_opened(struct ffs_data
*ffs
)
1494 atomic_inc(&ffs
->ref
);
1495 if (atomic_add_return(1, &ffs
->opened
) == 1 &&
1496 ffs
->state
== FFS_DEACTIVATED
) {
1497 ffs
->state
= FFS_CLOSING
;
1498 ffs_data_reset(ffs
);
1502 static void ffs_data_put(struct ffs_data
*ffs
)
1506 if (unlikely(atomic_dec_and_test(&ffs
->ref
))) {
1507 pr_info("%s(): freeing\n", __func__
);
1508 ffs_data_clear(ffs
);
1509 BUG_ON(waitqueue_active(&ffs
->ev
.waitq
) ||
1510 waitqueue_active(&ffs
->ep0req_completion
.wait
));
1511 kfree(ffs
->dev_name
);
1516 static void ffs_data_closed(struct ffs_data
*ffs
)
1520 if (atomic_dec_and_test(&ffs
->opened
)) {
1521 if (ffs
->no_disconnect
) {
1522 ffs
->state
= FFS_DEACTIVATED
;
1524 ffs_epfiles_destroy(ffs
->epfiles
,
1526 ffs
->epfiles
= NULL
;
1528 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
1529 __ffs_ep0_stall(ffs
);
1531 ffs
->state
= FFS_CLOSING
;
1532 ffs_data_reset(ffs
);
1535 if (atomic_read(&ffs
->opened
) < 0) {
1536 ffs
->state
= FFS_CLOSING
;
1537 ffs_data_reset(ffs
);
1543 static struct ffs_data
*ffs_data_new(void)
1545 struct ffs_data
*ffs
= kzalloc(sizeof *ffs
, GFP_KERNEL
);
1551 atomic_set(&ffs
->ref
, 1);
1552 atomic_set(&ffs
->opened
, 0);
1553 ffs
->state
= FFS_READ_DESCRIPTORS
;
1554 mutex_init(&ffs
->mutex
);
1555 spin_lock_init(&ffs
->eps_lock
);
1556 init_waitqueue_head(&ffs
->ev
.waitq
);
1557 init_completion(&ffs
->ep0req_completion
);
1559 /* XXX REVISIT need to update it in some places, or do we? */
1560 ffs
->ev
.can_stall
= 1;
1565 static void ffs_data_clear(struct ffs_data
*ffs
)
1571 BUG_ON(ffs
->gadget
);
1574 ffs_epfiles_destroy(ffs
->epfiles
, ffs
->eps_count
);
1576 if (ffs
->ffs_eventfd
)
1577 eventfd_ctx_put(ffs
->ffs_eventfd
);
1579 kfree(ffs
->raw_descs_data
);
1580 kfree(ffs
->raw_strings
);
1581 kfree(ffs
->stringtabs
);
1584 static void ffs_data_reset(struct ffs_data
*ffs
)
1588 ffs_data_clear(ffs
);
1590 ffs
->epfiles
= NULL
;
1591 ffs
->raw_descs_data
= NULL
;
1592 ffs
->raw_descs
= NULL
;
1593 ffs
->raw_strings
= NULL
;
1594 ffs
->stringtabs
= NULL
;
1596 ffs
->raw_descs_length
= 0;
1597 ffs
->fs_descs_count
= 0;
1598 ffs
->hs_descs_count
= 0;
1599 ffs
->ss_descs_count
= 0;
1601 ffs
->strings_count
= 0;
1602 ffs
->interfaces_count
= 0;
1607 ffs
->state
= FFS_READ_DESCRIPTORS
;
1608 ffs
->setup_state
= FFS_NO_SETUP
;
1613 static int functionfs_bind(struct ffs_data
*ffs
, struct usb_composite_dev
*cdev
)
1615 struct usb_gadget_strings
**lang
;
1620 if (WARN_ON(ffs
->state
!= FFS_ACTIVE
1621 || test_and_set_bit(FFS_FL_BOUND
, &ffs
->flags
)))
1624 first_id
= usb_string_ids_n(cdev
, ffs
->strings_count
);
1625 if (unlikely(first_id
< 0))
1628 ffs
->ep0req
= usb_ep_alloc_request(cdev
->gadget
->ep0
, GFP_KERNEL
);
1629 if (unlikely(!ffs
->ep0req
))
1631 ffs
->ep0req
->complete
= ffs_ep0_complete
;
1632 ffs
->ep0req
->context
= ffs
;
1634 lang
= ffs
->stringtabs
;
1636 for (; *lang
; ++lang
) {
1637 struct usb_string
*str
= (*lang
)->strings
;
1639 for (; str
->s
; ++id
, ++str
)
1644 ffs
->gadget
= cdev
->gadget
;
1649 static void functionfs_unbind(struct ffs_data
*ffs
)
1653 if (!WARN_ON(!ffs
->gadget
)) {
1654 usb_ep_free_request(ffs
->gadget
->ep0
, ffs
->ep0req
);
1657 clear_bit(FFS_FL_BOUND
, &ffs
->flags
);
1662 static int ffs_epfiles_create(struct ffs_data
*ffs
)
1664 struct ffs_epfile
*epfile
, *epfiles
;
1669 count
= ffs
->eps_count
;
1670 epfiles
= kcalloc(count
, sizeof(*epfiles
), GFP_KERNEL
);
1675 for (i
= 1; i
<= count
; ++i
, ++epfile
) {
1677 mutex_init(&epfile
->mutex
);
1678 init_waitqueue_head(&epfile
->wait
);
1679 if (ffs
->user_flags
& FUNCTIONFS_VIRTUAL_ADDR
)
1680 sprintf(epfile
->name
, "ep%02x", ffs
->eps_addrmap
[i
]);
1682 sprintf(epfile
->name
, "ep%u", i
);
1683 epfile
->dentry
= ffs_sb_create_file(ffs
->sb
, epfile
->name
,
1685 &ffs_epfile_operations
);
1686 if (unlikely(!epfile
->dentry
)) {
1687 ffs_epfiles_destroy(epfiles
, i
- 1);
1692 ffs
->epfiles
= epfiles
;
1696 static void ffs_epfiles_destroy(struct ffs_epfile
*epfiles
, unsigned count
)
1698 struct ffs_epfile
*epfile
= epfiles
;
1702 for (; count
; --count
, ++epfile
) {
1703 BUG_ON(mutex_is_locked(&epfile
->mutex
) ||
1704 waitqueue_active(&epfile
->wait
));
1705 if (epfile
->dentry
) {
1706 d_delete(epfile
->dentry
);
1707 dput(epfile
->dentry
);
1708 epfile
->dentry
= NULL
;
1715 static void ffs_func_eps_disable(struct ffs_function
*func
)
1717 struct ffs_ep
*ep
= func
->eps
;
1718 struct ffs_epfile
*epfile
= func
->ffs
->epfiles
;
1719 unsigned count
= func
->ffs
->eps_count
;
1720 unsigned long flags
;
1724 mutex_lock(&epfile
->mutex
);
1725 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1726 /* pending requests get nuked */
1728 usb_ep_disable(ep
->ep
);
1730 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1734 kfree(epfile
->read_buffer
);
1735 epfile
->read_buffer
= NULL
;
1736 mutex_unlock(&epfile
->mutex
);
1742 static int ffs_func_eps_enable(struct ffs_function
*func
)
1744 struct ffs_data
*ffs
= func
->ffs
;
1745 struct ffs_ep
*ep
= func
->eps
;
1746 struct ffs_epfile
*epfile
= ffs
->epfiles
;
1747 unsigned count
= ffs
->eps_count
;
1748 unsigned long flags
;
1751 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
1753 struct usb_endpoint_descriptor
*ds
;
1756 if (ffs
->gadget
->speed
== USB_SPEED_SUPER
)
1758 else if (ffs
->gadget
->speed
== USB_SPEED_HIGH
)
1763 /* fall-back to lower speed if desc missing for current speed */
1765 ds
= ep
->descs
[desc_idx
];
1766 } while (!ds
&& --desc_idx
>= 0);
1773 ep
->ep
->driver_data
= ep
;
1775 ret
= usb_ep_enable(ep
->ep
);
1778 epfile
->in
= usb_endpoint_dir_in(ds
);
1779 epfile
->isoc
= usb_endpoint_xfer_isoc(ds
);
1784 wake_up(&epfile
->wait
);
1789 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
1795 /* Parsing and building descriptors and strings *****************************/
1798 * This validates if data pointed by data is a valid USB descriptor as
1799 * well as record how many interfaces, endpoints and strings are
1800 * required by given configuration. Returns address after the
1801 * descriptor or NULL if data is invalid.
1804 enum ffs_entity_type
{
1805 FFS_DESCRIPTOR
, FFS_INTERFACE
, FFS_STRING
, FFS_ENDPOINT
1808 enum ffs_os_desc_type
{
1809 FFS_OS_DESC
, FFS_OS_DESC_EXT_COMPAT
, FFS_OS_DESC_EXT_PROP
1812 typedef int (*ffs_entity_callback
)(enum ffs_entity_type entity
,
1814 struct usb_descriptor_header
*desc
,
1817 typedef int (*ffs_os_desc_callback
)(enum ffs_os_desc_type entity
,
1818 struct usb_os_desc_header
*h
, void *data
,
1819 unsigned len
, void *priv
);
1821 static int __must_check
ffs_do_single_desc(char *data
, unsigned len
,
1822 ffs_entity_callback entity
,
1825 struct usb_descriptor_header
*_ds
= (void *)data
;
1831 /* At least two bytes are required: length and type */
1833 pr_vdebug("descriptor too short\n");
1837 /* If we have at least as many bytes as the descriptor takes? */
1838 length
= _ds
->bLength
;
1840 pr_vdebug("descriptor longer then available data\n");
1844 #define __entity_check_INTERFACE(val) 1
1845 #define __entity_check_STRING(val) (val)
1846 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1847 #define __entity(type, val) do { \
1848 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1849 if (unlikely(!__entity_check_ ##type(val))) { \
1850 pr_vdebug("invalid entity's value\n"); \
1853 ret = entity(FFS_ ##type, &val, _ds, priv); \
1854 if (unlikely(ret < 0)) { \
1855 pr_debug("entity " #type "(%02x); ret = %d\n", \
1861 /* Parse descriptor depending on type. */
1862 switch (_ds
->bDescriptorType
) {
1866 case USB_DT_DEVICE_QUALIFIER
:
1867 /* function can't have any of those */
1868 pr_vdebug("descriptor reserved for gadget: %d\n",
1869 _ds
->bDescriptorType
);
1872 case USB_DT_INTERFACE
: {
1873 struct usb_interface_descriptor
*ds
= (void *)_ds
;
1874 pr_vdebug("interface descriptor\n");
1875 if (length
!= sizeof *ds
)
1878 __entity(INTERFACE
, ds
->bInterfaceNumber
);
1880 __entity(STRING
, ds
->iInterface
);
1884 case USB_DT_ENDPOINT
: {
1885 struct usb_endpoint_descriptor
*ds
= (void *)_ds
;
1886 pr_vdebug("endpoint descriptor\n");
1887 if (length
!= USB_DT_ENDPOINT_SIZE
&&
1888 length
!= USB_DT_ENDPOINT_AUDIO_SIZE
)
1890 __entity(ENDPOINT
, ds
->bEndpointAddress
);
1895 pr_vdebug("hid descriptor\n");
1896 if (length
!= sizeof(struct hid_descriptor
))
1901 if (length
!= sizeof(struct usb_otg_descriptor
))
1905 case USB_DT_INTERFACE_ASSOCIATION
: {
1906 struct usb_interface_assoc_descriptor
*ds
= (void *)_ds
;
1907 pr_vdebug("interface association descriptor\n");
1908 if (length
!= sizeof *ds
)
1911 __entity(STRING
, ds
->iFunction
);
1915 case USB_DT_SS_ENDPOINT_COMP
:
1916 pr_vdebug("EP SS companion descriptor\n");
1917 if (length
!= sizeof(struct usb_ss_ep_comp_descriptor
))
1921 case USB_DT_OTHER_SPEED_CONFIG
:
1922 case USB_DT_INTERFACE_POWER
:
1924 case USB_DT_SECURITY
:
1925 case USB_DT_CS_RADIO_CONTROL
:
1927 pr_vdebug("unimplemented descriptor: %d\n", _ds
->bDescriptorType
);
1931 /* We should never be here */
1932 pr_vdebug("unknown descriptor: %d\n", _ds
->bDescriptorType
);
1936 pr_vdebug("invalid length: %d (descriptor %d)\n",
1937 _ds
->bLength
, _ds
->bDescriptorType
);
1942 #undef __entity_check_DESCRIPTOR
1943 #undef __entity_check_INTERFACE
1944 #undef __entity_check_STRING
1945 #undef __entity_check_ENDPOINT
1950 static int __must_check
ffs_do_descs(unsigned count
, char *data
, unsigned len
,
1951 ffs_entity_callback entity
, void *priv
)
1953 const unsigned _len
= len
;
1954 unsigned long num
= 0;
1964 /* Record "descriptor" entity */
1965 ret
= entity(FFS_DESCRIPTOR
, (u8
*)num
, (void *)data
, priv
);
1966 if (unlikely(ret
< 0)) {
1967 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1975 ret
= ffs_do_single_desc(data
, len
, entity
, priv
);
1976 if (unlikely(ret
< 0)) {
1977 pr_debug("%s returns %d\n", __func__
, ret
);
1987 static int __ffs_data_do_entity(enum ffs_entity_type type
,
1988 u8
*valuep
, struct usb_descriptor_header
*desc
,
1991 struct ffs_desc_helper
*helper
= priv
;
1992 struct usb_endpoint_descriptor
*d
;
1997 case FFS_DESCRIPTOR
:
2002 * Interfaces are indexed from zero so if we
2003 * encountered interface "n" then there are at least
2006 if (*valuep
>= helper
->interfaces_count
)
2007 helper
->interfaces_count
= *valuep
+ 1;
2012 * Strings are indexed from 1 (0 is magic ;) reserved
2013 * for languages list or some such)
2015 if (*valuep
> helper
->ffs
->strings_count
)
2016 helper
->ffs
->strings_count
= *valuep
;
2021 helper
->eps_count
++;
2022 if (helper
->eps_count
>= 15)
2024 /* Check if descriptors for any speed were already parsed */
2025 if (!helper
->ffs
->eps_count
&& !helper
->ffs
->interfaces_count
)
2026 helper
->ffs
->eps_addrmap
[helper
->eps_count
] =
2027 d
->bEndpointAddress
;
2028 else if (helper
->ffs
->eps_addrmap
[helper
->eps_count
] !=
2029 d
->bEndpointAddress
)
2037 static int __ffs_do_os_desc_header(enum ffs_os_desc_type
*next_type
,
2038 struct usb_os_desc_header
*desc
)
2040 u16 bcd_version
= le16_to_cpu(desc
->bcdVersion
);
2041 u16 w_index
= le16_to_cpu(desc
->wIndex
);
2043 if (bcd_version
!= 1) {
2044 pr_vdebug("unsupported os descriptors version: %d",
2050 *next_type
= FFS_OS_DESC_EXT_COMPAT
;
2053 *next_type
= FFS_OS_DESC_EXT_PROP
;
2056 pr_vdebug("unsupported os descriptor type: %d", w_index
);
2060 return sizeof(*desc
);
2064 * Process all extended compatibility/extended property descriptors
2065 * of a feature descriptor
2067 static int __must_check
ffs_do_single_os_desc(char *data
, unsigned len
,
2068 enum ffs_os_desc_type type
,
2070 ffs_os_desc_callback entity
,
2072 struct usb_os_desc_header
*h
)
2075 const unsigned _len
= len
;
2079 /* loop over all ext compat/ext prop descriptors */
2080 while (feature_count
--) {
2081 ret
= entity(type
, h
, data
, len
, priv
);
2082 if (unlikely(ret
< 0)) {
2083 pr_debug("bad OS descriptor, type: %d\n", type
);
2092 /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2093 static int __must_check
ffs_do_os_descs(unsigned count
,
2094 char *data
, unsigned len
,
2095 ffs_os_desc_callback entity
, void *priv
)
2097 const unsigned _len
= len
;
2098 unsigned long num
= 0;
2102 for (num
= 0; num
< count
; ++num
) {
2104 enum ffs_os_desc_type type
;
2106 struct usb_os_desc_header
*desc
= (void *)data
;
2108 if (len
< sizeof(*desc
))
2112 * Record "descriptor" entity.
2113 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2114 * Move the data pointer to the beginning of extended
2115 * compatibilities proper or extended properties proper
2116 * portions of the data
2118 if (le32_to_cpu(desc
->dwLength
) > len
)
2121 ret
= __ffs_do_os_desc_header(&type
, desc
);
2122 if (unlikely(ret
< 0)) {
2123 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2128 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2130 feature_count
= le16_to_cpu(desc
->wCount
);
2131 if (type
== FFS_OS_DESC_EXT_COMPAT
&&
2132 (feature_count
> 255 || desc
->Reserved
))
2138 * Process all function/property descriptors
2139 * of this Feature Descriptor
2141 ret
= ffs_do_single_os_desc(data
, len
, type
,
2142 feature_count
, entity
, priv
, desc
);
2143 if (unlikely(ret
< 0)) {
2144 pr_debug("%s returns %d\n", __func__
, ret
);
2155 * Validate contents of the buffer from userspace related to OS descriptors.
2157 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type
,
2158 struct usb_os_desc_header
*h
, void *data
,
2159 unsigned len
, void *priv
)
2161 struct ffs_data
*ffs
= priv
;
2167 case FFS_OS_DESC_EXT_COMPAT
: {
2168 struct usb_ext_compat_desc
*d
= data
;
2171 if (len
< sizeof(*d
) ||
2172 d
->bFirstInterfaceNumber
>= ffs
->interfaces_count
||
2175 for (i
= 0; i
< ARRAY_SIZE(d
->Reserved2
); ++i
)
2176 if (d
->Reserved2
[i
])
2179 length
= sizeof(struct usb_ext_compat_desc
);
2182 case FFS_OS_DESC_EXT_PROP
: {
2183 struct usb_ext_prop_desc
*d
= data
;
2187 if (len
< sizeof(*d
) || h
->interface
>= ffs
->interfaces_count
)
2189 length
= le32_to_cpu(d
->dwSize
);
2190 type
= le32_to_cpu(d
->dwPropertyDataType
);
2191 if (type
< USB_EXT_PROP_UNICODE
||
2192 type
> USB_EXT_PROP_UNICODE_MULTI
) {
2193 pr_vdebug("unsupported os descriptor property type: %d",
2197 pnl
= le16_to_cpu(d
->wPropertyNameLength
);
2198 pdl
= le32_to_cpu(*(u32
*)((u8
*)data
+ 10 + pnl
));
2199 if (length
!= 14 + pnl
+ pdl
) {
2200 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2201 length
, pnl
, pdl
, type
);
2204 ++ffs
->ms_os_descs_ext_prop_count
;
2205 /* property name reported to the host as "WCHAR"s */
2206 ffs
->ms_os_descs_ext_prop_name_len
+= pnl
* 2;
2207 ffs
->ms_os_descs_ext_prop_data_len
+= pdl
;
2211 pr_vdebug("unknown descriptor: %d\n", type
);
2217 static int __ffs_data_got_descs(struct ffs_data
*ffs
,
2218 char *const _data
, size_t len
)
2220 char *data
= _data
, *raw_descs
;
2221 unsigned os_descs_count
= 0, counts
[3], flags
;
2222 int ret
= -EINVAL
, i
;
2223 struct ffs_desc_helper helper
;
2227 if (get_unaligned_le32(data
+ 4) != len
)
2230 switch (get_unaligned_le32(data
)) {
2231 case FUNCTIONFS_DESCRIPTORS_MAGIC
:
2232 flags
= FUNCTIONFS_HAS_FS_DESC
| FUNCTIONFS_HAS_HS_DESC
;
2236 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2
:
2237 flags
= get_unaligned_le32(data
+ 8);
2238 ffs
->user_flags
= flags
;
2239 if (flags
& ~(FUNCTIONFS_HAS_FS_DESC
|
2240 FUNCTIONFS_HAS_HS_DESC
|
2241 FUNCTIONFS_HAS_SS_DESC
|
2242 FUNCTIONFS_HAS_MS_OS_DESC
|
2243 FUNCTIONFS_VIRTUAL_ADDR
|
2244 FUNCTIONFS_EVENTFD
)) {
2255 if (flags
& FUNCTIONFS_EVENTFD
) {
2259 eventfd_ctx_fdget((int)get_unaligned_le32(data
));
2260 if (IS_ERR(ffs
->ffs_eventfd
)) {
2261 ret
= PTR_ERR(ffs
->ffs_eventfd
);
2262 ffs
->ffs_eventfd
= NULL
;
2269 /* Read fs_count, hs_count and ss_count (if present) */
2270 for (i
= 0; i
< 3; ++i
) {
2271 if (!(flags
& (1 << i
))) {
2273 } else if (len
< 4) {
2276 counts
[i
] = get_unaligned_le32(data
);
2281 if (flags
& (1 << i
)) {
2282 os_descs_count
= get_unaligned_le32(data
);
2287 /* Read descriptors */
2290 for (i
= 0; i
< 3; ++i
) {
2293 helper
.interfaces_count
= 0;
2294 helper
.eps_count
= 0;
2295 ret
= ffs_do_descs(counts
[i
], data
, len
,
2296 __ffs_data_do_entity
, &helper
);
2299 if (!ffs
->eps_count
&& !ffs
->interfaces_count
) {
2300 ffs
->eps_count
= helper
.eps_count
;
2301 ffs
->interfaces_count
= helper
.interfaces_count
;
2303 if (ffs
->eps_count
!= helper
.eps_count
) {
2307 if (ffs
->interfaces_count
!= helper
.interfaces_count
) {
2315 if (os_descs_count
) {
2316 ret
= ffs_do_os_descs(os_descs_count
, data
, len
,
2317 __ffs_data_do_os_desc
, ffs
);
2324 if (raw_descs
== data
|| len
) {
2329 ffs
->raw_descs_data
= _data
;
2330 ffs
->raw_descs
= raw_descs
;
2331 ffs
->raw_descs_length
= data
- raw_descs
;
2332 ffs
->fs_descs_count
= counts
[0];
2333 ffs
->hs_descs_count
= counts
[1];
2334 ffs
->ss_descs_count
= counts
[2];
2335 ffs
->ms_os_descs_count
= os_descs_count
;
2344 static int __ffs_data_got_strings(struct ffs_data
*ffs
,
2345 char *const _data
, size_t len
)
2347 u32 str_count
, needed_count
, lang_count
;
2348 struct usb_gadget_strings
**stringtabs
, *t
;
2349 const char *data
= _data
;
2350 struct usb_string
*s
;
2354 if (unlikely(get_unaligned_le32(data
) != FUNCTIONFS_STRINGS_MAGIC
||
2355 get_unaligned_le32(data
+ 4) != len
))
2357 str_count
= get_unaligned_le32(data
+ 8);
2358 lang_count
= get_unaligned_le32(data
+ 12);
2360 /* if one is zero the other must be zero */
2361 if (unlikely(!str_count
!= !lang_count
))
2364 /* Do we have at least as many strings as descriptors need? */
2365 needed_count
= ffs
->strings_count
;
2366 if (unlikely(str_count
< needed_count
))
2370 * If we don't need any strings just return and free all
2373 if (!needed_count
) {
2378 /* Allocate everything in one chunk so there's less maintenance. */
2382 vla_item(d
, struct usb_gadget_strings
*, stringtabs
,
2384 vla_item(d
, struct usb_gadget_strings
, stringtab
, lang_count
);
2385 vla_item(d
, struct usb_string
, strings
,
2386 lang_count
*(needed_count
+1));
2388 char *vlabuf
= kmalloc(vla_group_size(d
), GFP_KERNEL
);
2390 if (unlikely(!vlabuf
)) {
2395 /* Initialize the VLA pointers */
2396 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
2397 t
= vla_ptr(vlabuf
, d
, stringtab
);
2400 *stringtabs
++ = t
++;
2404 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2405 stringtabs
= vla_ptr(vlabuf
, d
, stringtabs
);
2406 t
= vla_ptr(vlabuf
, d
, stringtab
);
2407 s
= vla_ptr(vlabuf
, d
, strings
);
2410 /* For each language */
2414 do { /* lang_count > 0 so we can use do-while */
2415 unsigned needed
= needed_count
;
2417 if (unlikely(len
< 3))
2419 t
->language
= get_unaligned_le16(data
);
2426 /* For each string */
2427 do { /* str_count > 0 so we can use do-while */
2428 size_t length
= strnlen(data
, len
);
2430 if (unlikely(length
== len
))
2434 * User may provide more strings then we need,
2435 * if that's the case we simply ignore the
2438 if (likely(needed
)) {
2440 * s->id will be set while adding
2441 * function to configuration so for
2442 * now just leave garbage here.
2451 } while (--str_count
);
2453 s
->id
= 0; /* terminator */
2457 } while (--lang_count
);
2459 /* Some garbage left? */
2464 ffs
->stringtabs
= stringtabs
;
2465 ffs
->raw_strings
= _data
;
2477 /* Events handling and management *******************************************/
2479 static void __ffs_event_add(struct ffs_data
*ffs
,
2480 enum usb_functionfs_event_type type
)
2482 enum usb_functionfs_event_type rem_type1
, rem_type2
= type
;
2486 * Abort any unhandled setup
2488 * We do not need to worry about some cmpxchg() changing value
2489 * of ffs->setup_state without holding the lock because when
2490 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2491 * the source does nothing.
2493 if (ffs
->setup_state
== FFS_SETUP_PENDING
)
2494 ffs
->setup_state
= FFS_SETUP_CANCELLED
;
2497 * Logic of this function guarantees that there are at most four pending
2498 * evens on ffs->ev.types queue. This is important because the queue
2499 * has space for four elements only and __ffs_ep0_read_events function
2500 * depends on that limit as well. If more event types are added, those
2501 * limits have to be revisited or guaranteed to still hold.
2504 case FUNCTIONFS_RESUME
:
2505 rem_type2
= FUNCTIONFS_SUSPEND
;
2507 case FUNCTIONFS_SUSPEND
:
2508 case FUNCTIONFS_SETUP
:
2510 /* Discard all similar events */
2513 case FUNCTIONFS_BIND
:
2514 case FUNCTIONFS_UNBIND
:
2515 case FUNCTIONFS_DISABLE
:
2516 case FUNCTIONFS_ENABLE
:
2517 /* Discard everything other then power management. */
2518 rem_type1
= FUNCTIONFS_SUSPEND
;
2519 rem_type2
= FUNCTIONFS_RESUME
;
2524 WARN(1, "%d: unknown event, this should not happen\n", type
);
2529 u8
*ev
= ffs
->ev
.types
, *out
= ev
;
2530 unsigned n
= ffs
->ev
.count
;
2531 for (; n
; --n
, ++ev
)
2532 if ((*ev
== rem_type1
|| *ev
== rem_type2
) == neg
)
2535 pr_vdebug("purging event %d\n", *ev
);
2536 ffs
->ev
.count
= out
- ffs
->ev
.types
;
2539 pr_vdebug("adding event %d\n", type
);
2540 ffs
->ev
.types
[ffs
->ev
.count
++] = type
;
2541 wake_up_locked(&ffs
->ev
.waitq
);
2542 if (ffs
->ffs_eventfd
)
2543 eventfd_signal(ffs
->ffs_eventfd
, 1);
2546 static void ffs_event_add(struct ffs_data
*ffs
,
2547 enum usb_functionfs_event_type type
)
2549 unsigned long flags
;
2550 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
2551 __ffs_event_add(ffs
, type
);
2552 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
2555 /* Bind/unbind USB function hooks *******************************************/
2557 static int ffs_ep_addr2idx(struct ffs_data
*ffs
, u8 endpoint_address
)
2561 for (i
= 1; i
< ARRAY_SIZE(ffs
->eps_addrmap
); ++i
)
2562 if (ffs
->eps_addrmap
[i
] == endpoint_address
)
2567 static int __ffs_func_bind_do_descs(enum ffs_entity_type type
, u8
*valuep
,
2568 struct usb_descriptor_header
*desc
,
2571 struct usb_endpoint_descriptor
*ds
= (void *)desc
;
2572 struct ffs_function
*func
= priv
;
2573 struct ffs_ep
*ffs_ep
;
2574 unsigned ep_desc_id
;
2576 static const char *speed_names
[] = { "full", "high", "super" };
2578 if (type
!= FFS_DESCRIPTOR
)
2582 * If ss_descriptors is not NULL, we are reading super speed
2583 * descriptors; if hs_descriptors is not NULL, we are reading high
2584 * speed descriptors; otherwise, we are reading full speed
2587 if (func
->function
.ss_descriptors
) {
2589 func
->function
.ss_descriptors
[(long)valuep
] = desc
;
2590 } else if (func
->function
.hs_descriptors
) {
2592 func
->function
.hs_descriptors
[(long)valuep
] = desc
;
2595 func
->function
.fs_descriptors
[(long)valuep
] = desc
;
2598 if (!desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
2601 idx
= ffs_ep_addr2idx(func
->ffs
, ds
->bEndpointAddress
) - 1;
2605 ffs_ep
= func
->eps
+ idx
;
2607 if (unlikely(ffs_ep
->descs
[ep_desc_id
])) {
2608 pr_err("two %sspeed descriptors for EP %d\n",
2609 speed_names
[ep_desc_id
],
2610 ds
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2613 ffs_ep
->descs
[ep_desc_id
] = ds
;
2615 ffs_dump_mem(": Original ep desc", ds
, ds
->bLength
);
2617 ds
->bEndpointAddress
= ffs_ep
->descs
[0]->bEndpointAddress
;
2618 if (!ds
->wMaxPacketSize
)
2619 ds
->wMaxPacketSize
= ffs_ep
->descs
[0]->wMaxPacketSize
;
2621 struct usb_request
*req
;
2623 u8 bEndpointAddress
;
2626 * We back up bEndpointAddress because autoconfig overwrites
2627 * it with physical endpoint address.
2629 bEndpointAddress
= ds
->bEndpointAddress
;
2630 pr_vdebug("autoconfig\n");
2631 ep
= usb_ep_autoconfig(func
->gadget
, ds
);
2634 ep
->driver_data
= func
->eps
+ idx
;
2636 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
2642 func
->eps_revmap
[ds
->bEndpointAddress
&
2643 USB_ENDPOINT_NUMBER_MASK
] = idx
+ 1;
2645 * If we use virtual address mapping, we restore
2646 * original bEndpointAddress value.
2648 if (func
->ffs
->user_flags
& FUNCTIONFS_VIRTUAL_ADDR
)
2649 ds
->bEndpointAddress
= bEndpointAddress
;
2651 ffs_dump_mem(": Rewritten ep desc", ds
, ds
->bLength
);
2656 static int __ffs_func_bind_do_nums(enum ffs_entity_type type
, u8
*valuep
,
2657 struct usb_descriptor_header
*desc
,
2660 struct ffs_function
*func
= priv
;
2666 case FFS_DESCRIPTOR
:
2667 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2672 if (func
->interfaces_nums
[idx
] < 0) {
2673 int id
= usb_interface_id(func
->conf
, &func
->function
);
2674 if (unlikely(id
< 0))
2676 func
->interfaces_nums
[idx
] = id
;
2678 newValue
= func
->interfaces_nums
[idx
];
2682 /* String' IDs are allocated when fsf_data is bound to cdev */
2683 newValue
= func
->ffs
->stringtabs
[0]->strings
[*valuep
- 1].id
;
2688 * USB_DT_ENDPOINT are handled in
2689 * __ffs_func_bind_do_descs().
2691 if (desc
->bDescriptorType
== USB_DT_ENDPOINT
)
2694 idx
= (*valuep
& USB_ENDPOINT_NUMBER_MASK
) - 1;
2695 if (unlikely(!func
->eps
[idx
].ep
))
2699 struct usb_endpoint_descriptor
**descs
;
2700 descs
= func
->eps
[idx
].descs
;
2701 newValue
= descs
[descs
[0] ? 0 : 1]->bEndpointAddress
;
2706 pr_vdebug("%02x -> %02x\n", *valuep
, newValue
);
2711 static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type
,
2712 struct usb_os_desc_header
*h
, void *data
,
2713 unsigned len
, void *priv
)
2715 struct ffs_function
*func
= priv
;
2719 case FFS_OS_DESC_EXT_COMPAT
: {
2720 struct usb_ext_compat_desc
*desc
= data
;
2721 struct usb_os_desc_table
*t
;
2723 t
= &func
->function
.os_desc_table
[desc
->bFirstInterfaceNumber
];
2724 t
->if_id
= func
->interfaces_nums
[desc
->bFirstInterfaceNumber
];
2725 memcpy(t
->os_desc
->ext_compat_id
, &desc
->CompatibleID
,
2726 ARRAY_SIZE(desc
->CompatibleID
) +
2727 ARRAY_SIZE(desc
->SubCompatibleID
));
2728 length
= sizeof(*desc
);
2731 case FFS_OS_DESC_EXT_PROP
: {
2732 struct usb_ext_prop_desc
*desc
= data
;
2733 struct usb_os_desc_table
*t
;
2734 struct usb_os_desc_ext_prop
*ext_prop
;
2735 char *ext_prop_name
;
2736 char *ext_prop_data
;
2738 t
= &func
->function
.os_desc_table
[h
->interface
];
2739 t
->if_id
= func
->interfaces_nums
[h
->interface
];
2741 ext_prop
= func
->ffs
->ms_os_descs_ext_prop_avail
;
2742 func
->ffs
->ms_os_descs_ext_prop_avail
+= sizeof(*ext_prop
);
2744 ext_prop
->type
= le32_to_cpu(desc
->dwPropertyDataType
);
2745 ext_prop
->name_len
= le16_to_cpu(desc
->wPropertyNameLength
);
2746 ext_prop
->data_len
= le32_to_cpu(*(u32
*)
2747 usb_ext_prop_data_len_ptr(data
, ext_prop
->name_len
));
2748 length
= ext_prop
->name_len
+ ext_prop
->data_len
+ 14;
2750 ext_prop_name
= func
->ffs
->ms_os_descs_ext_prop_name_avail
;
2751 func
->ffs
->ms_os_descs_ext_prop_name_avail
+=
2754 ext_prop_data
= func
->ffs
->ms_os_descs_ext_prop_data_avail
;
2755 func
->ffs
->ms_os_descs_ext_prop_data_avail
+=
2757 memcpy(ext_prop_data
,
2758 usb_ext_prop_data_ptr(data
, ext_prop
->name_len
),
2759 ext_prop
->data_len
);
2760 /* unicode data reported to the host as "WCHAR"s */
2761 switch (ext_prop
->type
) {
2762 case USB_EXT_PROP_UNICODE
:
2763 case USB_EXT_PROP_UNICODE_ENV
:
2764 case USB_EXT_PROP_UNICODE_LINK
:
2765 case USB_EXT_PROP_UNICODE_MULTI
:
2766 ext_prop
->data_len
*= 2;
2769 ext_prop
->data
= ext_prop_data
;
2771 memcpy(ext_prop_name
, usb_ext_prop_name_ptr(data
),
2772 ext_prop
->name_len
);
2773 /* property name reported to the host as "WCHAR"s */
2774 ext_prop
->name_len
*= 2;
2775 ext_prop
->name
= ext_prop_name
;
2777 t
->os_desc
->ext_prop_len
+=
2778 ext_prop
->name_len
+ ext_prop
->data_len
+ 14;
2779 ++t
->os_desc
->ext_prop_count
;
2780 list_add_tail(&ext_prop
->entry
, &t
->os_desc
->ext_prop
);
2784 pr_vdebug("unknown descriptor: %d\n", type
);
2790 static inline struct f_fs_opts
*ffs_do_functionfs_bind(struct usb_function
*f
,
2791 struct usb_configuration
*c
)
2793 struct ffs_function
*func
= ffs_func_from_usb(f
);
2794 struct f_fs_opts
*ffs_opts
=
2795 container_of(f
->fi
, struct f_fs_opts
, func_inst
);
2801 * Legacy gadget triggers binding in functionfs_ready_callback,
2802 * which already uses locking; taking the same lock here would
2805 * Configfs-enabled gadgets however do need ffs_dev_lock.
2807 if (!ffs_opts
->no_configfs
)
2809 ret
= ffs_opts
->dev
->desc_ready
? 0 : -ENODEV
;
2810 func
->ffs
= ffs_opts
->dev
->ffs_data
;
2811 if (!ffs_opts
->no_configfs
)
2814 return ERR_PTR(ret
);
2817 func
->gadget
= c
->cdev
->gadget
;
2820 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2821 * configurations are bound in sequence with list_for_each_entry,
2822 * in each configuration its functions are bound in sequence
2823 * with list_for_each_entry, so we assume no race condition
2824 * with regard to ffs_opts->bound access
2826 if (!ffs_opts
->refcnt
) {
2827 ret
= functionfs_bind(func
->ffs
, c
->cdev
);
2829 return ERR_PTR(ret
);
2832 func
->function
.strings
= func
->ffs
->stringtabs
;
2837 static int _ffs_func_bind(struct usb_configuration
*c
,
2838 struct usb_function
*f
)
2840 struct ffs_function
*func
= ffs_func_from_usb(f
);
2841 struct ffs_data
*ffs
= func
->ffs
;
2843 const int full
= !!func
->ffs
->fs_descs_count
;
2844 const int high
= gadget_is_dualspeed(func
->gadget
) &&
2845 func
->ffs
->hs_descs_count
;
2846 const int super
= gadget_is_superspeed(func
->gadget
) &&
2847 func
->ffs
->ss_descs_count
;
2849 int fs_len
, hs_len
, ss_len
, ret
, i
;
2850 struct ffs_ep
*eps_ptr
;
2852 /* Make it a single chunk, less management later on */
2854 vla_item_with_sz(d
, struct ffs_ep
, eps
, ffs
->eps_count
);
2855 vla_item_with_sz(d
, struct usb_descriptor_header
*, fs_descs
,
2856 full
? ffs
->fs_descs_count
+ 1 : 0);
2857 vla_item_with_sz(d
, struct usb_descriptor_header
*, hs_descs
,
2858 high
? ffs
->hs_descs_count
+ 1 : 0);
2859 vla_item_with_sz(d
, struct usb_descriptor_header
*, ss_descs
,
2860 super
? ffs
->ss_descs_count
+ 1 : 0);
2861 vla_item_with_sz(d
, short, inums
, ffs
->interfaces_count
);
2862 vla_item_with_sz(d
, struct usb_os_desc_table
, os_desc_table
,
2863 c
->cdev
->use_os_string
? ffs
->interfaces_count
: 0);
2864 vla_item_with_sz(d
, char[16], ext_compat
,
2865 c
->cdev
->use_os_string
? ffs
->interfaces_count
: 0);
2866 vla_item_with_sz(d
, struct usb_os_desc
, os_desc
,
2867 c
->cdev
->use_os_string
? ffs
->interfaces_count
: 0);
2868 vla_item_with_sz(d
, struct usb_os_desc_ext_prop
, ext_prop
,
2869 ffs
->ms_os_descs_ext_prop_count
);
2870 vla_item_with_sz(d
, char, ext_prop_name
,
2871 ffs
->ms_os_descs_ext_prop_name_len
);
2872 vla_item_with_sz(d
, char, ext_prop_data
,
2873 ffs
->ms_os_descs_ext_prop_data_len
);
2874 vla_item_with_sz(d
, char, raw_descs
, ffs
->raw_descs_length
);
2879 /* Has descriptors only for speeds gadget does not support */
2880 if (unlikely(!(full
| high
| super
)))
2883 /* Allocate a single chunk, less management later on */
2884 vlabuf
= kzalloc(vla_group_size(d
), GFP_KERNEL
);
2885 if (unlikely(!vlabuf
))
2888 ffs
->ms_os_descs_ext_prop_avail
= vla_ptr(vlabuf
, d
, ext_prop
);
2889 ffs
->ms_os_descs_ext_prop_name_avail
=
2890 vla_ptr(vlabuf
, d
, ext_prop_name
);
2891 ffs
->ms_os_descs_ext_prop_data_avail
=
2892 vla_ptr(vlabuf
, d
, ext_prop_data
);
2894 /* Copy descriptors */
2895 memcpy(vla_ptr(vlabuf
, d
, raw_descs
), ffs
->raw_descs
,
2896 ffs
->raw_descs_length
);
2898 memset(vla_ptr(vlabuf
, d
, inums
), 0xff, d_inums__sz
);
2899 eps_ptr
= vla_ptr(vlabuf
, d
, eps
);
2900 for (i
= 0; i
< ffs
->eps_count
; i
++)
2901 eps_ptr
[i
].num
= -1;
2904 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2906 func
->eps
= vla_ptr(vlabuf
, d
, eps
);
2907 func
->interfaces_nums
= vla_ptr(vlabuf
, d
, inums
);
2910 * Go through all the endpoint descriptors and allocate
2911 * endpoints first, so that later we can rewrite the endpoint
2912 * numbers without worrying that it may be described later on.
2915 func
->function
.fs_descriptors
= vla_ptr(vlabuf
, d
, fs_descs
);
2916 fs_len
= ffs_do_descs(ffs
->fs_descs_count
,
2917 vla_ptr(vlabuf
, d
, raw_descs
),
2919 __ffs_func_bind_do_descs
, func
);
2920 if (unlikely(fs_len
< 0)) {
2929 func
->function
.hs_descriptors
= vla_ptr(vlabuf
, d
, hs_descs
);
2930 hs_len
= ffs_do_descs(ffs
->hs_descs_count
,
2931 vla_ptr(vlabuf
, d
, raw_descs
) + fs_len
,
2932 d_raw_descs__sz
- fs_len
,
2933 __ffs_func_bind_do_descs
, func
);
2934 if (unlikely(hs_len
< 0)) {
2942 if (likely(super
)) {
2943 func
->function
.ss_descriptors
= vla_ptr(vlabuf
, d
, ss_descs
);
2944 ss_len
= ffs_do_descs(ffs
->ss_descs_count
,
2945 vla_ptr(vlabuf
, d
, raw_descs
) + fs_len
+ hs_len
,
2946 d_raw_descs__sz
- fs_len
- hs_len
,
2947 __ffs_func_bind_do_descs
, func
);
2948 if (unlikely(ss_len
< 0)) {
2957 * Now handle interface numbers allocation and interface and
2958 * endpoint numbers rewriting. We can do that in one go
2961 ret
= ffs_do_descs(ffs
->fs_descs_count
+
2962 (high
? ffs
->hs_descs_count
: 0) +
2963 (super
? ffs
->ss_descs_count
: 0),
2964 vla_ptr(vlabuf
, d
, raw_descs
), d_raw_descs__sz
,
2965 __ffs_func_bind_do_nums
, func
);
2966 if (unlikely(ret
< 0))
2969 func
->function
.os_desc_table
= vla_ptr(vlabuf
, d
, os_desc_table
);
2970 if (c
->cdev
->use_os_string
) {
2971 for (i
= 0; i
< ffs
->interfaces_count
; ++i
) {
2972 struct usb_os_desc
*desc
;
2974 desc
= func
->function
.os_desc_table
[i
].os_desc
=
2975 vla_ptr(vlabuf
, d
, os_desc
) +
2976 i
* sizeof(struct usb_os_desc
);
2977 desc
->ext_compat_id
=
2978 vla_ptr(vlabuf
, d
, ext_compat
) + i
* 16;
2979 INIT_LIST_HEAD(&desc
->ext_prop
);
2981 ret
= ffs_do_os_descs(ffs
->ms_os_descs_count
,
2982 vla_ptr(vlabuf
, d
, raw_descs
) +
2983 fs_len
+ hs_len
+ ss_len
,
2984 d_raw_descs__sz
- fs_len
- hs_len
-
2986 __ffs_func_bind_do_os_desc
, func
);
2987 if (unlikely(ret
< 0))
2990 func
->function
.os_desc_n
=
2991 c
->cdev
->use_os_string
? ffs
->interfaces_count
: 0;
2993 /* And we're done */
2994 ffs_event_add(ffs
, FUNCTIONFS_BIND
);
2998 /* XXX Do we need to release all claimed endpoints here? */
3002 static int ffs_func_bind(struct usb_configuration
*c
,
3003 struct usb_function
*f
)
3005 struct f_fs_opts
*ffs_opts
= ffs_do_functionfs_bind(f
, c
);
3006 struct ffs_function
*func
= ffs_func_from_usb(f
);
3009 if (IS_ERR(ffs_opts
))
3010 return PTR_ERR(ffs_opts
);
3012 ret
= _ffs_func_bind(c
, f
);
3013 if (ret
&& !--ffs_opts
->refcnt
)
3014 functionfs_unbind(func
->ffs
);
3020 /* Other USB function hooks *************************************************/
3022 static void ffs_reset_work(struct work_struct
*work
)
3024 struct ffs_data
*ffs
= container_of(work
,
3025 struct ffs_data
, reset_work
);
3026 ffs_data_reset(ffs
);
3029 static int ffs_func_set_alt(struct usb_function
*f
,
3030 unsigned interface
, unsigned alt
)
3032 struct ffs_function
*func
= ffs_func_from_usb(f
);
3033 struct ffs_data
*ffs
= func
->ffs
;
3036 if (alt
!= (unsigned)-1) {
3037 intf
= ffs_func_revmap_intf(func
, interface
);
3038 if (unlikely(intf
< 0))
3043 ffs_func_eps_disable(ffs
->func
);
3045 if (ffs
->state
== FFS_DEACTIVATED
) {
3046 ffs
->state
= FFS_CLOSING
;
3047 INIT_WORK(&ffs
->reset_work
, ffs_reset_work
);
3048 schedule_work(&ffs
->reset_work
);
3052 if (ffs
->state
!= FFS_ACTIVE
)
3055 if (alt
== (unsigned)-1) {
3057 ffs_event_add(ffs
, FUNCTIONFS_DISABLE
);
3062 ret
= ffs_func_eps_enable(func
);
3063 if (likely(ret
>= 0))
3064 ffs_event_add(ffs
, FUNCTIONFS_ENABLE
);
3068 static void ffs_func_disable(struct usb_function
*f
)
3070 ffs_func_set_alt(f
, 0, (unsigned)-1);
3073 static int ffs_func_setup(struct usb_function
*f
,
3074 const struct usb_ctrlrequest
*creq
)
3076 struct ffs_function
*func
= ffs_func_from_usb(f
);
3077 struct ffs_data
*ffs
= func
->ffs
;
3078 unsigned long flags
;
3083 pr_vdebug("creq->bRequestType = %02x\n", creq
->bRequestType
);
3084 pr_vdebug("creq->bRequest = %02x\n", creq
->bRequest
);
3085 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq
->wValue
));
3086 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq
->wIndex
));
3087 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq
->wLength
));
3090 * Most requests directed to interface go through here
3091 * (notable exceptions are set/get interface) so we need to
3092 * handle them. All other either handled by composite or
3093 * passed to usb_configuration->setup() (if one is set). No
3094 * matter, we will handle requests directed to endpoint here
3095 * as well (as it's straightforward) but what to do with any
3098 if (ffs
->state
!= FFS_ACTIVE
)
3101 switch (creq
->bRequestType
& USB_RECIP_MASK
) {
3102 case USB_RECIP_INTERFACE
:
3103 ret
= ffs_func_revmap_intf(func
, le16_to_cpu(creq
->wIndex
));
3104 if (unlikely(ret
< 0))
3108 case USB_RECIP_ENDPOINT
:
3109 ret
= ffs_func_revmap_ep(func
, le16_to_cpu(creq
->wIndex
));
3110 if (unlikely(ret
< 0))
3112 if (func
->ffs
->user_flags
& FUNCTIONFS_VIRTUAL_ADDR
)
3113 ret
= func
->ffs
->eps_addrmap
[ret
];
3120 spin_lock_irqsave(&ffs
->ev
.waitq
.lock
, flags
);
3121 ffs
->ev
.setup
= *creq
;
3122 ffs
->ev
.setup
.wIndex
= cpu_to_le16(ret
);
3123 __ffs_event_add(ffs
, FUNCTIONFS_SETUP
);
3124 spin_unlock_irqrestore(&ffs
->ev
.waitq
.lock
, flags
);
3129 static void ffs_func_suspend(struct usb_function
*f
)
3132 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_SUSPEND
);
3135 static void ffs_func_resume(struct usb_function
*f
)
3138 ffs_event_add(ffs_func_from_usb(f
)->ffs
, FUNCTIONFS_RESUME
);
3142 /* Endpoint and interface numbers reverse mapping ***************************/
3144 static int ffs_func_revmap_ep(struct ffs_function
*func
, u8 num
)
3146 num
= func
->eps_revmap
[num
& USB_ENDPOINT_NUMBER_MASK
];
3147 return num
? num
: -EDOM
;
3150 static int ffs_func_revmap_intf(struct ffs_function
*func
, u8 intf
)
3152 short *nums
= func
->interfaces_nums
;
3153 unsigned count
= func
->ffs
->interfaces_count
;
3155 for (; count
; --count
, ++nums
) {
3156 if (*nums
>= 0 && *nums
== intf
)
3157 return nums
- func
->interfaces_nums
;
3164 /* Devices management *******************************************************/
3166 static LIST_HEAD(ffs_devices
);
3168 static struct ffs_dev
*_ffs_do_find_dev(const char *name
)
3170 struct ffs_dev
*dev
;
3172 list_for_each_entry(dev
, &ffs_devices
, entry
) {
3173 if (!dev
->name
|| !name
)
3175 if (strcmp(dev
->name
, name
) == 0)
3183 * ffs_lock must be taken by the caller of this function
3185 static struct ffs_dev
*_ffs_get_single_dev(void)
3187 struct ffs_dev
*dev
;
3189 if (list_is_singular(&ffs_devices
)) {
3190 dev
= list_first_entry(&ffs_devices
, struct ffs_dev
, entry
);
3199 * ffs_lock must be taken by the caller of this function
3201 static struct ffs_dev
*_ffs_find_dev(const char *name
)
3203 struct ffs_dev
*dev
;
3205 dev
= _ffs_get_single_dev();
3209 return _ffs_do_find_dev(name
);
3212 /* Configfs support *********************************************************/
3214 static inline struct f_fs_opts
*to_ffs_opts(struct config_item
*item
)
3216 return container_of(to_config_group(item
), struct f_fs_opts
,
3220 static void ffs_attr_release(struct config_item
*item
)
3222 struct f_fs_opts
*opts
= to_ffs_opts(item
);
3224 usb_put_function_instance(&opts
->func_inst
);
3227 static struct configfs_item_operations ffs_item_ops
= {
3228 .release
= ffs_attr_release
,
3231 static struct config_item_type ffs_func_type
= {
3232 .ct_item_ops
= &ffs_item_ops
,
3233 .ct_owner
= THIS_MODULE
,
3237 /* Function registration interface ******************************************/
3239 static void ffs_free_inst(struct usb_function_instance
*f
)
3241 struct f_fs_opts
*opts
;
3243 opts
= to_f_fs_opts(f
);
3245 _ffs_free_dev(opts
->dev
);
3250 #define MAX_INST_NAME_LEN 40
3252 static int ffs_set_inst_name(struct usb_function_instance
*fi
, const char *name
)
3254 struct f_fs_opts
*opts
;
3259 name_len
= strlen(name
) + 1;
3260 if (name_len
> MAX_INST_NAME_LEN
)
3261 return -ENAMETOOLONG
;
3263 ptr
= kstrndup(name
, name_len
, GFP_KERNEL
);
3267 opts
= to_f_fs_opts(fi
);
3272 tmp
= opts
->dev
->name_allocated
? opts
->dev
->name
: NULL
;
3273 ret
= _ffs_name_dev(opts
->dev
, ptr
);
3279 opts
->dev
->name_allocated
= true;
3288 static struct usb_function_instance
*ffs_alloc_inst(void)
3290 struct f_fs_opts
*opts
;
3291 struct ffs_dev
*dev
;
3293 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
3295 return ERR_PTR(-ENOMEM
);
3297 opts
->func_inst
.set_inst_name
= ffs_set_inst_name
;
3298 opts
->func_inst
.free_func_inst
= ffs_free_inst
;
3300 dev
= _ffs_alloc_dev();
3304 return ERR_CAST(dev
);
3309 config_group_init_type_name(&opts
->func_inst
.group
, "",
3311 return &opts
->func_inst
;
3314 static void ffs_free(struct usb_function
*f
)
3316 kfree(ffs_func_from_usb(f
));
3319 static void ffs_func_unbind(struct usb_configuration
*c
,
3320 struct usb_function
*f
)
3322 struct ffs_function
*func
= ffs_func_from_usb(f
);
3323 struct ffs_data
*ffs
= func
->ffs
;
3324 struct f_fs_opts
*opts
=
3325 container_of(f
->fi
, struct f_fs_opts
, func_inst
);
3326 struct ffs_ep
*ep
= func
->eps
;
3327 unsigned count
= ffs
->eps_count
;
3328 unsigned long flags
;
3331 if (ffs
->func
== func
) {
3332 ffs_func_eps_disable(func
);
3336 if (!--opts
->refcnt
)
3337 functionfs_unbind(ffs
);
3339 /* cleanup after autoconfig */
3340 spin_lock_irqsave(&func
->ffs
->eps_lock
, flags
);
3342 if (ep
->ep
&& ep
->req
)
3343 usb_ep_free_request(ep
->ep
, ep
->req
);
3347 spin_unlock_irqrestore(&func
->ffs
->eps_lock
, flags
);
3351 * eps, descriptors and interfaces_nums are allocated in the
3352 * same chunk so only one free is required.
3354 func
->function
.fs_descriptors
= NULL
;
3355 func
->function
.hs_descriptors
= NULL
;
3356 func
->function
.ss_descriptors
= NULL
;
3357 func
->interfaces_nums
= NULL
;
3359 ffs_event_add(ffs
, FUNCTIONFS_UNBIND
);
3362 static struct usb_function
*ffs_alloc(struct usb_function_instance
*fi
)
3364 struct ffs_function
*func
;
3368 func
= kzalloc(sizeof(*func
), GFP_KERNEL
);
3369 if (unlikely(!func
))
3370 return ERR_PTR(-ENOMEM
);
3372 func
->function
.name
= "Function FS Gadget";
3374 func
->function
.bind
= ffs_func_bind
;
3375 func
->function
.unbind
= ffs_func_unbind
;
3376 func
->function
.set_alt
= ffs_func_set_alt
;
3377 func
->function
.disable
= ffs_func_disable
;
3378 func
->function
.setup
= ffs_func_setup
;
3379 func
->function
.suspend
= ffs_func_suspend
;
3380 func
->function
.resume
= ffs_func_resume
;
3381 func
->function
.free_func
= ffs_free
;
3383 return &func
->function
;
3387 * ffs_lock must be taken by the caller of this function
3389 static struct ffs_dev
*_ffs_alloc_dev(void)
3391 struct ffs_dev
*dev
;
3394 if (_ffs_get_single_dev())
3395 return ERR_PTR(-EBUSY
);
3397 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
3399 return ERR_PTR(-ENOMEM
);
3401 if (list_empty(&ffs_devices
)) {
3402 ret
= functionfs_init();
3405 return ERR_PTR(ret
);
3409 list_add(&dev
->entry
, &ffs_devices
);
3415 * ffs_lock must be taken by the caller of this function
3416 * The caller is responsible for "name" being available whenever f_fs needs it
3418 static int _ffs_name_dev(struct ffs_dev
*dev
, const char *name
)
3420 struct ffs_dev
*existing
;
3422 existing
= _ffs_do_find_dev(name
);
3432 * The caller is responsible for "name" being available whenever f_fs needs it
3434 int ffs_name_dev(struct ffs_dev
*dev
, const char *name
)
3439 ret
= _ffs_name_dev(dev
, name
);
3444 EXPORT_SYMBOL_GPL(ffs_name_dev
);
3446 int ffs_single_dev(struct ffs_dev
*dev
)
3453 if (!list_is_singular(&ffs_devices
))
3461 EXPORT_SYMBOL_GPL(ffs_single_dev
);
3464 * ffs_lock must be taken by the caller of this function
3466 static void _ffs_free_dev(struct ffs_dev
*dev
)
3468 list_del(&dev
->entry
);
3469 if (dev
->name_allocated
)
3472 if (list_empty(&ffs_devices
))
3473 functionfs_cleanup();
3476 static void *ffs_acquire_dev(const char *dev_name
)
3478 struct ffs_dev
*ffs_dev
;
3483 ffs_dev
= _ffs_find_dev(dev_name
);
3485 ffs_dev
= ERR_PTR(-ENOENT
);
3486 else if (ffs_dev
->mounted
)
3487 ffs_dev
= ERR_PTR(-EBUSY
);
3488 else if (ffs_dev
->ffs_acquire_dev_callback
&&
3489 ffs_dev
->ffs_acquire_dev_callback(ffs_dev
))
3490 ffs_dev
= ERR_PTR(-ENOENT
);
3492 ffs_dev
->mounted
= true;
3498 static void ffs_release_dev(struct ffs_data
*ffs_data
)
3500 struct ffs_dev
*ffs_dev
;
3505 ffs_dev
= ffs_data
->private_data
;
3507 ffs_dev
->mounted
= false;
3509 if (ffs_dev
->ffs_release_dev_callback
)
3510 ffs_dev
->ffs_release_dev_callback(ffs_dev
);
3516 static int ffs_ready(struct ffs_data
*ffs
)
3518 struct ffs_dev
*ffs_obj
;
3524 ffs_obj
= ffs
->private_data
;
3529 if (WARN_ON(ffs_obj
->desc_ready
)) {
3534 ffs_obj
->desc_ready
= true;
3535 ffs_obj
->ffs_data
= ffs
;
3537 if (ffs_obj
->ffs_ready_callback
) {
3538 ret
= ffs_obj
->ffs_ready_callback(ffs
);
3543 set_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
);
3549 static void ffs_closed(struct ffs_data
*ffs
)
3551 struct ffs_dev
*ffs_obj
;
3552 struct f_fs_opts
*opts
;
3557 ffs_obj
= ffs
->private_data
;
3561 ffs_obj
->desc_ready
= false;
3563 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK
, &ffs
->flags
) &&
3564 ffs_obj
->ffs_closed_callback
)
3565 ffs_obj
->ffs_closed_callback(ffs
);
3568 opts
= ffs_obj
->opts
;
3572 if (opts
->no_configfs
|| !opts
->func_inst
.group
.cg_item
.ci_parent
3573 || !atomic_read(&opts
->func_inst
.group
.cg_item
.ci_kref
.refcount
))
3576 unregister_gadget_item(ffs_obj
->opts
->
3577 func_inst
.group
.cg_item
.ci_parent
->ci_parent
);
3582 /* Misc helper functions ****************************************************/
3584 static int ffs_mutex_lock(struct mutex
*mutex
, unsigned nonblock
)
3587 ? likely(mutex_trylock(mutex
)) ? 0 : -EAGAIN
3588 : mutex_lock_interruptible(mutex
);
3591 static char *ffs_prepare_buffer(const char __user
*buf
, size_t len
)
3598 data
= kmalloc(len
, GFP_KERNEL
);
3599 if (unlikely(!data
))
3600 return ERR_PTR(-ENOMEM
);
3602 if (unlikely(copy_from_user(data
, buf
, len
))) {
3604 return ERR_PTR(-EFAULT
);
3607 pr_vdebug("Buffer from user space:\n");
3608 ffs_dump_mem("", data
, len
);
3613 DECLARE_USB_FUNCTION_INIT(ffs
, ffs_alloc_inst
, ffs_alloc
);
3614 MODULE_LICENSE("GPL");
3615 MODULE_AUTHOR("Michal Nazarewicz");