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