Lines Matching +full:num +full:- +full:strings

1 // SPDX-License-Identifier: GPL-2.0+
3 * f_fs.c -- user mode file system API for USB composite function controllers
9 * Copyright (C) 2003-2004 David Brownell
57 /* Called with ffs->mutex held; take over ownership of data. */
91 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP); in ffs_setup_state_clear_cancelled()
111 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
118 struct usb_ep *ep; /* P: ffs->eps_lock */
119 struct usb_request *req; /* P: epfile->mutex */
124 u8 num; member
128 /* Protects ep->ep and ep->req. */
132 struct ffs_ep *ep; /* P: ffs->eps_lock */
147 * can never run concurrently (they are synchronised by epfile->mutex)
157 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
192 #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
196 unsigned char in; /* P: ffs->eps_lock */
197 unsigned char isoc; /* P: ffs->eps_lock */
271 struct ffs_data *ffs = req->context; in ffs_ep0_complete()
273 complete(&ffs->ep0req_completion); in ffs_ep0_complete()
277 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_queue_wait()
279 struct usb_request *req = ffs->ep0req; in __ffs_ep0_queue_wait()
283 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
284 return -EINVAL; in __ffs_ep0_queue_wait()
287 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); in __ffs_ep0_queue_wait()
289 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
291 req->buf = data; in __ffs_ep0_queue_wait()
292 req->length = len; in __ffs_ep0_queue_wait()
299 if (req->buf == NULL) in __ffs_ep0_queue_wait()
300 req->buf = (void *)0xDEADBABE; in __ffs_ep0_queue_wait()
302 reinit_completion(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
304 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); in __ffs_ep0_queue_wait()
308 ret = wait_for_completion_interruptible(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
310 usb_ep_dequeue(ffs->gadget->ep0, req); in __ffs_ep0_queue_wait()
311 return -EINTR; in __ffs_ep0_queue_wait()
314 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_queue_wait()
315 return req->status ? req->status : req->actual; in __ffs_ep0_queue_wait()
320 if (ffs->ev.can_stall) { in __ffs_ep0_stall()
322 usb_ep_set_halt(ffs->gadget->ep0); in __ffs_ep0_stall()
323 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_stall()
324 return -EL2HLT; in __ffs_ep0_stall()
327 return -ESRCH; in __ffs_ep0_stall()
334 struct ffs_data *ffs = file->private_data; in ffs_ep0_write()
340 return -EIDRM; in ffs_ep0_write()
343 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_write()
348 switch (ffs->state) { in ffs_ep0_write()
353 ret = -EINVAL; in ffs_ep0_write()
364 if (ffs->state == FFS_READ_DESCRIPTORS) { in ffs_ep0_write()
370 ffs->state = FFS_READ_STRINGS; in ffs_ep0_write()
373 pr_info("read strings\n"); in ffs_ep0_write()
380 ffs->state = FFS_CLOSING; in ffs_ep0_write()
384 ffs->state = FFS_ACTIVE; in ffs_ep0_write()
385 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
389 ffs->state = FFS_CLOSING; in ffs_ep0_write()
403 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
406 ret = -EIDRM; in ffs_ep0_write()
410 ret = -ESRCH; in ffs_ep0_write()
418 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { in ffs_ep0_write()
419 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
425 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_write()
427 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
435 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
445 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP in ffs_ep0_write()
451 ret = -EIDRM; in ffs_ep0_write()
453 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
462 ret = -EBADFD; in ffs_ep0_write()
466 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
470 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
473 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_read_events()
476 * n cannot be bigger than ffs->ev.count, which cannot be bigger than in __ffs_ep0_read_events()
477 * size of ffs->ev.types array (which is four) so that's how much space in __ffs_ep0_read_events()
480 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)]; in __ffs_ep0_read_events()
487 events[i].type = ffs->ev.types[i]; in __ffs_ep0_read_events()
489 events[i].u.setup = ffs->ev.setup; in __ffs_ep0_read_events()
490 ffs->setup_state = FFS_SETUP_PENDING; in __ffs_ep0_read_events()
494 ffs->ev.count -= n; in __ffs_ep0_read_events()
495 if (ffs->ev.count) in __ffs_ep0_read_events()
496 memmove(ffs->ev.types, ffs->ev.types + n, in __ffs_ep0_read_events()
497 ffs->ev.count * sizeof *ffs->ev.types); in __ffs_ep0_read_events()
499 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_read_events()
500 mutex_unlock(&ffs->mutex); in __ffs_ep0_read_events()
502 return copy_to_user(buf, events, size) ? -EFAULT : size; in __ffs_ep0_read_events()
508 struct ffs_data *ffs = file->private_data; in ffs_ep0_read()
515 return -EIDRM; in ffs_ep0_read()
518 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_read()
523 if (ffs->state != FFS_ACTIVE) { in ffs_ep0_read()
524 ret = -EBADFD; in ffs_ep0_read()
532 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
536 ret = -EIDRM; in ffs_ep0_read()
542 ret = -EINVAL; in ffs_ep0_read()
546 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { in ffs_ep0_read()
547 ret = -EAGAIN; in ffs_ep0_read()
551 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, in ffs_ep0_read()
552 ffs->ev.count)) { in ffs_ep0_read()
553 ret = -EINTR; in ffs_ep0_read()
559 min(n, (size_t)ffs->ev.count)); in ffs_ep0_read()
562 if (ffs->ev.setup.bRequestType & USB_DIR_IN) { in ffs_ep0_read()
563 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
568 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_read()
570 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
575 ret = -ENOMEM; in ffs_ep0_read()
580 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
585 ret = -EIDRM; in ffs_ep0_read()
592 ret = -EFAULT; in ffs_ep0_read()
596 ret = -EBADFD; in ffs_ep0_read()
600 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
602 mutex_unlock(&ffs->mutex); in ffs_ep0_read()
609 struct ffs_data *ffs = inode->i_private; in ffs_ep0_open()
611 if (ffs->state == FFS_CLOSING) in ffs_ep0_open()
612 return -EBUSY; in ffs_ep0_open()
614 file->private_data = ffs; in ffs_ep0_open()
622 struct ffs_data *ffs = file->private_data; in ffs_ep0_release()
631 struct ffs_data *ffs = file->private_data; in ffs_ep0_ioctl()
632 struct usb_gadget *gadget = ffs->gadget; in ffs_ep0_ioctl()
636 struct ffs_function *func = ffs->func; in ffs_ep0_ioctl()
637 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; in ffs_ep0_ioctl()
638 } else if (gadget && gadget->ops->ioctl) { in ffs_ep0_ioctl()
639 ret = gadget->ops->ioctl(gadget, code, value); in ffs_ep0_ioctl()
641 ret = -ENOTTY; in ffs_ep0_ioctl()
649 struct ffs_data *ffs = file->private_data; in ffs_ep0_poll()
653 poll_wait(file, &ffs->ev.waitq, wait); in ffs_ep0_poll()
655 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_poll()
659 switch (ffs->state) { in ffs_ep0_poll()
666 switch (ffs->setup_state) { in ffs_ep0_poll()
668 if (ffs->ev.count) in ffs_ep0_poll()
685 mutex_unlock(&ffs->mutex); in ffs_ep0_poll()
706 struct ffs_io_data *io_data = req->context; in ffs_epfile_io_complete()
708 if (req->status) in ffs_epfile_io_complete()
709 io_data->status = req->status; in ffs_epfile_io_complete()
711 io_data->status = req->actual; in ffs_epfile_io_complete()
713 complete(&io_data->done); in ffs_epfile_io_complete()
723 return -EFAULT; in ffs_copy_to_iter()
761 * @sg_table - pointer to a place to be filled with sg_table contents
762 * @size - required buffer size
799 if (io_data->use_sg) in ffs_alloc_buffer()
800 return ffs_build_sg_list(&io_data->sgt, data_len); in ffs_alloc_buffer()
807 if (!io_data->buf) in ffs_free_buffer()
810 if (io_data->use_sg) { in ffs_free_buffer()
811 sg_free_table(&io_data->sgt); in ffs_free_buffer()
812 vfree(io_data->buf); in ffs_free_buffer()
814 kfree(io_data->buf); in ffs_free_buffer()
822 int ret = io_data->status; in ffs_user_copy_worker()
823 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD; in ffs_user_copy_worker()
826 if (io_data->read && ret > 0) { in ffs_user_copy_worker()
827 kthread_use_mm(io_data->mm); in ffs_user_copy_worker()
828 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data); in ffs_user_copy_worker()
829 kthread_unuse_mm(io_data->mm); in ffs_user_copy_worker()
832 io_data->kiocb->ki_complete(io_data->kiocb, ret); in ffs_user_copy_worker()
834 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd) in ffs_user_copy_worker()
835 eventfd_signal(io_data->ffs->ffs_eventfd, 1); in ffs_user_copy_worker()
837 spin_lock_irqsave(&io_data->ffs->eps_lock, flags); in ffs_user_copy_worker()
838 usb_ep_free_request(io_data->ep, io_data->req); in ffs_user_copy_worker()
839 io_data->req = NULL; in ffs_user_copy_worker()
840 spin_unlock_irqrestore(&io_data->ffs->eps_lock, flags); in ffs_user_copy_worker()
842 if (io_data->read) in ffs_user_copy_worker()
843 kfree(io_data->to_free); in ffs_user_copy_worker()
851 struct ffs_io_data *io_data = req->context; in ffs_epfile_async_io_complete()
852 struct ffs_data *ffs = io_data->ffs; in ffs_epfile_async_io_complete()
854 io_data->status = req->status ? req->status : req->actual; in ffs_epfile_async_io_complete()
856 INIT_WORK(&io_data->work, ffs_user_copy_worker); in ffs_epfile_async_io_complete()
857 queue_work(ffs->io_completion_wq, &io_data->work); in ffs_epfile_async_io_complete()
866 struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP); in __ffs_epfile_read_buffer_free()
871 /* Assumes epfile->mutex is held. */
876 * Null out epfile->read_buffer so ffs_func_eps_disable does not free in __ffs_epfile_read_buffered()
880 struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL); in __ffs_epfile_read_buffered()
885 ret = copy_to_iter(buf->data, buf->length, iter); in __ffs_epfile_read_buffered()
886 if (buf->length == ret) { in __ffs_epfile_read_buffered()
892 ret = -EFAULT; in __ffs_epfile_read_buffered()
894 buf->length -= ret; in __ffs_epfile_read_buffered()
895 buf->data += ret; in __ffs_epfile_read_buffered()
898 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_buffered()
904 /* Assumes epfile->mutex is held. */
916 return -EFAULT; in __ffs_epfile_read_data()
922 data_len -= ret; in __ffs_epfile_read_data()
925 return -ENOMEM; in __ffs_epfile_read_data()
926 buf->length = data_len; in __ffs_epfile_read_data()
927 buf->data = buf->storage; in __ffs_epfile_read_data()
928 memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len)); in __ffs_epfile_read_data()
936 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_data()
944 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_io()
948 ssize_t ret, data_len = -EINVAL; in ffs_epfile_io()
952 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_io()
953 return -ENODEV; in ffs_epfile_io()
956 ep = epfile->ep; in ffs_epfile_io()
958 if (file->f_flags & O_NONBLOCK) in ffs_epfile_io()
959 return -EAGAIN; in ffs_epfile_io()
962 epfile->ffs->wait, (ep = epfile->ep)); in ffs_epfile_io()
964 return -EINTR; in ffs_epfile_io()
968 halt = (!io_data->read == !epfile->in); in ffs_epfile_io()
969 if (halt && epfile->isoc) in ffs_epfile_io()
970 return -EINVAL; in ffs_epfile_io()
973 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK); in ffs_epfile_io()
988 if (!io_data->aio && io_data->read) { in ffs_epfile_io()
989 ret = __ffs_epfile_read_buffered(epfile, &io_data->data); in ffs_epfile_io()
995 * if we _do_ wait above, the epfile->ffs->gadget might be NULL in ffs_epfile_io()
999 gadget = epfile->ffs->gadget; in ffs_epfile_io()
1001 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1003 if (epfile->ep != ep) { in ffs_epfile_io()
1004 ret = -ESHUTDOWN; in ffs_epfile_io()
1007 data_len = iov_iter_count(&io_data->data); in ffs_epfile_io()
1012 if (io_data->read) in ffs_epfile_io()
1013 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len); in ffs_epfile_io()
1015 io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE; in ffs_epfile_io()
1016 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1020 ret = -ENOMEM; in ffs_epfile_io()
1023 if (!io_data->read && in ffs_epfile_io()
1024 !copy_from_iter_full(data, data_len, &io_data->data)) { in ffs_epfile_io()
1025 ret = -EFAULT; in ffs_epfile_io()
1030 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1032 if (epfile->ep != ep) { in ffs_epfile_io()
1034 ret = -ESHUTDOWN; in ffs_epfile_io()
1036 ret = usb_ep_set_halt(ep->ep); in ffs_epfile_io()
1038 ret = -EBADMSG; in ffs_epfile_io()
1039 } else if (data_len == -EINVAL) { in ffs_epfile_io()
1045 * being initialized to -EINVAL during its declaration, which in ffs_epfile_io()
1051 WARN(1, "%s: data_len == -EINVAL\n", __func__); in ffs_epfile_io()
1052 ret = -EINVAL; in ffs_epfile_io()
1053 } else if (!io_data->aio) { in ffs_epfile_io()
1056 req = ep->req; in ffs_epfile_io()
1057 if (io_data->use_sg) { in ffs_epfile_io()
1058 req->buf = NULL; in ffs_epfile_io()
1059 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1060 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1062 req->buf = data; in ffs_epfile_io()
1063 req->num_sgs = 0; in ffs_epfile_io()
1065 req->length = data_len; in ffs_epfile_io()
1067 io_data->buf = data; in ffs_epfile_io()
1069 init_completion(&io_data->done); in ffs_epfile_io()
1070 req->context = io_data; in ffs_epfile_io()
1071 req->complete = ffs_epfile_io_complete; in ffs_epfile_io()
1073 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1077 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1079 if (wait_for_completion_interruptible(&io_data->done)) { in ffs_epfile_io()
1080 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1081 if (epfile->ep != ep) { in ffs_epfile_io()
1082 ret = -ESHUTDOWN; in ffs_epfile_io()
1089 * condition with req->complete callback. in ffs_epfile_io()
1091 usb_ep_dequeue(ep->ep, req); in ffs_epfile_io()
1092 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1093 wait_for_completion(&io_data->done); in ffs_epfile_io()
1094 interrupted = io_data->status < 0; in ffs_epfile_io()
1098 ret = -EINTR; in ffs_epfile_io()
1099 else if (io_data->read && io_data->status > 0) in ffs_epfile_io()
1100 ret = __ffs_epfile_read_data(epfile, data, io_data->status, in ffs_epfile_io()
1101 &io_data->data); in ffs_epfile_io()
1103 ret = io_data->status; in ffs_epfile_io()
1105 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) { in ffs_epfile_io()
1106 ret = -ENOMEM; in ffs_epfile_io()
1108 if (io_data->use_sg) { in ffs_epfile_io()
1109 req->buf = NULL; in ffs_epfile_io()
1110 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1111 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1113 req->buf = data; in ffs_epfile_io()
1114 req->num_sgs = 0; in ffs_epfile_io()
1116 req->length = data_len; in ffs_epfile_io()
1118 io_data->buf = data; in ffs_epfile_io()
1119 io_data->ep = ep->ep; in ffs_epfile_io()
1120 io_data->req = req; in ffs_epfile_io()
1121 io_data->ffs = epfile->ffs; in ffs_epfile_io()
1123 req->context = io_data; in ffs_epfile_io()
1124 req->complete = ffs_epfile_async_io_complete; in ffs_epfile_io()
1126 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1128 io_data->req = NULL; in ffs_epfile_io()
1129 usb_ep_free_request(ep->ep, req); in ffs_epfile_io()
1133 ret = -EIOCBQUEUED; in ffs_epfile_io()
1142 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1144 mutex_unlock(&epfile->mutex); in ffs_epfile_io()
1146 if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */ in ffs_epfile_io()
1154 struct ffs_epfile *epfile = inode->i_private; in ffs_epfile_open()
1156 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_open()
1157 return -ENODEV; in ffs_epfile_open()
1159 file->private_data = epfile; in ffs_epfile_open()
1160 ffs_data_opened(epfile->ffs); in ffs_epfile_open()
1167 struct ffs_io_data *io_data = kiocb->private; in ffs_aio_cancel()
1168 struct ffs_epfile *epfile = kiocb->ki_filp->private_data; in ffs_aio_cancel()
1172 spin_lock_irqsave(&epfile->ffs->eps_lock, flags); in ffs_aio_cancel()
1174 if (io_data && io_data->ep && io_data->req) in ffs_aio_cancel()
1175 value = usb_ep_dequeue(io_data->ep, io_data->req); in ffs_aio_cancel()
1177 value = -EINVAL; in ffs_aio_cancel()
1179 spin_unlock_irqrestore(&epfile->ffs->eps_lock, flags); in ffs_aio_cancel()
1192 return -ENOMEM; in ffs_epfile_write_iter()
1193 p->aio = true; in ffs_epfile_write_iter()
1196 p->aio = false; in ffs_epfile_write_iter()
1199 p->read = false; in ffs_epfile_write_iter()
1200 p->kiocb = kiocb; in ffs_epfile_write_iter()
1201 p->data = *from; in ffs_epfile_write_iter()
1202 p->mm = current->mm; in ffs_epfile_write_iter()
1204 kiocb->private = p; in ffs_epfile_write_iter()
1206 if (p->aio) in ffs_epfile_write_iter()
1209 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_write_iter()
1210 if (res == -EIOCBQUEUED) in ffs_epfile_write_iter()
1212 if (p->aio) in ffs_epfile_write_iter()
1215 *from = p->data; in ffs_epfile_write_iter()
1227 return -ENOMEM; in ffs_epfile_read_iter()
1228 p->aio = true; in ffs_epfile_read_iter()
1231 p->aio = false; in ffs_epfile_read_iter()
1234 p->read = true; in ffs_epfile_read_iter()
1235 p->kiocb = kiocb; in ffs_epfile_read_iter()
1236 if (p->aio) { in ffs_epfile_read_iter()
1237 p->to_free = dup_iter(&p->data, to, GFP_KERNEL); in ffs_epfile_read_iter()
1238 if (!iter_is_ubuf(&p->data) && !p->to_free) { in ffs_epfile_read_iter()
1240 return -ENOMEM; in ffs_epfile_read_iter()
1243 p->data = *to; in ffs_epfile_read_iter()
1244 p->to_free = NULL; in ffs_epfile_read_iter()
1246 p->mm = current->mm; in ffs_epfile_read_iter()
1248 kiocb->private = p; in ffs_epfile_read_iter()
1250 if (p->aio) in ffs_epfile_read_iter()
1253 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_read_iter()
1254 if (res == -EIOCBQUEUED) in ffs_epfile_read_iter()
1257 if (p->aio) { in ffs_epfile_read_iter()
1258 kfree(p->to_free); in ffs_epfile_read_iter()
1261 *to = p->data; in ffs_epfile_read_iter()
1269 struct ffs_epfile *epfile = inode->i_private; in ffs_epfile_release()
1272 ffs_data_closed(epfile->ffs); in ffs_epfile_release()
1280 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_ioctl()
1284 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_ioctl()
1285 return -ENODEV; in ffs_epfile_ioctl()
1288 ep = epfile->ep; in ffs_epfile_ioctl()
1290 if (file->f_flags & O_NONBLOCK) in ffs_epfile_ioctl()
1291 return -EAGAIN; in ffs_epfile_ioctl()
1294 epfile->ffs->wait, (ep = epfile->ep)); in ffs_epfile_ioctl()
1296 return -EINTR; in ffs_epfile_ioctl()
1299 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1302 if (epfile->ep != ep) { in ffs_epfile_ioctl()
1303 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1304 return -ESHUTDOWN; in ffs_epfile_ioctl()
1309 ret = usb_ep_fifo_status(epfile->ep->ep); in ffs_epfile_ioctl()
1312 usb_ep_fifo_flush(epfile->ep->ep); in ffs_epfile_ioctl()
1316 ret = usb_ep_clear_halt(epfile->ep->ep); in ffs_epfile_ioctl()
1319 ret = epfile->ep->num; in ffs_epfile_ioctl()
1326 switch (epfile->ffs->gadget->speed) { in ffs_epfile_ioctl()
1338 desc = epfile->ep->descs[desc_idx]; in ffs_epfile_ioctl()
1339 memcpy(&desc1, desc, desc->bLength); in ffs_epfile_ioctl()
1341 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1344 ret = -EFAULT; in ffs_epfile_ioctl()
1348 ret = -ENOTTY; in ffs_epfile_ioctl()
1350 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1387 inode->i_ino = get_next_ino(); in ffs_sb_make_inode()
1388 inode->i_mode = perms->mode; in ffs_sb_make_inode()
1389 inode->i_uid = perms->uid; in ffs_sb_make_inode()
1390 inode->i_gid = perms->gid; in ffs_sb_make_inode()
1391 inode->i_atime = ts; in ffs_sb_make_inode()
1392 inode->i_mtime = ts; in ffs_sb_make_inode()
1393 inode->i_private = data; in ffs_sb_make_inode()
1395 inode->i_fop = fops; in ffs_sb_make_inode()
1397 inode->i_op = iops; in ffs_sb_make_inode()
1408 struct ffs_data *ffs = sb->s_fs_info; in ffs_sb_create_file()
1412 dentry = d_alloc_name(sb->s_root, name); in ffs_sb_create_file()
1416 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); in ffs_sb_create_file()
1442 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_sb_fill()
1444 struct ffs_data *ffs = data->ffs_data; in ffs_sb_fill()
1446 ffs->sb = sb; in ffs_sb_fill()
1447 data->ffs_data = NULL; in ffs_sb_fill()
1448 sb->s_fs_info = ffs; in ffs_sb_fill()
1449 sb->s_blocksize = PAGE_SIZE; in ffs_sb_fill()
1450 sb->s_blocksize_bits = PAGE_SHIFT; in ffs_sb_fill()
1451 sb->s_magic = FUNCTIONFS_MAGIC; in ffs_sb_fill()
1452 sb->s_op = &ffs_sb_operations; in ffs_sb_fill()
1453 sb->s_time_gran = 1; in ffs_sb_fill()
1456 data->perms.mode = data->root_mode; in ffs_sb_fill()
1460 &data->perms); in ffs_sb_fill()
1461 sb->s_root = d_make_root(inode); in ffs_sb_fill()
1462 if (!sb->s_root) in ffs_sb_fill()
1463 return -ENOMEM; in ffs_sb_fill()
1467 return -ENOMEM; in ffs_sb_fill()
1493 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_fs_parse_param()
1503 data->no_disconnect = result.boolean; in ffs_fs_parse_param()
1506 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1509 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
1512 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1513 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
1517 data->perms.uid = make_kuid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
1518 if (!uid_valid(data->perms.uid)) in ffs_fs_parse_param()
1522 data->perms.gid = make_kgid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
1523 if (!gid_valid(data->perms.gid)) in ffs_fs_parse_param()
1528 return -ENOPARAM; in ffs_fs_parse_param()
1534 return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32); in ffs_fs_parse_param()
1542 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_get_tree()
1546 if (!fc->source) in ffs_fs_get_tree()
1549 ffs = ffs_data_new(fc->source); in ffs_fs_get_tree()
1551 return -ENOMEM; in ffs_fs_get_tree()
1552 ffs->file_perms = ctx->perms; in ffs_fs_get_tree()
1553 ffs->no_disconnect = ctx->no_disconnect; in ffs_fs_get_tree()
1555 ffs->dev_name = kstrdup(fc->source, GFP_KERNEL); in ffs_fs_get_tree()
1556 if (!ffs->dev_name) { in ffs_fs_get_tree()
1558 return -ENOMEM; in ffs_fs_get_tree()
1561 ret = ffs_acquire_dev(ffs->dev_name, ffs); in ffs_fs_get_tree()
1567 ctx->ffs_data = ffs; in ffs_fs_get_tree()
1573 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_free_fc()
1576 if (ctx->ffs_data) { in ffs_fs_free_fc()
1577 ffs_data_put(ctx->ffs_data); in ffs_fs_free_fc()
1596 return -ENOMEM; in ffs_fs_init_fs_context()
1598 ctx->perms.mode = S_IFREG | 0600; in ffs_fs_init_fs_context()
1599 ctx->perms.uid = GLOBAL_ROOT_UID; in ffs_fs_init_fs_context()
1600 ctx->perms.gid = GLOBAL_ROOT_GID; in ffs_fs_init_fs_context()
1601 ctx->root_mode = S_IFDIR | 0500; in ffs_fs_init_fs_context()
1602 ctx->no_disconnect = false; in ffs_fs_init_fs_context()
1604 fc->fs_private = ctx; in ffs_fs_init_fs_context()
1605 fc->ops = &ffs_fs_context_ops; in ffs_fs_init_fs_context()
1613 if (sb->s_fs_info) in ffs_fs_kill_sb()
1614 ffs_data_closed(sb->s_fs_info); in ffs_fs_kill_sb()
1656 refcount_inc(&ffs->ref); in ffs_data_get()
1661 refcount_inc(&ffs->ref); in ffs_data_opened()
1662 if (atomic_add_return(1, &ffs->opened) == 1 && in ffs_data_opened()
1663 ffs->state == FFS_DEACTIVATED) { in ffs_data_opened()
1664 ffs->state = FFS_CLOSING; in ffs_data_opened()
1671 if (refcount_dec_and_test(&ffs->ref)) { in ffs_data_put()
1674 ffs_release_dev(ffs->private_data); in ffs_data_put()
1675 BUG_ON(waitqueue_active(&ffs->ev.waitq) || in ffs_data_put()
1676 swait_active(&ffs->ep0req_completion.wait) || in ffs_data_put()
1677 waitqueue_active(&ffs->wait)); in ffs_data_put()
1678 destroy_workqueue(ffs->io_completion_wq); in ffs_data_put()
1679 kfree(ffs->dev_name); in ffs_data_put()
1689 if (atomic_dec_and_test(&ffs->opened)) { in ffs_data_closed()
1690 if (ffs->no_disconnect) { in ffs_data_closed()
1691 ffs->state = FFS_DEACTIVATED; in ffs_data_closed()
1692 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_closed()
1693 epfiles = ffs->epfiles; in ffs_data_closed()
1694 ffs->epfiles = NULL; in ffs_data_closed()
1695 spin_unlock_irqrestore(&ffs->eps_lock, in ffs_data_closed()
1700 ffs->eps_count); in ffs_data_closed()
1702 if (ffs->setup_state == FFS_SETUP_PENDING) in ffs_data_closed()
1705 ffs->state = FFS_CLOSING; in ffs_data_closed()
1709 if (atomic_read(&ffs->opened) < 0) { in ffs_data_closed()
1710 ffs->state = FFS_CLOSING; in ffs_data_closed()
1723 ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name); in ffs_data_new()
1724 if (!ffs->io_completion_wq) { in ffs_data_new()
1729 refcount_set(&ffs->ref, 1); in ffs_data_new()
1730 atomic_set(&ffs->opened, 0); in ffs_data_new()
1731 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_new()
1732 mutex_init(&ffs->mutex); in ffs_data_new()
1733 spin_lock_init(&ffs->eps_lock); in ffs_data_new()
1734 init_waitqueue_head(&ffs->ev.waitq); in ffs_data_new()
1735 init_waitqueue_head(&ffs->wait); in ffs_data_new()
1736 init_completion(&ffs->ep0req_completion); in ffs_data_new()
1739 ffs->ev.can_stall = 1; in ffs_data_new()
1751 BUG_ON(ffs->gadget); in ffs_data_clear()
1753 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_clear()
1754 epfiles = ffs->epfiles; in ffs_data_clear()
1755 ffs->epfiles = NULL; in ffs_data_clear()
1756 spin_unlock_irqrestore(&ffs->eps_lock, flags); in ffs_data_clear()
1761 * copy of epfile will save us from use-after-free. in ffs_data_clear()
1764 ffs_epfiles_destroy(epfiles, ffs->eps_count); in ffs_data_clear()
1765 ffs->epfiles = NULL; in ffs_data_clear()
1768 if (ffs->ffs_eventfd) { in ffs_data_clear()
1769 eventfd_ctx_put(ffs->ffs_eventfd); in ffs_data_clear()
1770 ffs->ffs_eventfd = NULL; in ffs_data_clear()
1773 kfree(ffs->raw_descs_data); in ffs_data_clear()
1774 kfree(ffs->raw_strings); in ffs_data_clear()
1775 kfree(ffs->stringtabs); in ffs_data_clear()
1782 ffs->raw_descs_data = NULL; in ffs_data_reset()
1783 ffs->raw_descs = NULL; in ffs_data_reset()
1784 ffs->raw_strings = NULL; in ffs_data_reset()
1785 ffs->stringtabs = NULL; in ffs_data_reset()
1787 ffs->raw_descs_length = 0; in ffs_data_reset()
1788 ffs->fs_descs_count = 0; in ffs_data_reset()
1789 ffs->hs_descs_count = 0; in ffs_data_reset()
1790 ffs->ss_descs_count = 0; in ffs_data_reset()
1792 ffs->strings_count = 0; in ffs_data_reset()
1793 ffs->interfaces_count = 0; in ffs_data_reset()
1794 ffs->eps_count = 0; in ffs_data_reset()
1796 ffs->ev.count = 0; in ffs_data_reset()
1798 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_reset()
1799 ffs->setup_state = FFS_NO_SETUP; in ffs_data_reset()
1800 ffs->flags = 0; in ffs_data_reset()
1802 ffs->ms_os_descs_ext_prop_count = 0; in ffs_data_reset()
1803 ffs->ms_os_descs_ext_prop_name_len = 0; in ffs_data_reset()
1804 ffs->ms_os_descs_ext_prop_data_len = 0; in ffs_data_reset()
1813 if ((ffs->state != FFS_ACTIVE in functionfs_bind()
1814 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) in functionfs_bind()
1815 return -EBADFD; in functionfs_bind()
1817 first_id = usb_string_ids_n(cdev, ffs->strings_count); in functionfs_bind()
1821 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); in functionfs_bind()
1822 if (!ffs->ep0req) in functionfs_bind()
1823 return -ENOMEM; in functionfs_bind()
1824 ffs->ep0req->complete = ffs_ep0_complete; in functionfs_bind()
1825 ffs->ep0req->context = ffs; in functionfs_bind()
1827 lang = ffs->stringtabs; in functionfs_bind()
1830 struct usb_string *str = (*lang)->strings; in functionfs_bind()
1832 for (; str->s; ++id, ++str) in functionfs_bind()
1833 str->id = id; in functionfs_bind()
1837 ffs->gadget = cdev->gadget; in functionfs_bind()
1844 if (!WARN_ON(!ffs->gadget)) { in functionfs_unbind()
1846 usb_ep_dequeue(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
1847 mutex_lock(&ffs->mutex); in functionfs_unbind()
1848 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
1849 ffs->ep0req = NULL; in functionfs_unbind()
1850 ffs->gadget = NULL; in functionfs_unbind()
1851 clear_bit(FFS_FL_BOUND, &ffs->flags); in functionfs_unbind()
1852 mutex_unlock(&ffs->mutex); in functionfs_unbind()
1862 count = ffs->eps_count; in ffs_epfiles_create()
1865 return -ENOMEM; in ffs_epfiles_create()
1869 epfile->ffs = ffs; in ffs_epfiles_create()
1870 mutex_init(&epfile->mutex); in ffs_epfiles_create()
1871 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_epfiles_create()
1872 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]); in ffs_epfiles_create()
1874 sprintf(epfile->name, "ep%u", i); in ffs_epfiles_create()
1875 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name, in ffs_epfiles_create()
1878 if (!epfile->dentry) { in ffs_epfiles_create()
1879 ffs_epfiles_destroy(epfiles, i - 1); in ffs_epfiles_create()
1880 return -ENOMEM; in ffs_epfiles_create()
1884 ffs->epfiles = epfiles; in ffs_epfiles_create()
1892 for (; count; --count, ++epfile) { in ffs_epfiles_destroy()
1893 BUG_ON(mutex_is_locked(&epfile->mutex)); in ffs_epfiles_destroy()
1894 if (epfile->dentry) { in ffs_epfiles_destroy()
1895 d_delete(epfile->dentry); in ffs_epfiles_destroy()
1896 dput(epfile->dentry); in ffs_epfiles_destroy()
1897 epfile->dentry = NULL; in ffs_epfiles_destroy()
1911 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
1912 count = func->ffs->eps_count; in ffs_func_eps_disable()
1913 epfile = func->ffs->epfiles; in ffs_func_eps_disable()
1914 ep = func->eps; in ffs_func_eps_disable()
1915 while (count--) { in ffs_func_eps_disable()
1917 if (ep->ep) in ffs_func_eps_disable()
1918 usb_ep_disable(ep->ep); in ffs_func_eps_disable()
1922 epfile->ep = NULL; in ffs_func_eps_disable()
1927 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
1939 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
1940 ffs = func->ffs; in ffs_func_eps_enable()
1941 ep = func->eps; in ffs_func_eps_enable()
1942 epfile = ffs->epfiles; in ffs_func_eps_enable()
1943 count = ffs->eps_count; in ffs_func_eps_enable()
1944 while(count--) { in ffs_func_eps_enable()
1945 ep->ep->driver_data = ep; in ffs_func_eps_enable()
1947 ret = config_ep_by_speed(func->gadget, &func->function, ep->ep); in ffs_func_eps_enable()
1950 __func__, ep->ep->name, ret); in ffs_func_eps_enable()
1954 ret = usb_ep_enable(ep->ep); in ffs_func_eps_enable()
1956 epfile->ep = ep; in ffs_func_eps_enable()
1957 epfile->in = usb_endpoint_dir_in(ep->ep->desc); in ffs_func_eps_enable()
1958 epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc); in ffs_func_eps_enable()
1967 wake_up_interruptible(&ffs->wait); in ffs_func_eps_enable()
1968 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
1974 /* Parsing and building descriptors and strings *****************************/
1978 * well as record how many interfaces, endpoints and strings are
2011 return -EINVAL; in ffs_do_single_desc()
2015 length = _ds->bLength; in ffs_do_single_desc()
2018 return -EINVAL; in ffs_do_single_desc()
2028 return -EINVAL; \ in ffs_do_single_desc()
2039 switch (_ds->bDescriptorType) { in ffs_do_single_desc()
2046 _ds->bDescriptorType); in ffs_do_single_desc()
2047 return -EINVAL; in ffs_do_single_desc()
2055 __entity(INTERFACE, ds->bInterfaceNumber); in ffs_do_single_desc()
2056 if (ds->iInterface) in ffs_do_single_desc()
2057 __entity(STRING, ds->iInterface); in ffs_do_single_desc()
2058 *current_class = ds->bInterfaceClass; in ffs_do_single_desc()
2068 __entity(ENDPOINT, ds->bEndpointAddress); in ffs_do_single_desc()
2085 _ds->bDescriptorType, *current_class); in ffs_do_single_desc()
2086 return -EINVAL; in ffs_do_single_desc()
2099 if (ds->iFunction) in ffs_do_single_desc()
2100 __entity(STRING, ds->iFunction); in ffs_do_single_desc()
2116 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2117 return -EINVAL; in ffs_do_single_desc()
2121 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2122 return -EINVAL; in ffs_do_single_desc()
2126 _ds->bLength, _ds->bDescriptorType); in ffs_do_single_desc()
2127 return -EINVAL; in ffs_do_single_desc()
2143 unsigned long num = 0; in ffs_do_descs() local
2144 int current_class = -1; in ffs_do_descs()
2149 if (num == count) in ffs_do_descs()
2153 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); in ffs_do_descs()
2156 num, ret); in ffs_do_descs()
2161 return _len - len; in ffs_do_descs()
2170 len -= ret; in ffs_do_descs()
2172 ++num; in ffs_do_descs()
2193 if (*valuep >= helper->interfaces_count) in __ffs_data_do_entity()
2194 helper->interfaces_count = *valuep + 1; in __ffs_data_do_entity()
2199 * Strings are indexed from 1 (0 is reserved in __ffs_data_do_entity()
2202 if (*valuep > helper->ffs->strings_count) in __ffs_data_do_entity()
2203 helper->ffs->strings_count = *valuep; in __ffs_data_do_entity()
2208 helper->eps_count++; in __ffs_data_do_entity()
2209 if (helper->eps_count >= FFS_MAX_EPS_COUNT) in __ffs_data_do_entity()
2210 return -EINVAL; in __ffs_data_do_entity()
2212 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count) in __ffs_data_do_entity()
2213 helper->ffs->eps_addrmap[helper->eps_count] = in __ffs_data_do_entity()
2214 d->bEndpointAddress; in __ffs_data_do_entity()
2215 else if (helper->ffs->eps_addrmap[helper->eps_count] != in __ffs_data_do_entity()
2216 d->bEndpointAddress) in __ffs_data_do_entity()
2217 return -EINVAL; in __ffs_data_do_entity()
2227 u16 bcd_version = le16_to_cpu(desc->bcdVersion); in __ffs_do_os_desc_header()
2228 u16 w_index = le16_to_cpu(desc->wIndex); in __ffs_do_os_desc_header()
2236 return -EINVAL; in __ffs_do_os_desc_header()
2247 return -EINVAL; in __ffs_do_os_desc_header()
2268 while (feature_count--) { in ffs_do_single_os_desc()
2275 len -= ret; in ffs_do_single_os_desc()
2277 return _len - len; in ffs_do_single_os_desc()
2286 unsigned long num = 0; in ffs_do_os_descs() local
2288 for (num = 0; num < count; ++num) { in ffs_do_os_descs()
2295 return -EINVAL; in ffs_do_os_descs()
2304 if (le32_to_cpu(desc->dwLength) > len) in ffs_do_os_descs()
2305 return -EINVAL; in ffs_do_os_descs()
2310 num, ret); in ffs_do_os_descs()
2314 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??" in ffs_do_os_descs()
2316 feature_count = le16_to_cpu(desc->wCount); in ffs_do_os_descs()
2318 (feature_count > 255 || desc->Reserved)) in ffs_do_os_descs()
2319 return -EINVAL; in ffs_do_os_descs()
2320 len -= ret; in ffs_do_os_descs()
2334 len -= ret; in ffs_do_os_descs()
2337 return _len - len; in ffs_do_os_descs()
2356 d->bFirstInterfaceNumber >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2357 return -EINVAL; in __ffs_data_do_os_desc()
2358 if (d->Reserved1 != 1) { in __ffs_data_do_os_desc()
2361 * but older kernels incorrectly rejected non-zero in __ffs_data_do_os_desc()
2366 d->Reserved1 = 1; in __ffs_data_do_os_desc()
2368 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i) in __ffs_data_do_os_desc()
2369 if (d->Reserved2[i]) in __ffs_data_do_os_desc()
2370 return -EINVAL; in __ffs_data_do_os_desc()
2380 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2381 return -EINVAL; in __ffs_data_do_os_desc()
2382 length = le32_to_cpu(d->dwSize); in __ffs_data_do_os_desc()
2384 return -EINVAL; in __ffs_data_do_os_desc()
2385 type = le32_to_cpu(d->dwPropertyDataType); in __ffs_data_do_os_desc()
2390 return -EINVAL; in __ffs_data_do_os_desc()
2392 pnl = le16_to_cpu(d->wPropertyNameLength); in __ffs_data_do_os_desc()
2396 return -EINVAL; in __ffs_data_do_os_desc()
2402 return -EINVAL; in __ffs_data_do_os_desc()
2404 ++ffs->ms_os_descs_ext_prop_count; in __ffs_data_do_os_desc()
2406 ffs->ms_os_descs_ext_prop_name_len += pnl * 2; in __ffs_data_do_os_desc()
2407 ffs->ms_os_descs_ext_prop_data_len += pdl; in __ffs_data_do_os_desc()
2412 return -EINVAL; in __ffs_data_do_os_desc()
2422 int ret = -EINVAL, i; in __ffs_data_got_descs()
2432 len -= 8; in __ffs_data_got_descs()
2436 ffs->user_flags = flags; in __ffs_data_got_descs()
2445 ret = -ENOSYS; in __ffs_data_got_descs()
2449 len -= 12; in __ffs_data_got_descs()
2458 ffs->ffs_eventfd = in __ffs_data_got_descs()
2460 if (IS_ERR(ffs->ffs_eventfd)) { in __ffs_data_got_descs()
2461 ret = PTR_ERR(ffs->ffs_eventfd); in __ffs_data_got_descs()
2462 ffs->ffs_eventfd = NULL; in __ffs_data_got_descs()
2466 len -= 4; in __ffs_data_got_descs()
2478 len -= 4; in __ffs_data_got_descs()
2487 len -= 4; in __ffs_data_got_descs()
2502 if (!ffs->eps_count && !ffs->interfaces_count) { in __ffs_data_got_descs()
2503 ffs->eps_count = helper.eps_count; in __ffs_data_got_descs()
2504 ffs->interfaces_count = helper.interfaces_count; in __ffs_data_got_descs()
2506 if (ffs->eps_count != helper.eps_count) { in __ffs_data_got_descs()
2507 ret = -EINVAL; in __ffs_data_got_descs()
2510 if (ffs->interfaces_count != helper.interfaces_count) { in __ffs_data_got_descs()
2511 ret = -EINVAL; in __ffs_data_got_descs()
2516 len -= ret; in __ffs_data_got_descs()
2524 len -= ret; in __ffs_data_got_descs()
2528 ret = -EINVAL; in __ffs_data_got_descs()
2532 ffs->raw_descs_data = _data; in __ffs_data_got_descs()
2533 ffs->raw_descs = raw_descs; in __ffs_data_got_descs()
2534 ffs->raw_descs_length = data - raw_descs; in __ffs_data_got_descs()
2535 ffs->fs_descs_count = counts[0]; in __ffs_data_got_descs()
2536 ffs->hs_descs_count = counts[1]; in __ffs_data_got_descs()
2537 ffs->ss_descs_count = counts[2]; in __ffs_data_got_descs()
2538 ffs->ms_os_descs_count = os_descs_count; in __ffs_data_got_descs()
2566 /* Do we have at least as many strings as descriptors need? */ in __ffs_data_got_strings()
2567 needed_count = ffs->strings_count; in __ffs_data_got_strings()
2572 * If we don't need any strings just return and free all in __ffs_data_got_strings()
2587 vla_item(d, struct usb_string, strings, in __ffs_data_got_strings()
2594 return -ENOMEM; in __ffs_data_got_strings()
2603 } while (--i); in __ffs_data_got_strings()
2609 s = vla_ptr(vlabuf, d, strings); in __ffs_data_got_strings()
2614 len -= 16; in __ffs_data_got_strings()
2616 do { /* lang_count > 0 so we can use do-while */ in __ffs_data_got_strings()
2622 t->language = get_unaligned_le16(data); in __ffs_data_got_strings()
2623 t->strings = s; in __ffs_data_got_strings()
2627 len -= 2; in __ffs_data_got_strings()
2630 do { /* str_count > 0 so we can use do-while */ in __ffs_data_got_strings()
2637 * User may provide more strings then we need, in __ffs_data_got_strings()
2643 * s->id will be set while adding in __ffs_data_got_strings()
2647 s->s = data; in __ffs_data_got_strings()
2648 --needed; in __ffs_data_got_strings()
2653 len -= length + 1; in __ffs_data_got_strings()
2654 } while (--str_per_lang); in __ffs_data_got_strings()
2656 s->id = 0; /* terminator */ in __ffs_data_got_strings()
2657 s->s = NULL; in __ffs_data_got_strings()
2660 } while (--lang_count); in __ffs_data_got_strings()
2667 ffs->stringtabs = stringtabs; in __ffs_data_got_strings()
2668 ffs->raw_strings = _data; in __ffs_data_got_strings()
2676 return -EINVAL; in __ffs_data_got_strings()
2692 * of ffs->setup_state without holding the lock because when in __ffs_event_add()
2696 if (ffs->setup_state == FFS_SETUP_PENDING) in __ffs_event_add()
2697 ffs->setup_state = FFS_SETUP_CANCELLED; in __ffs_event_add()
2701 * evens on ffs->ev.types queue. This is important because the queue in __ffs_event_add()
2732 u8 *ev = ffs->ev.types, *out = ev; in __ffs_event_add()
2733 unsigned n = ffs->ev.count; in __ffs_event_add()
2734 for (; n; --n, ++ev) in __ffs_event_add()
2739 ffs->ev.count = out - ffs->ev.types; in __ffs_event_add()
2743 ffs->ev.types[ffs->ev.count++] = type; in __ffs_event_add()
2744 wake_up_locked(&ffs->ev.waitq); in __ffs_event_add()
2745 if (ffs->ffs_eventfd) in __ffs_event_add()
2746 eventfd_signal(ffs->ffs_eventfd, 1); in __ffs_event_add()
2753 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_event_add()
2755 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_event_add()
2764 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i) in ffs_ep_addr2idx()
2765 if (ffs->eps_addrmap[i] == endpoint_address) in ffs_ep_addr2idx()
2767 return -ENOENT; in ffs_ep_addr2idx()
2790 if (func->function.ss_descriptors) { in __ffs_func_bind_do_descs()
2792 func->function.ss_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
2793 } else if (func->function.hs_descriptors) { in __ffs_func_bind_do_descs()
2795 func->function.hs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
2798 func->function.fs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
2801 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) in __ffs_func_bind_do_descs()
2804 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1; in __ffs_func_bind_do_descs()
2808 ffs_ep = func->eps + idx; in __ffs_func_bind_do_descs()
2810 if (ffs_ep->descs[ep_desc_id]) { in __ffs_func_bind_do_descs()
2813 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); in __ffs_func_bind_do_descs()
2814 return -EINVAL; in __ffs_func_bind_do_descs()
2816 ffs_ep->descs[ep_desc_id] = ds; in __ffs_func_bind_do_descs()
2818 ffs_dump_mem(": Original ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
2819 if (ffs_ep->ep) { in __ffs_func_bind_do_descs()
2820 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; in __ffs_func_bind_do_descs()
2821 if (!ds->wMaxPacketSize) in __ffs_func_bind_do_descs()
2822 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; in __ffs_func_bind_do_descs()
2833 bEndpointAddress = ds->bEndpointAddress; in __ffs_func_bind_do_descs()
2838 wMaxPacketSize = ds->wMaxPacketSize; in __ffs_func_bind_do_descs()
2840 ep = usb_ep_autoconfig(func->gadget, ds); in __ffs_func_bind_do_descs()
2842 return -ENOTSUPP; in __ffs_func_bind_do_descs()
2843 ep->driver_data = func->eps + idx; in __ffs_func_bind_do_descs()
2847 return -ENOMEM; in __ffs_func_bind_do_descs()
2849 ffs_ep->ep = ep; in __ffs_func_bind_do_descs()
2850 ffs_ep->req = req; in __ffs_func_bind_do_descs()
2851 func->eps_revmap[ds->bEndpointAddress & in __ffs_func_bind_do_descs()
2857 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in __ffs_func_bind_do_descs()
2858 ds->bEndpointAddress = bEndpointAddress; in __ffs_func_bind_do_descs()
2863 ds->wMaxPacketSize = wMaxPacketSize; in __ffs_func_bind_do_descs()
2865 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
2886 if (func->interfaces_nums[idx] < 0) { in __ffs_func_bind_do_nums()
2887 int id = usb_interface_id(func->conf, &func->function); in __ffs_func_bind_do_nums()
2890 func->interfaces_nums[idx] = id; in __ffs_func_bind_do_nums()
2892 newValue = func->interfaces_nums[idx]; in __ffs_func_bind_do_nums()
2897 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; in __ffs_func_bind_do_nums()
2905 if (desc->bDescriptorType == USB_DT_ENDPOINT) in __ffs_func_bind_do_nums()
2908 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; in __ffs_func_bind_do_nums()
2909 if (!func->eps[idx].ep) in __ffs_func_bind_do_nums()
2910 return -EINVAL; in __ffs_func_bind_do_nums()
2914 descs = func->eps[idx].descs; in __ffs_func_bind_do_nums()
2915 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; in __ffs_func_bind_do_nums()
2920 pr_vdebug("%02x -> %02x\n", *valuep, newValue); in __ffs_func_bind_do_nums()
2937 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
2938 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
2939 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID, in __ffs_func_bind_do_os_desc()
2940 ARRAY_SIZE(desc->CompatibleID) + in __ffs_func_bind_do_os_desc()
2941 ARRAY_SIZE(desc->SubCompatibleID)); in __ffs_func_bind_do_os_desc()
2952 t = &func->function.os_desc_table[h->interface]; in __ffs_func_bind_do_os_desc()
2953 t->if_id = func->interfaces_nums[h->interface]; in __ffs_func_bind_do_os_desc()
2955 ext_prop = func->ffs->ms_os_descs_ext_prop_avail; in __ffs_func_bind_do_os_desc()
2956 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop); in __ffs_func_bind_do_os_desc()
2958 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType); in __ffs_func_bind_do_os_desc()
2959 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength); in __ffs_func_bind_do_os_desc()
2960 ext_prop->data_len = le32_to_cpu(*(__le32 *) in __ffs_func_bind_do_os_desc()
2961 usb_ext_prop_data_len_ptr(data, ext_prop->name_len)); in __ffs_func_bind_do_os_desc()
2962 length = ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
2964 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail; in __ffs_func_bind_do_os_desc()
2965 func->ffs->ms_os_descs_ext_prop_name_avail += in __ffs_func_bind_do_os_desc()
2966 ext_prop->name_len; in __ffs_func_bind_do_os_desc()
2968 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail; in __ffs_func_bind_do_os_desc()
2969 func->ffs->ms_os_descs_ext_prop_data_avail += in __ffs_func_bind_do_os_desc()
2970 ext_prop->data_len; in __ffs_func_bind_do_os_desc()
2972 usb_ext_prop_data_ptr(data, ext_prop->name_len), in __ffs_func_bind_do_os_desc()
2973 ext_prop->data_len); in __ffs_func_bind_do_os_desc()
2975 switch (ext_prop->type) { in __ffs_func_bind_do_os_desc()
2980 ext_prop->data_len *= 2; in __ffs_func_bind_do_os_desc()
2983 ext_prop->data = ext_prop_data; in __ffs_func_bind_do_os_desc()
2986 ext_prop->name_len); in __ffs_func_bind_do_os_desc()
2988 ext_prop->name_len *= 2; in __ffs_func_bind_do_os_desc()
2989 ext_prop->name = ext_prop_name; in __ffs_func_bind_do_os_desc()
2991 t->os_desc->ext_prop_len += in __ffs_func_bind_do_os_desc()
2992 ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
2993 ++t->os_desc->ext_prop_count; in __ffs_func_bind_do_os_desc()
2994 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop); in __ffs_func_bind_do_os_desc()
3009 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_do_functionfs_bind()
3018 * Configfs-enabled gadgets however do need ffs_dev_lock. in ffs_do_functionfs_bind()
3020 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3022 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; in ffs_do_functionfs_bind()
3023 ffs_data = ffs_opts->dev->ffs_data; in ffs_do_functionfs_bind()
3024 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3029 func->ffs = ffs_data; in ffs_do_functionfs_bind()
3030 func->conf = c; in ffs_do_functionfs_bind()
3031 func->gadget = c->cdev->gadget; in ffs_do_functionfs_bind()
3038 * with regard to ffs_opts->bound access in ffs_do_functionfs_bind()
3040 if (!ffs_opts->refcnt) { in ffs_do_functionfs_bind()
3041 ret = functionfs_bind(func->ffs, c->cdev); in ffs_do_functionfs_bind()
3045 ffs_opts->refcnt++; in ffs_do_functionfs_bind()
3046 func->function.strings = func->ffs->stringtabs; in ffs_do_functionfs_bind()
3055 struct ffs_data *ffs = func->ffs; in _ffs_func_bind()
3057 const int full = !!func->ffs->fs_descs_count; in _ffs_func_bind()
3058 const int high = !!func->ffs->hs_descs_count; in _ffs_func_bind()
3059 const int super = !!func->ffs->ss_descs_count; in _ffs_func_bind()
3066 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count); in _ffs_func_bind()
3068 full ? ffs->fs_descs_count + 1 : 0); in _ffs_func_bind()
3070 high ? ffs->hs_descs_count + 1 : 0); in _ffs_func_bind()
3072 super ? ffs->ss_descs_count + 1 : 0); in _ffs_func_bind()
3073 vla_item_with_sz(d, short, inums, ffs->interfaces_count); in _ffs_func_bind()
3075 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3077 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3079 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3081 ffs->ms_os_descs_ext_prop_count); in _ffs_func_bind()
3083 ffs->ms_os_descs_ext_prop_name_len); in _ffs_func_bind()
3085 ffs->ms_os_descs_ext_prop_data_len); in _ffs_func_bind()
3086 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length); in _ffs_func_bind()
3091 return -ENOTSUPP; in _ffs_func_bind()
3096 return -ENOMEM; in _ffs_func_bind()
3098 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop); in _ffs_func_bind()
3099 ffs->ms_os_descs_ext_prop_name_avail = in _ffs_func_bind()
3101 ffs->ms_os_descs_ext_prop_data_avail = in _ffs_func_bind()
3105 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs, in _ffs_func_bind()
3106 ffs->raw_descs_length); in _ffs_func_bind()
3110 for (i = 0; i < ffs->eps_count; i++) in _ffs_func_bind()
3111 eps_ptr[i].num = -1; in _ffs_func_bind()
3114 * d_eps == vlabuf, func->eps used to kfree vlabuf later in _ffs_func_bind()
3116 func->eps = vla_ptr(vlabuf, d, eps); in _ffs_func_bind()
3117 func->interfaces_nums = vla_ptr(vlabuf, d, inums); in _ffs_func_bind()
3125 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); in _ffs_func_bind()
3126 fs_len = ffs_do_descs(ffs->fs_descs_count, in _ffs_func_bind()
3139 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); in _ffs_func_bind()
3140 hs_len = ffs_do_descs(ffs->hs_descs_count, in _ffs_func_bind()
3142 d_raw_descs__sz - fs_len, in _ffs_func_bind()
3153 func->function.ss_descriptors = func->function.ssp_descriptors = in _ffs_func_bind()
3155 ss_len = ffs_do_descs(ffs->ss_descs_count, in _ffs_func_bind()
3157 d_raw_descs__sz - fs_len - hs_len, in _ffs_func_bind()
3172 ret = ffs_do_descs(ffs->fs_descs_count + in _ffs_func_bind()
3173 (high ? ffs->hs_descs_count : 0) + in _ffs_func_bind()
3174 (super ? ffs->ss_descs_count : 0), in _ffs_func_bind()
3180 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table); in _ffs_func_bind()
3181 if (c->cdev->use_os_string) { in _ffs_func_bind()
3182 for (i = 0; i < ffs->interfaces_count; ++i) { in _ffs_func_bind()
3185 desc = func->function.os_desc_table[i].os_desc = in _ffs_func_bind()
3188 desc->ext_compat_id = in _ffs_func_bind()
3190 INIT_LIST_HEAD(&desc->ext_prop); in _ffs_func_bind()
3192 ret = ffs_do_os_descs(ffs->ms_os_descs_count, in _ffs_func_bind()
3195 d_raw_descs__sz - fs_len - hs_len - in _ffs_func_bind()
3201 func->function.os_desc_n = in _ffs_func_bind()
3202 c->cdev->use_os_string ? ffs->interfaces_count : 0; in _ffs_func_bind()
3224 if (ret && !--ffs_opts->refcnt) in ffs_func_bind()
3225 functionfs_unbind(func->ffs); in ffs_func_bind()
3244 struct ffs_data *ffs = func->ffs; in ffs_func_set_alt()
3247 if (alt != (unsigned)-1) { in ffs_func_set_alt()
3253 if (ffs->func) in ffs_func_set_alt()
3254 ffs_func_eps_disable(ffs->func); in ffs_func_set_alt()
3256 if (ffs->state == FFS_DEACTIVATED) { in ffs_func_set_alt()
3257 ffs->state = FFS_CLOSING; in ffs_func_set_alt()
3258 INIT_WORK(&ffs->reset_work, ffs_reset_work); in ffs_func_set_alt()
3259 schedule_work(&ffs->reset_work); in ffs_func_set_alt()
3260 return -ENODEV; in ffs_func_set_alt()
3263 if (ffs->state != FFS_ACTIVE) in ffs_func_set_alt()
3264 return -ENODEV; in ffs_func_set_alt()
3266 if (alt == (unsigned)-1) { in ffs_func_set_alt()
3267 ffs->func = NULL; in ffs_func_set_alt()
3272 ffs->func = func; in ffs_func_set_alt()
3281 ffs_func_set_alt(f, 0, (unsigned)-1); in ffs_func_disable()
3288 struct ffs_data *ffs = func->ffs; in ffs_func_setup()
3292 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType); in ffs_func_setup()
3293 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest); in ffs_func_setup()
3294 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue)); in ffs_func_setup()
3295 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3296 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength)); in ffs_func_setup()
3302 * passed to usb_configuration->setup() (if one is set). No in ffs_func_setup()
3308 if (ffs->state != FFS_ACTIVE) in ffs_func_setup()
3309 return -ENODEV; in ffs_func_setup()
3311 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_setup()
3313 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3319 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3322 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_func_setup()
3323 ret = func->ffs->eps_addrmap[ret]; in ffs_func_setup()
3327 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP) in ffs_func_setup()
3328 ret = le16_to_cpu(creq->wIndex); in ffs_func_setup()
3330 return -EOPNOTSUPP; in ffs_func_setup()
3333 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3334 ffs->ev.setup = *creq; in ffs_func_setup()
3335 ffs->ev.setup.wIndex = cpu_to_le16(ret); in ffs_func_setup()
3337 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3339 return ffs->ev.setup.wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0; in ffs_func_setup()
3348 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP)) in ffs_func_req_match()
3351 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_req_match()
3354 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3357 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3359 return (bool) (func->ffs->user_flags & in ffs_func_req_match()
3366 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); in ffs_func_suspend()
3371 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); in ffs_func_resume()
3377 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) in ffs_func_revmap_ep() argument
3379 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; in ffs_func_revmap_ep()
3380 return num ? num : -EDOM; in ffs_func_revmap_ep()
3385 short *nums = func->interfaces_nums; in ffs_func_revmap_intf()
3386 unsigned count = func->ffs->interfaces_count; in ffs_func_revmap_intf()
3388 for (; count; --count, ++nums) { in ffs_func_revmap_intf()
3390 return nums - func->interfaces_nums; in ffs_func_revmap_intf()
3393 return -EDOM; in ffs_func_revmap_intf()
3409 if (strcmp(dev->name, name) == 0) in _ffs_do_find_dev()
3425 if (dev->single) in _ffs_get_single_dev()
3458 usb_put_function_instance(&opts->func_inst); in ffs_attr_release()
3478 ffs_release_dev(opts->dev); in ffs_free_inst()
3480 _ffs_free_dev(opts->dev); in ffs_free_inst()
3488 return -ENAMETOOLONG; in ffs_set_inst_name()
3489 return ffs_name_dev(to_f_fs_opts(fi)->dev, name); in ffs_set_inst_name()
3499 return ERR_PTR(-ENOMEM); in ffs_alloc_inst()
3501 opts->func_inst.set_inst_name = ffs_set_inst_name; in ffs_alloc_inst()
3502 opts->func_inst.free_func_inst = ffs_free_inst; in ffs_alloc_inst()
3510 opts->dev = dev; in ffs_alloc_inst()
3511 dev->opts = opts; in ffs_alloc_inst()
3513 config_group_init_type_name(&opts->func_inst.group, "", in ffs_alloc_inst()
3515 return &opts->func_inst; in ffs_alloc_inst()
3527 struct ffs_data *ffs = func->ffs; in ffs_func_unbind()
3529 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_func_unbind()
3530 struct ffs_ep *ep = func->eps; in ffs_func_unbind()
3531 unsigned count = ffs->eps_count; in ffs_func_unbind()
3534 if (ffs->func == func) { in ffs_func_unbind()
3536 ffs->func = NULL; in ffs_func_unbind()
3540 drain_workqueue(ffs->io_completion_wq); in ffs_func_unbind()
3543 if (!--opts->refcnt) in ffs_func_unbind()
3547 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_unbind()
3548 while (count--) { in ffs_func_unbind()
3549 if (ep->ep && ep->req) in ffs_func_unbind()
3550 usb_ep_free_request(ep->ep, ep->req); in ffs_func_unbind()
3551 ep->req = NULL; in ffs_func_unbind()
3554 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_unbind()
3555 kfree(func->eps); in ffs_func_unbind()
3556 func->eps = NULL; in ffs_func_unbind()
3561 func->function.fs_descriptors = NULL; in ffs_func_unbind()
3562 func->function.hs_descriptors = NULL; in ffs_func_unbind()
3563 func->function.ss_descriptors = NULL; in ffs_func_unbind()
3564 func->function.ssp_descriptors = NULL; in ffs_func_unbind()
3565 func->interfaces_nums = NULL; in ffs_func_unbind()
3575 return ERR_PTR(-ENOMEM); in ffs_alloc()
3577 func->function.name = "Function FS Gadget"; in ffs_alloc()
3579 func->function.bind = ffs_func_bind; in ffs_alloc()
3580 func->function.unbind = ffs_func_unbind; in ffs_alloc()
3581 func->function.set_alt = ffs_func_set_alt; in ffs_alloc()
3582 func->function.disable = ffs_func_disable; in ffs_alloc()
3583 func->function.setup = ffs_func_setup; in ffs_alloc()
3584 func->function.req_match = ffs_func_req_match; in ffs_alloc()
3585 func->function.suspend = ffs_func_suspend; in ffs_alloc()
3586 func->function.resume = ffs_func_resume; in ffs_alloc()
3587 func->function.free_func = ffs_free; in ffs_alloc()
3589 return &func->function; in ffs_alloc()
3601 return ERR_PTR(-EBUSY); in _ffs_alloc_dev()
3605 return ERR_PTR(-ENOMEM); in _ffs_alloc_dev()
3615 list_add(&dev->entry, &ffs_devices); in _ffs_alloc_dev()
3629 strscpy(dev->name, name, ARRAY_SIZE(dev->name)); in ffs_name_dev()
3631 ret = -EBUSY; in ffs_name_dev()
3647 ret = -EBUSY; in ffs_single_dev()
3649 dev->single = true; in ffs_single_dev()
3661 list_del(&dev->entry); in _ffs_free_dev()
3677 ret = -ENOENT; in ffs_acquire_dev()
3678 } else if (ffs_dev->mounted) { in ffs_acquire_dev()
3679 ret = -EBUSY; in ffs_acquire_dev()
3680 } else if (ffs_dev->ffs_acquire_dev_callback && in ffs_acquire_dev()
3681 ffs_dev->ffs_acquire_dev_callback(ffs_dev)) { in ffs_acquire_dev()
3682 ret = -ENOENT; in ffs_acquire_dev()
3684 ffs_dev->mounted = true; in ffs_acquire_dev()
3685 ffs_dev->ffs_data = ffs_data; in ffs_acquire_dev()
3686 ffs_data->private_data = ffs_dev; in ffs_acquire_dev()
3697 if (ffs_dev && ffs_dev->mounted) { in ffs_release_dev()
3698 ffs_dev->mounted = false; in ffs_release_dev()
3699 if (ffs_dev->ffs_data) { in ffs_release_dev()
3700 ffs_dev->ffs_data->private_data = NULL; in ffs_release_dev()
3701 ffs_dev->ffs_data = NULL; in ffs_release_dev()
3704 if (ffs_dev->ffs_release_dev_callback) in ffs_release_dev()
3705 ffs_dev->ffs_release_dev_callback(ffs_dev); in ffs_release_dev()
3718 ffs_obj = ffs->private_data; in ffs_ready()
3720 ret = -EINVAL; in ffs_ready()
3723 if (WARN_ON(ffs_obj->desc_ready)) { in ffs_ready()
3724 ret = -EBUSY; in ffs_ready()
3728 ffs_obj->desc_ready = true; in ffs_ready()
3730 if (ffs_obj->ffs_ready_callback) { in ffs_ready()
3731 ret = ffs_obj->ffs_ready_callback(ffs); in ffs_ready()
3736 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); in ffs_ready()
3750 ffs_obj = ffs->private_data; in ffs_closed()
3754 ffs_obj->desc_ready = false; in ffs_closed()
3756 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) && in ffs_closed()
3757 ffs_obj->ffs_closed_callback) in ffs_closed()
3758 ffs_obj->ffs_closed_callback(ffs); in ffs_closed()
3760 if (ffs_obj->opts) in ffs_closed()
3761 opts = ffs_obj->opts; in ffs_closed()
3765 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent in ffs_closed()
3766 || !kref_read(&opts->func_inst.group.cg_item.ci_kref)) in ffs_closed()
3769 ci = opts->func_inst.group.cg_item.ci_parent->ci_parent; in ffs_closed()
3772 if (test_bit(FFS_FL_BOUND, &ffs->flags)) in ffs_closed()
3784 ? mutex_trylock(mutex) ? 0 : -EAGAIN in ffs_mutex_lock()