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