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 */
174 * can never run concurrently (they are synchronised by epfile->mutex)
184 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
219 #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
223 unsigned char in; /* P: ffs->eps_lock */
224 unsigned char isoc; /* P: ffs->eps_lock */
303 struct ffs_data *ffs = req->context; in ffs_ep0_complete()
305 complete(&ffs->ep0req_completion); in ffs_ep0_complete()
309 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_queue_wait()
311 struct usb_request *req = ffs->ep0req; in __ffs_ep0_queue_wait()
315 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
316 return -EINVAL; in __ffs_ep0_queue_wait()
319 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); in __ffs_ep0_queue_wait()
321 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_queue_wait()
323 req->buf = data; in __ffs_ep0_queue_wait()
324 req->length = len; in __ffs_ep0_queue_wait()
331 if (req->buf == NULL) in __ffs_ep0_queue_wait()
332 req->buf = (void *)0xDEADBABE; in __ffs_ep0_queue_wait()
334 reinit_completion(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
336 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); in __ffs_ep0_queue_wait()
340 ret = wait_for_completion_interruptible(&ffs->ep0req_completion); in __ffs_ep0_queue_wait()
342 usb_ep_dequeue(ffs->gadget->ep0, req); in __ffs_ep0_queue_wait()
343 return -EINTR; in __ffs_ep0_queue_wait()
346 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_queue_wait()
347 return req->status ? req->status : req->actual; in __ffs_ep0_queue_wait()
352 if (ffs->ev.can_stall) { in __ffs_ep0_stall()
354 usb_ep_set_halt(ffs->gadget->ep0); in __ffs_ep0_stall()
355 ffs->setup_state = FFS_NO_SETUP; in __ffs_ep0_stall()
356 return -EL2HLT; in __ffs_ep0_stall()
359 return -ESRCH; in __ffs_ep0_stall()
366 struct ffs_data *ffs = file->private_data; in ffs_ep0_write()
372 return -EIDRM; in ffs_ep0_write()
375 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_write()
380 switch (ffs->state) { in ffs_ep0_write()
385 ret = -EINVAL; in ffs_ep0_write()
396 if (ffs->state == FFS_READ_DESCRIPTORS) { in ffs_ep0_write()
402 ffs->state = FFS_READ_STRINGS; in ffs_ep0_write()
412 ffs->state = FFS_CLOSING; in ffs_ep0_write()
416 ffs->state = FFS_ACTIVE; in ffs_ep0_write()
417 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
421 ffs->state = FFS_CLOSING; in ffs_ep0_write()
435 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
438 ret = -EIDRM; in ffs_ep0_write()
442 ret = -ESRCH; in ffs_ep0_write()
450 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { in ffs_ep0_write()
451 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
457 len = min_t(size_t, len, le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_write()
459 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
467 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
477 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP in ffs_ep0_write()
483 ret = -EIDRM; in ffs_ep0_write()
485 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_write()
494 ret = -EBADFD; in ffs_ep0_write()
498 mutex_unlock(&ffs->mutex); in ffs_ep0_write()
502 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
505 __releases(&ffs->ev.waitq.lock) in __ffs_ep0_read_events()
508 * n cannot be bigger than ffs->ev.count, which cannot be bigger than in __ffs_ep0_read_events()
509 * size of ffs->ev.types array (which is four) so that's how much space in __ffs_ep0_read_events()
512 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)]; in __ffs_ep0_read_events()
519 events[i].type = ffs->ev.types[i]; in __ffs_ep0_read_events()
521 events[i].u.setup = ffs->ev.setup; in __ffs_ep0_read_events()
522 ffs->setup_state = FFS_SETUP_PENDING; in __ffs_ep0_read_events()
526 ffs->ev.count -= n; in __ffs_ep0_read_events()
527 if (ffs->ev.count) in __ffs_ep0_read_events()
528 memmove(ffs->ev.types, ffs->ev.types + n, in __ffs_ep0_read_events()
529 ffs->ev.count * sizeof *ffs->ev.types); in __ffs_ep0_read_events()
531 spin_unlock_irq(&ffs->ev.waitq.lock); in __ffs_ep0_read_events()
532 mutex_unlock(&ffs->mutex); in __ffs_ep0_read_events()
534 return copy_to_user(buf, events, size) ? -EFAULT : size; in __ffs_ep0_read_events()
540 struct ffs_data *ffs = file->private_data; in ffs_ep0_read()
547 return -EIDRM; in ffs_ep0_read()
550 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_read()
555 if (ffs->state != FFS_ACTIVE) { in ffs_ep0_read()
556 ret = -EBADFD; in ffs_ep0_read()
564 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
568 ret = -EIDRM; in ffs_ep0_read()
574 ret = -EINVAL; in ffs_ep0_read()
578 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { in ffs_ep0_read()
579 ret = -EAGAIN; in ffs_ep0_read()
583 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, in ffs_ep0_read()
584 ffs->ev.count)) { in ffs_ep0_read()
585 ret = -EINTR; in ffs_ep0_read()
591 min_t(size_t, n, ffs->ev.count)); in ffs_ep0_read()
594 if (ffs->ev.setup.bRequestType & USB_DIR_IN) { in ffs_ep0_read()
595 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
600 len = min_t(size_t, len, le16_to_cpu(ffs->ev.setup.wLength)); in ffs_ep0_read()
602 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
607 ret = -ENOMEM; in ffs_ep0_read()
612 spin_lock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
617 ret = -EIDRM; in ffs_ep0_read()
624 ret = -EFAULT; in ffs_ep0_read()
628 ret = -EBADFD; in ffs_ep0_read()
632 spin_unlock_irq(&ffs->ev.waitq.lock); in ffs_ep0_read()
634 mutex_unlock(&ffs->mutex); in ffs_ep0_read()
641 struct ffs_data *ffs = inode->i_sb->s_fs_info; in ffs_ep0_open()
645 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_open()
650 if (ffs->state == FFS_CLOSING) { in ffs_ep0_open()
652 mutex_unlock(&ffs->mutex); in ffs_ep0_open()
653 return -EBUSY; in ffs_ep0_open()
655 mutex_unlock(&ffs->mutex); in ffs_ep0_open()
656 file->private_data = ffs; in ffs_ep0_open()
663 struct ffs_data *ffs = file->private_data; in ffs_ep0_release()
672 struct ffs_data *ffs = file->private_data; in ffs_ep0_ioctl()
673 struct usb_gadget *gadget = ffs->gadget; in ffs_ep0_ioctl()
677 struct ffs_function *func = ffs->func; in ffs_ep0_ioctl()
678 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; in ffs_ep0_ioctl()
679 } else if (gadget && gadget->ops->ioctl) { in ffs_ep0_ioctl()
680 ret = gadget->ops->ioctl(gadget, code, value); in ffs_ep0_ioctl()
682 ret = -ENOTTY; in ffs_ep0_ioctl()
690 struct ffs_data *ffs = file->private_data; in ffs_ep0_poll()
694 poll_wait(file, &ffs->ev.waitq, wait); in ffs_ep0_poll()
696 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_ep0_poll()
700 switch (ffs->state) { in ffs_ep0_poll()
707 switch (ffs->setup_state) { in ffs_ep0_poll()
709 if (ffs->ev.count) in ffs_ep0_poll()
726 mutex_unlock(&ffs->mutex); in ffs_ep0_poll()
733 .open = ffs_ep0_open,
746 struct ffs_io_data *io_data = req->context; in ffs_epfile_io_complete()
748 if (req->status) in ffs_epfile_io_complete()
749 io_data->status = req->status; in ffs_epfile_io_complete()
751 io_data->status = req->actual; in ffs_epfile_io_complete()
753 complete(&io_data->done); in ffs_epfile_io_complete()
763 return -EFAULT; in ffs_copy_to_iter()
801 * @sg_table - pointer to a place to be filled with sg_table contents
802 * @size - required buffer size
839 if (io_data->use_sg) in ffs_alloc_buffer()
840 return ffs_build_sg_list(&io_data->sgt, data_len); in ffs_alloc_buffer()
847 if (!io_data->buf) in ffs_free_buffer()
850 if (io_data->use_sg) { in ffs_free_buffer()
851 sg_free_table(&io_data->sgt); in ffs_free_buffer()
852 vfree(io_data->buf); in ffs_free_buffer()
854 kfree(io_data->buf); in ffs_free_buffer()
862 int ret = io_data->status; in ffs_user_copy_worker()
863 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD; in ffs_user_copy_worker()
865 if (io_data->read && ret > 0) { in ffs_user_copy_worker()
866 kthread_use_mm(io_data->mm); in ffs_user_copy_worker()
867 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data); in ffs_user_copy_worker()
868 kthread_unuse_mm(io_data->mm); in ffs_user_copy_worker()
871 io_data->kiocb->ki_complete(io_data->kiocb, ret); in ffs_user_copy_worker()
873 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd) in ffs_user_copy_worker()
874 eventfd_signal(io_data->ffs->ffs_eventfd); in ffs_user_copy_worker()
876 usb_ep_free_request(io_data->ep, io_data->req); in ffs_user_copy_worker()
878 if (io_data->read) in ffs_user_copy_worker()
879 kfree(io_data->to_free); in ffs_user_copy_worker()
887 struct ffs_io_data *io_data = req->context; in ffs_epfile_async_io_complete()
888 struct ffs_data *ffs = io_data->ffs; in ffs_epfile_async_io_complete()
890 io_data->status = req->status ? req->status : req->actual; in ffs_epfile_async_io_complete()
892 INIT_WORK(&io_data->work, ffs_user_copy_worker); in ffs_epfile_async_io_complete()
893 queue_work(ffs->io_completion_wq, &io_data->work); in ffs_epfile_async_io_complete()
902 struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP); in __ffs_epfile_read_buffer_free()
907 /* Assumes epfile->mutex is held. */
912 * Null out epfile->read_buffer so ffs_func_eps_disable does not free in __ffs_epfile_read_buffered()
916 struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL); in __ffs_epfile_read_buffered()
921 ret = copy_to_iter(buf->data, buf->length, iter); in __ffs_epfile_read_buffered()
922 if (buf->length == ret) { in __ffs_epfile_read_buffered()
928 ret = -EFAULT; in __ffs_epfile_read_buffered()
930 buf->length -= ret; in __ffs_epfile_read_buffered()
931 buf->data += ret; in __ffs_epfile_read_buffered()
934 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_buffered()
940 /* Assumes epfile->mutex is held. */
952 return -EFAULT; in __ffs_epfile_read_data()
958 data_len -= ret; in __ffs_epfile_read_data()
961 return -ENOMEM; in __ffs_epfile_read_data()
962 buf->length = data_len; in __ffs_epfile_read_data()
963 buf->data = buf->storage; in __ffs_epfile_read_data()
964 memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len)); in __ffs_epfile_read_data()
972 if (cmpxchg(&epfile->read_buffer, NULL, buf)) in __ffs_epfile_read_data()
980 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_wait_ep()
985 ep = epfile->ep; in ffs_epfile_wait_ep()
987 if (file->f_flags & O_NONBLOCK) in ffs_epfile_wait_ep()
988 return ERR_PTR(-EAGAIN); in ffs_epfile_wait_ep()
991 epfile->ffs->wait, (ep = epfile->ep)); in ffs_epfile_wait_ep()
993 return ERR_PTR(-EINTR); in ffs_epfile_wait_ep()
1001 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_io()
1005 ssize_t ret, data_len = -EINVAL; in ffs_epfile_io()
1009 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_io()
1010 return -ENODEV; in ffs_epfile_io()
1017 halt = (!io_data->read == !epfile->in); in ffs_epfile_io()
1018 if (halt && epfile->isoc) in ffs_epfile_io()
1019 return -EINVAL; in ffs_epfile_io()
1022 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK); in ffs_epfile_io()
1037 if (!io_data->aio && io_data->read) { in ffs_epfile_io()
1038 ret = __ffs_epfile_read_buffered(epfile, &io_data->data); in ffs_epfile_io()
1044 * if we _do_ wait above, the epfile->ffs->gadget might be NULL in ffs_epfile_io()
1048 gadget = epfile->ffs->gadget; in ffs_epfile_io()
1050 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1052 if (epfile->ep != ep) { in ffs_epfile_io()
1053 ret = -ESHUTDOWN; in ffs_epfile_io()
1056 data_len = iov_iter_count(&io_data->data); in ffs_epfile_io()
1061 if (io_data->read) in ffs_epfile_io()
1062 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len); in ffs_epfile_io()
1064 io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE; in ffs_epfile_io()
1065 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1069 ret = -ENOMEM; in ffs_epfile_io()
1072 if (!io_data->read && in ffs_epfile_io()
1073 !copy_from_iter_full(data, data_len, &io_data->data)) { in ffs_epfile_io()
1074 ret = -EFAULT; in ffs_epfile_io()
1079 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1081 if (epfile->ep != ep) { in ffs_epfile_io()
1083 ret = -ESHUTDOWN; in ffs_epfile_io()
1085 ret = usb_ep_set_halt(ep->ep); in ffs_epfile_io()
1087 ret = -EBADMSG; in ffs_epfile_io()
1088 } else if (data_len == -EINVAL) { in ffs_epfile_io()
1094 * being initialized to -EINVAL during its declaration, which in ffs_epfile_io()
1100 WARN(1, "%s: data_len == -EINVAL\n", __func__); in ffs_epfile_io()
1101 ret = -EINVAL; in ffs_epfile_io()
1102 } else if (!io_data->aio) { in ffs_epfile_io()
1105 req = ep->req; in ffs_epfile_io()
1106 if (io_data->use_sg) { in ffs_epfile_io()
1107 req->buf = NULL; in ffs_epfile_io()
1108 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1109 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1111 req->buf = data; in ffs_epfile_io()
1112 req->num_sgs = 0; in ffs_epfile_io()
1114 req->length = data_len; in ffs_epfile_io()
1116 io_data->buf = data; in ffs_epfile_io()
1118 init_completion(&io_data->done); in ffs_epfile_io()
1119 req->context = io_data; in ffs_epfile_io()
1120 req->complete = ffs_epfile_io_complete; in ffs_epfile_io()
1122 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1126 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1128 if (wait_for_completion_interruptible(&io_data->done)) { in ffs_epfile_io()
1129 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1130 if (epfile->ep != ep) { in ffs_epfile_io()
1131 ret = -ESHUTDOWN; in ffs_epfile_io()
1138 * condition with req->complete callback. in ffs_epfile_io()
1140 usb_ep_dequeue(ep->ep, req); in ffs_epfile_io()
1141 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1142 wait_for_completion(&io_data->done); in ffs_epfile_io()
1143 interrupted = io_data->status < 0; in ffs_epfile_io()
1147 ret = -EINTR; in ffs_epfile_io()
1148 else if (io_data->read && io_data->status > 0) in ffs_epfile_io()
1149 ret = __ffs_epfile_read_data(epfile, data, io_data->status, in ffs_epfile_io()
1150 &io_data->data); in ffs_epfile_io()
1152 ret = io_data->status; in ffs_epfile_io()
1154 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) { in ffs_epfile_io()
1155 ret = -ENOMEM; in ffs_epfile_io()
1157 if (io_data->use_sg) { in ffs_epfile_io()
1158 req->buf = NULL; in ffs_epfile_io()
1159 req->sg = io_data->sgt.sgl; in ffs_epfile_io()
1160 req->num_sgs = io_data->sgt.nents; in ffs_epfile_io()
1162 req->buf = data; in ffs_epfile_io()
1163 req->num_sgs = 0; in ffs_epfile_io()
1165 req->length = data_len; in ffs_epfile_io()
1167 io_data->buf = data; in ffs_epfile_io()
1168 io_data->ep = ep->ep; in ffs_epfile_io()
1169 io_data->req = req; in ffs_epfile_io()
1170 io_data->ffs = epfile->ffs; in ffs_epfile_io()
1172 req->context = io_data; in ffs_epfile_io()
1173 req->complete = ffs_epfile_async_io_complete; in ffs_epfile_io()
1175 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); in ffs_epfile_io()
1177 io_data->req = NULL; in ffs_epfile_io()
1178 usb_ep_free_request(ep->ep, req); in ffs_epfile_io()
1182 ret = -EIOCBQUEUED; in ffs_epfile_io()
1191 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_io()
1193 mutex_unlock(&epfile->mutex); in ffs_epfile_io()
1195 if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */ in ffs_epfile_io()
1203 struct ffs_data *ffs = inode->i_sb->s_fs_info; in ffs_epfile_open()
1208 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); in ffs_epfile_open()
1212 if (!atomic_inc_not_zero(&ffs->opened)) { in ffs_epfile_open()
1213 mutex_unlock(&ffs->mutex); in ffs_epfile_open()
1214 return -ENODEV; in ffs_epfile_open()
1218 * not enough, though - we might have been through FFS_CLOSING in ffs_epfile_open()
1221 epfile = smp_load_acquire(&inode->i_private); in ffs_epfile_open()
1222 if (unlikely(ffs->state != FFS_ACTIVE || !epfile)) { in ffs_epfile_open()
1223 mutex_unlock(&ffs->mutex); in ffs_epfile_open()
1225 return -ENODEV; in ffs_epfile_open()
1227 mutex_unlock(&ffs->mutex); in ffs_epfile_open()
1229 file->private_data = epfile; in ffs_epfile_open()
1235 struct ffs_io_data *io_data = kiocb->private; in ffs_aio_cancel()
1238 if (io_data && io_data->ep && io_data->req) in ffs_aio_cancel()
1239 value = usb_ep_dequeue(io_data->ep, io_data->req); in ffs_aio_cancel()
1241 value = -EINVAL; in ffs_aio_cancel()
1254 return -ENOMEM; in ffs_epfile_write_iter()
1255 p->aio = true; in ffs_epfile_write_iter()
1258 p->aio = false; in ffs_epfile_write_iter()
1261 p->read = false; in ffs_epfile_write_iter()
1262 p->kiocb = kiocb; in ffs_epfile_write_iter()
1263 p->data = *from; in ffs_epfile_write_iter()
1264 p->mm = current->mm; in ffs_epfile_write_iter()
1266 kiocb->private = p; in ffs_epfile_write_iter()
1268 if (p->aio) in ffs_epfile_write_iter()
1271 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_write_iter()
1272 if (res == -EIOCBQUEUED) in ffs_epfile_write_iter()
1274 if (p->aio) in ffs_epfile_write_iter()
1277 *from = p->data; in ffs_epfile_write_iter()
1289 return -ENOMEM; in ffs_epfile_read_iter()
1290 p->aio = true; in ffs_epfile_read_iter()
1293 p->aio = false; in ffs_epfile_read_iter()
1296 p->read = true; in ffs_epfile_read_iter()
1297 p->kiocb = kiocb; in ffs_epfile_read_iter()
1298 if (p->aio) { in ffs_epfile_read_iter()
1299 p->to_free = dup_iter(&p->data, to, GFP_KERNEL); in ffs_epfile_read_iter()
1300 if (!iter_is_ubuf(&p->data) && !p->to_free) { in ffs_epfile_read_iter()
1302 return -ENOMEM; in ffs_epfile_read_iter()
1305 p->data = *to; in ffs_epfile_read_iter()
1306 p->to_free = NULL; in ffs_epfile_read_iter()
1308 p->mm = current->mm; in ffs_epfile_read_iter()
1310 kiocb->private = p; in ffs_epfile_read_iter()
1312 if (p->aio) in ffs_epfile_read_iter()
1315 res = ffs_epfile_io(kiocb->ki_filp, p); in ffs_epfile_read_iter()
1316 if (res == -EIOCBQUEUED) in ffs_epfile_read_iter()
1319 if (p->aio) { in ffs_epfile_read_iter()
1320 kfree(p->to_free); in ffs_epfile_read_iter()
1323 *to = p->data; in ffs_epfile_read_iter()
1331 struct dma_buf_attachment *attach = priv->attach; in ffs_dmabuf_release()
1332 struct dma_buf *dmabuf = attach->dmabuf; in ffs_dmabuf_release()
1335 dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir); in ffs_dmabuf_release()
1337 dma_buf_detach(attach->dmabuf, attach); in ffs_dmabuf_release()
1344 struct ffs_dmabuf_priv *priv = attach->importer_priv; in ffs_dmabuf_get()
1346 kref_get(&priv->ref); in ffs_dmabuf_get()
1351 struct ffs_dmabuf_priv *priv = attach->importer_priv; in ffs_dmabuf_put()
1353 kref_put(&priv->ref, ffs_dmabuf_release); in ffs_dmabuf_put()
1359 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_release()
1361 struct ffs_data *ffs = epfile->ffs; in ffs_epfile_release()
1363 mutex_lock(&epfile->dmabufs_mutex); in ffs_epfile_release()
1366 list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) { in ffs_epfile_release()
1368 spin_lock_irq(&ffs->eps_lock); in ffs_epfile_release()
1369 if (priv->ep && priv->req) in ffs_epfile_release()
1370 usb_ep_dequeue(priv->ep, priv->req); in ffs_epfile_release()
1371 spin_unlock_irq(&ffs->eps_lock); in ffs_epfile_release()
1373 list_del(&priv->entry); in ffs_epfile_release()
1374 ffs_dmabuf_put(priv->attach); in ffs_epfile_release()
1377 mutex_unlock(&epfile->dmabufs_mutex); in ffs_epfile_release()
1380 ffs_data_closed(epfile->ffs); in ffs_epfile_release()
1389 struct ffs_dmabuf_priv *priv = dma_fence->priv; in ffs_dmabuf_cleanup()
1390 struct dma_buf_attachment *attach = priv->attach; in ffs_dmabuf_cleanup()
1391 struct dma_fence *fence = &dma_fence->base; in ffs_dmabuf_cleanup()
1399 struct ffs_dmabuf_priv *priv = dma_fence->priv; in ffs_dmabuf_signal_done()
1400 struct dma_fence *fence = &dma_fence->base; in ffs_dmabuf_signal_done()
1404 fence->error = ret; in ffs_dmabuf_signal_done()
1413 INIT_WORK(&dma_fence->work, ffs_dmabuf_cleanup); in ffs_dmabuf_signal_done()
1414 queue_work(priv->ffs->io_completion_wq, &dma_fence->work); in ffs_dmabuf_signal_done()
1420 pr_vdebug("FFS: DMABUF transfer complete, status=%d\n", req->status); in ffs_epfile_dmabuf_io_complete()
1421 ffs_dmabuf_signal_done(req->context, req->status); in ffs_epfile_dmabuf_io_complete()
1452 return dma_resv_lock_interruptible(dmabuf->resv, NULL); in ffs_dma_resv_lock()
1454 if (!dma_resv_trylock(dmabuf->resv)) in ffs_dma_resv_lock()
1455 return -EBUSY; in ffs_dma_resv_lock()
1463 struct device *dev = epfile->ffs->gadget->dev.parent; in ffs_dmabuf_find_attachment()
1467 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_find_attachment()
1469 list_for_each_entry(priv, &epfile->dmabufs, entry) { in ffs_dmabuf_find_attachment()
1470 if (priv->attach->dev == dev in ffs_dmabuf_find_attachment()
1471 && priv->attach->dmabuf == dmabuf) { in ffs_dmabuf_find_attachment()
1472 attach = priv->attach; in ffs_dmabuf_find_attachment()
1480 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_find_attachment()
1482 return attach ?: ERR_PTR(-EPERM); in ffs_dmabuf_find_attachment()
1487 bool nonblock = file->f_flags & O_NONBLOCK; in ffs_dmabuf_attach()
1488 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_attach()
1489 struct usb_gadget *gadget = epfile->ffs->gadget; in ffs_dmabuf_attach()
1497 if (!gadget || !gadget->sg_supported) in ffs_dmabuf_attach()
1498 return -EPERM; in ffs_dmabuf_attach()
1504 attach = dma_buf_attach(dmabuf, gadget->dev.parent); in ffs_dmabuf_attach()
1512 err = -ENOMEM; in ffs_dmabuf_attach()
1516 dir = epfile->in ? DMA_FROM_DEVICE : DMA_TO_DEVICE; in ffs_dmabuf_attach()
1523 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_attach()
1530 attach->importer_priv = priv; in ffs_dmabuf_attach()
1532 priv->sgt = sg_table; in ffs_dmabuf_attach()
1533 priv->dir = dir; in ffs_dmabuf_attach()
1534 priv->ffs = epfile->ffs; in ffs_dmabuf_attach()
1535 priv->attach = attach; in ffs_dmabuf_attach()
1536 spin_lock_init(&priv->lock); in ffs_dmabuf_attach()
1537 kref_init(&priv->ref); in ffs_dmabuf_attach()
1538 priv->context = dma_fence_context_alloc(1); in ffs_dmabuf_attach()
1540 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_attach()
1541 list_add(&priv->entry, &epfile->dmabufs); in ffs_dmabuf_attach()
1542 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_attach()
1558 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_detach()
1559 struct ffs_data *ffs = epfile->ffs; in ffs_dmabuf_detach()
1560 struct device *dev = ffs->gadget->dev.parent; in ffs_dmabuf_detach()
1563 int ret = -EPERM; in ffs_dmabuf_detach()
1569 mutex_lock(&epfile->dmabufs_mutex); in ffs_dmabuf_detach()
1571 list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) { in ffs_dmabuf_detach()
1572 if (priv->attach->dev == dev in ffs_dmabuf_detach()
1573 && priv->attach->dmabuf == dmabuf) { in ffs_dmabuf_detach()
1575 spin_lock_irq(&ffs->eps_lock); in ffs_dmabuf_detach()
1576 if (priv->ep && priv->req) in ffs_dmabuf_detach()
1577 usb_ep_dequeue(priv->ep, priv->req); in ffs_dmabuf_detach()
1578 spin_unlock_irq(&ffs->eps_lock); in ffs_dmabuf_detach()
1580 list_del(&priv->entry); in ffs_dmabuf_detach()
1583 ffs_dmabuf_put(priv->attach); in ffs_dmabuf_detach()
1589 mutex_unlock(&epfile->dmabufs_mutex); in ffs_dmabuf_detach()
1598 bool nonblock = file->f_flags & O_NONBLOCK; in ffs_dmabuf_transfer()
1599 struct ffs_epfile *epfile = file->private_data; in ffs_dmabuf_transfer()
1613 if (req->flags & ~USB_FFS_DMABUF_TRANSFER_MASK) in ffs_dmabuf_transfer()
1614 return -EINVAL; in ffs_dmabuf_transfer()
1616 dmabuf = dma_buf_get(req->fd); in ffs_dmabuf_transfer()
1620 if (req->length > dmabuf->size || req->length == 0) { in ffs_dmabuf_transfer()
1621 ret = -EINVAL; in ffs_dmabuf_transfer()
1631 priv = attach->importer_priv; in ffs_dmabuf_transfer()
1645 retl = dma_resv_wait_timeout(dmabuf->resv, in ffs_dmabuf_transfer()
1646 dma_resv_usage_rw(epfile->in), in ffs_dmabuf_transfer()
1649 retl = -EBUSY; in ffs_dmabuf_transfer()
1655 ret = dma_resv_reserve_fences(dmabuf->resv, 1); in ffs_dmabuf_transfer()
1661 ret = -ENOMEM; in ffs_dmabuf_transfer()
1665 fence->priv = priv; in ffs_dmabuf_transfer()
1667 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1670 if (epfile->ep != ep) { in ffs_dmabuf_transfer()
1671 ret = -ESHUTDOWN; in ffs_dmabuf_transfer()
1675 usb_req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC); in ffs_dmabuf_transfer()
1677 ret = -ENOMEM; in ffs_dmabuf_transfer()
1686 seqno = atomic_add_return(1, &epfile->seqno); in ffs_dmabuf_transfer()
1688 dma_fence_init(&fence->base, &ffs_dmabuf_fence_ops, in ffs_dmabuf_transfer()
1689 &priv->lock, priv->context, seqno); in ffs_dmabuf_transfer()
1691 resv_dir = epfile->in ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ; in ffs_dmabuf_transfer()
1693 dma_resv_add_fence(dmabuf->resv, &fence->base, resv_dir); in ffs_dmabuf_transfer()
1694 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_transfer()
1698 usb_req->length = req->length; in ffs_dmabuf_transfer()
1699 usb_req->buf = NULL; in ffs_dmabuf_transfer()
1700 usb_req->sg = priv->sgt->sgl; in ffs_dmabuf_transfer()
1701 usb_req->num_sgs = sg_nents_for_len(priv->sgt->sgl, req->length); in ffs_dmabuf_transfer()
1702 usb_req->sg_was_mapped = true; in ffs_dmabuf_transfer()
1703 usb_req->context = fence; in ffs_dmabuf_transfer()
1704 usb_req->complete = ffs_epfile_dmabuf_io_complete; in ffs_dmabuf_transfer()
1707 ret = usb_ep_queue(ep->ep, usb_req, GFP_ATOMIC); in ffs_dmabuf_transfer()
1710 priv->req = usb_req; in ffs_dmabuf_transfer()
1711 priv->ep = ep->ep; in ffs_dmabuf_transfer()
1715 usb_ep_free_request(ep->ep, usb_req); in ffs_dmabuf_transfer()
1718 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1724 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_dmabuf_transfer()
1725 dma_fence_put(&fence->base); in ffs_dmabuf_transfer()
1727 dma_resv_unlock(dmabuf->resv); in ffs_dmabuf_transfer()
1739 struct ffs_epfile *epfile = file->private_data; in ffs_epfile_ioctl()
1743 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) in ffs_epfile_ioctl()
1744 return -ENODEV; in ffs_epfile_ioctl()
1752 ret = -EFAULT; in ffs_epfile_ioctl()
1763 ret = -EFAULT; in ffs_epfile_ioctl()
1774 ret = -EFAULT; in ffs_epfile_ioctl()
1789 spin_lock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1792 if (epfile->ep != ep) { in ffs_epfile_ioctl()
1793 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1794 return -ESHUTDOWN; in ffs_epfile_ioctl()
1799 ret = usb_ep_fifo_status(epfile->ep->ep); in ffs_epfile_ioctl()
1802 usb_ep_fifo_flush(epfile->ep->ep); in ffs_epfile_ioctl()
1806 ret = usb_ep_clear_halt(epfile->ep->ep); in ffs_epfile_ioctl()
1809 ret = epfile->ep->num; in ffs_epfile_ioctl()
1816 switch (epfile->ffs->gadget->speed) { in ffs_epfile_ioctl()
1828 desc = epfile->ep->descs[desc_idx]; in ffs_epfile_ioctl()
1829 memcpy(&desc1, desc, desc->bLength); in ffs_epfile_ioctl()
1831 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1834 ret = -EFAULT; in ffs_epfile_ioctl()
1838 ret = -ENOTTY; in ffs_epfile_ioctl()
1840 spin_unlock_irq(&epfile->ffs->eps_lock); in ffs_epfile_ioctl()
1847 .open = ffs_epfile_open,
1876 inode->i_ino = get_next_ino(); in ffs_sb_make_inode()
1877 inode->i_mode = perms->mode; in ffs_sb_make_inode()
1878 inode->i_uid = perms->uid; in ffs_sb_make_inode()
1879 inode->i_gid = perms->gid; in ffs_sb_make_inode()
1882 inode->i_private = data; in ffs_sb_make_inode()
1884 inode->i_fop = fops; in ffs_sb_make_inode()
1886 inode->i_op = iops; in ffs_sb_make_inode()
1896 struct ffs_data *ffs = sb->s_fs_info; in ffs_sb_create_file()
1900 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); in ffs_sb_create_file()
1902 return -ENOMEM; in ffs_sb_create_file()
1903 dentry = simple_start_creating(sb->s_root, name); in ffs_sb_create_file()
1931 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_sb_fill()
1933 struct ffs_data *ffs = data->ffs_data; in ffs_sb_fill()
1935 ffs->sb = sb; in ffs_sb_fill()
1936 data->ffs_data = NULL; in ffs_sb_fill()
1937 sb->s_fs_info = ffs; in ffs_sb_fill()
1938 sb->s_blocksize = PAGE_SIZE; in ffs_sb_fill()
1939 sb->s_blocksize_bits = PAGE_SHIFT; in ffs_sb_fill()
1940 sb->s_magic = FUNCTIONFS_MAGIC; in ffs_sb_fill()
1941 sb->s_op = &ffs_sb_operations; in ffs_sb_fill()
1942 sb->s_time_gran = 1; in ffs_sb_fill()
1945 data->perms.mode = data->root_mode; in ffs_sb_fill()
1949 &data->perms); in ffs_sb_fill()
1950 sb->s_root = d_make_root(inode); in ffs_sb_fill()
1951 if (!sb->s_root) in ffs_sb_fill()
1952 return -ENOMEM; in ffs_sb_fill()
1979 struct ffs_sb_fill_data *data = fc->fs_private; in ffs_fs_parse_param()
1989 data->no_disconnect = result.boolean; in ffs_fs_parse_param()
1992 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1995 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
1998 data->root_mode = (result.uint_32 & 0555) | S_IFDIR; in ffs_fs_parse_param()
1999 data->perms.mode = (result.uint_32 & 0666) | S_IFREG; in ffs_fs_parse_param()
2003 data->perms.uid = make_kuid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
2004 if (!uid_valid(data->perms.uid)) in ffs_fs_parse_param()
2008 data->perms.gid = make_kgid(current_user_ns(), result.uint_32); in ffs_fs_parse_param()
2009 if (!gid_valid(data->perms.gid)) in ffs_fs_parse_param()
2014 return -ENOPARAM; in ffs_fs_parse_param()
2020 return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32); in ffs_fs_parse_param()
2028 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_get_tree()
2032 if (!fc->source) in ffs_fs_get_tree()
2035 ffs = ffs_data_new(fc->source); in ffs_fs_get_tree()
2037 return -ENOMEM; in ffs_fs_get_tree()
2038 ffs->file_perms = ctx->perms; in ffs_fs_get_tree()
2039 ffs->no_disconnect = ctx->no_disconnect; in ffs_fs_get_tree()
2041 ffs->dev_name = kstrdup(fc->source, GFP_KERNEL); in ffs_fs_get_tree()
2042 if (!ffs->dev_name) { in ffs_fs_get_tree()
2044 return -ENOMEM; in ffs_fs_get_tree()
2047 ret = ffs_acquire_dev(ffs->dev_name, ffs); in ffs_fs_get_tree()
2053 ctx->ffs_data = ffs; in ffs_fs_get_tree()
2059 struct ffs_sb_fill_data *ctx = fc->fs_private; in ffs_fs_free_fc()
2062 if (ctx->ffs_data) { in ffs_fs_free_fc()
2063 ffs_data_put(ctx->ffs_data); in ffs_fs_free_fc()
2082 return -ENOMEM; in ffs_fs_init_fs_context()
2084 ctx->perms.mode = S_IFREG | 0600; in ffs_fs_init_fs_context()
2085 ctx->perms.uid = GLOBAL_ROOT_UID; in ffs_fs_init_fs_context()
2086 ctx->perms.gid = GLOBAL_ROOT_GID; in ffs_fs_init_fs_context()
2087 ctx->root_mode = S_IFDIR | 0500; in ffs_fs_init_fs_context()
2088 ctx->no_disconnect = false; in ffs_fs_init_fs_context()
2090 fc->fs_private = ctx; in ffs_fs_init_fs_context()
2091 fc->ops = &ffs_fs_context_ops; in ffs_fs_init_fs_context()
2101 if (sb->s_fs_info) { in ffs_fs_kill_sb()
2102 struct ffs_data *ffs = sb->s_fs_info; in ffs_fs_kill_sb()
2103 ffs->state = FFS_CLOSING; in ffs_fs_kill_sb()
2107 cancel_work_sync(&ffs->reset_work); in ffs_fs_kill_sb()
2150 refcount_inc(&ffs->ref); in ffs_data_get()
2155 if (atomic_add_return(1, &ffs->opened) == 1 && in ffs_data_opened()
2156 ffs->state == FFS_DEACTIVATED) { in ffs_data_opened()
2157 ffs->state = FFS_CLOSING; in ffs_data_opened()
2164 if (refcount_dec_and_test(&ffs->ref)) { in ffs_data_put()
2167 ffs_release_dev(ffs->private_data); in ffs_data_put()
2168 BUG_ON(waitqueue_active(&ffs->ev.waitq) || in ffs_data_put()
2169 swait_active(&ffs->ep0req_completion.wait) || in ffs_data_put()
2170 waitqueue_active(&ffs->wait)); in ffs_data_put()
2171 destroy_workqueue(ffs->io_completion_wq); in ffs_data_put()
2172 kfree(ffs->dev_name); in ffs_data_put()
2179 if (atomic_dec_and_test(&ffs->opened)) { in ffs_data_closed()
2180 if (ffs->no_disconnect) { in ffs_data_closed()
2184 ffs->state = FFS_DEACTIVATED; in ffs_data_closed()
2185 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_closed()
2186 epfiles = ffs->epfiles; in ffs_data_closed()
2187 ffs->epfiles = NULL; in ffs_data_closed()
2188 spin_unlock_irqrestore(&ffs->eps_lock, in ffs_data_closed()
2192 ffs_epfiles_destroy(ffs->sb, epfiles, in ffs_data_closed()
2193 ffs->eps_count); in ffs_data_closed()
2195 if (ffs->setup_state == FFS_SETUP_PENDING) in ffs_data_closed()
2198 ffs->state = FFS_CLOSING; in ffs_data_closed()
2210 ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name); in ffs_data_new()
2211 if (!ffs->io_completion_wq) { in ffs_data_new()
2216 refcount_set(&ffs->ref, 1); in ffs_data_new()
2217 atomic_set(&ffs->opened, 0); in ffs_data_new()
2218 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_new()
2219 mutex_init(&ffs->mutex); in ffs_data_new()
2220 spin_lock_init(&ffs->eps_lock); in ffs_data_new()
2221 init_waitqueue_head(&ffs->ev.waitq); in ffs_data_new()
2222 init_waitqueue_head(&ffs->wait); in ffs_data_new()
2223 init_completion(&ffs->ep0req_completion); in ffs_data_new()
2226 ffs->ev.can_stall = 1; in ffs_data_new()
2238 BUG_ON(ffs->gadget); in ffs_data_clear()
2240 spin_lock_irqsave(&ffs->eps_lock, flags); in ffs_data_clear()
2241 epfiles = ffs->epfiles; in ffs_data_clear()
2242 ffs->epfiles = NULL; in ffs_data_clear()
2243 spin_unlock_irqrestore(&ffs->eps_lock, flags); in ffs_data_clear()
2248 * copy of epfile will save us from use-after-free. in ffs_data_clear()
2251 ffs_epfiles_destroy(ffs->sb, epfiles, ffs->eps_count); in ffs_data_clear()
2252 ffs->epfiles = NULL; in ffs_data_clear()
2255 if (ffs->ffs_eventfd) { in ffs_data_clear()
2256 eventfd_ctx_put(ffs->ffs_eventfd); in ffs_data_clear()
2257 ffs->ffs_eventfd = NULL; in ffs_data_clear()
2260 kfree(ffs->raw_descs_data); in ffs_data_clear()
2261 kfree(ffs->raw_strings); in ffs_data_clear()
2262 kfree(ffs->stringtabs); in ffs_data_clear()
2269 ffs->raw_descs_data = NULL; in ffs_data_reset()
2270 ffs->raw_descs = NULL; in ffs_data_reset()
2271 ffs->raw_strings = NULL; in ffs_data_reset()
2272 ffs->stringtabs = NULL; in ffs_data_reset()
2274 ffs->raw_descs_length = 0; in ffs_data_reset()
2275 ffs->fs_descs_count = 0; in ffs_data_reset()
2276 ffs->hs_descs_count = 0; in ffs_data_reset()
2277 ffs->ss_descs_count = 0; in ffs_data_reset()
2279 ffs->strings_count = 0; in ffs_data_reset()
2280 ffs->interfaces_count = 0; in ffs_data_reset()
2281 ffs->eps_count = 0; in ffs_data_reset()
2283 ffs->ev.count = 0; in ffs_data_reset()
2285 ffs->state = FFS_READ_DESCRIPTORS; in ffs_data_reset()
2286 ffs->setup_state = FFS_NO_SETUP; in ffs_data_reset()
2287 ffs->flags = 0; in ffs_data_reset()
2289 ffs->ms_os_descs_ext_prop_count = 0; in ffs_data_reset()
2290 ffs->ms_os_descs_ext_prop_name_len = 0; in ffs_data_reset()
2291 ffs->ms_os_descs_ext_prop_data_len = 0; in ffs_data_reset()
2300 if ((ffs->state != FFS_ACTIVE in functionfs_bind()
2301 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) in functionfs_bind()
2302 return -EBADFD; in functionfs_bind()
2304 first_id = usb_string_ids_n(cdev, ffs->strings_count); in functionfs_bind()
2308 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); in functionfs_bind()
2309 if (!ffs->ep0req) in functionfs_bind()
2310 return -ENOMEM; in functionfs_bind()
2311 ffs->ep0req->complete = ffs_ep0_complete; in functionfs_bind()
2312 ffs->ep0req->context = ffs; in functionfs_bind()
2314 lang = ffs->stringtabs; in functionfs_bind()
2317 struct usb_string *str = (*lang)->strings; in functionfs_bind()
2319 for (; str->s; ++id, ++str) in functionfs_bind()
2320 str->id = id; in functionfs_bind()
2324 ffs->gadget = cdev->gadget; in functionfs_bind()
2331 if (!WARN_ON(!ffs->gadget)) { in functionfs_unbind()
2333 usb_ep_dequeue(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
2334 mutex_lock(&ffs->mutex); in functionfs_unbind()
2335 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); in functionfs_unbind()
2336 ffs->ep0req = NULL; in functionfs_unbind()
2337 ffs->gadget = NULL; in functionfs_unbind()
2338 clear_bit(FFS_FL_BOUND, &ffs->flags); in functionfs_unbind()
2339 mutex_unlock(&ffs->mutex); in functionfs_unbind()
2350 count = ffs->eps_count; in ffs_epfiles_create()
2353 return -ENOMEM; in ffs_epfiles_create()
2357 epfile->ffs = ffs; in ffs_epfiles_create()
2358 mutex_init(&epfile->mutex); in ffs_epfiles_create()
2359 mutex_init(&epfile->dmabufs_mutex); in ffs_epfiles_create()
2360 INIT_LIST_HEAD(&epfile->dmabufs); in ffs_epfiles_create()
2361 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_epfiles_create()
2362 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]); in ffs_epfiles_create()
2364 sprintf(epfile->name, "ep%u", i); in ffs_epfiles_create()
2365 err = ffs_sb_create_file(ffs->sb, epfile->name, in ffs_epfiles_create()
2368 ffs_epfiles_destroy(ffs->sb, epfiles, i - 1); in ffs_epfiles_create()
2373 ffs->epfiles = epfiles; in ffs_epfiles_create()
2379 smp_store_release(&dentry->d_inode->i_private, NULL); in clear_one()
2386 struct dentry *root = sb->s_root; in ffs_epfiles_destroy()
2388 for (; count; --count, ++epfile) { in ffs_epfiles_destroy()
2389 BUG_ON(mutex_is_locked(&epfile->mutex)); in ffs_epfiles_destroy()
2390 simple_remove_by_name(root, epfile->name, clear_one); in ffs_epfiles_destroy()
2403 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
2404 count = func->ffs->eps_count; in ffs_func_eps_disable()
2405 epfile = func->ffs->epfiles; in ffs_func_eps_disable()
2406 ep = func->eps; in ffs_func_eps_disable()
2407 while (count--) { in ffs_func_eps_disable()
2409 if (ep->ep) in ffs_func_eps_disable()
2410 usb_ep_disable(ep->ep); in ffs_func_eps_disable()
2414 epfile->ep = NULL; in ffs_func_eps_disable()
2419 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_disable()
2431 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
2432 ffs = func->ffs; in ffs_func_eps_enable()
2433 ep = func->eps; in ffs_func_eps_enable()
2434 epfile = ffs->epfiles; in ffs_func_eps_enable()
2435 count = ffs->eps_count; in ffs_func_eps_enable()
2437 ret = -ENOMEM; in ffs_func_eps_enable()
2441 while (count--) { in ffs_func_eps_enable()
2442 ep->ep->driver_data = ep; in ffs_func_eps_enable()
2444 ret = config_ep_by_speed(func->gadget, &func->function, ep->ep); in ffs_func_eps_enable()
2447 __func__, ep->ep->name, ret); in ffs_func_eps_enable()
2451 ret = usb_ep_enable(ep->ep); in ffs_func_eps_enable()
2453 epfile->ep = ep; in ffs_func_eps_enable()
2454 epfile->in = usb_endpoint_dir_in(ep->ep->desc); in ffs_func_eps_enable()
2455 epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc); in ffs_func_eps_enable()
2464 wake_up_interruptible(&ffs->wait); in ffs_func_eps_enable()
2466 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_eps_enable()
2509 return -EINVAL; in ffs_do_single_desc()
2513 length = _ds->bLength; in ffs_do_single_desc()
2516 return -EINVAL; in ffs_do_single_desc()
2526 return -EINVAL; \ in ffs_do_single_desc()
2537 switch (_ds->bDescriptorType) { in ffs_do_single_desc()
2544 _ds->bDescriptorType); in ffs_do_single_desc()
2545 return -EINVAL; in ffs_do_single_desc()
2553 __entity(INTERFACE, ds->bInterfaceNumber); in ffs_do_single_desc()
2554 if (ds->iInterface) in ffs_do_single_desc()
2555 __entity(STRING, ds->iInterface); in ffs_do_single_desc()
2556 *current_class = ds->bInterfaceClass; in ffs_do_single_desc()
2557 *current_subclass = ds->bInterfaceSubClass; in ffs_do_single_desc()
2567 __entity(ENDPOINT, ds->bEndpointAddress); in ffs_do_single_desc()
2590 _ds->bDescriptorType, *current_class); in ffs_do_single_desc()
2591 return -EINVAL; in ffs_do_single_desc()
2604 if (ds->iFunction) in ffs_do_single_desc()
2605 __entity(STRING, ds->iFunction); in ffs_do_single_desc()
2621 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2622 return -EINVAL; in ffs_do_single_desc()
2626 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType); in ffs_do_single_desc()
2627 return -EINVAL; in ffs_do_single_desc()
2631 _ds->bLength, _ds->bDescriptorType); in ffs_do_single_desc()
2632 return -EINVAL; in ffs_do_single_desc()
2649 int current_class = -1; in ffs_do_descs()
2650 int current_subclass = -1; in ffs_do_descs()
2667 return _len - len; in ffs_do_descs()
2676 len -= ret; in ffs_do_descs()
2699 if (*valuep >= helper->interfaces_count) in __ffs_data_do_entity()
2700 helper->interfaces_count = *valuep + 1; in __ffs_data_do_entity()
2708 if (*valuep > helper->ffs->strings_count) in __ffs_data_do_entity()
2709 helper->ffs->strings_count = *valuep; in __ffs_data_do_entity()
2714 helper->eps_count++; in __ffs_data_do_entity()
2715 if (helper->eps_count >= FFS_MAX_EPS_COUNT) in __ffs_data_do_entity()
2716 return -EINVAL; in __ffs_data_do_entity()
2718 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count) in __ffs_data_do_entity()
2719 helper->ffs->eps_addrmap[helper->eps_count] = in __ffs_data_do_entity()
2720 d->bEndpointAddress; in __ffs_data_do_entity()
2721 else if (helper->ffs->eps_addrmap[helper->eps_count] != in __ffs_data_do_entity()
2722 d->bEndpointAddress) in __ffs_data_do_entity()
2723 return -EINVAL; in __ffs_data_do_entity()
2733 u16 bcd_version = le16_to_cpu(desc->bcdVersion); in __ffs_do_os_desc_header()
2734 u16 w_index = le16_to_cpu(desc->wIndex); in __ffs_do_os_desc_header()
2742 return -EINVAL; in __ffs_do_os_desc_header()
2753 return -EINVAL; in __ffs_do_os_desc_header()
2774 while (feature_count--) { in ffs_do_single_os_desc()
2781 len -= ret; in ffs_do_single_os_desc()
2783 return _len - len; in ffs_do_single_os_desc()
2801 return -EINVAL; in ffs_do_os_descs()
2810 if (le32_to_cpu(desc->dwLength) > len) in ffs_do_os_descs()
2811 return -EINVAL; in ffs_do_os_descs()
2820 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??" in ffs_do_os_descs()
2822 feature_count = le16_to_cpu(desc->wCount); in ffs_do_os_descs()
2824 (feature_count > 255 || desc->Reserved)) in ffs_do_os_descs()
2825 return -EINVAL; in ffs_do_os_descs()
2826 len -= ret; in ffs_do_os_descs()
2840 len -= ret; in ffs_do_os_descs()
2843 return _len - len; in ffs_do_os_descs()
2862 d->bFirstInterfaceNumber >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2863 return -EINVAL; in __ffs_data_do_os_desc()
2864 if (d->Reserved1 != 1) { in __ffs_data_do_os_desc()
2867 * but older kernels incorrectly rejected non-zero in __ffs_data_do_os_desc()
2872 d->Reserved1 = 1; in __ffs_data_do_os_desc()
2874 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i) in __ffs_data_do_os_desc()
2875 if (d->Reserved2[i]) in __ffs_data_do_os_desc()
2876 return -EINVAL; in __ffs_data_do_os_desc()
2886 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count) in __ffs_data_do_os_desc()
2887 return -EINVAL; in __ffs_data_do_os_desc()
2888 length = le32_to_cpu(d->dwSize); in __ffs_data_do_os_desc()
2890 return -EINVAL; in __ffs_data_do_os_desc()
2891 type = le32_to_cpu(d->dwPropertyDataType); in __ffs_data_do_os_desc()
2896 return -EINVAL; in __ffs_data_do_os_desc()
2898 pnl = le16_to_cpu(d->wPropertyNameLength); in __ffs_data_do_os_desc()
2902 return -EINVAL; in __ffs_data_do_os_desc()
2908 return -EINVAL; in __ffs_data_do_os_desc()
2910 ++ffs->ms_os_descs_ext_prop_count; in __ffs_data_do_os_desc()
2912 ffs->ms_os_descs_ext_prop_name_len += pnl * 2; in __ffs_data_do_os_desc()
2913 ffs->ms_os_descs_ext_prop_data_len += pdl; in __ffs_data_do_os_desc()
2918 return -EINVAL; in __ffs_data_do_os_desc()
2928 int ret = -EINVAL, i; in __ffs_data_got_descs()
2938 len -= 8; in __ffs_data_got_descs()
2942 ffs->user_flags = flags; in __ffs_data_got_descs()
2951 ret = -ENOSYS; in __ffs_data_got_descs()
2955 len -= 12; in __ffs_data_got_descs()
2964 ffs->ffs_eventfd = in __ffs_data_got_descs()
2966 if (IS_ERR(ffs->ffs_eventfd)) { in __ffs_data_got_descs()
2967 ret = PTR_ERR(ffs->ffs_eventfd); in __ffs_data_got_descs()
2968 ffs->ffs_eventfd = NULL; in __ffs_data_got_descs()
2972 len -= 4; in __ffs_data_got_descs()
2984 len -= 4; in __ffs_data_got_descs()
2993 len -= 4; in __ffs_data_got_descs()
3008 if (!ffs->eps_count && !ffs->interfaces_count) { in __ffs_data_got_descs()
3009 ffs->eps_count = helper.eps_count; in __ffs_data_got_descs()
3010 ffs->interfaces_count = helper.interfaces_count; in __ffs_data_got_descs()
3012 if (ffs->eps_count != helper.eps_count) { in __ffs_data_got_descs()
3013 ret = -EINVAL; in __ffs_data_got_descs()
3016 if (ffs->interfaces_count != helper.interfaces_count) { in __ffs_data_got_descs()
3017 ret = -EINVAL; in __ffs_data_got_descs()
3022 len -= ret; in __ffs_data_got_descs()
3030 len -= ret; in __ffs_data_got_descs()
3034 ret = -EINVAL; in __ffs_data_got_descs()
3038 ffs->raw_descs_data = _data; in __ffs_data_got_descs()
3039 ffs->raw_descs = raw_descs; in __ffs_data_got_descs()
3040 ffs->raw_descs_length = data - raw_descs; in __ffs_data_got_descs()
3041 ffs->fs_descs_count = counts[0]; in __ffs_data_got_descs()
3042 ffs->hs_descs_count = counts[1]; in __ffs_data_got_descs()
3043 ffs->ss_descs_count = counts[2]; in __ffs_data_got_descs()
3044 ffs->ms_os_descs_count = os_descs_count; in __ffs_data_got_descs()
3073 needed_count = ffs->strings_count; in __ffs_data_got_strings()
3100 return -ENOMEM; in __ffs_data_got_strings()
3109 } while (--i); in __ffs_data_got_strings()
3120 len -= 16; in __ffs_data_got_strings()
3122 do { /* lang_count > 0 so we can use do-while */ in __ffs_data_got_strings()
3128 t->language = get_unaligned_le16(data); in __ffs_data_got_strings()
3129 t->strings = s; in __ffs_data_got_strings()
3133 len -= 2; in __ffs_data_got_strings()
3136 do { /* str_count > 0 so we can use do-while */ in __ffs_data_got_strings()
3149 * s->id will be set while adding in __ffs_data_got_strings()
3153 s->s = data; in __ffs_data_got_strings()
3154 --needed; in __ffs_data_got_strings()
3159 len -= length + 1; in __ffs_data_got_strings()
3160 } while (--str_per_lang); in __ffs_data_got_strings()
3162 s->id = 0; /* terminator */ in __ffs_data_got_strings()
3163 s->s = NULL; in __ffs_data_got_strings()
3166 } while (--lang_count); in __ffs_data_got_strings()
3173 ffs->stringtabs = stringtabs; in __ffs_data_got_strings()
3174 ffs->raw_strings = _data; in __ffs_data_got_strings()
3182 return -EINVAL; in __ffs_data_got_strings()
3198 * of ffs->setup_state without holding the lock because when in __ffs_event_add()
3202 if (ffs->setup_state == FFS_SETUP_PENDING) in __ffs_event_add()
3203 ffs->setup_state = FFS_SETUP_CANCELLED; in __ffs_event_add()
3207 * evens on ffs->ev.types queue. This is important because the queue in __ffs_event_add()
3238 u8 *ev = ffs->ev.types, *out = ev; in __ffs_event_add()
3239 unsigned n = ffs->ev.count; in __ffs_event_add()
3240 for (; n; --n, ++ev) in __ffs_event_add()
3245 ffs->ev.count = out - ffs->ev.types; in __ffs_event_add()
3249 ffs->ev.types[ffs->ev.count++] = type; in __ffs_event_add()
3250 wake_up_locked(&ffs->ev.waitq); in __ffs_event_add()
3251 if (ffs->ffs_eventfd) in __ffs_event_add()
3252 eventfd_signal(ffs->ffs_eventfd); in __ffs_event_add()
3259 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_event_add()
3261 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_event_add()
3270 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i) in ffs_ep_addr2idx()
3271 if (ffs->eps_addrmap[i] == endpoint_address) in ffs_ep_addr2idx()
3273 return -ENOENT; in ffs_ep_addr2idx()
3296 if (func->function.ss_descriptors) { in __ffs_func_bind_do_descs()
3298 func->function.ss_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3299 } else if (func->function.hs_descriptors) { in __ffs_func_bind_do_descs()
3301 func->function.hs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3304 func->function.fs_descriptors[(long)valuep] = desc; in __ffs_func_bind_do_descs()
3307 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) in __ffs_func_bind_do_descs()
3310 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1; in __ffs_func_bind_do_descs()
3314 ffs_ep = func->eps + idx; in __ffs_func_bind_do_descs()
3316 if (ffs_ep->descs[ep_desc_id]) { in __ffs_func_bind_do_descs()
3320 return -EINVAL; in __ffs_func_bind_do_descs()
3322 ffs_ep->descs[ep_desc_id] = ds; in __ffs_func_bind_do_descs()
3324 ffs_dump_mem(": Original ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
3325 if (ffs_ep->ep) { in __ffs_func_bind_do_descs()
3326 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; in __ffs_func_bind_do_descs()
3327 if (!ds->wMaxPacketSize) in __ffs_func_bind_do_descs()
3328 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; in __ffs_func_bind_do_descs()
3339 bEndpointAddress = ds->bEndpointAddress; in __ffs_func_bind_do_descs()
3344 wMaxPacketSize = ds->wMaxPacketSize; in __ffs_func_bind_do_descs()
3346 ep = usb_ep_autoconfig(func->gadget, ds); in __ffs_func_bind_do_descs()
3348 return -ENOTSUPP; in __ffs_func_bind_do_descs()
3349 ep->driver_data = func->eps + idx; in __ffs_func_bind_do_descs()
3353 return -ENOMEM; in __ffs_func_bind_do_descs()
3355 ffs_ep->ep = ep; in __ffs_func_bind_do_descs()
3356 ffs_ep->req = req; in __ffs_func_bind_do_descs()
3357 func->eps_revmap[ds->bEndpointAddress & in __ffs_func_bind_do_descs()
3363 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in __ffs_func_bind_do_descs()
3364 ds->bEndpointAddress = bEndpointAddress; in __ffs_func_bind_do_descs()
3369 ds->wMaxPacketSize = wMaxPacketSize; in __ffs_func_bind_do_descs()
3371 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); in __ffs_func_bind_do_descs()
3392 if (func->interfaces_nums[idx] < 0) { in __ffs_func_bind_do_nums()
3393 int id = usb_interface_id(func->conf, &func->function); in __ffs_func_bind_do_nums()
3396 func->interfaces_nums[idx] = id; in __ffs_func_bind_do_nums()
3398 newValue = func->interfaces_nums[idx]; in __ffs_func_bind_do_nums()
3403 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; in __ffs_func_bind_do_nums()
3411 if (desc->bDescriptorType == USB_DT_ENDPOINT) in __ffs_func_bind_do_nums()
3414 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; in __ffs_func_bind_do_nums()
3415 if (!func->eps[idx].ep) in __ffs_func_bind_do_nums()
3416 return -EINVAL; in __ffs_func_bind_do_nums()
3420 descs = func->eps[idx].descs; in __ffs_func_bind_do_nums()
3421 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; in __ffs_func_bind_do_nums()
3426 pr_vdebug("%02x -> %02x\n", *valuep, newValue); in __ffs_func_bind_do_nums()
3443 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
3444 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber]; in __ffs_func_bind_do_os_desc()
3445 memcpy(t->os_desc->ext_compat_id, &desc->IDs, in __ffs_func_bind_do_os_desc()
3457 t = &func->function.os_desc_table[h->interface]; in __ffs_func_bind_do_os_desc()
3458 t->if_id = func->interfaces_nums[h->interface]; in __ffs_func_bind_do_os_desc()
3460 ext_prop = func->ffs->ms_os_descs_ext_prop_avail; in __ffs_func_bind_do_os_desc()
3461 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop); in __ffs_func_bind_do_os_desc()
3463 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType); in __ffs_func_bind_do_os_desc()
3464 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength); in __ffs_func_bind_do_os_desc()
3465 ext_prop->data_len = le32_to_cpu(*(__le32 *) in __ffs_func_bind_do_os_desc()
3466 usb_ext_prop_data_len_ptr(data, ext_prop->name_len)); in __ffs_func_bind_do_os_desc()
3467 length = ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
3469 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail; in __ffs_func_bind_do_os_desc()
3470 func->ffs->ms_os_descs_ext_prop_name_avail += in __ffs_func_bind_do_os_desc()
3471 ext_prop->name_len; in __ffs_func_bind_do_os_desc()
3473 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail; in __ffs_func_bind_do_os_desc()
3474 func->ffs->ms_os_descs_ext_prop_data_avail += in __ffs_func_bind_do_os_desc()
3475 ext_prop->data_len; in __ffs_func_bind_do_os_desc()
3477 usb_ext_prop_data_ptr(data, ext_prop->name_len), in __ffs_func_bind_do_os_desc()
3478 ext_prop->data_len); in __ffs_func_bind_do_os_desc()
3480 switch (ext_prop->type) { in __ffs_func_bind_do_os_desc()
3485 ext_prop->data_len *= 2; in __ffs_func_bind_do_os_desc()
3488 ext_prop->data = ext_prop_data; in __ffs_func_bind_do_os_desc()
3491 ext_prop->name_len); in __ffs_func_bind_do_os_desc()
3493 ext_prop->name_len *= 2; in __ffs_func_bind_do_os_desc()
3494 ext_prop->name = ext_prop_name; in __ffs_func_bind_do_os_desc()
3496 t->os_desc->ext_prop_len += in __ffs_func_bind_do_os_desc()
3497 ext_prop->name_len + ext_prop->data_len + 14; in __ffs_func_bind_do_os_desc()
3498 ++t->os_desc->ext_prop_count; in __ffs_func_bind_do_os_desc()
3499 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop); in __ffs_func_bind_do_os_desc()
3514 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_do_functionfs_bind()
3523 * Configfs-enabled gadgets however do need ffs_dev_lock. in ffs_do_functionfs_bind()
3525 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3527 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; in ffs_do_functionfs_bind()
3528 ffs_data = ffs_opts->dev->ffs_data; in ffs_do_functionfs_bind()
3529 if (!ffs_opts->no_configfs) in ffs_do_functionfs_bind()
3534 func->ffs = ffs_data; in ffs_do_functionfs_bind()
3535 func->conf = c; in ffs_do_functionfs_bind()
3536 func->gadget = c->cdev->gadget; in ffs_do_functionfs_bind()
3543 * with regard to ffs_opts->bound access in ffs_do_functionfs_bind()
3545 if (!ffs_opts->refcnt) { in ffs_do_functionfs_bind()
3546 ret = functionfs_bind(func->ffs, c->cdev); in ffs_do_functionfs_bind()
3550 ffs_opts->refcnt++; in ffs_do_functionfs_bind()
3551 func->function.strings = func->ffs->stringtabs; in ffs_do_functionfs_bind()
3560 struct ffs_data *ffs = func->ffs; in _ffs_func_bind()
3562 const int full = !!func->ffs->fs_descs_count; in _ffs_func_bind()
3563 const int high = !!func->ffs->hs_descs_count; in _ffs_func_bind()
3564 const int super = !!func->ffs->ss_descs_count; in _ffs_func_bind()
3571 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count); in _ffs_func_bind()
3573 full ? ffs->fs_descs_count + 1 : 0); in _ffs_func_bind()
3575 high ? ffs->hs_descs_count + 1 : 0); in _ffs_func_bind()
3577 super ? ffs->ss_descs_count + 1 : 0); in _ffs_func_bind()
3578 vla_item_with_sz(d, short, inums, ffs->interfaces_count); in _ffs_func_bind()
3580 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3582 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3584 c->cdev->use_os_string ? ffs->interfaces_count : 0); in _ffs_func_bind()
3586 ffs->ms_os_descs_ext_prop_count); in _ffs_func_bind()
3588 ffs->ms_os_descs_ext_prop_name_len); in _ffs_func_bind()
3590 ffs->ms_os_descs_ext_prop_data_len); in _ffs_func_bind()
3591 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length); in _ffs_func_bind()
3596 return -ENOTSUPP; in _ffs_func_bind()
3601 return -ENOMEM; in _ffs_func_bind()
3603 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop); in _ffs_func_bind()
3604 ffs->ms_os_descs_ext_prop_name_avail = in _ffs_func_bind()
3606 ffs->ms_os_descs_ext_prop_data_avail = in _ffs_func_bind()
3610 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs, in _ffs_func_bind()
3611 ffs->raw_descs_length); in _ffs_func_bind()
3615 for (i = 0; i < ffs->eps_count; i++) in _ffs_func_bind()
3616 eps_ptr[i].num = -1; in _ffs_func_bind()
3619 * d_eps == vlabuf, func->eps used to kfree vlabuf later in _ffs_func_bind()
3621 func->eps = vla_ptr(vlabuf, d, eps); in _ffs_func_bind()
3622 func->interfaces_nums = vla_ptr(vlabuf, d, inums); in _ffs_func_bind()
3630 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); in _ffs_func_bind()
3631 fs_len = ffs_do_descs(ffs->fs_descs_count, in _ffs_func_bind()
3644 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); in _ffs_func_bind()
3645 hs_len = ffs_do_descs(ffs->hs_descs_count, in _ffs_func_bind()
3647 d_raw_descs__sz - fs_len, in _ffs_func_bind()
3658 func->function.ss_descriptors = func->function.ssp_descriptors = in _ffs_func_bind()
3660 ss_len = ffs_do_descs(ffs->ss_descs_count, in _ffs_func_bind()
3662 d_raw_descs__sz - fs_len - hs_len, in _ffs_func_bind()
3677 ret = ffs_do_descs(ffs->fs_descs_count + in _ffs_func_bind()
3678 (high ? ffs->hs_descs_count : 0) + in _ffs_func_bind()
3679 (super ? ffs->ss_descs_count : 0), in _ffs_func_bind()
3685 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table); in _ffs_func_bind()
3686 if (c->cdev->use_os_string) { in _ffs_func_bind()
3687 for (i = 0; i < ffs->interfaces_count; ++i) { in _ffs_func_bind()
3690 desc = func->function.os_desc_table[i].os_desc = in _ffs_func_bind()
3693 desc->ext_compat_id = in _ffs_func_bind()
3695 INIT_LIST_HEAD(&desc->ext_prop); in _ffs_func_bind()
3697 ret = ffs_do_os_descs(ffs->ms_os_descs_count, in _ffs_func_bind()
3700 d_raw_descs__sz - fs_len - hs_len - in _ffs_func_bind()
3706 func->function.os_desc_n = in _ffs_func_bind()
3707 c->cdev->use_os_string ? ffs->interfaces_count : 0; in _ffs_func_bind()
3729 if (ret && !--ffs_opts->refcnt) in ffs_func_bind()
3730 functionfs_unbind(func->ffs); in ffs_func_bind()
3751 return (intf < 0) ? intf : func->cur_alt[interface]; in ffs_func_get_alt()
3758 struct ffs_data *ffs = func->ffs; in ffs_func_set_alt()
3762 return -EINVAL; in ffs_func_set_alt()
3768 if (ffs->func) in ffs_func_set_alt()
3769 ffs_func_eps_disable(ffs->func); in ffs_func_set_alt()
3771 if (ffs->state == FFS_DEACTIVATED) { in ffs_func_set_alt()
3772 ffs->state = FFS_CLOSING; in ffs_func_set_alt()
3773 INIT_WORK(&ffs->reset_work, ffs_reset_work); in ffs_func_set_alt()
3774 schedule_work(&ffs->reset_work); in ffs_func_set_alt()
3775 return -ENODEV; in ffs_func_set_alt()
3778 if (ffs->state != FFS_ACTIVE) in ffs_func_set_alt()
3779 return -ENODEV; in ffs_func_set_alt()
3781 ffs->func = func; in ffs_func_set_alt()
3785 func->cur_alt[interface] = alt; in ffs_func_set_alt()
3793 struct ffs_data *ffs = func->ffs; in ffs_func_disable()
3795 if (ffs->func) in ffs_func_disable()
3796 ffs_func_eps_disable(ffs->func); in ffs_func_disable()
3798 if (ffs->state == FFS_DEACTIVATED) { in ffs_func_disable()
3799 ffs->state = FFS_CLOSING; in ffs_func_disable()
3800 INIT_WORK(&ffs->reset_work, ffs_reset_work); in ffs_func_disable()
3801 schedule_work(&ffs->reset_work); in ffs_func_disable()
3805 if (ffs->state == FFS_ACTIVE) { in ffs_func_disable()
3806 ffs->func = NULL; in ffs_func_disable()
3815 struct ffs_data *ffs = func->ffs; in ffs_func_setup()
3819 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType); in ffs_func_setup()
3820 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest); in ffs_func_setup()
3821 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue)); in ffs_func_setup()
3822 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3823 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength)); in ffs_func_setup()
3829 * passed to usb_configuration->setup() (if one is set). No in ffs_func_setup()
3835 if (ffs->state != FFS_ACTIVE) in ffs_func_setup()
3836 return -ENODEV; in ffs_func_setup()
3838 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_setup()
3840 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3846 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); in ffs_func_setup()
3849 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) in ffs_func_setup()
3850 ret = func->ffs->eps_addrmap[ret]; in ffs_func_setup()
3854 if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP) in ffs_func_setup()
3855 ret = le16_to_cpu(creq->wIndex); in ffs_func_setup()
3857 return -EOPNOTSUPP; in ffs_func_setup()
3860 spin_lock_irqsave(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3861 ffs->ev.setup = *creq; in ffs_func_setup()
3862 ffs->ev.setup.wIndex = cpu_to_le16(ret); in ffs_func_setup()
3864 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); in ffs_func_setup()
3866 return ffs->ev.setup.wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0; in ffs_func_setup()
3875 if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP)) in ffs_func_req_match()
3878 switch (creq->bRequestType & USB_RECIP_MASK) { in ffs_func_req_match()
3881 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3884 le16_to_cpu(creq->wIndex)) >= 0); in ffs_func_req_match()
3886 return (bool) (func->ffs->user_flags & in ffs_func_req_match()
3893 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); in ffs_func_suspend()
3898 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); in ffs_func_resume()
3906 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; in ffs_func_revmap_ep()
3907 return num ? num : -EDOM; in ffs_func_revmap_ep()
3912 short *nums = func->interfaces_nums; in ffs_func_revmap_intf()
3913 unsigned count = func->ffs->interfaces_count; in ffs_func_revmap_intf()
3915 for (; count; --count, ++nums) { in ffs_func_revmap_intf()
3917 return nums - func->interfaces_nums; in ffs_func_revmap_intf()
3920 return -EDOM; in ffs_func_revmap_intf()
3936 if (strcmp(dev->name, name) == 0) in _ffs_do_find_dev()
3952 if (dev->single) in _ffs_get_single_dev()
3987 ready = opts->dev->desc_ready; in f_fs_opts_ready_show()
4004 usb_put_function_instance(&opts->func_inst); in ffs_attr_release()
4025 ffs_release_dev(opts->dev); in ffs_free_inst()
4027 _ffs_free_dev(opts->dev); in ffs_free_inst()
4035 return -ENAMETOOLONG; in ffs_set_inst_name()
4036 return ffs_name_dev(to_f_fs_opts(fi)->dev, name); in ffs_set_inst_name()
4046 return ERR_PTR(-ENOMEM); in ffs_alloc_inst()
4048 opts->func_inst.set_inst_name = ffs_set_inst_name; in ffs_alloc_inst()
4049 opts->func_inst.free_func_inst = ffs_free_inst; in ffs_alloc_inst()
4057 opts->dev = dev; in ffs_alloc_inst()
4058 dev->opts = opts; in ffs_alloc_inst()
4060 config_group_init_type_name(&opts->func_inst.group, "", in ffs_alloc_inst()
4062 return &opts->func_inst; in ffs_alloc_inst()
4074 struct ffs_data *ffs = func->ffs; in ffs_func_unbind()
4076 container_of(f->fi, struct f_fs_opts, func_inst); in ffs_func_unbind()
4077 struct ffs_ep *ep = func->eps; in ffs_func_unbind()
4078 unsigned count = ffs->eps_count; in ffs_func_unbind()
4081 if (ffs->func == func) { in ffs_func_unbind()
4083 ffs->func = NULL; in ffs_func_unbind()
4087 drain_workqueue(ffs->io_completion_wq); in ffs_func_unbind()
4090 if (!--opts->refcnt) in ffs_func_unbind()
4094 spin_lock_irqsave(&func->ffs->eps_lock, flags); in ffs_func_unbind()
4095 while (count--) { in ffs_func_unbind()
4096 if (ep->ep && ep->req) in ffs_func_unbind()
4097 usb_ep_free_request(ep->ep, ep->req); in ffs_func_unbind()
4098 ep->req = NULL; in ffs_func_unbind()
4101 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); in ffs_func_unbind()
4102 kfree(func->eps); in ffs_func_unbind()
4103 func->eps = NULL; in ffs_func_unbind()
4108 func->function.fs_descriptors = NULL; in ffs_func_unbind()
4109 func->function.hs_descriptors = NULL; in ffs_func_unbind()
4110 func->function.ss_descriptors = NULL; in ffs_func_unbind()
4111 func->function.ssp_descriptors = NULL; in ffs_func_unbind()
4112 func->interfaces_nums = NULL; in ffs_func_unbind()
4122 return ERR_PTR(-ENOMEM); in ffs_alloc()
4124 func->function.name = "Function FS Gadget"; in ffs_alloc()
4126 func->function.bind = ffs_func_bind; in ffs_alloc()
4127 func->function.unbind = ffs_func_unbind; in ffs_alloc()
4128 func->function.set_alt = ffs_func_set_alt; in ffs_alloc()
4129 func->function.get_alt = ffs_func_get_alt; in ffs_alloc()
4130 func->function.disable = ffs_func_disable; in ffs_alloc()
4131 func->function.setup = ffs_func_setup; in ffs_alloc()
4132 func->function.req_match = ffs_func_req_match; in ffs_alloc()
4133 func->function.suspend = ffs_func_suspend; in ffs_alloc()
4134 func->function.resume = ffs_func_resume; in ffs_alloc()
4135 func->function.free_func = ffs_free; in ffs_alloc()
4137 return &func->function; in ffs_alloc()
4149 return ERR_PTR(-EBUSY); in _ffs_alloc_dev()
4153 return ERR_PTR(-ENOMEM); in _ffs_alloc_dev()
4163 list_add(&dev->entry, &ffs_devices); in _ffs_alloc_dev()
4177 strscpy(dev->name, name, ARRAY_SIZE(dev->name)); in ffs_name_dev()
4179 ret = -EBUSY; in ffs_name_dev()
4195 ret = -EBUSY; in ffs_single_dev()
4197 dev->single = true; in ffs_single_dev()
4209 list_del(&dev->entry); in _ffs_free_dev()
4225 ret = -ENOENT; in ffs_acquire_dev()
4226 } else if (ffs_dev->mounted) { in ffs_acquire_dev()
4227 ret = -EBUSY; in ffs_acquire_dev()
4228 } else if (ffs_dev->ffs_acquire_dev_callback && in ffs_acquire_dev()
4229 ffs_dev->ffs_acquire_dev_callback(ffs_dev)) { in ffs_acquire_dev()
4230 ret = -ENOENT; in ffs_acquire_dev()
4232 ffs_dev->mounted = true; in ffs_acquire_dev()
4233 ffs_dev->ffs_data = ffs_data; in ffs_acquire_dev()
4234 ffs_data->private_data = ffs_dev; in ffs_acquire_dev()
4245 if (ffs_dev && ffs_dev->mounted) { in ffs_release_dev()
4246 ffs_dev->mounted = false; in ffs_release_dev()
4247 if (ffs_dev->ffs_data) { in ffs_release_dev()
4248 ffs_dev->ffs_data->private_data = NULL; in ffs_release_dev()
4249 ffs_dev->ffs_data = NULL; in ffs_release_dev()
4252 if (ffs_dev->ffs_release_dev_callback) in ffs_release_dev()
4253 ffs_dev->ffs_release_dev_callback(ffs_dev); in ffs_release_dev()
4266 ffs_obj = ffs->private_data; in ffs_ready()
4268 ret = -EINVAL; in ffs_ready()
4271 if (WARN_ON(ffs_obj->desc_ready)) { in ffs_ready()
4272 ret = -EBUSY; in ffs_ready()
4276 ffs_obj->desc_ready = true; in ffs_ready()
4278 if (ffs_obj->ffs_ready_callback) { in ffs_ready()
4279 ret = ffs_obj->ffs_ready_callback(ffs); in ffs_ready()
4284 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); in ffs_ready()
4298 ffs_obj = ffs->private_data; in ffs_closed()
4302 ffs_obj->desc_ready = false; in ffs_closed()
4304 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) && in ffs_closed()
4305 ffs_obj->ffs_closed_callback) in ffs_closed()
4306 ffs_obj->ffs_closed_callback(ffs); in ffs_closed()
4308 if (ffs_obj->opts) in ffs_closed()
4309 opts = ffs_obj->opts; in ffs_closed()
4313 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent in ffs_closed()
4314 || !kref_read(&opts->func_inst.group.cg_item.ci_kref)) in ffs_closed()
4317 ci = opts->func_inst.group.cg_item.ci_parent->ci_parent; in ffs_closed()
4320 if (test_bit(FFS_FL_BOUND, &ffs->flags)) in ffs_closed()
4332 ? mutex_trylock(mutex) ? 0 : -EAGAIN in ffs_mutex_lock()