Lines Matching +full:open +full:- +full:dice
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
18 #include <linux/dma-buf.h>
19 #include <linux/dma-fence.h>
20 #include <linux/dma-resv.h>
47 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
65 /* Called with ffs->mutex held; take over ownership of data. */
100 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP); in ffs_setup_state_clear_cancelled()
128 struct usb_ep *ep; /* P: ffs->eps_lock */
129 struct usb_request *req; /* P: epfile->mutex */
146 struct usb_request *req; /* P: ffs->eps_lock */
147 struct usb_ep *ep; /* P: ffs->eps_lock */
157 /* Protects ep->ep and ep->req. */
161 struct ffs_ep *ep; /* P: ffs->eps_lock */
176 * can never run concurrently (they are synchronised by epfile->mutex)
186 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
221 #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
225 unsigned char in; /* P: ffs->eps_lock */
226 unsigned char isoc; /* P: ffs->eps_lock */
305 struct ffs_data *ffs = req->context; in ffs_ep0_complete()
307 complete(&ffs->ep0req_completion); in ffs_ep0_complete()
311 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_queue_wait()
313 struct usb_request *req = ffs->ep0req; in __ffs_ep0_queue_wait()
317 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
318 return -EINVAL; in __ffs_ep0_queue_wait()
321 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); in __ffs_ep0_queue_wait()
323 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
325 req->buf = data; in __ffs_ep0_queue_wait()
326 req->length = len; in __ffs_ep0_queue_wait()
333 if (req->buf == NULL) in __ffs_ep0_queue_wait()
334 req->buf = (void *)0xDEADBABE; in __ffs_ep0_queue_wait()
336 reinit_completion(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
338 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); in __ffs_ep0_queue_wait()
342 ret = wait_for_completion_interruptible(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
344 usb_ep_dequeue(ffs->gadget->ep0, req); in __ffs_ep0_queue_wait()
345 return -EINTR; in __ffs_ep0_queue_wait()
348 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_queue_wait()
349 return req->status ? req->status : req->actual; in __ffs_ep0_queue_wait()
354 if (ffs->ev.can_stall) { in __ffs_ep0_stall()
356 usb_ep_set_halt(ffs->gadget->ep0); in __ffs_ep0_stall()
357 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_stall()
358 return -EL2HLT; in __ffs_ep0_stall()
361 return -ESRCH; in __ffs_ep0_stall()
368 struct ffs_data *ffs = file->private_data; in ffs_ep0_write()
374 return -EIDRM; in ffs_ep0_write()
377 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_write()
382 switch (ffs->state) { in ffs_ep0_write()
387 ret = -EINVAL; in ffs_ep0_write()
398 if (ffs->state == FFS_READ_DESCRIPTORS) { in ffs_ep0_write()
404 ffs->state = FFS_READ_STRINGS; in ffs_ep0_write()
414 ffs->state = FFS_CLOSING; in ffs_ep0_write()
418 ffs->state = FFS_ACTIVE; in ffs_ep0_write()
419 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
423 ffs->state = FFS_CLOSING; in ffs_ep0_write()
437 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
440 ret = -EIDRM; in ffs_ep0_write()
444 ret = -ESRCH; in ffs_ep0_write()
452 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { in ffs_ep0_write()
453 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
459 len = min_t(size_t, len, le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_write()
461 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
469 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
479 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP in ffs_ep0_write()
485 ret = -EIDRM; in ffs_ep0_write()
487 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
496 ret = -EBADFD; in ffs_ep0_write()
500 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
504 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
507 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_read_events()
510 * n cannot be bigger than ffs->ev.count, which cannot be bigger than in __ffs_ep0_read_events()
511 * size of ffs->ev.types array (which is four) so that's how much space in __ffs_ep0_read_events()
514 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)]; in __ffs_ep0_read_events()
521 events[i].type = ffs->ev.types[i]; in __ffs_ep0_read_events()
523 events[i].u.setup = ffs->ev.setup; in __ffs_ep0_read_events()
524 ffs->setup_state = FFS_SETUP_PENDING; in __ffs_ep0_read_events()
528 ffs->ev.count -= n; in __ffs_ep0_read_events()
529 if (ffs->ev.count) in __ffs_ep0_read_events()
530 memmove(ffs->ev.types, ffs->ev.types + n, in __ffs_ep0_read_events()
531 ffs->ev.count * sizeof *ffs->ev.types); in __ffs_ep0_read_events()
533 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_read_events()
534 mutex_unlock(&ffs->mutex); in __ffs_ep0_read_events()
536 return copy_to_user(buf, events, size) ? -EFAULT : size; in __ffs_ep0_read_events()
542 struct ffs_data *ffs = file->private_data; in ffs_ep0_read()
549 return -EIDRM; in ffs_ep0_read()
552 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_read()
557 if (ffs->state != FFS_ACTIVE) { in ffs_ep0_read()
558 ret = -EBADFD; in ffs_ep0_read()
566 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
570 ret = -EIDRM; in ffs_ep0_read()
576 ret = -EINVAL; in ffs_ep0_read()
580 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { in ffs_ep0_read()
581 ret = -EAGAIN; in ffs_ep0_read()
585 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, in ffs_ep0_read()
586 ffs->ev.count)) { in ffs_ep0_read()
587 ret = -EINTR; in ffs_ep0_read()
593 min_t(size_t, n, ffs->ev.count)); in ffs_ep0_read()
596 if (ffs->ev.setup.bRequestType & USB_DIR_IN) { in ffs_ep0_read()
597 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
602 len = min_t(size_t, len, le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_read()
604 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
609 ret = -ENOMEM; in ffs_ep0_read()
614 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
619 ret = -EIDRM; in ffs_ep0_read()
626 ret = -EFAULT; in ffs_ep0_read()
630 ret = -EBADFD; in ffs_ep0_read()
634 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
636 mutex_unlock(&ffs->mutex); in ffs_ep0_read()
643 struct ffs_data *ffs = inode->i_private; in ffs_ep0_open()
645 if (ffs->state == FFS_CLOSING) in ffs_ep0_open()
646 return -EBUSY; in ffs_ep0_open()
648 file->private_data = ffs; in ffs_ep0_open()
656 struct ffs_data *ffs = file->private_data; in ffs_ep0_release()
665 struct ffs_data *ffs = file->private_data; in ffs_ep0_ioctl()
666 struct usb_gadget *gadget = ffs->gadget; in ffs_ep0_ioctl()
670 struct ffs_function *func = ffs->func; in ffs_ep0_ioctl()
671 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; in ffs_ep0_ioctl()
672 } else if (gadget && gadget->ops->ioctl) { in ffs_ep0_ioctl()
673 ret = gadget->ops->ioctl(gadget, code, value); in ffs_ep0_ioctl()
675 ret = -ENOTTY; in ffs_ep0_ioctl()
683 struct ffs_data *ffs = file->private_data; in ffs_ep0_poll()
687 poll_wait(file, &ffs->ev.waitq, wait); in ffs_ep0_poll()
689 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_poll()
693 switch (ffs->state) { in ffs_ep0_poll()
700 switch (ffs->setup_state) { in ffs_ep0_poll()
702 if (ffs->ev.count) in ffs_ep0_poll()
719 mutex_unlock(&ffs->mutex); in ffs_ep0_poll()
726 .open = ffs_ep0_open,
739 struct ffs_io_data *io_data = req->context; in ffs_epfile_io_complete()
741 if (req->status) in ffs_epfile_io_complete()
742 io_data->status = req->status; in ffs_epfile_io_complete()
744 io_data->status = req->actual; in ffs_epfile_io_complete()
746 complete(&io_data->done); in ffs_epfile_io_complete()
756 return -EFAULT; in ffs_copy_to_iter()
794 * @sg_table - pointer to a place to be filled with sg_table contents
795 * @size - required buffer size
832 if (io_data->use_sg) in ffs_alloc_buffer()
833 return ffs_build_sg_list(&io_data->sgt, data_len); in ffs_alloc_buffer()
840 if (!io_data->buf) in ffs_free_buffer()
843 if (io_data->use_sg) { in ffs_free_buffer()
844 sg_free_table(&io_data->sgt); in ffs_free_buffer()
845 vfree(io_data->buf); in ffs_free_buffer()
847 kfree(io_data->buf); in ffs_free_buffer()
855 int ret = io_data->status; in ffs_user_copy_worker()
856 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD; in ffs_user_copy_worker()
859 if (io_data->read && ret > 0) { in ffs_user_copy_worker()
860 kthread_use_mm(io_data->mm); in ffs_user_copy_worker()
861 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data); in ffs_user_copy_worker()
862 kthread_unuse_mm(io_data->mm); in ffs_user_copy_worker()
865 io_data->kiocb->ki_complete(io_data->kiocb, ret); in ffs_user_copy_worker()
867 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd) in ffs_user_copy_worker()
868 eventfd_signal(io_data->ffs->ffs_eventfd); in ffs_user_copy_worker()
870 spin_lock_irqsave(&io_data->ffs->eps_lock, flags); in ffs_user_copy_worker()
871 usb_ep_free_request(io_data->ep, io_data->req); in ffs_user_copy_worker()
872 io_data->req = NULL; in ffs_user_copy_worker()
873 spin_unlock_irqrestore(&io_data->ffs->eps_lock, flags); in ffs_user_copy_worker()
875 if (io_data->read) in ffs_user_copy_worker()
876 kfree(io_data->to_free); in ffs_user_copy_worker()
884 struct ffs_io_data *io_data = req->context; in ffs_epfile_async_io_complete()
885 struct ffs_data *ffs = io_data->ffs; in ffs_epfile_async_io_complete()
887 io_data->status = req->status ? req->status : req->actual; in ffs_epfile_async_io_complete()
889 INIT_WORK(&io_data->work, ffs_user_copy_worker); in ffs_epfile_async_io_complete()
890 queue_work(ffs->io_completion_wq, &io_data->work); in ffs_epfile_async_io_complete()
899 struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP); in __ffs_epfile_read_buffer_free()
904 /* Assumes epfile->mutex is held. */
909 * Null out epfile->read_buffer so ffs_func_eps_disable does not free in __ffs_epfile_read_buffered()
913 struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL); in __ffs_epfile_read_buffered()
918 ret = copy_to_iter(buf->data, buf->length, iter); in __ffs_epfile_read_buffered()
919 if (buf->length == ret) { in __ffs_epfile_read_buffered()
925 ret = -EFAULT; in __ffs_epfile_read_buffered()
927 buf->length -= ret; in __ffs_epfile_read_buffered()
928 buf->data += ret; in __ffs_epfile_read_buffered()
931 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_buffered()
937 /* Assumes epfile->mutex is held. */
949 return -EFAULT; in __ffs_epfile_read_data()
955 data_len -= ret; in __ffs_epfile_read_data()
958 return -ENOMEM; in __ffs_epfile_read_data()
959 buf->length = data_len; in __ffs_epfile_read_data()
960 buf->data = buf->storage; in __ffs_epfile_read_data()
961 memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len)); in __ffs_epfile_read_data()
969 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_data()
977 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_wait_ep()
982 ep = epfile->ep; in ffs_epfile_wait_ep()
984 if (file->f_flags & O_NONBLOCK) in ffs_epfile_wait_ep()
985 return ERR_PTR(-EAGAIN); in ffs_epfile_wait_ep()
988 epfile->ffs->wait, (ep = epfile->ep)); in ffs_epfile_wait_ep()
990 return ERR_PTR(-EINTR); in ffs_epfile_wait_ep()
998 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_io()
1002 ssize_t ret, data_len = -EINVAL; in ffs_epfile_io()
1006 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_io()
1007 return -ENODEV; in ffs_epfile_io()
1014 halt = (!io_data->read == !epfile->in); in ffs_epfile_io()
1015 if (halt && epfile->isoc) in ffs_epfile_io()
1016 return -EINVAL; in ffs_epfile_io()
1019 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK); in ffs_epfile_io()
1034 if (!io_data->aio && io_data->read) { in ffs_epfile_io()
1035 ret = __ffs_epfile_read_buffered(epfile, &io_data->data); in ffs_epfile_io()
1041 * if we _do_ wait above, the epfile->ffs->gadget might be NULL in ffs_epfile_io()
1045 gadget = epfile->ffs->gadget; in ffs_epfile_io()
1047 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1049 if (epfile->ep != ep) { in ffs_epfile_io()
1050 ret = -ESHUTDOWN; in ffs_epfile_io()
1053 data_len = iov_iter_count(&io_data->data); in ffs_epfile_io()
1058 if (io_data->read) in ffs_epfile_io()
1059 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len); in ffs_epfile_io()
1061 io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE; in ffs_epfile_io()
1062 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1066 ret = -ENOMEM; in ffs_epfile_io()
1069 if (!io_data->read && in ffs_epfile_io()
1070 !copy_from_iter_full(data, data_len, &io_data->data)) { in ffs_epfile_io()
1071 ret = -EFAULT; in ffs_epfile_io()
1076 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1078 if (epfile->ep != ep) { in ffs_epfile_io()
1080 ret = -ESHUTDOWN; in ffs_epfile_io()
1082 ret = usb_ep_set_halt(ep->ep); in ffs_epfile_io()
1084 ret = -EBADMSG; in ffs_epfile_io()
1085 } else if (data_len == -EINVAL) { in ffs_epfile_io()
1091 * being initialized to -EINVAL during its declaration, which in ffs_epfile_io()
1097 WARN(1, "%s: data_len == -EINVAL\n", __func__); in ffs_epfile_io()
1098 ret = -EINVAL; in ffs_epfile_io()
1099 } else if (!io_data->aio) { in ffs_epfile_io()
1102 req = ep->req; in ffs_epfile_io()
1103 if (io_data->use_sg) { in ffs_epfile_io()
1104 req->buf = NULL; in ffs_epfile_io()
1105 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1106 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1108 req->buf = data; in ffs_epfile_io()
1109 req->num_sgs = 0; in ffs_epfile_io()
1111 req->length = data_len; in ffs_epfile_io()
1113 io_data->buf = data; in ffs_epfile_io()
1115 init_completion(&io_data->done); in ffs_epfile_io()
1116 req->context = io_data; in ffs_epfile_io()
1117 req->complete = ffs_epfile_io_complete; in ffs_epfile_io()
1119 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1123 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1125 if (wait_for_completion_interruptible(&io_data->done)) { in ffs_epfile_io()
1126 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1127 if (epfile->ep != ep) { in ffs_epfile_io()
1128 ret = -ESHUTDOWN; in ffs_epfile_io()
1135 * condition with req->complete callback. in ffs_epfile_io()
1137 usb_ep_dequeue(ep->ep, req); in ffs_epfile_io()
1138 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1139 wait_for_completion(&io_data->done); in ffs_epfile_io()
1140 interrupted = io_data->status < 0; in ffs_epfile_io()
1144 ret = -EINTR; in ffs_epfile_io()
1145 else if (io_data->read && io_data->status > 0) in ffs_epfile_io()
1146 ret = __ffs_epfile_read_data(epfile, data, io_data->status, in ffs_epfile_io()
1147 &io_data->data); in ffs_epfile_io()
1149 ret = io_data->status; in ffs_epfile_io()
1151 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) { in ffs_epfile_io()
1152 ret = -ENOMEM; in ffs_epfile_io()
1154 if (io_data->use_sg) { in ffs_epfile_io()
1155 req->buf = NULL; in ffs_epfile_io()
1156 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1157 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1159 req->buf = data; in ffs_epfile_io()
1160 req->num_sgs = 0; in ffs_epfile_io()
1162 req->length = data_len; in ffs_epfile_io()
1164 io_data->buf = data; in ffs_epfile_io()
1165 io_data->ep = ep->ep; in ffs_epfile_io()
1166 io_data->req = req; in ffs_epfile_io()
1167 io_data->ffs = epfile->ffs; in ffs_epfile_io()
1169 req->context = io_data; in ffs_epfile_io()
1170 req->complete = ffs_epfile_async_io_complete; in ffs_epfile_io()
1172 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1174 io_data->req = NULL; in ffs_epfile_io()
1175 usb_ep_free_request(ep->ep, req); in ffs_epfile_io()
1179 ret = -EIOCBQUEUED; in ffs_epfile_io()
1188 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1190 mutex_unlock(&epfile->mutex); in ffs_epfile_io()
1192 if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */ in ffs_epfile_io()
1200 struct ffs_epfile *epfile = inode->i_private; in ffs_epfile_open()
1202 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_open()
1203 return -ENODEV; in ffs_epfile_open()
1205 file->private_data = epfile; in ffs_epfile_open()
1206 ffs_data_opened(epfile->ffs); in ffs_epfile_open()
1213 struct ffs_io_data *io_data = kiocb->private; in ffs_aio_cancel()
1214 struct ffs_epfile *epfile = kiocb->ki_filp->private_data; in ffs_aio_cancel()
1218 spin_lock_irqsave(&epfile->ffs->eps_lock, flags); in ffs_aio_cancel()
1220 if (io_data && io_data->ep && io_data->req) in ffs_aio_cancel()
1221 value = usb_ep_dequeue(io_data->ep, io_data->req); in ffs_aio_cancel()
1223 value = -EINVAL; in ffs_aio_cancel()
1225 spin_unlock_irqrestore(&epfile->ffs->eps_lock, flags); in ffs_aio_cancel()
1238 return -ENOMEM; in ffs_epfile_write_iter()
1239 p->aio = true; in ffs_epfile_write_iter()
1242 p->aio = false; in ffs_epfile_write_iter()
1245 p->read = false; in ffs_epfile_write_iter()
1246 p->kiocb = kiocb; in ffs_epfile_write_iter()
1247 p->data = *from; in ffs_epfile_write_iter()
1248 p->mm = current->mm; in ffs_epfile_write_iter()
1250 kiocb->private = p; in ffs_epfile_write_iter()
1252 if (p->aio) in ffs_epfile_write_iter()
1255 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_write_iter()
1256 if (res == -EIOCBQUEUED) in ffs_epfile_write_iter()
1258 if (p->aio) in ffs_epfile_write_iter()
1261 *from = p->data; in ffs_epfile_write_iter()
1273 return -ENOMEM; in ffs_epfile_read_iter()
1274 p->aio = true; in ffs_epfile_read_iter()
1277 p->aio = false; in ffs_epfile_read_iter()
1280 p->read = true; in ffs_epfile_read_iter()
1281 p->kiocb = kiocb; in ffs_epfile_read_iter()
1282 if (p->aio) { in ffs_epfile_read_iter()
1283 p->to_free = dup_iter(&p->data, to, GFP_KERNEL); in ffs_epfile_read_iter()
1284 if (!iter_is_ubuf(&p->data) && !p->to_free) { in ffs_epfile_read_iter()
1286 return -ENOMEM; in ffs_epfile_read_iter()
1289 p->data = *to; in ffs_epfile_read_iter()
1290 p->to_free = NULL; in ffs_epfile_read_iter()
1292 p->mm = current->mm; in ffs_epfile_read_iter()
1294 kiocb->private = p; in ffs_epfile_read_iter()
1296 if (p->aio) in ffs_epfile_read_iter()
1299 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_read_iter()
1300 if (res == -EIOCBQUEUED) in ffs_epfile_read_iter()
1303 if (p->aio) { in ffs_epfile_read_iter()
1304 kfree(p->to_free); in ffs_epfile_read_iter()
1307 *to = p->data; in ffs_epfile_read_iter()
1315 struct dma_buf_attachment *attach = priv->attach; in ffs_dmabuf_release()
1316 struct dma_buf *dmabuf = attach->dmabuf; in ffs_dmabuf_release()
1319 dma_resv_lock(dmabuf->resv, NULL); in ffs_dmabuf_release()
1320 dma_buf_unmap_attachment(attach, priv->sgt, priv->dir); in ffs_dmabuf_release()
1321 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_release()
1323 dma_buf_detach(attach->dmabuf, attach); in ffs_dmabuf_release()
1330 struct ffs_dmabuf_priv *priv = attach->importer_priv; in ffs_dmabuf_get()
1332 kref_get(&priv->ref); in ffs_dmabuf_get()
1337 struct ffs_dmabuf_priv *priv = attach->importer_priv; in ffs_dmabuf_put()
1339 kref_put(&priv->ref, ffs_dmabuf_release); in ffs_dmabuf_put()
1345 struct ffs_epfile *epfile = inode->i_private; in ffs_epfile_release()
1347 struct ffs_data *ffs = epfile->ffs; in ffs_epfile_release()
1349 mutex_lock(&epfile->dmabufs_mutex); in ffs_epfile_release()
1352 list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) { in ffs_epfile_release()
1354 spin_lock_irq(&ffs->eps_lock); in ffs_epfile_release()
1355 if (priv->ep && priv->req) in ffs_epfile_release()
1356 usb_ep_dequeue(priv->ep, priv->req); in ffs_epfile_release()
1357 spin_unlock_irq(&ffs->eps_lock); in ffs_epfile_release()
1359 list_del(&priv->entry); in ffs_epfile_release()
1360 ffs_dmabuf_put(priv->attach); in ffs_epfile_release()
1363 mutex_unlock(&epfile->dmabufs_mutex); in ffs_epfile_release()
1366 ffs_data_closed(epfile->ffs); in ffs_epfile_release()
1375 struct ffs_dmabuf_priv *priv = dma_fence->priv; in ffs_dmabuf_cleanup()
1376 struct dma_buf_attachment *attach = priv->attach; in ffs_dmabuf_cleanup()
1377 struct dma_fence *fence = &dma_fence->base; in ffs_dmabuf_cleanup()
1385 struct ffs_dmabuf_priv *priv = dma_fence->priv; in ffs_dmabuf_signal_done()
1386 struct dma_fence *fence = &dma_fence->base; in ffs_dmabuf_signal_done()
1390 fence->error = ret; in ffs_dmabuf_signal_done()
1399 INIT_WORK(&dma_fence->work, ffs_dmabuf_cleanup); in ffs_dmabuf_signal_done()
1400 queue_work(priv->ffs->io_completion_wq, &dma_fence->work); in ffs_dmabuf_signal_done()
1406 pr_vdebug("FFS: DMABUF transfer complete, status=%d\n", req->status); in ffs_epfile_dmabuf_io_complete()
1407 ffs_dmabuf_signal_done(req->context, req->status); in ffs_epfile_dmabuf_io_complete()
1438 return dma_resv_lock_interruptible(dmabuf->resv, NULL); in ffs_dma_resv_lock()
1440 if (!dma_resv_trylock(dmabuf->resv)) in ffs_dma_resv_lock()
1441 return -EBUSY; in ffs_dma_resv_lock()
1449 struct device *dev = epfile->ffs->gadget->dev.parent; in ffs_dmabuf_find_attachment()
1453 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_find_attachment()
1455 list_for_each_entry(priv, &epfile->dmabufs, entry) { in ffs_dmabuf_find_attachment()
1456 if (priv->attach->dev == dev in ffs_dmabuf_find_attachment()
1457 && priv->attach->dmabuf == dmabuf) { in ffs_dmabuf_find_attachment()
1458 attach = priv->attach; in ffs_dmabuf_find_attachment()
1466 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_find_attachment()
1468 return attach ?: ERR_PTR(-EPERM); in ffs_dmabuf_find_attachment()
1473 bool nonblock = file->f_flags & O_NONBLOCK; in ffs_dmabuf_attach()
1474 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_attach()
1475 struct usb_gadget *gadget = epfile->ffs->gadget; in ffs_dmabuf_attach()
1483 if (!gadget || !gadget->sg_supported) in ffs_dmabuf_attach()
1484 return -EPERM; in ffs_dmabuf_attach()
1490 attach = dma_buf_attach(dmabuf, gadget->dev.parent); in ffs_dmabuf_attach()
1498 err = -ENOMEM; in ffs_dmabuf_attach()
1502 dir = epfile->in ? DMA_FROM_DEVICE : DMA_TO_DEVICE; in ffs_dmabuf_attach()
1509 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_attach()
1516 attach->importer_priv = priv; in ffs_dmabuf_attach()
1518 priv->sgt = sg_table; in ffs_dmabuf_attach()
1519 priv->dir = dir; in ffs_dmabuf_attach()
1520 priv->ffs = epfile->ffs; in ffs_dmabuf_attach()
1521 priv->attach = attach; in ffs_dmabuf_attach()
1522 spin_lock_init(&priv->lock); in ffs_dmabuf_attach()
1523 kref_init(&priv->ref); in ffs_dmabuf_attach()
1524 priv->context = dma_fence_context_alloc(1); in ffs_dmabuf_attach()
1526 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_attach()
1527 list_add(&priv->entry, &epfile->dmabufs); in ffs_dmabuf_attach()
1528 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_attach()
1544 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_detach()
1545 struct ffs_data *ffs = epfile->ffs; in ffs_dmabuf_detach()
1546 struct device *dev = ffs->gadget->dev.parent; in ffs_dmabuf_detach()
1549 int ret = -EPERM; in ffs_dmabuf_detach()
1555 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_detach()
1557 list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) { in ffs_dmabuf_detach()
1558 if (priv->attach->dev == dev in ffs_dmabuf_detach()
1559 && priv->attach->dmabuf == dmabuf) { in ffs_dmabuf_detach()
1561 spin_lock_irq(&ffs->eps_lock); in ffs_dmabuf_detach()
1562 if (priv->ep && priv->req) in ffs_dmabuf_detach()
1563 usb_ep_dequeue(priv->ep, priv->req); in ffs_dmabuf_detach()
1564 spin_unlock_irq(&ffs->eps_lock); in ffs_dmabuf_detach()
1566 list_del(&priv->entry); in ffs_dmabuf_detach()
1569 ffs_dmabuf_put(priv->attach); in ffs_dmabuf_detach()
1575 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_detach()
1584 bool nonblock = file->f_flags & O_NONBLOCK; in ffs_dmabuf_transfer()
1585 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_transfer()
1599 if (req->flags & ~USB_FFS_DMABUF_TRANSFER_MASK) in ffs_dmabuf_transfer()
1600 return -EINVAL; in ffs_dmabuf_transfer()
1602 dmabuf = dma_buf_get(req->fd); in ffs_dmabuf_transfer()
1606 if (req->length > dmabuf->size || req->length == 0) { in ffs_dmabuf_transfer()
1607 ret = -EINVAL; in ffs_dmabuf_transfer()
1617 priv = attach->importer_priv; in ffs_dmabuf_transfer()
1631 retl = dma_resv_wait_timeout(dmabuf->resv, in ffs_dmabuf_transfer()
1632 dma_resv_usage_rw(epfile->in), in ffs_dmabuf_transfer()
1635 retl = -EBUSY; in ffs_dmabuf_transfer()
1641 ret = dma_resv_reserve_fences(dmabuf->resv, 1); in ffs_dmabuf_transfer()
1647 ret = -ENOMEM; in ffs_dmabuf_transfer()
1651 fence->priv = priv; in ffs_dmabuf_transfer()
1653 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1656 if (epfile->ep != ep) { in ffs_dmabuf_transfer()
1657 ret = -ESHUTDOWN; in ffs_dmabuf_transfer()
1661 usb_req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC); in ffs_dmabuf_transfer()
1663 ret = -ENOMEM; in ffs_dmabuf_transfer()
1672 seqno = atomic_add_return(1, &epfile->seqno); in ffs_dmabuf_transfer()
1674 dma_fence_init(&fence->base, &ffs_dmabuf_fence_ops, in ffs_dmabuf_transfer()
1675 &priv->lock, priv->context, seqno); in ffs_dmabuf_transfer()
1677 resv_dir = epfile->in ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ; in ffs_dmabuf_transfer()
1679 dma_resv_add_fence(dmabuf->resv, &fence->base, resv_dir); in ffs_dmabuf_transfer()
1680 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_transfer()
1684 usb_req->length = req->length; in ffs_dmabuf_transfer()
1685 usb_req->buf = NULL; in ffs_dmabuf_transfer()
1686 usb_req->sg = priv->sgt->sgl; in ffs_dmabuf_transfer()
1687 usb_req->num_sgs = sg_nents_for_len(priv->sgt->sgl, req->length); in ffs_dmabuf_transfer()
1688 usb_req->sg_was_mapped = true; in ffs_dmabuf_transfer()
1689 usb_req->context = fence; in ffs_dmabuf_transfer()
1690 usb_req->complete = ffs_epfile_dmabuf_io_complete; in ffs_dmabuf_transfer()
1693 ret = usb_ep_queue(ep->ep, usb_req, GFP_ATOMIC); in ffs_dmabuf_transfer()
1696 priv->req = usb_req; in ffs_dmabuf_transfer()
1697 priv->ep = ep->ep; in ffs_dmabuf_transfer()
1701 usb_ep_free_request(ep->ep, usb_req); in ffs_dmabuf_transfer()
1704 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1710 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1711 dma_fence_put(&fence->base); in ffs_dmabuf_transfer()
1713 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_transfer()
1725 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_ioctl()
1729 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_ioctl()
1730 return -ENODEV; in ffs_epfile_ioctl()
1738 ret = -EFAULT; in ffs_epfile_ioctl()
1749 ret = -EFAULT; in ffs_epfile_ioctl()
1760 ret = -EFAULT; in ffs_epfile_ioctl()
1775 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1778 if (epfile->ep != ep) { in ffs_epfile_ioctl()
1779 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1780 return -ESHUTDOWN; in ffs_epfile_ioctl()
1785 ret = usb_ep_fifo_status(epfile->ep->ep); in ffs_epfile_ioctl()
1788 usb_ep_fifo_flush(epfile->ep->ep); in ffs_epfile_ioctl()
1792 ret = usb_ep_clear_halt(epfile->ep->ep); in ffs_epfile_ioctl()
1795 ret = epfile->ep->num; in ffs_epfile_ioctl()
1802 switch (epfile->ffs->gadget->speed) { in ffs_epfile_ioctl()
1814 desc = epfile->ep->descs[desc_idx]; in ffs_epfile_ioctl()
1815 memcpy(&desc1, desc, desc->bLength); in ffs_epfile_ioctl()
1817 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1820 ret = -EFAULT; in ffs_epfile_ioctl()
1824 ret = -ENOTTY; in ffs_epfile_ioctl()
1826 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1833 .open = ffs_epfile_open,
1862 inode->i_ino = get_next_ino(); in ffs_sb_make_inode()
1863 inode->i_mode = perms->mode; in ffs_sb_make_inode()
1864 inode->i_uid = perms->uid; in ffs_sb_make_inode()
1865 inode->i_gid = perms->gid; in ffs_sb_make_inode()
1868 inode->i_private = data; in ffs_sb_make_inode()
1870 inode->i_fop = fops; in ffs_sb_make_inode()
1872 inode->i_op = iops; in ffs_sb_make_inode()
1883 struct ffs_data *ffs = sb->s_fs_info; in ffs_sb_create_file()
1887 dentry = d_alloc_name(sb->s_root, name); in ffs_sb_create_file()
1891 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); in ffs_sb_create_file()
1917 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_sb_fill()
1919 struct ffs_data *ffs = data->ffs_data; in ffs_sb_fill()
1921 ffs->sb = sb; in ffs_sb_fill()
1922 data->ffs_data = NULL; in ffs_sb_fill()
1923 sb->s_fs_info = ffs; in ffs_sb_fill()
1924 sb->s_blocksize = PAGE_SIZE; in ffs_sb_fill()
1925 sb->s_blocksize_bits = PAGE_SHIFT; in ffs_sb_fill()
1926 sb->s_magic = FUNCTIONFS_MAGIC; in ffs_sb_fill()
1927 sb->s_op = &ffs_sb_operations; in ffs_sb_fill()
1928 sb->s_time_gran = 1; in ffs_sb_fill()
1931 data->perms.mode = data->root_mode; in ffs_sb_fill()
1935 &data->perms); in ffs_sb_fill()
1936 sb->s_root = d_make_root(inode); in ffs_sb_fill()
1937 if (!sb->s_root) in ffs_sb_fill()
1938 return -ENOMEM; in ffs_sb_fill()
1942 return -ENOMEM; in ffs_sb_fill()
1968 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_fs_parse_param()
1978 data->no_disconnect = result.boolean; in ffs_fs_parse_param()
1981 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1984 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
1987 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1988 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
1992 data->perms.uid = make_kuid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
1993 if (!uid_valid(data->perms.uid)) in ffs_fs_parse_param()
1997 data->perms.gid = make_kgid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
1998 if (!gid_valid(data->perms.gid)) in ffs_fs_parse_param()
2003 return -ENOPARAM; in ffs_fs_parse_param()
2009 return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32); in ffs_fs_parse_param()
2017 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_get_tree()
2021 if (!fc->source) in ffs_fs_get_tree()
2024 ffs = ffs_data_new(fc->source); in ffs_fs_get_tree()
2026 return -ENOMEM; in ffs_fs_get_tree()
2027 ffs->file_perms = ctx->perms; in ffs_fs_get_tree()
2028 ffs->no_disconnect = ctx->no_disconnect; in ffs_fs_get_tree()
2030 ffs->dev_name = kstrdup(fc->source, GFP_KERNEL); in ffs_fs_get_tree()
2031 if (!ffs->dev_name) { in ffs_fs_get_tree()
2033 return -ENOMEM; in ffs_fs_get_tree()
2036 ret = ffs_acquire_dev(ffs->dev_name, ffs); in ffs_fs_get_tree()
2042 ctx->ffs_data = ffs; in ffs_fs_get_tree()
2048 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_free_fc()
2051 if (ctx->ffs_data) { in ffs_fs_free_fc()
2052 ffs_data_put(ctx->ffs_data); in ffs_fs_free_fc()
2071 return -ENOMEM; in ffs_fs_init_fs_context()
2073 ctx->perms.mode = S_IFREG | 0600; in ffs_fs_init_fs_context()
2074 ctx->perms.uid = GLOBAL_ROOT_UID; in ffs_fs_init_fs_context()
2075 ctx->perms.gid = GLOBAL_ROOT_GID; in ffs_fs_init_fs_context()
2076 ctx->root_mode = S_IFDIR | 0500; in ffs_fs_init_fs_context()
2077 ctx->no_disconnect = false; in ffs_fs_init_fs_context()
2079 fc->fs_private = ctx; in ffs_fs_init_fs_context()
2080 fc->ops = &ffs_fs_context_ops; in ffs_fs_init_fs_context()
2088 if (sb->s_fs_info) in ffs_fs_kill_sb()
2089 ffs_data_closed(sb->s_fs_info); in ffs_fs_kill_sb()
2131 refcount_inc(&ffs->ref); in ffs_data_get()
2136 refcount_inc(&ffs->ref); in ffs_data_opened()
2137 if (atomic_add_return(1, &ffs->opened) == 1 && in ffs_data_opened()
2138 ffs->state == FFS_DEACTIVATED) { in ffs_data_opened()
2139 ffs->state = FFS_CLOSING; in ffs_data_opened()
2146 if (refcount_dec_and_test(&ffs->ref)) { in ffs_data_put()
2149 ffs_release_dev(ffs->private_data); in ffs_data_put()
2150 BUG_ON(waitqueue_active(&ffs->ev.waitq) || in ffs_data_put()
2151 swait_active(&ffs->ep0req_completion.wait) || in ffs_data_put()
2152 waitqueue_active(&ffs->wait)); in ffs_data_put()
2153 destroy_workqueue(ffs->io_completion_wq); in ffs_data_put()
2154 kfree(ffs->dev_name); in ffs_data_put()
2164 if (atomic_dec_and_test(&ffs->opened)) { in ffs_data_closed()
2165 if (ffs->no_disconnect) { in ffs_data_closed()
2166 ffs->state = FFS_DEACTIVATED; in ffs_data_closed()
2167 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_closed()
2168 epfiles = ffs->epfiles; in ffs_data_closed()
2169 ffs->epfiles = NULL; in ffs_data_closed()
2170 spin_unlock_irqrestore(&ffs->eps_lock, in ffs_data_closed()
2175 ffs->eps_count); in ffs_data_closed()
2177 if (ffs->setup_state == FFS_SETUP_PENDING) in ffs_data_closed()
2180 ffs->state = FFS_CLOSING; in ffs_data_closed()
2184 if (atomic_read(&ffs->opened) < 0) { in ffs_data_closed()
2185 ffs->state = FFS_CLOSING; in ffs_data_closed()
2198 ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name); in ffs_data_new()
2199 if (!ffs->io_completion_wq) { in ffs_data_new()
2204 refcount_set(&ffs->ref, 1); in ffs_data_new()
2205 atomic_set(&ffs->opened, 0); in ffs_data_new()
2206 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_new()
2207 mutex_init(&ffs->mutex); in ffs_data_new()
2208 spin_lock_init(&ffs->eps_lock); in ffs_data_new()
2209 init_waitqueue_head(&ffs->ev.waitq); in ffs_data_new()
2210 init_waitqueue_head(&ffs->wait); in ffs_data_new()
2211 init_completion(&ffs->ep0req_completion); in ffs_data_new()
2214 ffs->ev.can_stall = 1; in ffs_data_new()
2226 BUG_ON(ffs->gadget); in ffs_data_clear()
2228 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_clear()
2229 epfiles = ffs->epfiles; in ffs_data_clear()
2230 ffs->epfiles = NULL; in ffs_data_clear()
2231 spin_unlock_irqrestore(&ffs->eps_lock, flags); in ffs_data_clear()
2236 * copy of epfile will save us from use-after-free. in ffs_data_clear()
2239 ffs_epfiles_destroy(epfiles, ffs->eps_count); in ffs_data_clear()
2240 ffs->epfiles = NULL; in ffs_data_clear()
2243 if (ffs->ffs_eventfd) { in ffs_data_clear()
2244 eventfd_ctx_put(ffs->ffs_eventfd); in ffs_data_clear()
2245 ffs->ffs_eventfd = NULL; in ffs_data_clear()
2248 kfree(ffs->raw_descs_data); in ffs_data_clear()
2249 kfree(ffs->raw_strings); in ffs_data_clear()
2250 kfree(ffs->stringtabs); in ffs_data_clear()
2257 ffs->raw_descs_data = NULL; in ffs_data_reset()
2258 ffs->raw_descs = NULL; in ffs_data_reset()
2259 ffs->raw_strings = NULL; in ffs_data_reset()
2260 ffs->stringtabs = NULL; in ffs_data_reset()
2262 ffs->raw_descs_length = 0; in ffs_data_reset()
2263 ffs->fs_descs_count = 0; in ffs_data_reset()
2264 ffs->hs_descs_count = 0; in ffs_data_reset()
2265 ffs->ss_descs_count = 0; in ffs_data_reset()
2267 ffs->strings_count = 0; in ffs_data_reset()
2268 ffs->interfaces_count = 0; in ffs_data_reset()
2269 ffs->eps_count = 0; in ffs_data_reset()
2271 ffs->ev.count = 0; in ffs_data_reset()
2273 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_reset()
2274 ffs->setup_state = FFS_NO_SETUP; in ffs_data_reset()
2275 ffs->flags = 0; in ffs_data_reset()
2277 ffs->ms_os_descs_ext_prop_count = 0; in ffs_data_reset()
2278 ffs->ms_os_descs_ext_prop_name_len = 0; in ffs_data_reset()
2279 ffs->ms_os_descs_ext_prop_data_len = 0; in ffs_data_reset()
2288 if ((ffs->state != FFS_ACTIVE in functionfs_bind()
2289 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) in functionfs_bind()
2290 return -EBADFD; in functionfs_bind()
2292 first_id = usb_string_ids_n(cdev, ffs->strings_count); in functionfs_bind()
2296 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); in functionfs_bind()
2297 if (!ffs->ep0req) in functionfs_bind()
2298 return -ENOMEM; in functionfs_bind()
2299 ffs->ep0req->complete = ffs_ep0_complete; in functionfs_bind()
2300 ffs->ep0req->context = ffs; in functionfs_bind()
2302 lang = ffs->stringtabs; in functionfs_bind()
2305 struct usb_string *str = (*lang)->strings; in functionfs_bind()
2307 for (; str->s; ++id, ++str) in functionfs_bind()
2308 str->id = id; in functionfs_bind()
2312 ffs->gadget = cdev->gadget; in functionfs_bind()
2319 if (!WARN_ON(!ffs->gadget)) { in functionfs_unbind()
2321 usb_ep_dequeue(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
2322 mutex_lock(&ffs->mutex); in functionfs_unbind()
2323 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
2324 ffs->ep0req = NULL; in functionfs_unbind()
2325 ffs->gadget = NULL; in functionfs_unbind()
2326 clear_bit(FFS_FL_BOUND, &ffs->flags); in functionfs_unbind()
2327 mutex_unlock(&ffs->mutex); in functionfs_unbind()
2337 count = ffs->eps_count; in ffs_epfiles_create()
2340 return -ENOMEM; in ffs_epfiles_create()
2344 epfile->ffs = ffs; in ffs_epfiles_create()
2345 mutex_init(&epfile->mutex); in ffs_epfiles_create()
2346 mutex_init(&epfile->dmabufs_mutex); in ffs_epfiles_create()
2347 INIT_LIST_HEAD(&epfile->dmabufs); in ffs_epfiles_create()
2348 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_epfiles_create()
2349 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]); in ffs_epfiles_create()
2351 sprintf(epfile->name, "ep%u", i); in ffs_epfiles_create()
2352 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name, in ffs_epfiles_create()
2355 if (!epfile->dentry) { in ffs_epfiles_create()
2356 ffs_epfiles_destroy(epfiles, i - 1); in ffs_epfiles_create()
2357 return -ENOMEM; in ffs_epfiles_create()
2361 ffs->epfiles = epfiles; in ffs_epfiles_create()
2369 for (; count; --count, ++epfile) { in ffs_epfiles_destroy()
2370 BUG_ON(mutex_is_locked(&epfile->mutex)); in ffs_epfiles_destroy()
2371 if (epfile->dentry) { in ffs_epfiles_destroy()
2372 d_delete(epfile->dentry); in ffs_epfiles_destroy()
2373 dput(epfile->dentry); in ffs_epfiles_destroy()
2374 epfile->dentry = NULL; in ffs_epfiles_destroy()
2388 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
2389 count = func->ffs->eps_count; in ffs_func_eps_disable()
2390 epfile = func->ffs->epfiles; in ffs_func_eps_disable()
2391 ep = func->eps; in ffs_func_eps_disable()
2392 while (count--) { in ffs_func_eps_disable()
2394 if (ep->ep) in ffs_func_eps_disable()
2395 usb_ep_disable(ep->ep); in ffs_func_eps_disable()
2399 epfile->ep = NULL; in ffs_func_eps_disable()
2404 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
2416 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
2417 ffs = func->ffs; in ffs_func_eps_enable()
2418 ep = func->eps; in ffs_func_eps_enable()
2419 epfile = ffs->epfiles; in ffs_func_eps_enable()
2420 count = ffs->eps_count; in ffs_func_eps_enable()
2421 while(count--) { in ffs_func_eps_enable()
2422 ep->ep->driver_data = ep; in ffs_func_eps_enable()
2424 ret = config_ep_by_speed(func->gadget, &func->function, ep->ep); in ffs_func_eps_enable()
2427 __func__, ep->ep->name, ret); in ffs_func_eps_enable()
2431 ret = usb_ep_enable(ep->ep); in ffs_func_eps_enable()
2433 epfile->ep = ep; in ffs_func_eps_enable()
2434 epfile->in = usb_endpoint_dir_in(ep->ep->desc); in ffs_func_eps_enable()
2435 epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc); in ffs_func_eps_enable()
2444 wake_up_interruptible(&ffs->wait); in ffs_func_eps_enable()
2445 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
2488 return -EINVAL; in ffs_do_single_desc()
2492 length = _ds->bLength; in ffs_do_single_desc()
2495 return -EINVAL; in ffs_do_single_desc()
2505 return -EINVAL; \ in ffs_do_single_desc()
2516 switch (_ds->bDescriptorType) { in ffs_do_single_desc()
2523 _ds->bDescriptorType); in ffs_do_single_desc()
2524 return -EINVAL; in ffs_do_single_desc()
2532 __entity(INTERFACE, ds->bInterfaceNumber); in ffs_do_single_desc()
2533 if (ds->iInterface) in ffs_do_single_desc()
2534 __entity(STRING, ds->iInterface); in ffs_do_single_desc()
2535 *current_class = ds->bInterfaceClass; in ffs_do_single_desc()
2536 *current_subclass = ds->bInterfaceSubClass; in ffs_do_single_desc()
2546 __entity(ENDPOINT, ds->bEndpointAddress); in ffs_do_single_desc()
2569 _ds->bDescriptorType, *current_class); in ffs_do_single_desc()
2570 return -EINVAL; in ffs_do_single_desc()
2583 if (ds->iFunction) in ffs_do_single_desc()
2584 __entity(STRING, ds->iFunction); in ffs_do_single_desc()
2600 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2601 return -EINVAL; in ffs_do_single_desc()
2605 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2606 return -EINVAL; in ffs_do_single_desc()
2610 _ds->bLength, _ds->bDescriptorType); in ffs_do_single_desc()
2611 return -EINVAL; in ffs_do_single_desc()
2628 int current_class = -1; in ffs_do_descs()
2629 int current_subclass = -1; in ffs_do_descs()
2646 return _len - len; in ffs_do_descs()
2655 len -= ret; in ffs_do_descs()
2678 if (*valuep >= helper->interfaces_count) in __ffs_data_do_entity()
2679 helper->interfaces_count = *valuep + 1; in __ffs_data_do_entity()
2687 if (*valuep > helper->ffs->strings_count) in __ffs_data_do_entity()
2688 helper->ffs->strings_count = *valuep; in __ffs_data_do_entity()
2693 helper->eps_count++; in __ffs_data_do_entity()
2694 if (helper->eps_count >= FFS_MAX_EPS_COUNT) in __ffs_data_do_entity()
2695 return -EINVAL; in __ffs_data_do_entity()
2697 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count) in __ffs_data_do_entity()
2698 helper->ffs->eps_addrmap[helper->eps_count] = in __ffs_data_do_entity()
2699 d->bEndpointAddress; in __ffs_data_do_entity()
2700 else if (helper->ffs->eps_addrmap[helper->eps_count] != in __ffs_data_do_entity()
2701 d->bEndpointAddress) in __ffs_data_do_entity()
2702 return -EINVAL; in __ffs_data_do_entity()
2712 u16 bcd_version = le16_to_cpu(desc->bcdVersion); in __ffs_do_os_desc_header()
2713 u16 w_index = le16_to_cpu(desc->wIndex); in __ffs_do_os_desc_header()
2721 return -EINVAL; in __ffs_do_os_desc_header()
2732 return -EINVAL; in __ffs_do_os_desc_header()
2753 while (feature_count--) { in ffs_do_single_os_desc()
2760 len -= ret; in ffs_do_single_os_desc()
2762 return _len - len; in ffs_do_single_os_desc()
2780 return -EINVAL; in ffs_do_os_descs()
2789 if (le32_to_cpu(desc->dwLength) > len) in ffs_do_os_descs()
2790 return -EINVAL; in ffs_do_os_descs()
2799 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??" in ffs_do_os_descs()
2801 feature_count = le16_to_cpu(desc->wCount); in ffs_do_os_descs()
2803 (feature_count > 255 || desc->Reserved)) in ffs_do_os_descs()
2804 return -EINVAL; in ffs_do_os_descs()
2805 len -= ret; in ffs_do_os_descs()
2819 len -= ret; in ffs_do_os_descs()
2822 return _len - len; in ffs_do_os_descs()
2841 d->bFirstInterfaceNumber >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2842 return -EINVAL; in __ffs_data_do_os_desc()
2843 if (d->Reserved1 != 1) { in __ffs_data_do_os_desc()
2846 * but older kernels incorrectly rejected non-zero in __ffs_data_do_os_desc()
2851 d->Reserved1 = 1; in __ffs_data_do_os_desc()
2853 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i) in __ffs_data_do_os_desc()
2854 if (d->Reserved2[i]) in __ffs_data_do_os_desc()
2855 return -EINVAL; in __ffs_data_do_os_desc()
2865 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2866 return -EINVAL; in __ffs_data_do_os_desc()
2867 length = le32_to_cpu(d->dwSize); in __ffs_data_do_os_desc()
2869 return -EINVAL; in __ffs_data_do_os_desc()
2870 type = le32_to_cpu(d->dwPropertyDataType); in __ffs_data_do_os_desc()
2875 return -EINVAL; in __ffs_data_do_os_desc()
2877 pnl = le16_to_cpu(d->wPropertyNameLength); in __ffs_data_do_os_desc()
2881 return -EINVAL; in __ffs_data_do_os_desc()
2887 return -EINVAL; in __ffs_data_do_os_desc()
2889 ++ffs->ms_os_descs_ext_prop_count; in __ffs_data_do_os_desc()
2891 ffs->ms_os_descs_ext_prop_name_len += pnl * 2; in __ffs_data_do_os_desc()
2892 ffs->ms_os_descs_ext_prop_data_len += pdl; in __ffs_data_do_os_desc()
2897 return -EINVAL; in __ffs_data_do_os_desc()
2907 int ret = -EINVAL, i; in __ffs_data_got_descs()
2917 len -= 8; in __ffs_data_got_descs()
2921 ffs->user_flags = flags; in __ffs_data_got_descs()
2930 ret = -ENOSYS; in __ffs_data_got_descs()
2934 len -= 12; in __ffs_data_got_descs()
2943 ffs->ffs_eventfd = in __ffs_data_got_descs()
2945 if (IS_ERR(ffs->ffs_eventfd)) { in __ffs_data_got_descs()
2946 ret = PTR_ERR(ffs->ffs_eventfd); in __ffs_data_got_descs()
2947 ffs->ffs_eventfd = NULL; in __ffs_data_got_descs()
2951 len -= 4; in __ffs_data_got_descs()
2963 len -= 4; in __ffs_data_got_descs()
2972 len -= 4; in __ffs_data_got_descs()
2987 if (!ffs->eps_count && !ffs->interfaces_count) { in __ffs_data_got_descs()
2988 ffs->eps_count = helper.eps_count; in __ffs_data_got_descs()
2989 ffs->interfaces_count = helper.interfaces_count; in __ffs_data_got_descs()
2991 if (ffs->eps_count != helper.eps_count) { in __ffs_data_got_descs()
2992 ret = -EINVAL; in __ffs_data_got_descs()
2995 if (ffs->interfaces_count != helper.interfaces_count) { in __ffs_data_got_descs()
2996 ret = -EINVAL; in __ffs_data_got_descs()
3001 len -= ret; in __ffs_data_got_descs()
3009 len -= ret; in __ffs_data_got_descs()
3013 ret = -EINVAL; in __ffs_data_got_descs()
3017 ffs->raw_descs_data = _data; in __ffs_data_got_descs()
3018 ffs->raw_descs = raw_descs; in __ffs_data_got_descs()
3019 ffs->raw_descs_length = data - raw_descs; in __ffs_data_got_descs()
3020 ffs->fs_descs_count = counts[0]; in __ffs_data_got_descs()
3021 ffs->hs_descs_count = counts[1]; in __ffs_data_got_descs()
3022 ffs->ss_descs_count = counts[2]; in __ffs_data_got_descs()
3023 ffs->ms_os_descs_count = os_descs_count; in __ffs_data_got_descs()
3052 needed_count = ffs->strings_count; in __ffs_data_got_strings()
3079 return -ENOMEM; in __ffs_data_got_strings()
3088 } while (--i); in __ffs_data_got_strings()
3099 len -= 16; in __ffs_data_got_strings()
3101 do { /* lang_count > 0 so we can use do-while */ in __ffs_data_got_strings()
3107 t->language = get_unaligned_le16(data); in __ffs_data_got_strings()
3108 t->strings = s; in __ffs_data_got_strings()
3112 len -= 2; in __ffs_data_got_strings()
3115 do { /* str_count > 0 so we can use do-while */ in __ffs_data_got_strings()
3128 * s->id will be set while adding in __ffs_data_got_strings()
3132 s->s = data; in __ffs_data_got_strings()
3133 --needed; in __ffs_data_got_strings()
3138 len -= length + 1; in __ffs_data_got_strings()
3139 } while (--str_per_lang); in __ffs_data_got_strings()
3141 s->id = 0; /* terminator */ in __ffs_data_got_strings()
3142 s->s = NULL; in __ffs_data_got_strings()
3145 } while (--lang_count); in __ffs_data_got_strings()
3152 ffs->stringtabs = stringtabs; in __ffs_data_got_strings()
3153 ffs->raw_strings = _data; in __ffs_data_got_strings()
3161 return -EINVAL; in __ffs_data_got_strings()
3177 * of ffs->setup_state without holding the lock because when in __ffs_event_add()
3181 if (ffs->setup_state == FFS_SETUP_PENDING) in __ffs_event_add()
3182 ffs->setup_state = FFS_SETUP_CANCELLED; in __ffs_event_add()
3186 * evens on ffs->ev.types queue. This is important because the queue in __ffs_event_add()
3217 u8 *ev = ffs->ev.types, *out = ev; in __ffs_event_add()
3218 unsigned n = ffs->ev.count; in __ffs_event_add()
3219 for (; n; --n, ++ev) in __ffs_event_add()
3224 ffs->ev.count = out - ffs->ev.types; in __ffs_event_add()
3228 ffs->ev.types[ffs->ev.count++] = type; in __ffs_event_add()
3229 wake_up_locked(&ffs->ev.waitq); in __ffs_event_add()
3230 if (ffs->ffs_eventfd) in __ffs_event_add()
3231 eventfd_signal(ffs->ffs_eventfd); in __ffs_event_add()
3238 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_event_add()
3240 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_event_add()
3249 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i) in ffs_ep_addr2idx()
3250 if (ffs->eps_addrmap[i] == endpoint_address) in ffs_ep_addr2idx()
3252 return -ENOENT; in ffs_ep_addr2idx()
3275 if (func->function.ss_descriptors) { in __ffs_func_bind_do_descs()
3277 func->function.ss_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3278 } else if (func->function.hs_descriptors) { in __ffs_func_bind_do_descs()
3280 func->function.hs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3283 func->function.fs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3286 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) in __ffs_func_bind_do_descs()
3289 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1; in __ffs_func_bind_do_descs()
3293 ffs_ep = func->eps + idx; in __ffs_func_bind_do_descs()
3295 if (ffs_ep->descs[ep_desc_id]) { in __ffs_func_bind_do_descs()
3298 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); in __ffs_func_bind_do_descs()
3299 return -EINVAL; in __ffs_func_bind_do_descs()
3301 ffs_ep->descs[ep_desc_id] = ds; in __ffs_func_bind_do_descs()
3303 ffs_dump_mem(": Original ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
3304 if (ffs_ep->ep) { in __ffs_func_bind_do_descs()
3305 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; in __ffs_func_bind_do_descs()
3306 if (!ds->wMaxPacketSize) in __ffs_func_bind_do_descs()
3307 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; in __ffs_func_bind_do_descs()
3318 bEndpointAddress = ds->bEndpointAddress; in __ffs_func_bind_do_descs()
3323 wMaxPacketSize = ds->wMaxPacketSize; in __ffs_func_bind_do_descs()
3325 ep = usb_ep_autoconfig(func->gadget, ds); in __ffs_func_bind_do_descs()
3327 return -ENOTSUPP; in __ffs_func_bind_do_descs()
3328 ep->driver_data = func->eps + idx; in __ffs_func_bind_do_descs()
3332 return -ENOMEM; in __ffs_func_bind_do_descs()
3334 ffs_ep->ep = ep; in __ffs_func_bind_do_descs()
3335 ffs_ep->req = req; in __ffs_func_bind_do_descs()
3336 func->eps_revmap[ds->bEndpointAddress & in __ffs_func_bind_do_descs()
3342 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in __ffs_func_bind_do_descs()
3343 ds->bEndpointAddress = bEndpointAddress; in __ffs_func_bind_do_descs()
3348 ds->wMaxPacketSize = wMaxPacketSize; in __ffs_func_bind_do_descs()
3350 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
3371 if (func->interfaces_nums[idx] < 0) { in __ffs_func_bind_do_nums()
3372 int id = usb_interface_id(func->conf, &func->function); in __ffs_func_bind_do_nums()
3375 func->interfaces_nums[idx] = id; in __ffs_func_bind_do_nums()
3377 newValue = func->interfaces_nums[idx]; in __ffs_func_bind_do_nums()
3382 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; in __ffs_func_bind_do_nums()
3390 if (desc->bDescriptorType == USB_DT_ENDPOINT) in __ffs_func_bind_do_nums()
3393 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; in __ffs_func_bind_do_nums()
3394 if (!func->eps[idx].ep) in __ffs_func_bind_do_nums()
3395 return -EINVAL; in __ffs_func_bind_do_nums()
3399 descs = func->eps[idx].descs; in __ffs_func_bind_do_nums()
3400 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; in __ffs_func_bind_do_nums()
3405 pr_vdebug("%02x -> %02x\n", *valuep, newValue); in __ffs_func_bind_do_nums()
3422 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
3423 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
3424 memcpy(t->os_desc->ext_compat_id, &desc->IDs, in __ffs_func_bind_do_os_desc()
3436 t = &func->function.os_desc_table[h->interface]; in __ffs_func_bind_do_os_desc()
3437 t->if_id = func->interfaces_nums[h->interface]; in __ffs_func_bind_do_os_desc()
3439 ext_prop = func->ffs->ms_os_descs_ext_prop_avail; in __ffs_func_bind_do_os_desc()
3440 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop); in __ffs_func_bind_do_os_desc()
3442 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType); in __ffs_func_bind_do_os_desc()
3443 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength); in __ffs_func_bind_do_os_desc()
3444 ext_prop->data_len = le32_to_cpu(*(__le32 *) in __ffs_func_bind_do_os_desc()
3445 usb_ext_prop_data_len_ptr(data, ext_prop->name_len)); in __ffs_func_bind_do_os_desc()
3446 length = ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
3448 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail; in __ffs_func_bind_do_os_desc()
3449 func->ffs->ms_os_descs_ext_prop_name_avail += in __ffs_func_bind_do_os_desc()
3450 ext_prop->name_len; in __ffs_func_bind_do_os_desc()
3452 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail; in __ffs_func_bind_do_os_desc()
3453 func->ffs->ms_os_descs_ext_prop_data_avail += in __ffs_func_bind_do_os_desc()
3454 ext_prop->data_len; in __ffs_func_bind_do_os_desc()
3456 usb_ext_prop_data_ptr(data, ext_prop->name_len), in __ffs_func_bind_do_os_desc()
3457 ext_prop->data_len); in __ffs_func_bind_do_os_desc()
3459 switch (ext_prop->type) { in __ffs_func_bind_do_os_desc()
3464 ext_prop->data_len *= 2; in __ffs_func_bind_do_os_desc()
3467 ext_prop->data = ext_prop_data; in __ffs_func_bind_do_os_desc()
3470 ext_prop->name_len); in __ffs_func_bind_do_os_desc()
3472 ext_prop->name_len *= 2; in __ffs_func_bind_do_os_desc()
3473 ext_prop->name = ext_prop_name; in __ffs_func_bind_do_os_desc()
3475 t->os_desc->ext_prop_len += in __ffs_func_bind_do_os_desc()
3476 ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
3477 ++t->os_desc->ext_prop_count; in __ffs_func_bind_do_os_desc()
3478 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop); in __ffs_func_bind_do_os_desc()
3493 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_do_functionfs_bind()
3502 * Configfs-enabled gadgets however do need ffs_dev_lock. in ffs_do_functionfs_bind()
3504 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3506 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; in ffs_do_functionfs_bind()
3507 ffs_data = ffs_opts->dev->ffs_data; in ffs_do_functionfs_bind()
3508 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3513 func->ffs = ffs_data; in ffs_do_functionfs_bind()
3514 func->conf = c; in ffs_do_functionfs_bind()
3515 func->gadget = c->cdev->gadget; in ffs_do_functionfs_bind()
3522 * with regard to ffs_opts->bound access in ffs_do_functionfs_bind()
3524 if (!ffs_opts->refcnt) { in ffs_do_functionfs_bind()
3525 ret = functionfs_bind(func->ffs, c->cdev); in ffs_do_functionfs_bind()
3529 ffs_opts->refcnt++; in ffs_do_functionfs_bind()
3530 func->function.strings = func->ffs->stringtabs; in ffs_do_functionfs_bind()
3539 struct ffs_data *ffs = func->ffs; in _ffs_func_bind()
3541 const int full = !!func->ffs->fs_descs_count; in _ffs_func_bind()
3542 const int high = !!func->ffs->hs_descs_count; in _ffs_func_bind()
3543 const int super = !!func->ffs->ss_descs_count; in _ffs_func_bind()
3550 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count); in _ffs_func_bind()
3552 full ? ffs->fs_descs_count + 1 : 0); in _ffs_func_bind()
3554 high ? ffs->hs_descs_count + 1 : 0); in _ffs_func_bind()
3556 super ? ffs->ss_descs_count + 1 : 0); in _ffs_func_bind()
3557 vla_item_with_sz(d, short, inums, ffs->interfaces_count); in _ffs_func_bind()
3559 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3561 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3563 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3565 ffs->ms_os_descs_ext_prop_count); in _ffs_func_bind()
3567 ffs->ms_os_descs_ext_prop_name_len); in _ffs_func_bind()
3569 ffs->ms_os_descs_ext_prop_data_len); in _ffs_func_bind()
3570 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length); in _ffs_func_bind()
3575 return -ENOTSUPP; in _ffs_func_bind()
3580 return -ENOMEM; in _ffs_func_bind()
3582 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop); in _ffs_func_bind()
3583 ffs->ms_os_descs_ext_prop_name_avail = in _ffs_func_bind()
3585 ffs->ms_os_descs_ext_prop_data_avail = in _ffs_func_bind()
3589 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs, in _ffs_func_bind()
3590 ffs->raw_descs_length); in _ffs_func_bind()
3594 for (i = 0; i < ffs->eps_count; i++) in _ffs_func_bind()
3595 eps_ptr[i].num = -1; in _ffs_func_bind()
3598 * d_eps == vlabuf, func->eps used to kfree vlabuf later in _ffs_func_bind()
3600 func->eps = vla_ptr(vlabuf, d, eps); in _ffs_func_bind()
3601 func->interfaces_nums = vla_ptr(vlabuf, d, inums); in _ffs_func_bind()
3609 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); in _ffs_func_bind()
3610 fs_len = ffs_do_descs(ffs->fs_descs_count, in _ffs_func_bind()
3623 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); in _ffs_func_bind()
3624 hs_len = ffs_do_descs(ffs->hs_descs_count, in _ffs_func_bind()
3626 d_raw_descs__sz - fs_len, in _ffs_func_bind()
3637 func->function.ss_descriptors = func->function.ssp_descriptors = in _ffs_func_bind()
3639 ss_len = ffs_do_descs(ffs->ss_descs_count, in _ffs_func_bind()
3641 d_raw_descs__sz - fs_len - hs_len, in _ffs_func_bind()
3656 ret = ffs_do_descs(ffs->fs_descs_count + in _ffs_func_bind()
3657 (high ? ffs->hs_descs_count : 0) + in _ffs_func_bind()
3658 (super ? ffs->ss_descs_count : 0), in _ffs_func_bind()
3664 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table); in _ffs_func_bind()
3665 if (c->cdev->use_os_string) { in _ffs_func_bind()
3666 for (i = 0; i < ffs->interfaces_count; ++i) { in _ffs_func_bind()
3669 desc = func->function.os_desc_table[i].os_desc = in _ffs_func_bind()
3672 desc->ext_compat_id = in _ffs_func_bind()
3674 INIT_LIST_HEAD(&desc->ext_prop); in _ffs_func_bind()
3676 ret = ffs_do_os_descs(ffs->ms_os_descs_count, in _ffs_func_bind()
3679 d_raw_descs__sz - fs_len - hs_len - in _ffs_func_bind()
3685 func->function.os_desc_n = in _ffs_func_bind()
3686 c->cdev->use_os_string ? ffs->interfaces_count : 0; in _ffs_func_bind()
3708 if (ret && !--ffs_opts->refcnt) in ffs_func_bind()
3709 functionfs_unbind(func->ffs); in ffs_func_bind()
3730 return (intf < 0) ? intf : func->cur_alt[interface]; in ffs_func_get_alt()
3737 struct ffs_data *ffs = func->ffs; in ffs_func_set_alt()
3741 return -EINVAL; in ffs_func_set_alt()
3747 if (ffs->func) in ffs_func_set_alt()
3748 ffs_func_eps_disable(ffs->func); in ffs_func_set_alt()
3750 if (ffs->state == FFS_DEACTIVATED) { in ffs_func_set_alt()
3751 ffs->state = FFS_CLOSING; in ffs_func_set_alt()
3752 INIT_WORK(&ffs->reset_work, ffs_reset_work); in ffs_func_set_alt()
3753 schedule_work(&ffs->reset_work); in ffs_func_set_alt()
3754 return -ENODEV; in ffs_func_set_alt()
3757 if (ffs->state != FFS_ACTIVE) in ffs_func_set_alt()
3758 return -ENODEV; in ffs_func_set_alt()
3760 ffs->func = func; in ffs_func_set_alt()
3764 func->cur_alt[interface] = alt; in ffs_func_set_alt()
3772 struct ffs_data *ffs = func->ffs; in ffs_func_disable()
3774 if (ffs->func) in ffs_func_disable()
3775 ffs_func_eps_disable(ffs->func); in ffs_func_disable()
3777 if (ffs->state == FFS_DEACTIVATED) { in ffs_func_disable()
3778 ffs->state = FFS_CLOSING; in ffs_func_disable()
3779 INIT_WORK(&ffs->reset_work, ffs_reset_work); in ffs_func_disable()
3780 schedule_work(&ffs->reset_work); in ffs_func_disable()
3784 if (ffs->state == FFS_ACTIVE) { in ffs_func_disable()
3785 ffs->func = NULL; in ffs_func_disable()
3794 struct ffs_data *ffs = func->ffs; in ffs_func_setup()
3798 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType); in ffs_func_setup()
3799 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest); in ffs_func_setup()
3800 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue)); in ffs_func_setup()
3801 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3802 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength)); in ffs_func_setup()
3808 * passed to usb_configuration->setup() (if one is set). No in ffs_func_setup()
3814 if (ffs->state != FFS_ACTIVE) in ffs_func_setup()
3815 return -ENODEV; in ffs_func_setup()
3817 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_setup()
3819 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3825 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3828 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_func_setup()
3829 ret = func->ffs->eps_addrmap[ret]; in ffs_func_setup()
3833 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP) in ffs_func_setup()
3834 ret = le16_to_cpu(creq->wIndex); in ffs_func_setup()
3836 return -EOPNOTSUPP; in ffs_func_setup()
3839 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3840 ffs->ev.setup = *creq; in ffs_func_setup()
3841 ffs->ev.setup.wIndex = cpu_to_le16(ret); in ffs_func_setup()
3843 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3845 return ffs->ev.setup.wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0; in ffs_func_setup()
3854 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP)) in ffs_func_req_match()
3857 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_req_match()
3860 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3863 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3865 return (bool) (func->ffs->user_flags & in ffs_func_req_match()
3872 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); in ffs_func_suspend()
3877 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); in ffs_func_resume()
3885 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; in ffs_func_revmap_ep()
3886 return num ? num : -EDOM; in ffs_func_revmap_ep()
3891 short *nums = func->interfaces_nums; in ffs_func_revmap_intf()
3892 unsigned count = func->ffs->interfaces_count; in ffs_func_revmap_intf()
3894 for (; count; --count, ++nums) { in ffs_func_revmap_intf()
3896 return nums - func->interfaces_nums; in ffs_func_revmap_intf()
3899 return -EDOM; in ffs_func_revmap_intf()
3915 if (strcmp(dev->name, name) == 0) in _ffs_do_find_dev()
3931 if (dev->single) in _ffs_get_single_dev()
3966 ready = opts->dev->desc_ready; in f_fs_opts_ready_show()
3983 usb_put_function_instance(&opts->func_inst); in ffs_attr_release()
4004 ffs_release_dev(opts->dev); in ffs_free_inst()
4006 _ffs_free_dev(opts->dev); in ffs_free_inst()
4014 return -ENAMETOOLONG; in ffs_set_inst_name()
4015 return ffs_name_dev(to_f_fs_opts(fi)->dev, name); in ffs_set_inst_name()
4025 return ERR_PTR(-ENOMEM); in ffs_alloc_inst()
4027 opts->func_inst.set_inst_name = ffs_set_inst_name; in ffs_alloc_inst()
4028 opts->func_inst.free_func_inst = ffs_free_inst; in ffs_alloc_inst()
4036 opts->dev = dev; in ffs_alloc_inst()
4037 dev->opts = opts; in ffs_alloc_inst()
4039 config_group_init_type_name(&opts->func_inst.group, "", in ffs_alloc_inst()
4041 return &opts->func_inst; in ffs_alloc_inst()
4053 struct ffs_data *ffs = func->ffs; in ffs_func_unbind()
4055 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_func_unbind()
4056 struct ffs_ep *ep = func->eps; in ffs_func_unbind()
4057 unsigned count = ffs->eps_count; in ffs_func_unbind()
4060 if (ffs->func == func) { in ffs_func_unbind()
4062 ffs->func = NULL; in ffs_func_unbind()
4066 drain_workqueue(ffs->io_completion_wq); in ffs_func_unbind()
4069 if (!--opts->refcnt) in ffs_func_unbind()
4073 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_unbind()
4074 while (count--) { in ffs_func_unbind()
4075 if (ep->ep && ep->req) in ffs_func_unbind()
4076 usb_ep_free_request(ep->ep, ep->req); in ffs_func_unbind()
4077 ep->req = NULL; in ffs_func_unbind()
4080 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_unbind()
4081 kfree(func->eps); in ffs_func_unbind()
4082 func->eps = NULL; in ffs_func_unbind()
4087 func->function.fs_descriptors = NULL; in ffs_func_unbind()
4088 func->function.hs_descriptors = NULL; in ffs_func_unbind()
4089 func->function.ss_descriptors = NULL; in ffs_func_unbind()
4090 func->function.ssp_descriptors = NULL; in ffs_func_unbind()
4091 func->interfaces_nums = NULL; in ffs_func_unbind()
4101 return ERR_PTR(-ENOMEM); in ffs_alloc()
4103 func->function.name = "Function FS Gadget"; in ffs_alloc()
4105 func->function.bind = ffs_func_bind; in ffs_alloc()
4106 func->function.unbind = ffs_func_unbind; in ffs_alloc()
4107 func->function.set_alt = ffs_func_set_alt; in ffs_alloc()
4108 func->function.get_alt = ffs_func_get_alt; in ffs_alloc()
4109 func->function.disable = ffs_func_disable; in ffs_alloc()
4110 func->function.setup = ffs_func_setup; in ffs_alloc()
4111 func->function.req_match = ffs_func_req_match; in ffs_alloc()
4112 func->function.suspend = ffs_func_suspend; in ffs_alloc()
4113 func->function.resume = ffs_func_resume; in ffs_alloc()
4114 func->function.free_func = ffs_free; in ffs_alloc()
4116 return &func->function; in ffs_alloc()
4128 return ERR_PTR(-EBUSY); in _ffs_alloc_dev()
4132 return ERR_PTR(-ENOMEM); in _ffs_alloc_dev()
4142 list_add(&dev->entry, &ffs_devices); in _ffs_alloc_dev()
4156 strscpy(dev->name, name, ARRAY_SIZE(dev->name)); in ffs_name_dev()
4158 ret = -EBUSY; in ffs_name_dev()
4174 ret = -EBUSY; in ffs_single_dev()
4176 dev->single = true; in ffs_single_dev()
4188 list_del(&dev->entry); in _ffs_free_dev()
4204 ret = -ENOENT; in ffs_acquire_dev()
4205 } else if (ffs_dev->mounted) { in ffs_acquire_dev()
4206 ret = -EBUSY; in ffs_acquire_dev()
4207 } else if (ffs_dev->ffs_acquire_dev_callback && in ffs_acquire_dev()
4208 ffs_dev->ffs_acquire_dev_callback(ffs_dev)) { in ffs_acquire_dev()
4209 ret = -ENOENT; in ffs_acquire_dev()
4211 ffs_dev->mounted = true; in ffs_acquire_dev()
4212 ffs_dev->ffs_data = ffs_data; in ffs_acquire_dev()
4213 ffs_data->private_data = ffs_dev; in ffs_acquire_dev()
4224 if (ffs_dev && ffs_dev->mounted) { in ffs_release_dev()
4225 ffs_dev->mounted = false; in ffs_release_dev()
4226 if (ffs_dev->ffs_data) { in ffs_release_dev()
4227 ffs_dev->ffs_data->private_data = NULL; in ffs_release_dev()
4228 ffs_dev->ffs_data = NULL; in ffs_release_dev()
4231 if (ffs_dev->ffs_release_dev_callback) in ffs_release_dev()
4232 ffs_dev->ffs_release_dev_callback(ffs_dev); in ffs_release_dev()
4245 ffs_obj = ffs->private_data; in ffs_ready()
4247 ret = -EINVAL; in ffs_ready()
4250 if (WARN_ON(ffs_obj->desc_ready)) { in ffs_ready()
4251 ret = -EBUSY; in ffs_ready()
4255 ffs_obj->desc_ready = true; in ffs_ready()
4257 if (ffs_obj->ffs_ready_callback) { in ffs_ready()
4258 ret = ffs_obj->ffs_ready_callback(ffs); in ffs_ready()
4263 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); in ffs_ready()
4277 ffs_obj = ffs->private_data; in ffs_closed()
4281 ffs_obj->desc_ready = false; in ffs_closed()
4283 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) && in ffs_closed()
4284 ffs_obj->ffs_closed_callback) in ffs_closed()
4285 ffs_obj->ffs_closed_callback(ffs); in ffs_closed()
4287 if (ffs_obj->opts) in ffs_closed()
4288 opts = ffs_obj->opts; in ffs_closed()
4292 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent in ffs_closed()
4293 || !kref_read(&opts->func_inst.group.cg_item.ci_kref)) in ffs_closed()
4296 ci = opts->func_inst.group.cg_item.ci_parent->ci_parent; in ffs_closed()
4299 if (test_bit(FFS_FL_BOUND, &ffs->flags)) in ffs_closed()
4311 ? mutex_trylock(mutex) ? 0 : -EAGAIN in ffs_mutex_lock()