xref: /openbmc/linux/drivers/usb/gadget/function/f_fs.c (revision 19e85d93)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * f_fs.c -- user mode file system API for USB composite function controllers
4  *
5  * Copyright (C) 2010 Samsung Electronics
6  * Author: Michal Nazarewicz <mina86@mina86.com>
7  *
8  * Based on inode.c (GadgetFS) which was:
9  * Copyright (C) 2003-2004 David Brownell
10  * Copyright (C) 2003 Agilent Technologies
11  */
12 
13 
14 /* #define DEBUG */
15 /* #define VERBOSE_DEBUG */
16 
17 #include <linux/blkdev.h>
18 #include <linux/pagemap.h>
19 #include <linux/export.h>
20 #include <linux/fs_parser.h>
21 #include <linux/hid.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/scatterlist.h>
25 #include <linux/sched/signal.h>
26 #include <linux/uio.h>
27 #include <linux/vmalloc.h>
28 #include <asm/unaligned.h>
29 
30 #include <linux/usb/ccid.h>
31 #include <linux/usb/composite.h>
32 #include <linux/usb/functionfs.h>
33 
34 #include <linux/aio.h>
35 #include <linux/kthread.h>
36 #include <linux/poll.h>
37 #include <linux/eventfd.h>
38 
39 #include "u_fs.h"
40 #include "u_f.h"
41 #include "u_os_desc.h"
42 #include "configfs.h"
43 
44 #define FUNCTIONFS_MAGIC	0xa647361 /* Chosen by a honest dice roll ;) */
45 
46 /* Reference counter handling */
47 static void ffs_data_get(struct ffs_data *ffs);
48 static void ffs_data_put(struct ffs_data *ffs);
49 /* Creates new ffs_data object. */
50 static struct ffs_data *__must_check ffs_data_new(const char *dev_name)
51 	__attribute__((malloc));
52 
53 /* Opened counter handling. */
54 static void ffs_data_opened(struct ffs_data *ffs);
55 static void ffs_data_closed(struct ffs_data *ffs);
56 
57 /* Called with ffs->mutex held; take over ownership of data. */
58 static int __must_check
59 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
60 static int __must_check
61 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
62 
63 
64 /* The function structure ***************************************************/
65 
66 struct ffs_ep;
67 
68 struct ffs_function {
69 	struct usb_configuration	*conf;
70 	struct usb_gadget		*gadget;
71 	struct ffs_data			*ffs;
72 
73 	struct ffs_ep			*eps;
74 	u8				eps_revmap[16];
75 	short				*interfaces_nums;
76 
77 	struct usb_function		function;
78 };
79 
80 
81 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
82 {
83 	return container_of(f, struct ffs_function, function);
84 }
85 
86 
87 static inline enum ffs_setup_state
88 ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
89 {
90 	return (enum ffs_setup_state)
91 		cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
92 }
93 
94 
95 static void ffs_func_eps_disable(struct ffs_function *func);
96 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
97 
98 static int ffs_func_bind(struct usb_configuration *,
99 			 struct usb_function *);
100 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
101 static void ffs_func_disable(struct usb_function *);
102 static int ffs_func_setup(struct usb_function *,
103 			  const struct usb_ctrlrequest *);
104 static bool ffs_func_req_match(struct usb_function *,
105 			       const struct usb_ctrlrequest *,
106 			       bool config0);
107 static void ffs_func_suspend(struct usb_function *);
108 static void ffs_func_resume(struct usb_function *);
109 
110 
111 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
112 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
113 
114 
115 /* The endpoints structures *************************************************/
116 
117 struct ffs_ep {
118 	struct usb_ep			*ep;	/* P: ffs->eps_lock */
119 	struct usb_request		*req;	/* P: epfile->mutex */
120 
121 	/* [0]: full speed, [1]: high speed, [2]: super speed */
122 	struct usb_endpoint_descriptor	*descs[3];
123 
124 	u8				num;
125 };
126 
127 struct ffs_epfile {
128 	/* Protects ep->ep and ep->req. */
129 	struct mutex			mutex;
130 
131 	struct ffs_data			*ffs;
132 	struct ffs_ep			*ep;	/* P: ffs->eps_lock */
133 
134 	struct dentry			*dentry;
135 
136 	/*
137 	 * Buffer for holding data from partial reads which may happen since
138 	 * we’re rounding user read requests to a multiple of a max packet size.
139 	 *
140 	 * The pointer is initialised with NULL value and may be set by
141 	 * __ffs_epfile_read_data function to point to a temporary buffer.
142 	 *
143 	 * In normal operation, calls to __ffs_epfile_read_buffered will consume
144 	 * data from said buffer and eventually free it.  Importantly, while the
145 	 * function is using the buffer, it sets the pointer to NULL.  This is
146 	 * all right since __ffs_epfile_read_data and __ffs_epfile_read_buffered
147 	 * can never run concurrently (they are synchronised by epfile->mutex)
148 	 * so the latter will not assign a new value to the pointer.
149 	 *
150 	 * Meanwhile ffs_func_eps_disable frees the buffer (if the pointer is
151 	 * valid) and sets the pointer to READ_BUFFER_DROP value.  This special
152 	 * value is crux of the synchronisation between ffs_func_eps_disable and
153 	 * __ffs_epfile_read_data.
154 	 *
155 	 * Once __ffs_epfile_read_data is about to finish it will try to set the
156 	 * pointer back to its old value (as described above), but seeing as the
157 	 * pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
158 	 * the buffer.
159 	 *
160 	 * == State transitions ==
161 	 *
162 	 * • ptr == NULL:  (initial state)
163 	 *   ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP
164 	 *   ◦ __ffs_epfile_read_buffered:    nop
165 	 *   ◦ __ffs_epfile_read_data allocates temp buffer: go to ptr == buf
166 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
167 	 * • ptr == DROP:
168 	 *   ◦ __ffs_epfile_read_buffer_free: nop
169 	 *   ◦ __ffs_epfile_read_buffered:    go to ptr == NULL
170 	 *   ◦ __ffs_epfile_read_data allocates temp buffer: free buf, nop
171 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
172 	 * • ptr == buf:
173 	 *   ◦ __ffs_epfile_read_buffer_free: free buf, go to ptr == DROP
174 	 *   ◦ __ffs_epfile_read_buffered:    go to ptr == NULL and reading
175 	 *   ◦ __ffs_epfile_read_data:        n/a, __ffs_epfile_read_buffered
176 	 *                                    is always called first
177 	 *   ◦ reading finishes:              n/a, not in ‘and reading’ state
178 	 * • ptr == NULL and reading:
179 	 *   ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP and reading
180 	 *   ◦ __ffs_epfile_read_buffered:    n/a, mutex is held
181 	 *   ◦ __ffs_epfile_read_data:        n/a, mutex is held
182 	 *   ◦ reading finishes and …
183 	 *     … all data read:               free buf, go to ptr == NULL
184 	 *     … otherwise:                   go to ptr == buf and reading
185 	 * • ptr == DROP and reading:
186 	 *   ◦ __ffs_epfile_read_buffer_free: nop
187 	 *   ◦ __ffs_epfile_read_buffered:    n/a, mutex is held
188 	 *   ◦ __ffs_epfile_read_data:        n/a, mutex is held
189 	 *   ◦ reading finishes:              free buf, go to ptr == DROP
190 	 */
191 	struct ffs_buffer		*read_buffer;
192 #define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
193 
194 	char				name[5];
195 
196 	unsigned char			in;	/* P: ffs->eps_lock */
197 	unsigned char			isoc;	/* P: ffs->eps_lock */
198 
199 	unsigned char			_pad;
200 };
201 
202 struct ffs_buffer {
203 	size_t length;
204 	char *data;
205 	char storage[];
206 };
207 
208 /*  ffs_io_data structure ***************************************************/
209 
210 struct ffs_io_data {
211 	bool aio;
212 	bool read;
213 
214 	struct kiocb *kiocb;
215 	struct iov_iter data;
216 	const void *to_free;
217 	char *buf;
218 
219 	struct mm_struct *mm;
220 	struct work_struct work;
221 
222 	struct usb_ep *ep;
223 	struct usb_request *req;
224 	struct sg_table sgt;
225 	bool use_sg;
226 
227 	struct ffs_data *ffs;
228 
229 	int status;
230 	struct completion done;
231 };
232 
233 struct ffs_desc_helper {
234 	struct ffs_data *ffs;
235 	unsigned interfaces_count;
236 	unsigned eps_count;
237 };
238 
239 static int  __must_check ffs_epfiles_create(struct ffs_data *ffs);
240 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
241 
242 static struct dentry *
243 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
244 		   const struct file_operations *fops);
245 
246 /* Devices management *******************************************************/
247 
248 DEFINE_MUTEX(ffs_lock);
249 EXPORT_SYMBOL_GPL(ffs_lock);
250 
251 static struct ffs_dev *_ffs_find_dev(const char *name);
252 static struct ffs_dev *_ffs_alloc_dev(void);
253 static void _ffs_free_dev(struct ffs_dev *dev);
254 static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data);
255 static void ffs_release_dev(struct ffs_dev *ffs_dev);
256 static int ffs_ready(struct ffs_data *ffs);
257 static void ffs_closed(struct ffs_data *ffs);
258 
259 /* Misc helper functions ****************************************************/
260 
261 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
262 	__attribute__((warn_unused_result, nonnull));
263 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
264 	__attribute__((warn_unused_result, nonnull));
265 
266 
267 /* Control file aka ep0 *****************************************************/
268 
269 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
270 {
271 	struct ffs_data *ffs = req->context;
272 
273 	complete(&ffs->ep0req_completion);
274 }
275 
276 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
277 	__releases(&ffs->ev.waitq.lock)
278 {
279 	struct usb_request *req = ffs->ep0req;
280 	int ret;
281 
282 	if (!req) {
283 		spin_unlock_irq(&ffs->ev.waitq.lock);
284 		return -EINVAL;
285 	}
286 
287 	req->zero     = len < le16_to_cpu(ffs->ev.setup.wLength);
288 
289 	spin_unlock_irq(&ffs->ev.waitq.lock);
290 
291 	req->buf      = data;
292 	req->length   = len;
293 
294 	/*
295 	 * UDC layer requires to provide a buffer even for ZLP, but should
296 	 * not use it at all. Let's provide some poisoned pointer to catch
297 	 * possible bug in the driver.
298 	 */
299 	if (req->buf == NULL)
300 		req->buf = (void *)0xDEADBABE;
301 
302 	reinit_completion(&ffs->ep0req_completion);
303 
304 	ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
305 	if (ret < 0)
306 		return ret;
307 
308 	ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
309 	if (ret) {
310 		usb_ep_dequeue(ffs->gadget->ep0, req);
311 		return -EINTR;
312 	}
313 
314 	ffs->setup_state = FFS_NO_SETUP;
315 	return req->status ? req->status : req->actual;
316 }
317 
318 static int __ffs_ep0_stall(struct ffs_data *ffs)
319 {
320 	if (ffs->ev.can_stall) {
321 		pr_vdebug("ep0 stall\n");
322 		usb_ep_set_halt(ffs->gadget->ep0);
323 		ffs->setup_state = FFS_NO_SETUP;
324 		return -EL2HLT;
325 	} else {
326 		pr_debug("bogus ep0 stall!\n");
327 		return -ESRCH;
328 	}
329 }
330 
331 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
332 			     size_t len, loff_t *ptr)
333 {
334 	struct ffs_data *ffs = file->private_data;
335 	ssize_t ret;
336 	char *data;
337 
338 	/* Fast check if setup was canceled */
339 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
340 		return -EIDRM;
341 
342 	/* Acquire mutex */
343 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
344 	if (ret < 0)
345 		return ret;
346 
347 	/* Check state */
348 	switch (ffs->state) {
349 	case FFS_READ_DESCRIPTORS:
350 	case FFS_READ_STRINGS:
351 		/* Copy data */
352 		if (len < 16) {
353 			ret = -EINVAL;
354 			break;
355 		}
356 
357 		data = ffs_prepare_buffer(buf, len);
358 		if (IS_ERR(data)) {
359 			ret = PTR_ERR(data);
360 			break;
361 		}
362 
363 		/* Handle data */
364 		if (ffs->state == FFS_READ_DESCRIPTORS) {
365 			pr_info("read descriptors\n");
366 			ret = __ffs_data_got_descs(ffs, data, len);
367 			if (ret < 0)
368 				break;
369 
370 			ffs->state = FFS_READ_STRINGS;
371 			ret = len;
372 		} else {
373 			pr_info("read strings\n");
374 			ret = __ffs_data_got_strings(ffs, data, len);
375 			if (ret < 0)
376 				break;
377 
378 			ret = ffs_epfiles_create(ffs);
379 			if (ret) {
380 				ffs->state = FFS_CLOSING;
381 				break;
382 			}
383 
384 			ffs->state = FFS_ACTIVE;
385 			mutex_unlock(&ffs->mutex);
386 
387 			ret = ffs_ready(ffs);
388 			if (ret < 0) {
389 				ffs->state = FFS_CLOSING;
390 				return ret;
391 			}
392 
393 			return len;
394 		}
395 		break;
396 
397 	case FFS_ACTIVE:
398 		data = NULL;
399 		/*
400 		 * We're called from user space, we can use _irq
401 		 * rather then _irqsave
402 		 */
403 		spin_lock_irq(&ffs->ev.waitq.lock);
404 		switch (ffs_setup_state_clear_cancelled(ffs)) {
405 		case FFS_SETUP_CANCELLED:
406 			ret = -EIDRM;
407 			goto done_spin;
408 
409 		case FFS_NO_SETUP:
410 			ret = -ESRCH;
411 			goto done_spin;
412 
413 		case FFS_SETUP_PENDING:
414 			break;
415 		}
416 
417 		/* FFS_SETUP_PENDING */
418 		if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
419 			spin_unlock_irq(&ffs->ev.waitq.lock);
420 			ret = __ffs_ep0_stall(ffs);
421 			break;
422 		}
423 
424 		/* FFS_SETUP_PENDING and not stall */
425 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
426 
427 		spin_unlock_irq(&ffs->ev.waitq.lock);
428 
429 		data = ffs_prepare_buffer(buf, len);
430 		if (IS_ERR(data)) {
431 			ret = PTR_ERR(data);
432 			break;
433 		}
434 
435 		spin_lock_irq(&ffs->ev.waitq.lock);
436 
437 		/*
438 		 * We are guaranteed to be still in FFS_ACTIVE state
439 		 * but the state of setup could have changed from
440 		 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
441 		 * to check for that.  If that happened we copied data
442 		 * from user space in vain but it's unlikely.
443 		 *
444 		 * For sure we are not in FFS_NO_SETUP since this is
445 		 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
446 		 * transition can be performed and it's protected by
447 		 * mutex.
448 		 */
449 		if (ffs_setup_state_clear_cancelled(ffs) ==
450 		    FFS_SETUP_CANCELLED) {
451 			ret = -EIDRM;
452 done_spin:
453 			spin_unlock_irq(&ffs->ev.waitq.lock);
454 		} else {
455 			/* unlocks spinlock */
456 			ret = __ffs_ep0_queue_wait(ffs, data, len);
457 		}
458 		kfree(data);
459 		break;
460 
461 	default:
462 		ret = -EBADFD;
463 		break;
464 	}
465 
466 	mutex_unlock(&ffs->mutex);
467 	return ret;
468 }
469 
470 /* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
471 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
472 				     size_t n)
473 	__releases(&ffs->ev.waitq.lock)
474 {
475 	/*
476 	 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
477 	 * size of ffs->ev.types array (which is four) so that's how much space
478 	 * we reserve.
479 	 */
480 	struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
481 	const size_t size = n * sizeof *events;
482 	unsigned i = 0;
483 
484 	memset(events, 0, size);
485 
486 	do {
487 		events[i].type = ffs->ev.types[i];
488 		if (events[i].type == FUNCTIONFS_SETUP) {
489 			events[i].u.setup = ffs->ev.setup;
490 			ffs->setup_state = FFS_SETUP_PENDING;
491 		}
492 	} while (++i < n);
493 
494 	ffs->ev.count -= n;
495 	if (ffs->ev.count)
496 		memmove(ffs->ev.types, ffs->ev.types + n,
497 			ffs->ev.count * sizeof *ffs->ev.types);
498 
499 	spin_unlock_irq(&ffs->ev.waitq.lock);
500 	mutex_unlock(&ffs->mutex);
501 
502 	return copy_to_user(buf, events, size) ? -EFAULT : size;
503 }
504 
505 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
506 			    size_t len, loff_t *ptr)
507 {
508 	struct ffs_data *ffs = file->private_data;
509 	char *data = NULL;
510 	size_t n;
511 	int ret;
512 
513 	/* Fast check if setup was canceled */
514 	if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
515 		return -EIDRM;
516 
517 	/* Acquire mutex */
518 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
519 	if (ret < 0)
520 		return ret;
521 
522 	/* Check state */
523 	if (ffs->state != FFS_ACTIVE) {
524 		ret = -EBADFD;
525 		goto done_mutex;
526 	}
527 
528 	/*
529 	 * We're called from user space, we can use _irq rather then
530 	 * _irqsave
531 	 */
532 	spin_lock_irq(&ffs->ev.waitq.lock);
533 
534 	switch (ffs_setup_state_clear_cancelled(ffs)) {
535 	case FFS_SETUP_CANCELLED:
536 		ret = -EIDRM;
537 		break;
538 
539 	case FFS_NO_SETUP:
540 		n = len / sizeof(struct usb_functionfs_event);
541 		if (!n) {
542 			ret = -EINVAL;
543 			break;
544 		}
545 
546 		if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
547 			ret = -EAGAIN;
548 			break;
549 		}
550 
551 		if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
552 							ffs->ev.count)) {
553 			ret = -EINTR;
554 			break;
555 		}
556 
557 		/* unlocks spinlock */
558 		return __ffs_ep0_read_events(ffs, buf,
559 					     min(n, (size_t)ffs->ev.count));
560 
561 	case FFS_SETUP_PENDING:
562 		if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
563 			spin_unlock_irq(&ffs->ev.waitq.lock);
564 			ret = __ffs_ep0_stall(ffs);
565 			goto done_mutex;
566 		}
567 
568 		len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
569 
570 		spin_unlock_irq(&ffs->ev.waitq.lock);
571 
572 		if (len) {
573 			data = kmalloc(len, GFP_KERNEL);
574 			if (!data) {
575 				ret = -ENOMEM;
576 				goto done_mutex;
577 			}
578 		}
579 
580 		spin_lock_irq(&ffs->ev.waitq.lock);
581 
582 		/* See ffs_ep0_write() */
583 		if (ffs_setup_state_clear_cancelled(ffs) ==
584 		    FFS_SETUP_CANCELLED) {
585 			ret = -EIDRM;
586 			break;
587 		}
588 
589 		/* unlocks spinlock */
590 		ret = __ffs_ep0_queue_wait(ffs, data, len);
591 		if ((ret > 0) && (copy_to_user(buf, data, len)))
592 			ret = -EFAULT;
593 		goto done_mutex;
594 
595 	default:
596 		ret = -EBADFD;
597 		break;
598 	}
599 
600 	spin_unlock_irq(&ffs->ev.waitq.lock);
601 done_mutex:
602 	mutex_unlock(&ffs->mutex);
603 	kfree(data);
604 	return ret;
605 }
606 
607 static int ffs_ep0_open(struct inode *inode, struct file *file)
608 {
609 	struct ffs_data *ffs = inode->i_private;
610 
611 	if (ffs->state == FFS_CLOSING)
612 		return -EBUSY;
613 
614 	file->private_data = ffs;
615 	ffs_data_opened(ffs);
616 
617 	return stream_open(inode, file);
618 }
619 
620 static int ffs_ep0_release(struct inode *inode, struct file *file)
621 {
622 	struct ffs_data *ffs = file->private_data;
623 
624 	ffs_data_closed(ffs);
625 
626 	return 0;
627 }
628 
629 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
630 {
631 	struct ffs_data *ffs = file->private_data;
632 	struct usb_gadget *gadget = ffs->gadget;
633 	long ret;
634 
635 	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
636 		struct ffs_function *func = ffs->func;
637 		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
638 	} else if (gadget && gadget->ops->ioctl) {
639 		ret = gadget->ops->ioctl(gadget, code, value);
640 	} else {
641 		ret = -ENOTTY;
642 	}
643 
644 	return ret;
645 }
646 
647 static __poll_t ffs_ep0_poll(struct file *file, poll_table *wait)
648 {
649 	struct ffs_data *ffs = file->private_data;
650 	__poll_t mask = EPOLLWRNORM;
651 	int ret;
652 
653 	poll_wait(file, &ffs->ev.waitq, wait);
654 
655 	ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
656 	if (ret < 0)
657 		return mask;
658 
659 	switch (ffs->state) {
660 	case FFS_READ_DESCRIPTORS:
661 	case FFS_READ_STRINGS:
662 		mask |= EPOLLOUT;
663 		break;
664 
665 	case FFS_ACTIVE:
666 		switch (ffs->setup_state) {
667 		case FFS_NO_SETUP:
668 			if (ffs->ev.count)
669 				mask |= EPOLLIN;
670 			break;
671 
672 		case FFS_SETUP_PENDING:
673 		case FFS_SETUP_CANCELLED:
674 			mask |= (EPOLLIN | EPOLLOUT);
675 			break;
676 		}
677 		break;
678 
679 	case FFS_CLOSING:
680 		break;
681 	case FFS_DEACTIVATED:
682 		break;
683 	}
684 
685 	mutex_unlock(&ffs->mutex);
686 
687 	return mask;
688 }
689 
690 static const struct file_operations ffs_ep0_operations = {
691 	.llseek =	no_llseek,
692 
693 	.open =		ffs_ep0_open,
694 	.write =	ffs_ep0_write,
695 	.read =		ffs_ep0_read,
696 	.release =	ffs_ep0_release,
697 	.unlocked_ioctl =	ffs_ep0_ioctl,
698 	.poll =		ffs_ep0_poll,
699 };
700 
701 
702 /* "Normal" endpoints operations ********************************************/
703 
704 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
705 {
706 	struct ffs_io_data *io_data = req->context;
707 
708 	if (req->status)
709 		io_data->status = req->status;
710 	else
711 		io_data->status = req->actual;
712 
713 	complete(&io_data->done);
714 }
715 
716 static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
717 {
718 	ssize_t ret = copy_to_iter(data, data_len, iter);
719 	if (ret == data_len)
720 		return ret;
721 
722 	if (iov_iter_count(iter))
723 		return -EFAULT;
724 
725 	/*
726 	 * Dear user space developer!
727 	 *
728 	 * TL;DR: To stop getting below error message in your kernel log, change
729 	 * user space code using functionfs to align read buffers to a max
730 	 * packet size.
731 	 *
732 	 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
733 	 * packet size.  When unaligned buffer is passed to functionfs, it
734 	 * internally uses a larger, aligned buffer so that such UDCs are happy.
735 	 *
736 	 * Unfortunately, this means that host may send more data than was
737 	 * requested in read(2) system call.  f_fs doesn’t know what to do with
738 	 * that excess data so it simply drops it.
739 	 *
740 	 * Was the buffer aligned in the first place, no such problem would
741 	 * happen.
742 	 *
743 	 * Data may be dropped only in AIO reads.  Synchronous reads are handled
744 	 * by splitting a request into multiple parts.  This splitting may still
745 	 * be a problem though so it’s likely best to align the buffer
746 	 * regardless of it being AIO or not..
747 	 *
748 	 * This only affects OUT endpoints, i.e. reading data with a read(2),
749 	 * aio_read(2) etc. system calls.  Writing data to an IN endpoint is not
750 	 * affected.
751 	 */
752 	pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
753 	       "Align read buffer size to max packet size to avoid the problem.\n",
754 	       data_len, ret);
755 
756 	return ret;
757 }
758 
759 /*
760  * allocate a virtually contiguous buffer and create a scatterlist describing it
761  * @sg_table	- pointer to a place to be filled with sg_table contents
762  * @size	- required buffer size
763  */
764 static void *ffs_build_sg_list(struct sg_table *sgt, size_t sz)
765 {
766 	struct page **pages;
767 	void *vaddr, *ptr;
768 	unsigned int n_pages;
769 	int i;
770 
771 	vaddr = vmalloc(sz);
772 	if (!vaddr)
773 		return NULL;
774 
775 	n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
776 	pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
777 	if (!pages) {
778 		vfree(vaddr);
779 
780 		return NULL;
781 	}
782 	for (i = 0, ptr = vaddr; i < n_pages; ++i, ptr += PAGE_SIZE)
783 		pages[i] = vmalloc_to_page(ptr);
784 
785 	if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL)) {
786 		kvfree(pages);
787 		vfree(vaddr);
788 
789 		return NULL;
790 	}
791 	kvfree(pages);
792 
793 	return vaddr;
794 }
795 
796 static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
797 	size_t data_len)
798 {
799 	if (io_data->use_sg)
800 		return ffs_build_sg_list(&io_data->sgt, data_len);
801 
802 	return kmalloc(data_len, GFP_KERNEL);
803 }
804 
805 static inline void ffs_free_buffer(struct ffs_io_data *io_data)
806 {
807 	if (!io_data->buf)
808 		return;
809 
810 	if (io_data->use_sg) {
811 		sg_free_table(&io_data->sgt);
812 		vfree(io_data->buf);
813 	} else {
814 		kfree(io_data->buf);
815 	}
816 }
817 
818 static void ffs_user_copy_worker(struct work_struct *work)
819 {
820 	struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
821 						   work);
822 	int ret = io_data->status;
823 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
824 	unsigned long flags;
825 
826 	if (io_data->read && ret > 0) {
827 		kthread_use_mm(io_data->mm);
828 		ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
829 		kthread_unuse_mm(io_data->mm);
830 	}
831 
832 	io_data->kiocb->ki_complete(io_data->kiocb, ret);
833 
834 	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
835 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);
836 
837 	spin_lock_irqsave(&io_data->ffs->eps_lock, flags);
838 	usb_ep_free_request(io_data->ep, io_data->req);
839 	io_data->req = NULL;
840 	spin_unlock_irqrestore(&io_data->ffs->eps_lock, flags);
841 
842 	if (io_data->read)
843 		kfree(io_data->to_free);
844 	ffs_free_buffer(io_data);
845 	kfree(io_data);
846 }
847 
848 static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
849 					 struct usb_request *req)
850 {
851 	struct ffs_io_data *io_data = req->context;
852 	struct ffs_data *ffs = io_data->ffs;
853 
854 	io_data->status = req->status ? req->status : req->actual;
855 
856 	INIT_WORK(&io_data->work, ffs_user_copy_worker);
857 	queue_work(ffs->io_completion_wq, &io_data->work);
858 }
859 
860 static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
861 {
862 	/*
863 	 * See comment in struct ffs_epfile for full read_buffer pointer
864 	 * synchronisation story.
865 	 */
866 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP);
867 	if (buf && buf != READ_BUFFER_DROP)
868 		kfree(buf);
869 }
870 
871 /* Assumes epfile->mutex is held. */
872 static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
873 					  struct iov_iter *iter)
874 {
875 	/*
876 	 * Null out epfile->read_buffer so ffs_func_eps_disable does not free
877 	 * the buffer while we are using it.  See comment in struct ffs_epfile
878 	 * for full read_buffer pointer synchronisation story.
879 	 */
880 	struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL);
881 	ssize_t ret;
882 	if (!buf || buf == READ_BUFFER_DROP)
883 		return 0;
884 
885 	ret = copy_to_iter(buf->data, buf->length, iter);
886 	if (buf->length == ret) {
887 		kfree(buf);
888 		return ret;
889 	}
890 
891 	if (iov_iter_count(iter)) {
892 		ret = -EFAULT;
893 	} else {
894 		buf->length -= ret;
895 		buf->data += ret;
896 	}
897 
898 	if (cmpxchg(&epfile->read_buffer, NULL, buf))
899 		kfree(buf);
900 
901 	return ret;
902 }
903 
904 /* Assumes epfile->mutex is held. */
905 static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
906 				      void *data, int data_len,
907 				      struct iov_iter *iter)
908 {
909 	struct ffs_buffer *buf;
910 
911 	ssize_t ret = copy_to_iter(data, data_len, iter);
912 	if (data_len == ret)
913 		return ret;
914 
915 	if (iov_iter_count(iter))
916 		return -EFAULT;
917 
918 	/* See ffs_copy_to_iter for more context. */
919 	pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
920 		data_len, ret);
921 
922 	data_len -= ret;
923 	buf = kmalloc(struct_size(buf, storage, data_len), GFP_KERNEL);
924 	if (!buf)
925 		return -ENOMEM;
926 	buf->length = data_len;
927 	buf->data = buf->storage;
928 	memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len));
929 
930 	/*
931 	 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
932 	 * ffs_func_eps_disable has been called in the meanwhile).  See comment
933 	 * in struct ffs_epfile for full read_buffer pointer synchronisation
934 	 * story.
935 	 */
936 	if (cmpxchg(&epfile->read_buffer, NULL, buf))
937 		kfree(buf);
938 
939 	return ret;
940 }
941 
942 static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
943 {
944 	struct ffs_epfile *epfile = file->private_data;
945 	struct usb_request *req;
946 	struct ffs_ep *ep;
947 	char *data = NULL;
948 	ssize_t ret, data_len = -EINVAL;
949 	int halt;
950 
951 	/* Are we still active? */
952 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
953 		return -ENODEV;
954 
955 	/* Wait for endpoint to be enabled */
956 	ep = epfile->ep;
957 	if (!ep) {
958 		if (file->f_flags & O_NONBLOCK)
959 			return -EAGAIN;
960 
961 		ret = wait_event_interruptible(
962 				epfile->ffs->wait, (ep = epfile->ep));
963 		if (ret)
964 			return -EINTR;
965 	}
966 
967 	/* Do we halt? */
968 	halt = (!io_data->read == !epfile->in);
969 	if (halt && epfile->isoc)
970 		return -EINVAL;
971 
972 	/* We will be using request and read_buffer */
973 	ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
974 	if (ret)
975 		goto error;
976 
977 	/* Allocate & copy */
978 	if (!halt) {
979 		struct usb_gadget *gadget;
980 
981 		/*
982 		 * Do we have buffered data from previous partial read?  Check
983 		 * that for synchronous case only because we do not have
984 		 * facility to ‘wake up’ a pending asynchronous read and push
985 		 * buffered data to it which we would need to make things behave
986 		 * consistently.
987 		 */
988 		if (!io_data->aio && io_data->read) {
989 			ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
990 			if (ret)
991 				goto error_mutex;
992 		}
993 
994 		/*
995 		 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
996 		 * before the waiting completes, so do not assign to 'gadget'
997 		 * earlier
998 		 */
999 		gadget = epfile->ffs->gadget;
1000 
1001 		spin_lock_irq(&epfile->ffs->eps_lock);
1002 		/* In the meantime, endpoint got disabled or changed. */
1003 		if (epfile->ep != ep) {
1004 			ret = -ESHUTDOWN;
1005 			goto error_lock;
1006 		}
1007 		data_len = iov_iter_count(&io_data->data);
1008 		/*
1009 		 * Controller may require buffer size to be aligned to
1010 		 * maxpacketsize of an out endpoint.
1011 		 */
1012 		if (io_data->read)
1013 			data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
1014 
1015 		io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
1016 		spin_unlock_irq(&epfile->ffs->eps_lock);
1017 
1018 		data = ffs_alloc_buffer(io_data, data_len);
1019 		if (!data) {
1020 			ret = -ENOMEM;
1021 			goto error_mutex;
1022 		}
1023 		if (!io_data->read &&
1024 		    !copy_from_iter_full(data, data_len, &io_data->data)) {
1025 			ret = -EFAULT;
1026 			goto error_mutex;
1027 		}
1028 	}
1029 
1030 	spin_lock_irq(&epfile->ffs->eps_lock);
1031 
1032 	if (epfile->ep != ep) {
1033 		/* In the meantime, endpoint got disabled or changed. */
1034 		ret = -ESHUTDOWN;
1035 	} else if (halt) {
1036 		ret = usb_ep_set_halt(ep->ep);
1037 		if (!ret)
1038 			ret = -EBADMSG;
1039 	} else if (data_len == -EINVAL) {
1040 		/*
1041 		 * Sanity Check: even though data_len can't be used
1042 		 * uninitialized at the time I write this comment, some
1043 		 * compilers complain about this situation.
1044 		 * In order to keep the code clean from warnings, data_len is
1045 		 * being initialized to -EINVAL during its declaration, which
1046 		 * means we can't rely on compiler anymore to warn no future
1047 		 * changes won't result in data_len being used uninitialized.
1048 		 * For such reason, we're adding this redundant sanity check
1049 		 * here.
1050 		 */
1051 		WARN(1, "%s: data_len == -EINVAL\n", __func__);
1052 		ret = -EINVAL;
1053 	} else if (!io_data->aio) {
1054 		bool interrupted = false;
1055 
1056 		req = ep->req;
1057 		if (io_data->use_sg) {
1058 			req->buf = NULL;
1059 			req->sg	= io_data->sgt.sgl;
1060 			req->num_sgs = io_data->sgt.nents;
1061 		} else {
1062 			req->buf = data;
1063 			req->num_sgs = 0;
1064 		}
1065 		req->length = data_len;
1066 
1067 		io_data->buf = data;
1068 
1069 		init_completion(&io_data->done);
1070 		req->context  = io_data;
1071 		req->complete = ffs_epfile_io_complete;
1072 
1073 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
1074 		if (ret < 0)
1075 			goto error_lock;
1076 
1077 		spin_unlock_irq(&epfile->ffs->eps_lock);
1078 
1079 		if (wait_for_completion_interruptible(&io_data->done)) {
1080 			spin_lock_irq(&epfile->ffs->eps_lock);
1081 			if (epfile->ep != ep) {
1082 				ret = -ESHUTDOWN;
1083 				goto error_lock;
1084 			}
1085 			/*
1086 			 * To avoid race condition with ffs_epfile_io_complete,
1087 			 * dequeue the request first then check
1088 			 * status. usb_ep_dequeue API should guarantee no race
1089 			 * condition with req->complete callback.
1090 			 */
1091 			usb_ep_dequeue(ep->ep, req);
1092 			spin_unlock_irq(&epfile->ffs->eps_lock);
1093 			wait_for_completion(&io_data->done);
1094 			interrupted = io_data->status < 0;
1095 		}
1096 
1097 		if (interrupted)
1098 			ret = -EINTR;
1099 		else if (io_data->read && io_data->status > 0)
1100 			ret = __ffs_epfile_read_data(epfile, data, io_data->status,
1101 						     &io_data->data);
1102 		else
1103 			ret = io_data->status;
1104 		goto error_mutex;
1105 	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
1106 		ret = -ENOMEM;
1107 	} else {
1108 		if (io_data->use_sg) {
1109 			req->buf = NULL;
1110 			req->sg	= io_data->sgt.sgl;
1111 			req->num_sgs = io_data->sgt.nents;
1112 		} else {
1113 			req->buf = data;
1114 			req->num_sgs = 0;
1115 		}
1116 		req->length = data_len;
1117 
1118 		io_data->buf = data;
1119 		io_data->ep = ep->ep;
1120 		io_data->req = req;
1121 		io_data->ffs = epfile->ffs;
1122 
1123 		req->context  = io_data;
1124 		req->complete = ffs_epfile_async_io_complete;
1125 
1126 		ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
1127 		if (ret) {
1128 			io_data->req = NULL;
1129 			usb_ep_free_request(ep->ep, req);
1130 			goto error_lock;
1131 		}
1132 
1133 		ret = -EIOCBQUEUED;
1134 		/*
1135 		 * Do not kfree the buffer in this function.  It will be freed
1136 		 * by ffs_user_copy_worker.
1137 		 */
1138 		data = NULL;
1139 	}
1140 
1141 error_lock:
1142 	spin_unlock_irq(&epfile->ffs->eps_lock);
1143 error_mutex:
1144 	mutex_unlock(&epfile->mutex);
1145 error:
1146 	if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */
1147 		ffs_free_buffer(io_data);
1148 	return ret;
1149 }
1150 
1151 static int
1152 ffs_epfile_open(struct inode *inode, struct file *file)
1153 {
1154 	struct ffs_epfile *epfile = inode->i_private;
1155 
1156 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1157 		return -ENODEV;
1158 
1159 	file->private_data = epfile;
1160 	ffs_data_opened(epfile->ffs);
1161 
1162 	return stream_open(inode, file);
1163 }
1164 
1165 static int ffs_aio_cancel(struct kiocb *kiocb)
1166 {
1167 	struct ffs_io_data *io_data = kiocb->private;
1168 	struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
1169 	unsigned long flags;
1170 	int value;
1171 
1172 	spin_lock_irqsave(&epfile->ffs->eps_lock, flags);
1173 
1174 	if (io_data && io_data->ep && io_data->req)
1175 		value = usb_ep_dequeue(io_data->ep, io_data->req);
1176 	else
1177 		value = -EINVAL;
1178 
1179 	spin_unlock_irqrestore(&epfile->ffs->eps_lock, flags);
1180 
1181 	return value;
1182 }
1183 
1184 static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
1185 {
1186 	struct ffs_io_data io_data, *p = &io_data;
1187 	ssize_t res;
1188 
1189 	if (!is_sync_kiocb(kiocb)) {
1190 		p = kzalloc(sizeof(io_data), GFP_KERNEL);
1191 		if (!p)
1192 			return -ENOMEM;
1193 		p->aio = true;
1194 	} else {
1195 		memset(p, 0, sizeof(*p));
1196 		p->aio = false;
1197 	}
1198 
1199 	p->read = false;
1200 	p->kiocb = kiocb;
1201 	p->data = *from;
1202 	p->mm = current->mm;
1203 
1204 	kiocb->private = p;
1205 
1206 	if (p->aio)
1207 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1208 
1209 	res = ffs_epfile_io(kiocb->ki_filp, p);
1210 	if (res == -EIOCBQUEUED)
1211 		return res;
1212 	if (p->aio)
1213 		kfree(p);
1214 	else
1215 		*from = p->data;
1216 	return res;
1217 }
1218 
1219 static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
1220 {
1221 	struct ffs_io_data io_data, *p = &io_data;
1222 	ssize_t res;
1223 
1224 	if (!is_sync_kiocb(kiocb)) {
1225 		p = kzalloc(sizeof(io_data), GFP_KERNEL);
1226 		if (!p)
1227 			return -ENOMEM;
1228 		p->aio = true;
1229 	} else {
1230 		memset(p, 0, sizeof(*p));
1231 		p->aio = false;
1232 	}
1233 
1234 	p->read = true;
1235 	p->kiocb = kiocb;
1236 	if (p->aio) {
1237 		p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
1238 		if (!iter_is_ubuf(&p->data) && !p->to_free) {
1239 			kfree(p);
1240 			return -ENOMEM;
1241 		}
1242 	} else {
1243 		p->data = *to;
1244 		p->to_free = NULL;
1245 	}
1246 	p->mm = current->mm;
1247 
1248 	kiocb->private = p;
1249 
1250 	if (p->aio)
1251 		kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1252 
1253 	res = ffs_epfile_io(kiocb->ki_filp, p);
1254 	if (res == -EIOCBQUEUED)
1255 		return res;
1256 
1257 	if (p->aio) {
1258 		kfree(p->to_free);
1259 		kfree(p);
1260 	} else {
1261 		*to = p->data;
1262 	}
1263 	return res;
1264 }
1265 
1266 static int
1267 ffs_epfile_release(struct inode *inode, struct file *file)
1268 {
1269 	struct ffs_epfile *epfile = inode->i_private;
1270 
1271 	__ffs_epfile_read_buffer_free(epfile);
1272 	ffs_data_closed(epfile->ffs);
1273 
1274 	return 0;
1275 }
1276 
1277 static long ffs_epfile_ioctl(struct file *file, unsigned code,
1278 			     unsigned long value)
1279 {
1280 	struct ffs_epfile *epfile = file->private_data;
1281 	struct ffs_ep *ep;
1282 	int ret;
1283 
1284 	if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1285 		return -ENODEV;
1286 
1287 	/* Wait for endpoint to be enabled */
1288 	ep = epfile->ep;
1289 	if (!ep) {
1290 		if (file->f_flags & O_NONBLOCK)
1291 			return -EAGAIN;
1292 
1293 		ret = wait_event_interruptible(
1294 				epfile->ffs->wait, (ep = epfile->ep));
1295 		if (ret)
1296 			return -EINTR;
1297 	}
1298 
1299 	spin_lock_irq(&epfile->ffs->eps_lock);
1300 
1301 	/* In the meantime, endpoint got disabled or changed. */
1302 	if (epfile->ep != ep) {
1303 		spin_unlock_irq(&epfile->ffs->eps_lock);
1304 		return -ESHUTDOWN;
1305 	}
1306 
1307 	switch (code) {
1308 	case FUNCTIONFS_FIFO_STATUS:
1309 		ret = usb_ep_fifo_status(epfile->ep->ep);
1310 		break;
1311 	case FUNCTIONFS_FIFO_FLUSH:
1312 		usb_ep_fifo_flush(epfile->ep->ep);
1313 		ret = 0;
1314 		break;
1315 	case FUNCTIONFS_CLEAR_HALT:
1316 		ret = usb_ep_clear_halt(epfile->ep->ep);
1317 		break;
1318 	case FUNCTIONFS_ENDPOINT_REVMAP:
1319 		ret = epfile->ep->num;
1320 		break;
1321 	case FUNCTIONFS_ENDPOINT_DESC:
1322 	{
1323 		int desc_idx;
1324 		struct usb_endpoint_descriptor desc1, *desc;
1325 
1326 		switch (epfile->ffs->gadget->speed) {
1327 		case USB_SPEED_SUPER:
1328 		case USB_SPEED_SUPER_PLUS:
1329 			desc_idx = 2;
1330 			break;
1331 		case USB_SPEED_HIGH:
1332 			desc_idx = 1;
1333 			break;
1334 		default:
1335 			desc_idx = 0;
1336 		}
1337 
1338 		desc = epfile->ep->descs[desc_idx];
1339 		memcpy(&desc1, desc, desc->bLength);
1340 
1341 		spin_unlock_irq(&epfile->ffs->eps_lock);
1342 		ret = copy_to_user((void __user *)value, &desc1, desc1.bLength);
1343 		if (ret)
1344 			ret = -EFAULT;
1345 		return ret;
1346 	}
1347 	default:
1348 		ret = -ENOTTY;
1349 	}
1350 	spin_unlock_irq(&epfile->ffs->eps_lock);
1351 
1352 	return ret;
1353 }
1354 
1355 static const struct file_operations ffs_epfile_operations = {
1356 	.llseek =	no_llseek,
1357 
1358 	.open =		ffs_epfile_open,
1359 	.write_iter =	ffs_epfile_write_iter,
1360 	.read_iter =	ffs_epfile_read_iter,
1361 	.release =	ffs_epfile_release,
1362 	.unlocked_ioctl =	ffs_epfile_ioctl,
1363 	.compat_ioctl = compat_ptr_ioctl,
1364 };
1365 
1366 
1367 /* File system and super block operations ***********************************/
1368 
1369 /*
1370  * Mounting the file system creates a controller file, used first for
1371  * function configuration then later for event monitoring.
1372  */
1373 
1374 static struct inode *__must_check
1375 ffs_sb_make_inode(struct super_block *sb, void *data,
1376 		  const struct file_operations *fops,
1377 		  const struct inode_operations *iops,
1378 		  struct ffs_file_perms *perms)
1379 {
1380 	struct inode *inode;
1381 
1382 	inode = new_inode(sb);
1383 
1384 	if (inode) {
1385 		struct timespec64 ts = inode_set_ctime_current(inode);
1386 
1387 		inode->i_ino	 = get_next_ino();
1388 		inode->i_mode    = perms->mode;
1389 		inode->i_uid     = perms->uid;
1390 		inode->i_gid     = perms->gid;
1391 		inode->i_atime   = ts;
1392 		inode->i_mtime   = ts;
1393 		inode->i_private = data;
1394 		if (fops)
1395 			inode->i_fop = fops;
1396 		if (iops)
1397 			inode->i_op  = iops;
1398 	}
1399 
1400 	return inode;
1401 }
1402 
1403 /* Create "regular" file */
1404 static struct dentry *ffs_sb_create_file(struct super_block *sb,
1405 					const char *name, void *data,
1406 					const struct file_operations *fops)
1407 {
1408 	struct ffs_data	*ffs = sb->s_fs_info;
1409 	struct dentry	*dentry;
1410 	struct inode	*inode;
1411 
1412 	dentry = d_alloc_name(sb->s_root, name);
1413 	if (!dentry)
1414 		return NULL;
1415 
1416 	inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1417 	if (!inode) {
1418 		dput(dentry);
1419 		return NULL;
1420 	}
1421 
1422 	d_add(dentry, inode);
1423 	return dentry;
1424 }
1425 
1426 /* Super block */
1427 static const struct super_operations ffs_sb_operations = {
1428 	.statfs =	simple_statfs,
1429 	.drop_inode =	generic_delete_inode,
1430 };
1431 
1432 struct ffs_sb_fill_data {
1433 	struct ffs_file_perms perms;
1434 	umode_t root_mode;
1435 	const char *dev_name;
1436 	bool no_disconnect;
1437 	struct ffs_data *ffs_data;
1438 };
1439 
1440 static int ffs_sb_fill(struct super_block *sb, struct fs_context *fc)
1441 {
1442 	struct ffs_sb_fill_data *data = fc->fs_private;
1443 	struct inode	*inode;
1444 	struct ffs_data	*ffs = data->ffs_data;
1445 
1446 	ffs->sb              = sb;
1447 	data->ffs_data       = NULL;
1448 	sb->s_fs_info        = ffs;
1449 	sb->s_blocksize      = PAGE_SIZE;
1450 	sb->s_blocksize_bits = PAGE_SHIFT;
1451 	sb->s_magic          = FUNCTIONFS_MAGIC;
1452 	sb->s_op             = &ffs_sb_operations;
1453 	sb->s_time_gran      = 1;
1454 
1455 	/* Root inode */
1456 	data->perms.mode = data->root_mode;
1457 	inode = ffs_sb_make_inode(sb, NULL,
1458 				  &simple_dir_operations,
1459 				  &simple_dir_inode_operations,
1460 				  &data->perms);
1461 	sb->s_root = d_make_root(inode);
1462 	if (!sb->s_root)
1463 		return -ENOMEM;
1464 
1465 	/* EP0 file */
1466 	if (!ffs_sb_create_file(sb, "ep0", ffs, &ffs_ep0_operations))
1467 		return -ENOMEM;
1468 
1469 	return 0;
1470 }
1471 
1472 enum {
1473 	Opt_no_disconnect,
1474 	Opt_rmode,
1475 	Opt_fmode,
1476 	Opt_mode,
1477 	Opt_uid,
1478 	Opt_gid,
1479 };
1480 
1481 static const struct fs_parameter_spec ffs_fs_fs_parameters[] = {
1482 	fsparam_bool	("no_disconnect",	Opt_no_disconnect),
1483 	fsparam_u32	("rmode",		Opt_rmode),
1484 	fsparam_u32	("fmode",		Opt_fmode),
1485 	fsparam_u32	("mode",		Opt_mode),
1486 	fsparam_u32	("uid",			Opt_uid),
1487 	fsparam_u32	("gid",			Opt_gid),
1488 	{}
1489 };
1490 
1491 static int ffs_fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
1492 {
1493 	struct ffs_sb_fill_data *data = fc->fs_private;
1494 	struct fs_parse_result result;
1495 	int opt;
1496 
1497 	opt = fs_parse(fc, ffs_fs_fs_parameters, param, &result);
1498 	if (opt < 0)
1499 		return opt;
1500 
1501 	switch (opt) {
1502 	case Opt_no_disconnect:
1503 		data->no_disconnect = result.boolean;
1504 		break;
1505 	case Opt_rmode:
1506 		data->root_mode  = (result.uint_32 & 0555) | S_IFDIR;
1507 		break;
1508 	case Opt_fmode:
1509 		data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1510 		break;
1511 	case Opt_mode:
1512 		data->root_mode  = (result.uint_32 & 0555) | S_IFDIR;
1513 		data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1514 		break;
1515 
1516 	case Opt_uid:
1517 		data->perms.uid = make_kuid(current_user_ns(), result.uint_32);
1518 		if (!uid_valid(data->perms.uid))
1519 			goto unmapped_value;
1520 		break;
1521 	case Opt_gid:
1522 		data->perms.gid = make_kgid(current_user_ns(), result.uint_32);
1523 		if (!gid_valid(data->perms.gid))
1524 			goto unmapped_value;
1525 		break;
1526 
1527 	default:
1528 		return -ENOPARAM;
1529 	}
1530 
1531 	return 0;
1532 
1533 unmapped_value:
1534 	return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32);
1535 }
1536 
1537 /*
1538  * Set up the superblock for a mount.
1539  */
1540 static int ffs_fs_get_tree(struct fs_context *fc)
1541 {
1542 	struct ffs_sb_fill_data *ctx = fc->fs_private;
1543 	struct ffs_data	*ffs;
1544 	int ret;
1545 
1546 	if (!fc->source)
1547 		return invalf(fc, "No source specified");
1548 
1549 	ffs = ffs_data_new(fc->source);
1550 	if (!ffs)
1551 		return -ENOMEM;
1552 	ffs->file_perms = ctx->perms;
1553 	ffs->no_disconnect = ctx->no_disconnect;
1554 
1555 	ffs->dev_name = kstrdup(fc->source, GFP_KERNEL);
1556 	if (!ffs->dev_name) {
1557 		ffs_data_put(ffs);
1558 		return -ENOMEM;
1559 	}
1560 
1561 	ret = ffs_acquire_dev(ffs->dev_name, ffs);
1562 	if (ret) {
1563 		ffs_data_put(ffs);
1564 		return ret;
1565 	}
1566 
1567 	ctx->ffs_data = ffs;
1568 	return get_tree_nodev(fc, ffs_sb_fill);
1569 }
1570 
1571 static void ffs_fs_free_fc(struct fs_context *fc)
1572 {
1573 	struct ffs_sb_fill_data *ctx = fc->fs_private;
1574 
1575 	if (ctx) {
1576 		if (ctx->ffs_data) {
1577 			ffs_data_put(ctx->ffs_data);
1578 		}
1579 
1580 		kfree(ctx);
1581 	}
1582 }
1583 
1584 static const struct fs_context_operations ffs_fs_context_ops = {
1585 	.free		= ffs_fs_free_fc,
1586 	.parse_param	= ffs_fs_parse_param,
1587 	.get_tree	= ffs_fs_get_tree,
1588 };
1589 
1590 static int ffs_fs_init_fs_context(struct fs_context *fc)
1591 {
1592 	struct ffs_sb_fill_data *ctx;
1593 
1594 	ctx = kzalloc(sizeof(struct ffs_sb_fill_data), GFP_KERNEL);
1595 	if (!ctx)
1596 		return -ENOMEM;
1597 
1598 	ctx->perms.mode = S_IFREG | 0600;
1599 	ctx->perms.uid = GLOBAL_ROOT_UID;
1600 	ctx->perms.gid = GLOBAL_ROOT_GID;
1601 	ctx->root_mode = S_IFDIR | 0500;
1602 	ctx->no_disconnect = false;
1603 
1604 	fc->fs_private = ctx;
1605 	fc->ops = &ffs_fs_context_ops;
1606 	return 0;
1607 }
1608 
1609 static void
1610 ffs_fs_kill_sb(struct super_block *sb)
1611 {
1612 	kill_litter_super(sb);
1613 	if (sb->s_fs_info)
1614 		ffs_data_closed(sb->s_fs_info);
1615 }
1616 
1617 static struct file_system_type ffs_fs_type = {
1618 	.owner		= THIS_MODULE,
1619 	.name		= "functionfs",
1620 	.init_fs_context = ffs_fs_init_fs_context,
1621 	.parameters	= ffs_fs_fs_parameters,
1622 	.kill_sb	= ffs_fs_kill_sb,
1623 };
1624 MODULE_ALIAS_FS("functionfs");
1625 
1626 
1627 /* Driver's main init/cleanup functions *************************************/
1628 
1629 static int functionfs_init(void)
1630 {
1631 	int ret;
1632 
1633 	ret = register_filesystem(&ffs_fs_type);
1634 	if (!ret)
1635 		pr_info("file system registered\n");
1636 	else
1637 		pr_err("failed registering file system (%d)\n", ret);
1638 
1639 	return ret;
1640 }
1641 
1642 static void functionfs_cleanup(void)
1643 {
1644 	pr_info("unloading\n");
1645 	unregister_filesystem(&ffs_fs_type);
1646 }
1647 
1648 
1649 /* ffs_data and ffs_function construction and destruction code **************/
1650 
1651 static void ffs_data_clear(struct ffs_data *ffs);
1652 static void ffs_data_reset(struct ffs_data *ffs);
1653 
1654 static void ffs_data_get(struct ffs_data *ffs)
1655 {
1656 	refcount_inc(&ffs->ref);
1657 }
1658 
1659 static void ffs_data_opened(struct ffs_data *ffs)
1660 {
1661 	refcount_inc(&ffs->ref);
1662 	if (atomic_add_return(1, &ffs->opened) == 1 &&
1663 			ffs->state == FFS_DEACTIVATED) {
1664 		ffs->state = FFS_CLOSING;
1665 		ffs_data_reset(ffs);
1666 	}
1667 }
1668 
1669 static void ffs_data_put(struct ffs_data *ffs)
1670 {
1671 	if (refcount_dec_and_test(&ffs->ref)) {
1672 		pr_info("%s(): freeing\n", __func__);
1673 		ffs_data_clear(ffs);
1674 		ffs_release_dev(ffs->private_data);
1675 		BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1676 		       swait_active(&ffs->ep0req_completion.wait) ||
1677 		       waitqueue_active(&ffs->wait));
1678 		destroy_workqueue(ffs->io_completion_wq);
1679 		kfree(ffs->dev_name);
1680 		kfree(ffs);
1681 	}
1682 }
1683 
1684 static void ffs_data_closed(struct ffs_data *ffs)
1685 {
1686 	struct ffs_epfile *epfiles;
1687 	unsigned long flags;
1688 
1689 	if (atomic_dec_and_test(&ffs->opened)) {
1690 		if (ffs->no_disconnect) {
1691 			ffs->state = FFS_DEACTIVATED;
1692 			spin_lock_irqsave(&ffs->eps_lock, flags);
1693 			epfiles = ffs->epfiles;
1694 			ffs->epfiles = NULL;
1695 			spin_unlock_irqrestore(&ffs->eps_lock,
1696 							flags);
1697 
1698 			if (epfiles)
1699 				ffs_epfiles_destroy(epfiles,
1700 						 ffs->eps_count);
1701 
1702 			if (ffs->setup_state == FFS_SETUP_PENDING)
1703 				__ffs_ep0_stall(ffs);
1704 		} else {
1705 			ffs->state = FFS_CLOSING;
1706 			ffs_data_reset(ffs);
1707 		}
1708 	}
1709 	if (atomic_read(&ffs->opened) < 0) {
1710 		ffs->state = FFS_CLOSING;
1711 		ffs_data_reset(ffs);
1712 	}
1713 
1714 	ffs_data_put(ffs);
1715 }
1716 
1717 static struct ffs_data *ffs_data_new(const char *dev_name)
1718 {
1719 	struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1720 	if (!ffs)
1721 		return NULL;
1722 
1723 	ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name);
1724 	if (!ffs->io_completion_wq) {
1725 		kfree(ffs);
1726 		return NULL;
1727 	}
1728 
1729 	refcount_set(&ffs->ref, 1);
1730 	atomic_set(&ffs->opened, 0);
1731 	ffs->state = FFS_READ_DESCRIPTORS;
1732 	mutex_init(&ffs->mutex);
1733 	spin_lock_init(&ffs->eps_lock);
1734 	init_waitqueue_head(&ffs->ev.waitq);
1735 	init_waitqueue_head(&ffs->wait);
1736 	init_completion(&ffs->ep0req_completion);
1737 
1738 	/* XXX REVISIT need to update it in some places, or do we? */
1739 	ffs->ev.can_stall = 1;
1740 
1741 	return ffs;
1742 }
1743 
1744 static void ffs_data_clear(struct ffs_data *ffs)
1745 {
1746 	struct ffs_epfile *epfiles;
1747 	unsigned long flags;
1748 
1749 	ffs_closed(ffs);
1750 
1751 	BUG_ON(ffs->gadget);
1752 
1753 	spin_lock_irqsave(&ffs->eps_lock, flags);
1754 	epfiles = ffs->epfiles;
1755 	ffs->epfiles = NULL;
1756 	spin_unlock_irqrestore(&ffs->eps_lock, flags);
1757 
1758 	/*
1759 	 * potential race possible between ffs_func_eps_disable
1760 	 * & ffs_epfile_release therefore maintaining a local
1761 	 * copy of epfile will save us from use-after-free.
1762 	 */
1763 	if (epfiles) {
1764 		ffs_epfiles_destroy(epfiles, ffs->eps_count);
1765 		ffs->epfiles = NULL;
1766 	}
1767 
1768 	if (ffs->ffs_eventfd) {
1769 		eventfd_ctx_put(ffs->ffs_eventfd);
1770 		ffs->ffs_eventfd = NULL;
1771 	}
1772 
1773 	kfree(ffs->raw_descs_data);
1774 	kfree(ffs->raw_strings);
1775 	kfree(ffs->stringtabs);
1776 }
1777 
1778 static void ffs_data_reset(struct ffs_data *ffs)
1779 {
1780 	ffs_data_clear(ffs);
1781 
1782 	ffs->raw_descs_data = NULL;
1783 	ffs->raw_descs = NULL;
1784 	ffs->raw_strings = NULL;
1785 	ffs->stringtabs = NULL;
1786 
1787 	ffs->raw_descs_length = 0;
1788 	ffs->fs_descs_count = 0;
1789 	ffs->hs_descs_count = 0;
1790 	ffs->ss_descs_count = 0;
1791 
1792 	ffs->strings_count = 0;
1793 	ffs->interfaces_count = 0;
1794 	ffs->eps_count = 0;
1795 
1796 	ffs->ev.count = 0;
1797 
1798 	ffs->state = FFS_READ_DESCRIPTORS;
1799 	ffs->setup_state = FFS_NO_SETUP;
1800 	ffs->flags = 0;
1801 
1802 	ffs->ms_os_descs_ext_prop_count = 0;
1803 	ffs->ms_os_descs_ext_prop_name_len = 0;
1804 	ffs->ms_os_descs_ext_prop_data_len = 0;
1805 }
1806 
1807 
1808 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1809 {
1810 	struct usb_gadget_strings **lang;
1811 	int first_id;
1812 
1813 	if (WARN_ON(ffs->state != FFS_ACTIVE
1814 		 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1815 		return -EBADFD;
1816 
1817 	first_id = usb_string_ids_n(cdev, ffs->strings_count);
1818 	if (first_id < 0)
1819 		return first_id;
1820 
1821 	ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1822 	if (!ffs->ep0req)
1823 		return -ENOMEM;
1824 	ffs->ep0req->complete = ffs_ep0_complete;
1825 	ffs->ep0req->context = ffs;
1826 
1827 	lang = ffs->stringtabs;
1828 	if (lang) {
1829 		for (; *lang; ++lang) {
1830 			struct usb_string *str = (*lang)->strings;
1831 			int id = first_id;
1832 			for (; str->s; ++id, ++str)
1833 				str->id = id;
1834 		}
1835 	}
1836 
1837 	ffs->gadget = cdev->gadget;
1838 	ffs_data_get(ffs);
1839 	return 0;
1840 }
1841 
1842 static void functionfs_unbind(struct ffs_data *ffs)
1843 {
1844 	if (!WARN_ON(!ffs->gadget)) {
1845 		/* dequeue before freeing ep0req */
1846 		usb_ep_dequeue(ffs->gadget->ep0, ffs->ep0req);
1847 		mutex_lock(&ffs->mutex);
1848 		usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1849 		ffs->ep0req = NULL;
1850 		ffs->gadget = NULL;
1851 		clear_bit(FFS_FL_BOUND, &ffs->flags);
1852 		mutex_unlock(&ffs->mutex);
1853 		ffs_data_put(ffs);
1854 	}
1855 }
1856 
1857 static int ffs_epfiles_create(struct ffs_data *ffs)
1858 {
1859 	struct ffs_epfile *epfile, *epfiles;
1860 	unsigned i, count;
1861 
1862 	count = ffs->eps_count;
1863 	epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1864 	if (!epfiles)
1865 		return -ENOMEM;
1866 
1867 	epfile = epfiles;
1868 	for (i = 1; i <= count; ++i, ++epfile) {
1869 		epfile->ffs = ffs;
1870 		mutex_init(&epfile->mutex);
1871 		if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
1872 			sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
1873 		else
1874 			sprintf(epfile->name, "ep%u", i);
1875 		epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
1876 						 epfile,
1877 						 &ffs_epfile_operations);
1878 		if (!epfile->dentry) {
1879 			ffs_epfiles_destroy(epfiles, i - 1);
1880 			return -ENOMEM;
1881 		}
1882 	}
1883 
1884 	ffs->epfiles = epfiles;
1885 	return 0;
1886 }
1887 
1888 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1889 {
1890 	struct ffs_epfile *epfile = epfiles;
1891 
1892 	for (; count; --count, ++epfile) {
1893 		BUG_ON(mutex_is_locked(&epfile->mutex));
1894 		if (epfile->dentry) {
1895 			d_delete(epfile->dentry);
1896 			dput(epfile->dentry);
1897 			epfile->dentry = NULL;
1898 		}
1899 	}
1900 
1901 	kfree(epfiles);
1902 }
1903 
1904 static void ffs_func_eps_disable(struct ffs_function *func)
1905 {
1906 	struct ffs_ep *ep;
1907 	struct ffs_epfile *epfile;
1908 	unsigned short count;
1909 	unsigned long flags;
1910 
1911 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
1912 	count = func->ffs->eps_count;
1913 	epfile = func->ffs->epfiles;
1914 	ep = func->eps;
1915 	while (count--) {
1916 		/* pending requests get nuked */
1917 		if (ep->ep)
1918 			usb_ep_disable(ep->ep);
1919 		++ep;
1920 
1921 		if (epfile) {
1922 			epfile->ep = NULL;
1923 			__ffs_epfile_read_buffer_free(epfile);
1924 			++epfile;
1925 		}
1926 	}
1927 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1928 }
1929 
1930 static int ffs_func_eps_enable(struct ffs_function *func)
1931 {
1932 	struct ffs_data *ffs;
1933 	struct ffs_ep *ep;
1934 	struct ffs_epfile *epfile;
1935 	unsigned short count;
1936 	unsigned long flags;
1937 	int ret = 0;
1938 
1939 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
1940 	ffs = func->ffs;
1941 	ep = func->eps;
1942 	epfile = ffs->epfiles;
1943 	count = ffs->eps_count;
1944 	while(count--) {
1945 		ep->ep->driver_data = ep;
1946 
1947 		ret = config_ep_by_speed(func->gadget, &func->function, ep->ep);
1948 		if (ret) {
1949 			pr_err("%s: config_ep_by_speed(%s) returned %d\n",
1950 					__func__, ep->ep->name, ret);
1951 			break;
1952 		}
1953 
1954 		ret = usb_ep_enable(ep->ep);
1955 		if (!ret) {
1956 			epfile->ep = ep;
1957 			epfile->in = usb_endpoint_dir_in(ep->ep->desc);
1958 			epfile->isoc = usb_endpoint_xfer_isoc(ep->ep->desc);
1959 		} else {
1960 			break;
1961 		}
1962 
1963 		++ep;
1964 		++epfile;
1965 	}
1966 
1967 	wake_up_interruptible(&ffs->wait);
1968 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1969 
1970 	return ret;
1971 }
1972 
1973 
1974 /* Parsing and building descriptors and strings *****************************/
1975 
1976 /*
1977  * This validates if data pointed by data is a valid USB descriptor as
1978  * well as record how many interfaces, endpoints and strings are
1979  * required by given configuration.  Returns address after the
1980  * descriptor or NULL if data is invalid.
1981  */
1982 
1983 enum ffs_entity_type {
1984 	FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1985 };
1986 
1987 enum ffs_os_desc_type {
1988 	FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
1989 };
1990 
1991 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1992 				   u8 *valuep,
1993 				   struct usb_descriptor_header *desc,
1994 				   void *priv);
1995 
1996 typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
1997 				    struct usb_os_desc_header *h, void *data,
1998 				    unsigned len, void *priv);
1999 
2000 static int __must_check ffs_do_single_desc(char *data, unsigned len,
2001 					   ffs_entity_callback entity,
2002 					   void *priv, int *current_class)
2003 {
2004 	struct usb_descriptor_header *_ds = (void *)data;
2005 	u8 length;
2006 	int ret;
2007 
2008 	/* At least two bytes are required: length and type */
2009 	if (len < 2) {
2010 		pr_vdebug("descriptor too short\n");
2011 		return -EINVAL;
2012 	}
2013 
2014 	/* If we have at least as many bytes as the descriptor takes? */
2015 	length = _ds->bLength;
2016 	if (len < length) {
2017 		pr_vdebug("descriptor longer then available data\n");
2018 		return -EINVAL;
2019 	}
2020 
2021 #define __entity_check_INTERFACE(val)  1
2022 #define __entity_check_STRING(val)     (val)
2023 #define __entity_check_ENDPOINT(val)   ((val) & USB_ENDPOINT_NUMBER_MASK)
2024 #define __entity(type, val) do {					\
2025 		pr_vdebug("entity " #type "(%02x)\n", (val));		\
2026 		if (!__entity_check_ ##type(val)) {			\
2027 			pr_vdebug("invalid entity's value\n");		\
2028 			return -EINVAL;					\
2029 		}							\
2030 		ret = entity(FFS_ ##type, &val, _ds, priv);		\
2031 		if (ret < 0) {						\
2032 			pr_debug("entity " #type "(%02x); ret = %d\n",	\
2033 				 (val), ret);				\
2034 			return ret;					\
2035 		}							\
2036 	} while (0)
2037 
2038 	/* Parse descriptor depending on type. */
2039 	switch (_ds->bDescriptorType) {
2040 	case USB_DT_DEVICE:
2041 	case USB_DT_CONFIG:
2042 	case USB_DT_STRING:
2043 	case USB_DT_DEVICE_QUALIFIER:
2044 		/* function can't have any of those */
2045 		pr_vdebug("descriptor reserved for gadget: %d\n",
2046 		      _ds->bDescriptorType);
2047 		return -EINVAL;
2048 
2049 	case USB_DT_INTERFACE: {
2050 		struct usb_interface_descriptor *ds = (void *)_ds;
2051 		pr_vdebug("interface descriptor\n");
2052 		if (length != sizeof *ds)
2053 			goto inv_length;
2054 
2055 		__entity(INTERFACE, ds->bInterfaceNumber);
2056 		if (ds->iInterface)
2057 			__entity(STRING, ds->iInterface);
2058 		*current_class = ds->bInterfaceClass;
2059 	}
2060 		break;
2061 
2062 	case USB_DT_ENDPOINT: {
2063 		struct usb_endpoint_descriptor *ds = (void *)_ds;
2064 		pr_vdebug("endpoint descriptor\n");
2065 		if (length != USB_DT_ENDPOINT_SIZE &&
2066 		    length != USB_DT_ENDPOINT_AUDIO_SIZE)
2067 			goto inv_length;
2068 		__entity(ENDPOINT, ds->bEndpointAddress);
2069 	}
2070 		break;
2071 
2072 	case USB_TYPE_CLASS | 0x01:
2073 		if (*current_class == USB_INTERFACE_CLASS_HID) {
2074 			pr_vdebug("hid descriptor\n");
2075 			if (length != sizeof(struct hid_descriptor))
2076 				goto inv_length;
2077 			break;
2078 		} else if (*current_class == USB_INTERFACE_CLASS_CCID) {
2079 			pr_vdebug("ccid descriptor\n");
2080 			if (length != sizeof(struct ccid_descriptor))
2081 				goto inv_length;
2082 			break;
2083 		} else {
2084 			pr_vdebug("unknown descriptor: %d for class %d\n",
2085 			      _ds->bDescriptorType, *current_class);
2086 			return -EINVAL;
2087 		}
2088 
2089 	case USB_DT_OTG:
2090 		if (length != sizeof(struct usb_otg_descriptor))
2091 			goto inv_length;
2092 		break;
2093 
2094 	case USB_DT_INTERFACE_ASSOCIATION: {
2095 		struct usb_interface_assoc_descriptor *ds = (void *)_ds;
2096 		pr_vdebug("interface association descriptor\n");
2097 		if (length != sizeof *ds)
2098 			goto inv_length;
2099 		if (ds->iFunction)
2100 			__entity(STRING, ds->iFunction);
2101 	}
2102 		break;
2103 
2104 	case USB_DT_SS_ENDPOINT_COMP:
2105 		pr_vdebug("EP SS companion descriptor\n");
2106 		if (length != sizeof(struct usb_ss_ep_comp_descriptor))
2107 			goto inv_length;
2108 		break;
2109 
2110 	case USB_DT_OTHER_SPEED_CONFIG:
2111 	case USB_DT_INTERFACE_POWER:
2112 	case USB_DT_DEBUG:
2113 	case USB_DT_SECURITY:
2114 	case USB_DT_CS_RADIO_CONTROL:
2115 		/* TODO */
2116 		pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
2117 		return -EINVAL;
2118 
2119 	default:
2120 		/* We should never be here */
2121 		pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
2122 		return -EINVAL;
2123 
2124 inv_length:
2125 		pr_vdebug("invalid length: %d (descriptor %d)\n",
2126 			  _ds->bLength, _ds->bDescriptorType);
2127 		return -EINVAL;
2128 	}
2129 
2130 #undef __entity
2131 #undef __entity_check_DESCRIPTOR
2132 #undef __entity_check_INTERFACE
2133 #undef __entity_check_STRING
2134 #undef __entity_check_ENDPOINT
2135 
2136 	return length;
2137 }
2138 
2139 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
2140 				     ffs_entity_callback entity, void *priv)
2141 {
2142 	const unsigned _len = len;
2143 	unsigned long num = 0;
2144 	int current_class = -1;
2145 
2146 	for (;;) {
2147 		int ret;
2148 
2149 		if (num == count)
2150 			data = NULL;
2151 
2152 		/* Record "descriptor" entity */
2153 		ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
2154 		if (ret < 0) {
2155 			pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
2156 				 num, ret);
2157 			return ret;
2158 		}
2159 
2160 		if (!data)
2161 			return _len - len;
2162 
2163 		ret = ffs_do_single_desc(data, len, entity, priv,
2164 			&current_class);
2165 		if (ret < 0) {
2166 			pr_debug("%s returns %d\n", __func__, ret);
2167 			return ret;
2168 		}
2169 
2170 		len -= ret;
2171 		data += ret;
2172 		++num;
2173 	}
2174 }
2175 
2176 static int __ffs_data_do_entity(enum ffs_entity_type type,
2177 				u8 *valuep, struct usb_descriptor_header *desc,
2178 				void *priv)
2179 {
2180 	struct ffs_desc_helper *helper = priv;
2181 	struct usb_endpoint_descriptor *d;
2182 
2183 	switch (type) {
2184 	case FFS_DESCRIPTOR:
2185 		break;
2186 
2187 	case FFS_INTERFACE:
2188 		/*
2189 		 * Interfaces are indexed from zero so if we
2190 		 * encountered interface "n" then there are at least
2191 		 * "n+1" interfaces.
2192 		 */
2193 		if (*valuep >= helper->interfaces_count)
2194 			helper->interfaces_count = *valuep + 1;
2195 		break;
2196 
2197 	case FFS_STRING:
2198 		/*
2199 		 * Strings are indexed from 1 (0 is reserved
2200 		 * for languages list)
2201 		 */
2202 		if (*valuep > helper->ffs->strings_count)
2203 			helper->ffs->strings_count = *valuep;
2204 		break;
2205 
2206 	case FFS_ENDPOINT:
2207 		d = (void *)desc;
2208 		helper->eps_count++;
2209 		if (helper->eps_count >= FFS_MAX_EPS_COUNT)
2210 			return -EINVAL;
2211 		/* Check if descriptors for any speed were already parsed */
2212 		if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
2213 			helper->ffs->eps_addrmap[helper->eps_count] =
2214 				d->bEndpointAddress;
2215 		else if (helper->ffs->eps_addrmap[helper->eps_count] !=
2216 				d->bEndpointAddress)
2217 			return -EINVAL;
2218 		break;
2219 	}
2220 
2221 	return 0;
2222 }
2223 
2224 static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
2225 				   struct usb_os_desc_header *desc)
2226 {
2227 	u16 bcd_version = le16_to_cpu(desc->bcdVersion);
2228 	u16 w_index = le16_to_cpu(desc->wIndex);
2229 
2230 	if (bcd_version == 0x1) {
2231 		pr_warn("bcdVersion must be 0x0100, stored in Little Endian order. "
2232 			"Userspace driver should be fixed, accepting 0x0001 for compatibility.\n");
2233 	} else if (bcd_version != 0x100) {
2234 		pr_vdebug("unsupported os descriptors version: 0x%x\n",
2235 			  bcd_version);
2236 		return -EINVAL;
2237 	}
2238 	switch (w_index) {
2239 	case 0x4:
2240 		*next_type = FFS_OS_DESC_EXT_COMPAT;
2241 		break;
2242 	case 0x5:
2243 		*next_type = FFS_OS_DESC_EXT_PROP;
2244 		break;
2245 	default:
2246 		pr_vdebug("unsupported os descriptor type: %d", w_index);
2247 		return -EINVAL;
2248 	}
2249 
2250 	return sizeof(*desc);
2251 }
2252 
2253 /*
2254  * Process all extended compatibility/extended property descriptors
2255  * of a feature descriptor
2256  */
2257 static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
2258 					      enum ffs_os_desc_type type,
2259 					      u16 feature_count,
2260 					      ffs_os_desc_callback entity,
2261 					      void *priv,
2262 					      struct usb_os_desc_header *h)
2263 {
2264 	int ret;
2265 	const unsigned _len = len;
2266 
2267 	/* loop over all ext compat/ext prop descriptors */
2268 	while (feature_count--) {
2269 		ret = entity(type, h, data, len, priv);
2270 		if (ret < 0) {
2271 			pr_debug("bad OS descriptor, type: %d\n", type);
2272 			return ret;
2273 		}
2274 		data += ret;
2275 		len -= ret;
2276 	}
2277 	return _len - len;
2278 }
2279 
2280 /* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2281 static int __must_check ffs_do_os_descs(unsigned count,
2282 					char *data, unsigned len,
2283 					ffs_os_desc_callback entity, void *priv)
2284 {
2285 	const unsigned _len = len;
2286 	unsigned long num = 0;
2287 
2288 	for (num = 0; num < count; ++num) {
2289 		int ret;
2290 		enum ffs_os_desc_type type;
2291 		u16 feature_count;
2292 		struct usb_os_desc_header *desc = (void *)data;
2293 
2294 		if (len < sizeof(*desc))
2295 			return -EINVAL;
2296 
2297 		/*
2298 		 * Record "descriptor" entity.
2299 		 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2300 		 * Move the data pointer to the beginning of extended
2301 		 * compatibilities proper or extended properties proper
2302 		 * portions of the data
2303 		 */
2304 		if (le32_to_cpu(desc->dwLength) > len)
2305 			return -EINVAL;
2306 
2307 		ret = __ffs_do_os_desc_header(&type, desc);
2308 		if (ret < 0) {
2309 			pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2310 				 num, ret);
2311 			return ret;
2312 		}
2313 		/*
2314 		 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2315 		 */
2316 		feature_count = le16_to_cpu(desc->wCount);
2317 		if (type == FFS_OS_DESC_EXT_COMPAT &&
2318 		    (feature_count > 255 || desc->Reserved))
2319 				return -EINVAL;
2320 		len -= ret;
2321 		data += ret;
2322 
2323 		/*
2324 		 * Process all function/property descriptors
2325 		 * of this Feature Descriptor
2326 		 */
2327 		ret = ffs_do_single_os_desc(data, len, type,
2328 					    feature_count, entity, priv, desc);
2329 		if (ret < 0) {
2330 			pr_debug("%s returns %d\n", __func__, ret);
2331 			return ret;
2332 		}
2333 
2334 		len -= ret;
2335 		data += ret;
2336 	}
2337 	return _len - len;
2338 }
2339 
2340 /*
2341  * Validate contents of the buffer from userspace related to OS descriptors.
2342  */
2343 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2344 				 struct usb_os_desc_header *h, void *data,
2345 				 unsigned len, void *priv)
2346 {
2347 	struct ffs_data *ffs = priv;
2348 	u8 length;
2349 
2350 	switch (type) {
2351 	case FFS_OS_DESC_EXT_COMPAT: {
2352 		struct usb_ext_compat_desc *d = data;
2353 		int i;
2354 
2355 		if (len < sizeof(*d) ||
2356 		    d->bFirstInterfaceNumber >= ffs->interfaces_count)
2357 			return -EINVAL;
2358 		if (d->Reserved1 != 1) {
2359 			/*
2360 			 * According to the spec, Reserved1 must be set to 1
2361 			 * but older kernels incorrectly rejected non-zero
2362 			 * values.  We fix it here to avoid returning EINVAL
2363 			 * in response to values we used to accept.
2364 			 */
2365 			pr_debug("usb_ext_compat_desc::Reserved1 forced to 1\n");
2366 			d->Reserved1 = 1;
2367 		}
2368 		for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2369 			if (d->Reserved2[i])
2370 				return -EINVAL;
2371 
2372 		length = sizeof(struct usb_ext_compat_desc);
2373 	}
2374 		break;
2375 	case FFS_OS_DESC_EXT_PROP: {
2376 		struct usb_ext_prop_desc *d = data;
2377 		u32 type, pdl;
2378 		u16 pnl;
2379 
2380 		if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2381 			return -EINVAL;
2382 		length = le32_to_cpu(d->dwSize);
2383 		if (len < length)
2384 			return -EINVAL;
2385 		type = le32_to_cpu(d->dwPropertyDataType);
2386 		if (type < USB_EXT_PROP_UNICODE ||
2387 		    type > USB_EXT_PROP_UNICODE_MULTI) {
2388 			pr_vdebug("unsupported os descriptor property type: %d",
2389 				  type);
2390 			return -EINVAL;
2391 		}
2392 		pnl = le16_to_cpu(d->wPropertyNameLength);
2393 		if (length < 14 + pnl) {
2394 			pr_vdebug("invalid os descriptor length: %d pnl:%d (descriptor %d)\n",
2395 				  length, pnl, type);
2396 			return -EINVAL;
2397 		}
2398 		pdl = le32_to_cpu(*(__le32 *)((u8 *)data + 10 + pnl));
2399 		if (length != 14 + pnl + pdl) {
2400 			pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2401 				  length, pnl, pdl, type);
2402 			return -EINVAL;
2403 		}
2404 		++ffs->ms_os_descs_ext_prop_count;
2405 		/* property name reported to the host as "WCHAR"s */
2406 		ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2407 		ffs->ms_os_descs_ext_prop_data_len += pdl;
2408 	}
2409 		break;
2410 	default:
2411 		pr_vdebug("unknown descriptor: %d\n", type);
2412 		return -EINVAL;
2413 	}
2414 	return length;
2415 }
2416 
2417 static int __ffs_data_got_descs(struct ffs_data *ffs,
2418 				char *const _data, size_t len)
2419 {
2420 	char *data = _data, *raw_descs;
2421 	unsigned os_descs_count = 0, counts[3], flags;
2422 	int ret = -EINVAL, i;
2423 	struct ffs_desc_helper helper;
2424 
2425 	if (get_unaligned_le32(data + 4) != len)
2426 		goto error;
2427 
2428 	switch (get_unaligned_le32(data)) {
2429 	case FUNCTIONFS_DESCRIPTORS_MAGIC:
2430 		flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
2431 		data += 8;
2432 		len  -= 8;
2433 		break;
2434 	case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2435 		flags = get_unaligned_le32(data + 8);
2436 		ffs->user_flags = flags;
2437 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2438 			      FUNCTIONFS_HAS_HS_DESC |
2439 			      FUNCTIONFS_HAS_SS_DESC |
2440 			      FUNCTIONFS_HAS_MS_OS_DESC |
2441 			      FUNCTIONFS_VIRTUAL_ADDR |
2442 			      FUNCTIONFS_EVENTFD |
2443 			      FUNCTIONFS_ALL_CTRL_RECIP |
2444 			      FUNCTIONFS_CONFIG0_SETUP)) {
2445 			ret = -ENOSYS;
2446 			goto error;
2447 		}
2448 		data += 12;
2449 		len  -= 12;
2450 		break;
2451 	default:
2452 		goto error;
2453 	}
2454 
2455 	if (flags & FUNCTIONFS_EVENTFD) {
2456 		if (len < 4)
2457 			goto error;
2458 		ffs->ffs_eventfd =
2459 			eventfd_ctx_fdget((int)get_unaligned_le32(data));
2460 		if (IS_ERR(ffs->ffs_eventfd)) {
2461 			ret = PTR_ERR(ffs->ffs_eventfd);
2462 			ffs->ffs_eventfd = NULL;
2463 			goto error;
2464 		}
2465 		data += 4;
2466 		len  -= 4;
2467 	}
2468 
2469 	/* Read fs_count, hs_count and ss_count (if present) */
2470 	for (i = 0; i < 3; ++i) {
2471 		if (!(flags & (1 << i))) {
2472 			counts[i] = 0;
2473 		} else if (len < 4) {
2474 			goto error;
2475 		} else {
2476 			counts[i] = get_unaligned_le32(data);
2477 			data += 4;
2478 			len  -= 4;
2479 		}
2480 	}
2481 	if (flags & (1 << i)) {
2482 		if (len < 4) {
2483 			goto error;
2484 		}
2485 		os_descs_count = get_unaligned_le32(data);
2486 		data += 4;
2487 		len -= 4;
2488 	}
2489 
2490 	/* Read descriptors */
2491 	raw_descs = data;
2492 	helper.ffs = ffs;
2493 	for (i = 0; i < 3; ++i) {
2494 		if (!counts[i])
2495 			continue;
2496 		helper.interfaces_count = 0;
2497 		helper.eps_count = 0;
2498 		ret = ffs_do_descs(counts[i], data, len,
2499 				   __ffs_data_do_entity, &helper);
2500 		if (ret < 0)
2501 			goto error;
2502 		if (!ffs->eps_count && !ffs->interfaces_count) {
2503 			ffs->eps_count = helper.eps_count;
2504 			ffs->interfaces_count = helper.interfaces_count;
2505 		} else {
2506 			if (ffs->eps_count != helper.eps_count) {
2507 				ret = -EINVAL;
2508 				goto error;
2509 			}
2510 			if (ffs->interfaces_count != helper.interfaces_count) {
2511 				ret = -EINVAL;
2512 				goto error;
2513 			}
2514 		}
2515 		data += ret;
2516 		len  -= ret;
2517 	}
2518 	if (os_descs_count) {
2519 		ret = ffs_do_os_descs(os_descs_count, data, len,
2520 				      __ffs_data_do_os_desc, ffs);
2521 		if (ret < 0)
2522 			goto error;
2523 		data += ret;
2524 		len -= ret;
2525 	}
2526 
2527 	if (raw_descs == data || len) {
2528 		ret = -EINVAL;
2529 		goto error;
2530 	}
2531 
2532 	ffs->raw_descs_data	= _data;
2533 	ffs->raw_descs		= raw_descs;
2534 	ffs->raw_descs_length	= data - raw_descs;
2535 	ffs->fs_descs_count	= counts[0];
2536 	ffs->hs_descs_count	= counts[1];
2537 	ffs->ss_descs_count	= counts[2];
2538 	ffs->ms_os_descs_count	= os_descs_count;
2539 
2540 	return 0;
2541 
2542 error:
2543 	kfree(_data);
2544 	return ret;
2545 }
2546 
2547 static int __ffs_data_got_strings(struct ffs_data *ffs,
2548 				  char *const _data, size_t len)
2549 {
2550 	u32 str_count, needed_count, lang_count;
2551 	struct usb_gadget_strings **stringtabs, *t;
2552 	const char *data = _data;
2553 	struct usb_string *s;
2554 
2555 	if (len < 16 ||
2556 	    get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2557 	    get_unaligned_le32(data + 4) != len)
2558 		goto error;
2559 	str_count  = get_unaligned_le32(data + 8);
2560 	lang_count = get_unaligned_le32(data + 12);
2561 
2562 	/* if one is zero the other must be zero */
2563 	if (!str_count != !lang_count)
2564 		goto error;
2565 
2566 	/* Do we have at least as many strings as descriptors need? */
2567 	needed_count = ffs->strings_count;
2568 	if (str_count < needed_count)
2569 		goto error;
2570 
2571 	/*
2572 	 * If we don't need any strings just return and free all
2573 	 * memory.
2574 	 */
2575 	if (!needed_count) {
2576 		kfree(_data);
2577 		return 0;
2578 	}
2579 
2580 	/* Allocate everything in one chunk so there's less maintenance. */
2581 	{
2582 		unsigned i = 0;
2583 		vla_group(d);
2584 		vla_item(d, struct usb_gadget_strings *, stringtabs,
2585 			size_add(lang_count, 1));
2586 		vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2587 		vla_item(d, struct usb_string, strings,
2588 			size_mul(lang_count, (needed_count + 1)));
2589 
2590 		char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2591 
2592 		if (!vlabuf) {
2593 			kfree(_data);
2594 			return -ENOMEM;
2595 		}
2596 
2597 		/* Initialize the VLA pointers */
2598 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
2599 		t = vla_ptr(vlabuf, d, stringtab);
2600 		i = lang_count;
2601 		do {
2602 			*stringtabs++ = t++;
2603 		} while (--i);
2604 		*stringtabs = NULL;
2605 
2606 		/* stringtabs = vlabuf = d_stringtabs for later kfree */
2607 		stringtabs = vla_ptr(vlabuf, d, stringtabs);
2608 		t = vla_ptr(vlabuf, d, stringtab);
2609 		s = vla_ptr(vlabuf, d, strings);
2610 	}
2611 
2612 	/* For each language */
2613 	data += 16;
2614 	len -= 16;
2615 
2616 	do { /* lang_count > 0 so we can use do-while */
2617 		unsigned needed = needed_count;
2618 		u32 str_per_lang = str_count;
2619 
2620 		if (len < 3)
2621 			goto error_free;
2622 		t->language = get_unaligned_le16(data);
2623 		t->strings  = s;
2624 		++t;
2625 
2626 		data += 2;
2627 		len -= 2;
2628 
2629 		/* For each string */
2630 		do { /* str_count > 0 so we can use do-while */
2631 			size_t length = strnlen(data, len);
2632 
2633 			if (length == len)
2634 				goto error_free;
2635 
2636 			/*
2637 			 * User may provide more strings then we need,
2638 			 * if that's the case we simply ignore the
2639 			 * rest
2640 			 */
2641 			if (needed) {
2642 				/*
2643 				 * s->id will be set while adding
2644 				 * function to configuration so for
2645 				 * now just leave garbage here.
2646 				 */
2647 				s->s = data;
2648 				--needed;
2649 				++s;
2650 			}
2651 
2652 			data += length + 1;
2653 			len -= length + 1;
2654 		} while (--str_per_lang);
2655 
2656 		s->id = 0;   /* terminator */
2657 		s->s = NULL;
2658 		++s;
2659 
2660 	} while (--lang_count);
2661 
2662 	/* Some garbage left? */
2663 	if (len)
2664 		goto error_free;
2665 
2666 	/* Done! */
2667 	ffs->stringtabs = stringtabs;
2668 	ffs->raw_strings = _data;
2669 
2670 	return 0;
2671 
2672 error_free:
2673 	kfree(stringtabs);
2674 error:
2675 	kfree(_data);
2676 	return -EINVAL;
2677 }
2678 
2679 
2680 /* Events handling and management *******************************************/
2681 
2682 static void __ffs_event_add(struct ffs_data *ffs,
2683 			    enum usb_functionfs_event_type type)
2684 {
2685 	enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2686 	int neg = 0;
2687 
2688 	/*
2689 	 * Abort any unhandled setup
2690 	 *
2691 	 * We do not need to worry about some cmpxchg() changing value
2692 	 * of ffs->setup_state without holding the lock because when
2693 	 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2694 	 * the source does nothing.
2695 	 */
2696 	if (ffs->setup_state == FFS_SETUP_PENDING)
2697 		ffs->setup_state = FFS_SETUP_CANCELLED;
2698 
2699 	/*
2700 	 * Logic of this function guarantees that there are at most four pending
2701 	 * evens on ffs->ev.types queue.  This is important because the queue
2702 	 * has space for four elements only and __ffs_ep0_read_events function
2703 	 * depends on that limit as well.  If more event types are added, those
2704 	 * limits have to be revisited or guaranteed to still hold.
2705 	 */
2706 	switch (type) {
2707 	case FUNCTIONFS_RESUME:
2708 		rem_type2 = FUNCTIONFS_SUSPEND;
2709 		fallthrough;
2710 	case FUNCTIONFS_SUSPEND:
2711 	case FUNCTIONFS_SETUP:
2712 		rem_type1 = type;
2713 		/* Discard all similar events */
2714 		break;
2715 
2716 	case FUNCTIONFS_BIND:
2717 	case FUNCTIONFS_UNBIND:
2718 	case FUNCTIONFS_DISABLE:
2719 	case FUNCTIONFS_ENABLE:
2720 		/* Discard everything other then power management. */
2721 		rem_type1 = FUNCTIONFS_SUSPEND;
2722 		rem_type2 = FUNCTIONFS_RESUME;
2723 		neg = 1;
2724 		break;
2725 
2726 	default:
2727 		WARN(1, "%d: unknown event, this should not happen\n", type);
2728 		return;
2729 	}
2730 
2731 	{
2732 		u8 *ev  = ffs->ev.types, *out = ev;
2733 		unsigned n = ffs->ev.count;
2734 		for (; n; --n, ++ev)
2735 			if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2736 				*out++ = *ev;
2737 			else
2738 				pr_vdebug("purging event %d\n", *ev);
2739 		ffs->ev.count = out - ffs->ev.types;
2740 	}
2741 
2742 	pr_vdebug("adding event %d\n", type);
2743 	ffs->ev.types[ffs->ev.count++] = type;
2744 	wake_up_locked(&ffs->ev.waitq);
2745 	if (ffs->ffs_eventfd)
2746 		eventfd_signal(ffs->ffs_eventfd, 1);
2747 }
2748 
2749 static void ffs_event_add(struct ffs_data *ffs,
2750 			  enum usb_functionfs_event_type type)
2751 {
2752 	unsigned long flags;
2753 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2754 	__ffs_event_add(ffs, type);
2755 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2756 }
2757 
2758 /* Bind/unbind USB function hooks *******************************************/
2759 
2760 static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2761 {
2762 	int i;
2763 
2764 	for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2765 		if (ffs->eps_addrmap[i] == endpoint_address)
2766 			return i;
2767 	return -ENOENT;
2768 }
2769 
2770 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2771 				    struct usb_descriptor_header *desc,
2772 				    void *priv)
2773 {
2774 	struct usb_endpoint_descriptor *ds = (void *)desc;
2775 	struct ffs_function *func = priv;
2776 	struct ffs_ep *ffs_ep;
2777 	unsigned ep_desc_id;
2778 	int idx;
2779 	static const char *speed_names[] = { "full", "high", "super" };
2780 
2781 	if (type != FFS_DESCRIPTOR)
2782 		return 0;
2783 
2784 	/*
2785 	 * If ss_descriptors is not NULL, we are reading super speed
2786 	 * descriptors; if hs_descriptors is not NULL, we are reading high
2787 	 * speed descriptors; otherwise, we are reading full speed
2788 	 * descriptors.
2789 	 */
2790 	if (func->function.ss_descriptors) {
2791 		ep_desc_id = 2;
2792 		func->function.ss_descriptors[(long)valuep] = desc;
2793 	} else if (func->function.hs_descriptors) {
2794 		ep_desc_id = 1;
2795 		func->function.hs_descriptors[(long)valuep] = desc;
2796 	} else {
2797 		ep_desc_id = 0;
2798 		func->function.fs_descriptors[(long)valuep]    = desc;
2799 	}
2800 
2801 	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2802 		return 0;
2803 
2804 	idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2805 	if (idx < 0)
2806 		return idx;
2807 
2808 	ffs_ep = func->eps + idx;
2809 
2810 	if (ffs_ep->descs[ep_desc_id]) {
2811 		pr_err("two %sspeed descriptors for EP %d\n",
2812 			  speed_names[ep_desc_id],
2813 			  ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2814 		return -EINVAL;
2815 	}
2816 	ffs_ep->descs[ep_desc_id] = ds;
2817 
2818 	ffs_dump_mem(": Original  ep desc", ds, ds->bLength);
2819 	if (ffs_ep->ep) {
2820 		ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2821 		if (!ds->wMaxPacketSize)
2822 			ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2823 	} else {
2824 		struct usb_request *req;
2825 		struct usb_ep *ep;
2826 		u8 bEndpointAddress;
2827 		u16 wMaxPacketSize;
2828 
2829 		/*
2830 		 * We back up bEndpointAddress because autoconfig overwrites
2831 		 * it with physical endpoint address.
2832 		 */
2833 		bEndpointAddress = ds->bEndpointAddress;
2834 		/*
2835 		 * We back up wMaxPacketSize because autoconfig treats
2836 		 * endpoint descriptors as if they were full speed.
2837 		 */
2838 		wMaxPacketSize = ds->wMaxPacketSize;
2839 		pr_vdebug("autoconfig\n");
2840 		ep = usb_ep_autoconfig(func->gadget, ds);
2841 		if (!ep)
2842 			return -ENOTSUPP;
2843 		ep->driver_data = func->eps + idx;
2844 
2845 		req = usb_ep_alloc_request(ep, GFP_KERNEL);
2846 		if (!req)
2847 			return -ENOMEM;
2848 
2849 		ffs_ep->ep  = ep;
2850 		ffs_ep->req = req;
2851 		func->eps_revmap[ds->bEndpointAddress &
2852 				 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2853 		/*
2854 		 * If we use virtual address mapping, we restore
2855 		 * original bEndpointAddress value.
2856 		 */
2857 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
2858 			ds->bEndpointAddress = bEndpointAddress;
2859 		/*
2860 		 * Restore wMaxPacketSize which was potentially
2861 		 * overwritten by autoconfig.
2862 		 */
2863 		ds->wMaxPacketSize = wMaxPacketSize;
2864 	}
2865 	ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2866 
2867 	return 0;
2868 }
2869 
2870 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2871 				   struct usb_descriptor_header *desc,
2872 				   void *priv)
2873 {
2874 	struct ffs_function *func = priv;
2875 	unsigned idx;
2876 	u8 newValue;
2877 
2878 	switch (type) {
2879 	default:
2880 	case FFS_DESCRIPTOR:
2881 		/* Handled in previous pass by __ffs_func_bind_do_descs() */
2882 		return 0;
2883 
2884 	case FFS_INTERFACE:
2885 		idx = *valuep;
2886 		if (func->interfaces_nums[idx] < 0) {
2887 			int id = usb_interface_id(func->conf, &func->function);
2888 			if (id < 0)
2889 				return id;
2890 			func->interfaces_nums[idx] = id;
2891 		}
2892 		newValue = func->interfaces_nums[idx];
2893 		break;
2894 
2895 	case FFS_STRING:
2896 		/* String' IDs are allocated when fsf_data is bound to cdev */
2897 		newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2898 		break;
2899 
2900 	case FFS_ENDPOINT:
2901 		/*
2902 		 * USB_DT_ENDPOINT are handled in
2903 		 * __ffs_func_bind_do_descs().
2904 		 */
2905 		if (desc->bDescriptorType == USB_DT_ENDPOINT)
2906 			return 0;
2907 
2908 		idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2909 		if (!func->eps[idx].ep)
2910 			return -EINVAL;
2911 
2912 		{
2913 			struct usb_endpoint_descriptor **descs;
2914 			descs = func->eps[idx].descs;
2915 			newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2916 		}
2917 		break;
2918 	}
2919 
2920 	pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2921 	*valuep = newValue;
2922 	return 0;
2923 }
2924 
2925 static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2926 				      struct usb_os_desc_header *h, void *data,
2927 				      unsigned len, void *priv)
2928 {
2929 	struct ffs_function *func = priv;
2930 	u8 length = 0;
2931 
2932 	switch (type) {
2933 	case FFS_OS_DESC_EXT_COMPAT: {
2934 		struct usb_ext_compat_desc *desc = data;
2935 		struct usb_os_desc_table *t;
2936 
2937 		t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
2938 		t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
2939 		memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
2940 		       ARRAY_SIZE(desc->CompatibleID) +
2941 		       ARRAY_SIZE(desc->SubCompatibleID));
2942 		length = sizeof(*desc);
2943 	}
2944 		break;
2945 	case FFS_OS_DESC_EXT_PROP: {
2946 		struct usb_ext_prop_desc *desc = data;
2947 		struct usb_os_desc_table *t;
2948 		struct usb_os_desc_ext_prop *ext_prop;
2949 		char *ext_prop_name;
2950 		char *ext_prop_data;
2951 
2952 		t = &func->function.os_desc_table[h->interface];
2953 		t->if_id = func->interfaces_nums[h->interface];
2954 
2955 		ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
2956 		func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
2957 
2958 		ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
2959 		ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2960 		ext_prop->data_len = le32_to_cpu(*(__le32 *)
2961 			usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
2962 		length = ext_prop->name_len + ext_prop->data_len + 14;
2963 
2964 		ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
2965 		func->ffs->ms_os_descs_ext_prop_name_avail +=
2966 			ext_prop->name_len;
2967 
2968 		ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
2969 		func->ffs->ms_os_descs_ext_prop_data_avail +=
2970 			ext_prop->data_len;
2971 		memcpy(ext_prop_data,
2972 		       usb_ext_prop_data_ptr(data, ext_prop->name_len),
2973 		       ext_prop->data_len);
2974 		/* unicode data reported to the host as "WCHAR"s */
2975 		switch (ext_prop->type) {
2976 		case USB_EXT_PROP_UNICODE:
2977 		case USB_EXT_PROP_UNICODE_ENV:
2978 		case USB_EXT_PROP_UNICODE_LINK:
2979 		case USB_EXT_PROP_UNICODE_MULTI:
2980 			ext_prop->data_len *= 2;
2981 			break;
2982 		}
2983 		ext_prop->data = ext_prop_data;
2984 
2985 		memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
2986 		       ext_prop->name_len);
2987 		/* property name reported to the host as "WCHAR"s */
2988 		ext_prop->name_len *= 2;
2989 		ext_prop->name = ext_prop_name;
2990 
2991 		t->os_desc->ext_prop_len +=
2992 			ext_prop->name_len + ext_prop->data_len + 14;
2993 		++t->os_desc->ext_prop_count;
2994 		list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
2995 	}
2996 		break;
2997 	default:
2998 		pr_vdebug("unknown descriptor: %d\n", type);
2999 	}
3000 
3001 	return length;
3002 }
3003 
3004 static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
3005 						struct usb_configuration *c)
3006 {
3007 	struct ffs_function *func = ffs_func_from_usb(f);
3008 	struct f_fs_opts *ffs_opts =
3009 		container_of(f->fi, struct f_fs_opts, func_inst);
3010 	struct ffs_data *ffs_data;
3011 	int ret;
3012 
3013 	/*
3014 	 * Legacy gadget triggers binding in functionfs_ready_callback,
3015 	 * which already uses locking; taking the same lock here would
3016 	 * cause a deadlock.
3017 	 *
3018 	 * Configfs-enabled gadgets however do need ffs_dev_lock.
3019 	 */
3020 	if (!ffs_opts->no_configfs)
3021 		ffs_dev_lock();
3022 	ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
3023 	ffs_data = ffs_opts->dev->ffs_data;
3024 	if (!ffs_opts->no_configfs)
3025 		ffs_dev_unlock();
3026 	if (ret)
3027 		return ERR_PTR(ret);
3028 
3029 	func->ffs = ffs_data;
3030 	func->conf = c;
3031 	func->gadget = c->cdev->gadget;
3032 
3033 	/*
3034 	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
3035 	 * configurations are bound in sequence with list_for_each_entry,
3036 	 * in each configuration its functions are bound in sequence
3037 	 * with list_for_each_entry, so we assume no race condition
3038 	 * with regard to ffs_opts->bound access
3039 	 */
3040 	if (!ffs_opts->refcnt) {
3041 		ret = functionfs_bind(func->ffs, c->cdev);
3042 		if (ret)
3043 			return ERR_PTR(ret);
3044 	}
3045 	ffs_opts->refcnt++;
3046 	func->function.strings = func->ffs->stringtabs;
3047 
3048 	return ffs_opts;
3049 }
3050 
3051 static int _ffs_func_bind(struct usb_configuration *c,
3052 			  struct usb_function *f)
3053 {
3054 	struct ffs_function *func = ffs_func_from_usb(f);
3055 	struct ffs_data *ffs = func->ffs;
3056 
3057 	const int full = !!func->ffs->fs_descs_count;
3058 	const int high = !!func->ffs->hs_descs_count;
3059 	const int super = !!func->ffs->ss_descs_count;
3060 
3061 	int fs_len, hs_len, ss_len, ret, i;
3062 	struct ffs_ep *eps_ptr;
3063 
3064 	/* Make it a single chunk, less management later on */
3065 	vla_group(d);
3066 	vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
3067 	vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
3068 		full ? ffs->fs_descs_count + 1 : 0);
3069 	vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
3070 		high ? ffs->hs_descs_count + 1 : 0);
3071 	vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
3072 		super ? ffs->ss_descs_count + 1 : 0);
3073 	vla_item_with_sz(d, short, inums, ffs->interfaces_count);
3074 	vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
3075 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3076 	vla_item_with_sz(d, char[16], ext_compat,
3077 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3078 	vla_item_with_sz(d, struct usb_os_desc, os_desc,
3079 			 c->cdev->use_os_string ? ffs->interfaces_count : 0);
3080 	vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
3081 			 ffs->ms_os_descs_ext_prop_count);
3082 	vla_item_with_sz(d, char, ext_prop_name,
3083 			 ffs->ms_os_descs_ext_prop_name_len);
3084 	vla_item_with_sz(d, char, ext_prop_data,
3085 			 ffs->ms_os_descs_ext_prop_data_len);
3086 	vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
3087 	char *vlabuf;
3088 
3089 	/* Has descriptors only for speeds gadget does not support */
3090 	if (!(full | high | super))
3091 		return -ENOTSUPP;
3092 
3093 	/* Allocate a single chunk, less management later on */
3094 	vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
3095 	if (!vlabuf)
3096 		return -ENOMEM;
3097 
3098 	ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
3099 	ffs->ms_os_descs_ext_prop_name_avail =
3100 		vla_ptr(vlabuf, d, ext_prop_name);
3101 	ffs->ms_os_descs_ext_prop_data_avail =
3102 		vla_ptr(vlabuf, d, ext_prop_data);
3103 
3104 	/* Copy descriptors  */
3105 	memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
3106 	       ffs->raw_descs_length);
3107 
3108 	memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
3109 	eps_ptr = vla_ptr(vlabuf, d, eps);
3110 	for (i = 0; i < ffs->eps_count; i++)
3111 		eps_ptr[i].num = -1;
3112 
3113 	/* Save pointers
3114 	 * d_eps == vlabuf, func->eps used to kfree vlabuf later
3115 	*/
3116 	func->eps             = vla_ptr(vlabuf, d, eps);
3117 	func->interfaces_nums = vla_ptr(vlabuf, d, inums);
3118 
3119 	/*
3120 	 * Go through all the endpoint descriptors and allocate
3121 	 * endpoints first, so that later we can rewrite the endpoint
3122 	 * numbers without worrying that it may be described later on.
3123 	 */
3124 	if (full) {
3125 		func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
3126 		fs_len = ffs_do_descs(ffs->fs_descs_count,
3127 				      vla_ptr(vlabuf, d, raw_descs),
3128 				      d_raw_descs__sz,
3129 				      __ffs_func_bind_do_descs, func);
3130 		if (fs_len < 0) {
3131 			ret = fs_len;
3132 			goto error;
3133 		}
3134 	} else {
3135 		fs_len = 0;
3136 	}
3137 
3138 	if (high) {
3139 		func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
3140 		hs_len = ffs_do_descs(ffs->hs_descs_count,
3141 				      vla_ptr(vlabuf, d, raw_descs) + fs_len,
3142 				      d_raw_descs__sz - fs_len,
3143 				      __ffs_func_bind_do_descs, func);
3144 		if (hs_len < 0) {
3145 			ret = hs_len;
3146 			goto error;
3147 		}
3148 	} else {
3149 		hs_len = 0;
3150 	}
3151 
3152 	if (super) {
3153 		func->function.ss_descriptors = func->function.ssp_descriptors =
3154 			vla_ptr(vlabuf, d, ss_descs);
3155 		ss_len = ffs_do_descs(ffs->ss_descs_count,
3156 				vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
3157 				d_raw_descs__sz - fs_len - hs_len,
3158 				__ffs_func_bind_do_descs, func);
3159 		if (ss_len < 0) {
3160 			ret = ss_len;
3161 			goto error;
3162 		}
3163 	} else {
3164 		ss_len = 0;
3165 	}
3166 
3167 	/*
3168 	 * Now handle interface numbers allocation and interface and
3169 	 * endpoint numbers rewriting.  We can do that in one go
3170 	 * now.
3171 	 */
3172 	ret = ffs_do_descs(ffs->fs_descs_count +
3173 			   (high ? ffs->hs_descs_count : 0) +
3174 			   (super ? ffs->ss_descs_count : 0),
3175 			   vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
3176 			   __ffs_func_bind_do_nums, func);
3177 	if (ret < 0)
3178 		goto error;
3179 
3180 	func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
3181 	if (c->cdev->use_os_string) {
3182 		for (i = 0; i < ffs->interfaces_count; ++i) {
3183 			struct usb_os_desc *desc;
3184 
3185 			desc = func->function.os_desc_table[i].os_desc =
3186 				vla_ptr(vlabuf, d, os_desc) +
3187 				i * sizeof(struct usb_os_desc);
3188 			desc->ext_compat_id =
3189 				vla_ptr(vlabuf, d, ext_compat) + i * 16;
3190 			INIT_LIST_HEAD(&desc->ext_prop);
3191 		}
3192 		ret = ffs_do_os_descs(ffs->ms_os_descs_count,
3193 				      vla_ptr(vlabuf, d, raw_descs) +
3194 				      fs_len + hs_len + ss_len,
3195 				      d_raw_descs__sz - fs_len - hs_len -
3196 				      ss_len,
3197 				      __ffs_func_bind_do_os_desc, func);
3198 		if (ret < 0)
3199 			goto error;
3200 	}
3201 	func->function.os_desc_n =
3202 		c->cdev->use_os_string ? ffs->interfaces_count : 0;
3203 
3204 	/* And we're done */
3205 	ffs_event_add(ffs, FUNCTIONFS_BIND);
3206 	return 0;
3207 
3208 error:
3209 	/* XXX Do we need to release all claimed endpoints here? */
3210 	return ret;
3211 }
3212 
3213 static int ffs_func_bind(struct usb_configuration *c,
3214 			 struct usb_function *f)
3215 {
3216 	struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
3217 	struct ffs_function *func = ffs_func_from_usb(f);
3218 	int ret;
3219 
3220 	if (IS_ERR(ffs_opts))
3221 		return PTR_ERR(ffs_opts);
3222 
3223 	ret = _ffs_func_bind(c, f);
3224 	if (ret && !--ffs_opts->refcnt)
3225 		functionfs_unbind(func->ffs);
3226 
3227 	return ret;
3228 }
3229 
3230 
3231 /* Other USB function hooks *************************************************/
3232 
3233 static void ffs_reset_work(struct work_struct *work)
3234 {
3235 	struct ffs_data *ffs = container_of(work,
3236 		struct ffs_data, reset_work);
3237 	ffs_data_reset(ffs);
3238 }
3239 
3240 static int ffs_func_set_alt(struct usb_function *f,
3241 			    unsigned interface, unsigned alt)
3242 {
3243 	struct ffs_function *func = ffs_func_from_usb(f);
3244 	struct ffs_data *ffs = func->ffs;
3245 	int ret = 0, intf;
3246 
3247 	if (alt != (unsigned)-1) {
3248 		intf = ffs_func_revmap_intf(func, interface);
3249 		if (intf < 0)
3250 			return intf;
3251 	}
3252 
3253 	if (ffs->func)
3254 		ffs_func_eps_disable(ffs->func);
3255 
3256 	if (ffs->state == FFS_DEACTIVATED) {
3257 		ffs->state = FFS_CLOSING;
3258 		INIT_WORK(&ffs->reset_work, ffs_reset_work);
3259 		schedule_work(&ffs->reset_work);
3260 		return -ENODEV;
3261 	}
3262 
3263 	if (ffs->state != FFS_ACTIVE)
3264 		return -ENODEV;
3265 
3266 	if (alt == (unsigned)-1) {
3267 		ffs->func = NULL;
3268 		ffs_event_add(ffs, FUNCTIONFS_DISABLE);
3269 		return 0;
3270 	}
3271 
3272 	ffs->func = func;
3273 	ret = ffs_func_eps_enable(func);
3274 	if (ret >= 0)
3275 		ffs_event_add(ffs, FUNCTIONFS_ENABLE);
3276 	return ret;
3277 }
3278 
3279 static void ffs_func_disable(struct usb_function *f)
3280 {
3281 	ffs_func_set_alt(f, 0, (unsigned)-1);
3282 }
3283 
3284 static int ffs_func_setup(struct usb_function *f,
3285 			  const struct usb_ctrlrequest *creq)
3286 {
3287 	struct ffs_function *func = ffs_func_from_usb(f);
3288 	struct ffs_data *ffs = func->ffs;
3289 	unsigned long flags;
3290 	int ret;
3291 
3292 	pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
3293 	pr_vdebug("creq->bRequest     = %02x\n", creq->bRequest);
3294 	pr_vdebug("creq->wValue       = %04x\n", le16_to_cpu(creq->wValue));
3295 	pr_vdebug("creq->wIndex       = %04x\n", le16_to_cpu(creq->wIndex));
3296 	pr_vdebug("creq->wLength      = %04x\n", le16_to_cpu(creq->wLength));
3297 
3298 	/*
3299 	 * Most requests directed to interface go through here
3300 	 * (notable exceptions are set/get interface) so we need to
3301 	 * handle them.  All other either handled by composite or
3302 	 * passed to usb_configuration->setup() (if one is set).  No
3303 	 * matter, we will handle requests directed to endpoint here
3304 	 * as well (as it's straightforward).  Other request recipient
3305 	 * types are only handled when the user flag FUNCTIONFS_ALL_CTRL_RECIP
3306 	 * is being used.
3307 	 */
3308 	if (ffs->state != FFS_ACTIVE)
3309 		return -ENODEV;
3310 
3311 	switch (creq->bRequestType & USB_RECIP_MASK) {
3312 	case USB_RECIP_INTERFACE:
3313 		ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
3314 		if (ret < 0)
3315 			return ret;
3316 		break;
3317 
3318 	case USB_RECIP_ENDPOINT:
3319 		ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
3320 		if (ret < 0)
3321 			return ret;
3322 		if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
3323 			ret = func->ffs->eps_addrmap[ret];
3324 		break;
3325 
3326 	default:
3327 		if (func->ffs->user_flags & FUNCTIONFS_ALL_CTRL_RECIP)
3328 			ret = le16_to_cpu(creq->wIndex);
3329 		else
3330 			return -EOPNOTSUPP;
3331 	}
3332 
3333 	spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
3334 	ffs->ev.setup = *creq;
3335 	ffs->ev.setup.wIndex = cpu_to_le16(ret);
3336 	__ffs_event_add(ffs, FUNCTIONFS_SETUP);
3337 	spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
3338 
3339 	return ffs->ev.setup.wLength == 0 ? USB_GADGET_DELAYED_STATUS : 0;
3340 }
3341 
3342 static bool ffs_func_req_match(struct usb_function *f,
3343 			       const struct usb_ctrlrequest *creq,
3344 			       bool config0)
3345 {
3346 	struct ffs_function *func = ffs_func_from_usb(f);
3347 
3348 	if (config0 && !(func->ffs->user_flags & FUNCTIONFS_CONFIG0_SETUP))
3349 		return false;
3350 
3351 	switch (creq->bRequestType & USB_RECIP_MASK) {
3352 	case USB_RECIP_INTERFACE:
3353 		return (ffs_func_revmap_intf(func,
3354 					     le16_to_cpu(creq->wIndex)) >= 0);
3355 	case USB_RECIP_ENDPOINT:
3356 		return (ffs_func_revmap_ep(func,
3357 					   le16_to_cpu(creq->wIndex)) >= 0);
3358 	default:
3359 		return (bool) (func->ffs->user_flags &
3360 			       FUNCTIONFS_ALL_CTRL_RECIP);
3361 	}
3362 }
3363 
3364 static void ffs_func_suspend(struct usb_function *f)
3365 {
3366 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
3367 }
3368 
3369 static void ffs_func_resume(struct usb_function *f)
3370 {
3371 	ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
3372 }
3373 
3374 
3375 /* Endpoint and interface numbers reverse mapping ***************************/
3376 
3377 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
3378 {
3379 	num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
3380 	return num ? num : -EDOM;
3381 }
3382 
3383 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
3384 {
3385 	short *nums = func->interfaces_nums;
3386 	unsigned count = func->ffs->interfaces_count;
3387 
3388 	for (; count; --count, ++nums) {
3389 		if (*nums >= 0 && *nums == intf)
3390 			return nums - func->interfaces_nums;
3391 	}
3392 
3393 	return -EDOM;
3394 }
3395 
3396 
3397 /* Devices management *******************************************************/
3398 
3399 static LIST_HEAD(ffs_devices);
3400 
3401 static struct ffs_dev *_ffs_do_find_dev(const char *name)
3402 {
3403 	struct ffs_dev *dev;
3404 
3405 	if (!name)
3406 		return NULL;
3407 
3408 	list_for_each_entry(dev, &ffs_devices, entry) {
3409 		if (strcmp(dev->name, name) == 0)
3410 			return dev;
3411 	}
3412 
3413 	return NULL;
3414 }
3415 
3416 /*
3417  * ffs_lock must be taken by the caller of this function
3418  */
3419 static struct ffs_dev *_ffs_get_single_dev(void)
3420 {
3421 	struct ffs_dev *dev;
3422 
3423 	if (list_is_singular(&ffs_devices)) {
3424 		dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
3425 		if (dev->single)
3426 			return dev;
3427 	}
3428 
3429 	return NULL;
3430 }
3431 
3432 /*
3433  * ffs_lock must be taken by the caller of this function
3434  */
3435 static struct ffs_dev *_ffs_find_dev(const char *name)
3436 {
3437 	struct ffs_dev *dev;
3438 
3439 	dev = _ffs_get_single_dev();
3440 	if (dev)
3441 		return dev;
3442 
3443 	return _ffs_do_find_dev(name);
3444 }
3445 
3446 /* Configfs support *********************************************************/
3447 
3448 static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3449 {
3450 	return container_of(to_config_group(item), struct f_fs_opts,
3451 			    func_inst.group);
3452 }
3453 
3454 static void ffs_attr_release(struct config_item *item)
3455 {
3456 	struct f_fs_opts *opts = to_ffs_opts(item);
3457 
3458 	usb_put_function_instance(&opts->func_inst);
3459 }
3460 
3461 static struct configfs_item_operations ffs_item_ops = {
3462 	.release	= ffs_attr_release,
3463 };
3464 
3465 static const struct config_item_type ffs_func_type = {
3466 	.ct_item_ops	= &ffs_item_ops,
3467 	.ct_owner	= THIS_MODULE,
3468 };
3469 
3470 
3471 /* Function registration interface ******************************************/
3472 
3473 static void ffs_free_inst(struct usb_function_instance *f)
3474 {
3475 	struct f_fs_opts *opts;
3476 
3477 	opts = to_f_fs_opts(f);
3478 	ffs_release_dev(opts->dev);
3479 	ffs_dev_lock();
3480 	_ffs_free_dev(opts->dev);
3481 	ffs_dev_unlock();
3482 	kfree(opts);
3483 }
3484 
3485 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3486 {
3487 	if (strlen(name) >= sizeof_field(struct ffs_dev, name))
3488 		return -ENAMETOOLONG;
3489 	return ffs_name_dev(to_f_fs_opts(fi)->dev, name);
3490 }
3491 
3492 static struct usb_function_instance *ffs_alloc_inst(void)
3493 {
3494 	struct f_fs_opts *opts;
3495 	struct ffs_dev *dev;
3496 
3497 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3498 	if (!opts)
3499 		return ERR_PTR(-ENOMEM);
3500 
3501 	opts->func_inst.set_inst_name = ffs_set_inst_name;
3502 	opts->func_inst.free_func_inst = ffs_free_inst;
3503 	ffs_dev_lock();
3504 	dev = _ffs_alloc_dev();
3505 	ffs_dev_unlock();
3506 	if (IS_ERR(dev)) {
3507 		kfree(opts);
3508 		return ERR_CAST(dev);
3509 	}
3510 	opts->dev = dev;
3511 	dev->opts = opts;
3512 
3513 	config_group_init_type_name(&opts->func_inst.group, "",
3514 				    &ffs_func_type);
3515 	return &opts->func_inst;
3516 }
3517 
3518 static void ffs_free(struct usb_function *f)
3519 {
3520 	kfree(ffs_func_from_usb(f));
3521 }
3522 
3523 static void ffs_func_unbind(struct usb_configuration *c,
3524 			    struct usb_function *f)
3525 {
3526 	struct ffs_function *func = ffs_func_from_usb(f);
3527 	struct ffs_data *ffs = func->ffs;
3528 	struct f_fs_opts *opts =
3529 		container_of(f->fi, struct f_fs_opts, func_inst);
3530 	struct ffs_ep *ep = func->eps;
3531 	unsigned count = ffs->eps_count;
3532 	unsigned long flags;
3533 
3534 	if (ffs->func == func) {
3535 		ffs_func_eps_disable(func);
3536 		ffs->func = NULL;
3537 	}
3538 
3539 	/* Drain any pending AIO completions */
3540 	drain_workqueue(ffs->io_completion_wq);
3541 
3542 	ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3543 	if (!--opts->refcnt)
3544 		functionfs_unbind(ffs);
3545 
3546 	/* cleanup after autoconfig */
3547 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
3548 	while (count--) {
3549 		if (ep->ep && ep->req)
3550 			usb_ep_free_request(ep->ep, ep->req);
3551 		ep->req = NULL;
3552 		++ep;
3553 	}
3554 	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3555 	kfree(func->eps);
3556 	func->eps = NULL;
3557 	/*
3558 	 * eps, descriptors and interfaces_nums are allocated in the
3559 	 * same chunk so only one free is required.
3560 	 */
3561 	func->function.fs_descriptors = NULL;
3562 	func->function.hs_descriptors = NULL;
3563 	func->function.ss_descriptors = NULL;
3564 	func->function.ssp_descriptors = NULL;
3565 	func->interfaces_nums = NULL;
3566 
3567 }
3568 
3569 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3570 {
3571 	struct ffs_function *func;
3572 
3573 	func = kzalloc(sizeof(*func), GFP_KERNEL);
3574 	if (!func)
3575 		return ERR_PTR(-ENOMEM);
3576 
3577 	func->function.name    = "Function FS Gadget";
3578 
3579 	func->function.bind    = ffs_func_bind;
3580 	func->function.unbind  = ffs_func_unbind;
3581 	func->function.set_alt = ffs_func_set_alt;
3582 	func->function.disable = ffs_func_disable;
3583 	func->function.setup   = ffs_func_setup;
3584 	func->function.req_match = ffs_func_req_match;
3585 	func->function.suspend = ffs_func_suspend;
3586 	func->function.resume  = ffs_func_resume;
3587 	func->function.free_func = ffs_free;
3588 
3589 	return &func->function;
3590 }
3591 
3592 /*
3593  * ffs_lock must be taken by the caller of this function
3594  */
3595 static struct ffs_dev *_ffs_alloc_dev(void)
3596 {
3597 	struct ffs_dev *dev;
3598 	int ret;
3599 
3600 	if (_ffs_get_single_dev())
3601 			return ERR_PTR(-EBUSY);
3602 
3603 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3604 	if (!dev)
3605 		return ERR_PTR(-ENOMEM);
3606 
3607 	if (list_empty(&ffs_devices)) {
3608 		ret = functionfs_init();
3609 		if (ret) {
3610 			kfree(dev);
3611 			return ERR_PTR(ret);
3612 		}
3613 	}
3614 
3615 	list_add(&dev->entry, &ffs_devices);
3616 
3617 	return dev;
3618 }
3619 
3620 int ffs_name_dev(struct ffs_dev *dev, const char *name)
3621 {
3622 	struct ffs_dev *existing;
3623 	int ret = 0;
3624 
3625 	ffs_dev_lock();
3626 
3627 	existing = _ffs_do_find_dev(name);
3628 	if (!existing)
3629 		strscpy(dev->name, name, ARRAY_SIZE(dev->name));
3630 	else if (existing != dev)
3631 		ret = -EBUSY;
3632 
3633 	ffs_dev_unlock();
3634 
3635 	return ret;
3636 }
3637 EXPORT_SYMBOL_GPL(ffs_name_dev);
3638 
3639 int ffs_single_dev(struct ffs_dev *dev)
3640 {
3641 	int ret;
3642 
3643 	ret = 0;
3644 	ffs_dev_lock();
3645 
3646 	if (!list_is_singular(&ffs_devices))
3647 		ret = -EBUSY;
3648 	else
3649 		dev->single = true;
3650 
3651 	ffs_dev_unlock();
3652 	return ret;
3653 }
3654 EXPORT_SYMBOL_GPL(ffs_single_dev);
3655 
3656 /*
3657  * ffs_lock must be taken by the caller of this function
3658  */
3659 static void _ffs_free_dev(struct ffs_dev *dev)
3660 {
3661 	list_del(&dev->entry);
3662 
3663 	kfree(dev);
3664 	if (list_empty(&ffs_devices))
3665 		functionfs_cleanup();
3666 }
3667 
3668 static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data)
3669 {
3670 	int ret = 0;
3671 	struct ffs_dev *ffs_dev;
3672 
3673 	ffs_dev_lock();
3674 
3675 	ffs_dev = _ffs_find_dev(dev_name);
3676 	if (!ffs_dev) {
3677 		ret = -ENOENT;
3678 	} else if (ffs_dev->mounted) {
3679 		ret = -EBUSY;
3680 	} else if (ffs_dev->ffs_acquire_dev_callback &&
3681 		   ffs_dev->ffs_acquire_dev_callback(ffs_dev)) {
3682 		ret = -ENOENT;
3683 	} else {
3684 		ffs_dev->mounted = true;
3685 		ffs_dev->ffs_data = ffs_data;
3686 		ffs_data->private_data = ffs_dev;
3687 	}
3688 
3689 	ffs_dev_unlock();
3690 	return ret;
3691 }
3692 
3693 static void ffs_release_dev(struct ffs_dev *ffs_dev)
3694 {
3695 	ffs_dev_lock();
3696 
3697 	if (ffs_dev && ffs_dev->mounted) {
3698 		ffs_dev->mounted = false;
3699 		if (ffs_dev->ffs_data) {
3700 			ffs_dev->ffs_data->private_data = NULL;
3701 			ffs_dev->ffs_data = NULL;
3702 		}
3703 
3704 		if (ffs_dev->ffs_release_dev_callback)
3705 			ffs_dev->ffs_release_dev_callback(ffs_dev);
3706 	}
3707 
3708 	ffs_dev_unlock();
3709 }
3710 
3711 static int ffs_ready(struct ffs_data *ffs)
3712 {
3713 	struct ffs_dev *ffs_obj;
3714 	int ret = 0;
3715 
3716 	ffs_dev_lock();
3717 
3718 	ffs_obj = ffs->private_data;
3719 	if (!ffs_obj) {
3720 		ret = -EINVAL;
3721 		goto done;
3722 	}
3723 	if (WARN_ON(ffs_obj->desc_ready)) {
3724 		ret = -EBUSY;
3725 		goto done;
3726 	}
3727 
3728 	ffs_obj->desc_ready = true;
3729 
3730 	if (ffs_obj->ffs_ready_callback) {
3731 		ret = ffs_obj->ffs_ready_callback(ffs);
3732 		if (ret)
3733 			goto done;
3734 	}
3735 
3736 	set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
3737 done:
3738 	ffs_dev_unlock();
3739 	return ret;
3740 }
3741 
3742 static void ffs_closed(struct ffs_data *ffs)
3743 {
3744 	struct ffs_dev *ffs_obj;
3745 	struct f_fs_opts *opts;
3746 	struct config_item *ci;
3747 
3748 	ffs_dev_lock();
3749 
3750 	ffs_obj = ffs->private_data;
3751 	if (!ffs_obj)
3752 		goto done;
3753 
3754 	ffs_obj->desc_ready = false;
3755 
3756 	if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
3757 	    ffs_obj->ffs_closed_callback)
3758 		ffs_obj->ffs_closed_callback(ffs);
3759 
3760 	if (ffs_obj->opts)
3761 		opts = ffs_obj->opts;
3762 	else
3763 		goto done;
3764 
3765 	if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3766 	    || !kref_read(&opts->func_inst.group.cg_item.ci_kref))
3767 		goto done;
3768 
3769 	ci = opts->func_inst.group.cg_item.ci_parent->ci_parent;
3770 	ffs_dev_unlock();
3771 
3772 	if (test_bit(FFS_FL_BOUND, &ffs->flags))
3773 		unregister_gadget_item(ci);
3774 	return;
3775 done:
3776 	ffs_dev_unlock();
3777 }
3778 
3779 /* Misc helper functions ****************************************************/
3780 
3781 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3782 {
3783 	return nonblock
3784 		? mutex_trylock(mutex) ? 0 : -EAGAIN
3785 		: mutex_lock_interruptible(mutex);
3786 }
3787 
3788 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
3789 {
3790 	char *data;
3791 
3792 	if (!len)
3793 		return NULL;
3794 
3795 	data = memdup_user(buf, len);
3796 	if (IS_ERR(data))
3797 		return data;
3798 
3799 	pr_vdebug("Buffer from user space:\n");
3800 	ffs_dump_mem("", data, len);
3801 
3802 	return data;
3803 }
3804 
3805 DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3806 MODULE_LICENSE("GPL");
3807 MODULE_AUTHOR("Michal Nazarewicz");
3808