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