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