1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/blkdev.h>
22#include <linux/pagemap.h>
23#include <linux/export.h>
24#include <asm/unaligned.h>
25
26#include <linux/usb/composite.h>
27#include <linux/usb/functionfs.h>
28
29
30#define FUNCTIONFS_MAGIC 0xa647361
31
32
33
34
35#ifdef VERBOSE_DEBUG
36# define pr_vdebug pr_debug
37# define ffs_dump_mem(prefix, ptr, len) \
38 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
39#else
40# define pr_vdebug(...) do { } while (0)
41# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
42#endif
43
44#define ENTER() pr_vdebug("%s()\n", __func__)
45
46
47
48
49enum ffs_state {
50
51
52
53
54
55
56
57 FFS_READ_DESCRIPTORS,
58 FFS_READ_STRINGS,
59
60
61
62
63
64
65
66
67
68 FFS_ACTIVE,
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 FFS_CLOSING
84};
85
86
87enum ffs_setup_state {
88
89 FFS_NO_SETUP,
90
91
92
93
94
95 FFS_SETUP_PENDING,
96
97
98
99
100
101
102 FFS_SETUP_CANCELED
103};
104
105
106
107struct ffs_epfile;
108struct ffs_function;
109
110struct ffs_data {
111 struct usb_gadget *gadget;
112
113
114
115
116
117
118
119 struct mutex mutex;
120
121
122
123
124
125
126 spinlock_t eps_lock;
127
128
129
130
131
132
133
134
135
136 struct usb_request *ep0req;
137 struct completion ep0req_completion;
138 int ep0req_status;
139
140
141 atomic_t ref;
142
143 atomic_t opened;
144
145
146 enum ffs_state state;
147
148
149
150
151
152
153
154
155
156
157 enum ffs_setup_state setup_state;
158
159#define FFS_SETUP_STATE(ffs) \
160 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
161 FFS_SETUP_CANCELED, FFS_NO_SETUP))
162
163
164 struct {
165 u8 types[4];
166 unsigned short count;
167
168 unsigned short can_stall;
169 struct usb_ctrlrequest setup;
170
171 wait_queue_head_t waitq;
172 } ev;
173
174
175 unsigned long flags;
176#define FFS_FL_CALL_CLOSED_CALLBACK 0
177#define FFS_FL_BOUND 1
178
179
180 struct ffs_function *func;
181
182
183
184
185
186 const char *dev_name;
187
188 void *private_data;
189
190
191
192
193
194
195
196
197 const void *raw_descs;
198 unsigned raw_descs_length;
199 unsigned raw_fs_descs_length;
200 unsigned fs_descs_count;
201 unsigned hs_descs_count;
202
203 unsigned short strings_count;
204 unsigned short interfaces_count;
205 unsigned short eps_count;
206 unsigned short _pad1;
207
208
209
210 const void *raw_strings;
211 struct usb_gadget_strings **stringtabs;
212
213
214
215
216
217 struct super_block *sb;
218
219
220 struct ffs_file_perms {
221 umode_t mode;
222 uid_t uid;
223 gid_t gid;
224 } file_perms;
225
226
227
228
229
230 struct ffs_epfile *epfiles;
231};
232
233
234static void ffs_data_get(struct ffs_data *ffs);
235static void ffs_data_put(struct ffs_data *ffs);
236
237static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
238
239
240static void ffs_data_opened(struct ffs_data *ffs);
241static void ffs_data_closed(struct ffs_data *ffs);
242
243
244static int __must_check
245__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
246static int __must_check
247__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
248
249
250
251
252struct ffs_ep;
253
254struct ffs_function {
255 struct usb_configuration *conf;
256 struct usb_gadget *gadget;
257 struct ffs_data *ffs;
258
259 struct ffs_ep *eps;
260 u8 eps_revmap[16];
261 short *interfaces_nums;
262
263 struct usb_function function;
264};
265
266
267static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
268{
269 return container_of(f, struct ffs_function, function);
270}
271
272static void ffs_func_free(struct ffs_function *func);
273
274static void ffs_func_eps_disable(struct ffs_function *func);
275static int __must_check ffs_func_eps_enable(struct ffs_function *func);
276
277static int ffs_func_bind(struct usb_configuration *,
278 struct usb_function *);
279static void ffs_func_unbind(struct usb_configuration *,
280 struct usb_function *);
281static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
282static void ffs_func_disable(struct usb_function *);
283static int ffs_func_setup(struct usb_function *,
284 const struct usb_ctrlrequest *);
285static void ffs_func_suspend(struct usb_function *);
286static void ffs_func_resume(struct usb_function *);
287
288
289static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
290static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
291
292
293
294
295struct ffs_ep {
296 struct usb_ep *ep;
297 struct usb_request *req;
298
299
300 struct usb_endpoint_descriptor *descs[2];
301
302 u8 num;
303
304 int status;
305};
306
307struct ffs_epfile {
308
309 struct mutex mutex;
310 wait_queue_head_t wait;
311
312 struct ffs_data *ffs;
313 struct ffs_ep *ep;
314
315 struct dentry *dentry;
316
317 char name[5];
318
319 unsigned char in;
320 unsigned char isoc;
321
322 unsigned char _pad;
323};
324
325static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
326static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
327
328static struct inode *__must_check
329ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
330 const struct file_operations *fops,
331 struct dentry **dentry_p);
332
333
334
335
336static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
337 __attribute__((warn_unused_result, nonnull));
338static char *ffs_prepare_buffer(const char * __user buf, size_t len)
339 __attribute__((warn_unused_result, nonnull));
340
341
342
343
344static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
345{
346 struct ffs_data *ffs = req->context;
347
348 complete_all(&ffs->ep0req_completion);
349}
350
351static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
352{
353 struct usb_request *req = ffs->ep0req;
354 int ret;
355
356 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
357
358 spin_unlock_irq(&ffs->ev.waitq.lock);
359
360 req->buf = data;
361 req->length = len;
362
363
364
365
366
367
368 if (req->buf == NULL)
369 req->buf = (void *)0xDEADBABE;
370
371 INIT_COMPLETION(ffs->ep0req_completion);
372
373 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
374 if (unlikely(ret < 0))
375 return ret;
376
377 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
378 if (unlikely(ret)) {
379 usb_ep_dequeue(ffs->gadget->ep0, req);
380 return -EINTR;
381 }
382
383 ffs->setup_state = FFS_NO_SETUP;
384 return ffs->ep0req_status;
385}
386
387static int __ffs_ep0_stall(struct ffs_data *ffs)
388{
389 if (ffs->ev.can_stall) {
390 pr_vdebug("ep0 stall\n");
391 usb_ep_set_halt(ffs->gadget->ep0);
392 ffs->setup_state = FFS_NO_SETUP;
393 return -EL2HLT;
394 } else {
395 pr_debug("bogus ep0 stall!\n");
396 return -ESRCH;
397 }
398}
399
400static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
401 size_t len, loff_t *ptr)
402{
403 struct ffs_data *ffs = file->private_data;
404 ssize_t ret;
405 char *data;
406
407 ENTER();
408
409
410 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
411 return -EIDRM;
412
413
414 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
415 if (unlikely(ret < 0))
416 return ret;
417
418
419 switch (ffs->state) {
420 case FFS_READ_DESCRIPTORS:
421 case FFS_READ_STRINGS:
422
423 if (unlikely(len < 16)) {
424 ret = -EINVAL;
425 break;
426 }
427
428 data = ffs_prepare_buffer(buf, len);
429 if (IS_ERR(data)) {
430 ret = PTR_ERR(data);
431 break;
432 }
433
434
435 if (ffs->state == FFS_READ_DESCRIPTORS) {
436 pr_info("read descriptors\n");
437 ret = __ffs_data_got_descs(ffs, data, len);
438 if (unlikely(ret < 0))
439 break;
440
441 ffs->state = FFS_READ_STRINGS;
442 ret = len;
443 } else {
444 pr_info("read strings\n");
445 ret = __ffs_data_got_strings(ffs, data, len);
446 if (unlikely(ret < 0))
447 break;
448
449 ret = ffs_epfiles_create(ffs);
450 if (unlikely(ret)) {
451 ffs->state = FFS_CLOSING;
452 break;
453 }
454
455 ffs->state = FFS_ACTIVE;
456 mutex_unlock(&ffs->mutex);
457
458 ret = functionfs_ready_callback(ffs);
459 if (unlikely(ret < 0)) {
460 ffs->state = FFS_CLOSING;
461 return ret;
462 }
463
464 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
465 return len;
466 }
467 break;
468
469 case FFS_ACTIVE:
470 data = NULL;
471
472
473
474
475 spin_lock_irq(&ffs->ev.waitq.lock);
476 switch (FFS_SETUP_STATE(ffs)) {
477 case FFS_SETUP_CANCELED:
478 ret = -EIDRM;
479 goto done_spin;
480
481 case FFS_NO_SETUP:
482 ret = -ESRCH;
483 goto done_spin;
484
485 case FFS_SETUP_PENDING:
486 break;
487 }
488
489
490 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
491 spin_unlock_irq(&ffs->ev.waitq.lock);
492 ret = __ffs_ep0_stall(ffs);
493 break;
494 }
495
496
497 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
498
499 spin_unlock_irq(&ffs->ev.waitq.lock);
500
501 data = ffs_prepare_buffer(buf, len);
502 if (IS_ERR(data)) {
503 ret = PTR_ERR(data);
504 break;
505 }
506
507 spin_lock_irq(&ffs->ev.waitq.lock);
508
509
510
511
512
513
514
515
516
517
518
519
520
521 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
522 ret = -EIDRM;
523done_spin:
524 spin_unlock_irq(&ffs->ev.waitq.lock);
525 } else {
526
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
528 }
529 kfree(data);
530 break;
531
532 default:
533 ret = -EBADFD;
534 break;
535 }
536
537 mutex_unlock(&ffs->mutex);
538 return ret;
539}
540
541static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
542 size_t n)
543{
544
545
546
547
548 struct usb_functionfs_event events[n];
549 unsigned i = 0;
550
551 memset(events, 0, sizeof events);
552
553 do {
554 events[i].type = ffs->ev.types[i];
555 if (events[i].type == FUNCTIONFS_SETUP) {
556 events[i].u.setup = ffs->ev.setup;
557 ffs->setup_state = FFS_SETUP_PENDING;
558 }
559 } while (++i < n);
560
561 if (n < ffs->ev.count) {
562 ffs->ev.count -= n;
563 memmove(ffs->ev.types, ffs->ev.types + n,
564 ffs->ev.count * sizeof *ffs->ev.types);
565 } else {
566 ffs->ev.count = 0;
567 }
568
569 spin_unlock_irq(&ffs->ev.waitq.lock);
570 mutex_unlock(&ffs->mutex);
571
572 return unlikely(__copy_to_user(buf, events, sizeof events))
573 ? -EFAULT : sizeof events;
574}
575
576static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
577 size_t len, loff_t *ptr)
578{
579 struct ffs_data *ffs = file->private_data;
580 char *data = NULL;
581 size_t n;
582 int ret;
583
584 ENTER();
585
586
587 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
588 return -EIDRM;
589
590
591 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
592 if (unlikely(ret < 0))
593 return ret;
594
595
596 if (ffs->state != FFS_ACTIVE) {
597 ret = -EBADFD;
598 goto done_mutex;
599 }
600
601
602
603
604
605 spin_lock_irq(&ffs->ev.waitq.lock);
606
607 switch (FFS_SETUP_STATE(ffs)) {
608 case FFS_SETUP_CANCELED:
609 ret = -EIDRM;
610 break;
611
612 case FFS_NO_SETUP:
613 n = len / sizeof(struct usb_functionfs_event);
614 if (unlikely(!n)) {
615 ret = -EINVAL;
616 break;
617 }
618
619 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
620 ret = -EAGAIN;
621 break;
622 }
623
624 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
625 ffs->ev.count)) {
626 ret = -EINTR;
627 break;
628 }
629
630 return __ffs_ep0_read_events(ffs, buf,
631 min(n, (size_t)ffs->ev.count));
632
633 case FFS_SETUP_PENDING:
634 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
635 spin_unlock_irq(&ffs->ev.waitq.lock);
636 ret = __ffs_ep0_stall(ffs);
637 goto done_mutex;
638 }
639
640 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
641
642 spin_unlock_irq(&ffs->ev.waitq.lock);
643
644 if (likely(len)) {
645 data = kmalloc(len, GFP_KERNEL);
646 if (unlikely(!data)) {
647 ret = -ENOMEM;
648 goto done_mutex;
649 }
650 }
651
652 spin_lock_irq(&ffs->ev.waitq.lock);
653
654
655 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
656 ret = -EIDRM;
657 break;
658 }
659
660
661 ret = __ffs_ep0_queue_wait(ffs, data, len);
662 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
663 ret = -EFAULT;
664 goto done_mutex;
665
666 default:
667 ret = -EBADFD;
668 break;
669 }
670
671 spin_unlock_irq(&ffs->ev.waitq.lock);
672done_mutex:
673 mutex_unlock(&ffs->mutex);
674 kfree(data);
675 return ret;
676}
677
678static int ffs_ep0_open(struct inode *inode, struct file *file)
679{
680 struct ffs_data *ffs = inode->i_private;
681
682 ENTER();
683
684 if (unlikely(ffs->state == FFS_CLOSING))
685 return -EBUSY;
686
687 file->private_data = ffs;
688 ffs_data_opened(ffs);
689
690 return 0;
691}
692
693static int ffs_ep0_release(struct inode *inode, struct file *file)
694{
695 struct ffs_data *ffs = file->private_data;
696
697 ENTER();
698
699 ffs_data_closed(ffs);
700
701 return 0;
702}
703
704static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
705{
706 struct ffs_data *ffs = file->private_data;
707 struct usb_gadget *gadget = ffs->gadget;
708 long ret;
709
710 ENTER();
711
712 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
713 struct ffs_function *func = ffs->func;
714 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
715 } else if (gadget && gadget->ops->ioctl) {
716 ret = gadget->ops->ioctl(gadget, code, value);
717 } else {
718 ret = -ENOTTY;
719 }
720
721 return ret;
722}
723
724static const struct file_operations ffs_ep0_operations = {
725 .owner = THIS_MODULE,
726 .llseek = no_llseek,
727
728 .open = ffs_ep0_open,
729 .write = ffs_ep0_write,
730 .read = ffs_ep0_read,
731 .release = ffs_ep0_release,
732 .unlocked_ioctl = ffs_ep0_ioctl,
733};
734
735
736
737
738static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
739{
740 ENTER();
741 if (likely(req->context)) {
742 struct ffs_ep *ep = _ep->driver_data;
743 ep->status = req->status ? req->status : req->actual;
744 complete(req->context);
745 }
746}
747
748static ssize_t ffs_epfile_io(struct file *file,
749 char __user *buf, size_t len, int read)
750{
751 struct ffs_epfile *epfile = file->private_data;
752 struct ffs_ep *ep;
753 char *data = NULL;
754 ssize_t ret;
755 int halt;
756
757 goto first_try;
758 do {
759 spin_unlock_irq(&epfile->ffs->eps_lock);
760 mutex_unlock(&epfile->mutex);
761
762first_try:
763
764 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
765 ret = -ENODEV;
766 goto error;
767 }
768
769
770 ep = epfile->ep;
771 if (!ep) {
772 if (file->f_flags & O_NONBLOCK) {
773 ret = -EAGAIN;
774 goto error;
775 }
776
777 if (wait_event_interruptible(epfile->wait,
778 (ep = epfile->ep))) {
779 ret = -EINTR;
780 goto error;
781 }
782 }
783
784
785 halt = !read == !epfile->in;
786 if (halt && epfile->isoc) {
787 ret = -EINVAL;
788 goto error;
789 }
790
791
792 if (!halt && !data) {
793 data = kzalloc(len, GFP_KERNEL);
794 if (unlikely(!data))
795 return -ENOMEM;
796
797 if (!read &&
798 unlikely(__copy_from_user(data, buf, len))) {
799 ret = -EFAULT;
800 goto error;
801 }
802 }
803
804
805 ret = ffs_mutex_lock(&epfile->mutex,
806 file->f_flags & O_NONBLOCK);
807 if (unlikely(ret))
808 goto error;
809
810
811
812
813
814 spin_lock_irq(&epfile->ffs->eps_lock);
815
816
817
818
819
820 } while (unlikely(epfile->ep != ep));
821
822
823 if (unlikely(halt)) {
824 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
825 usb_ep_set_halt(ep->ep);
826 spin_unlock_irq(&epfile->ffs->eps_lock);
827 ret = -EBADMSG;
828 } else {
829
830 DECLARE_COMPLETION_ONSTACK(done);
831
832 struct usb_request *req = ep->req;
833 req->context = &done;
834 req->complete = ffs_epfile_io_complete;
835 req->buf = data;
836 req->length = len;
837
838 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
839
840 spin_unlock_irq(&epfile->ffs->eps_lock);
841
842 if (unlikely(ret < 0)) {
843
844 } else if (unlikely(wait_for_completion_interruptible(&done))) {
845 ret = -EINTR;
846 usb_ep_dequeue(ep->ep, req);
847 } else {
848 ret = ep->status;
849 if (read && ret > 0 &&
850 unlikely(copy_to_user(buf, data, ret)))
851 ret = -EFAULT;
852 }
853 }
854
855 mutex_unlock(&epfile->mutex);
856error:
857 kfree(data);
858 return ret;
859}
860
861static ssize_t
862ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
863 loff_t *ptr)
864{
865 ENTER();
866
867 return ffs_epfile_io(file, (char __user *)buf, len, 0);
868}
869
870static ssize_t
871ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
872{
873 ENTER();
874
875 return ffs_epfile_io(file, buf, len, 1);
876}
877
878static int
879ffs_epfile_open(struct inode *inode, struct file *file)
880{
881 struct ffs_epfile *epfile = inode->i_private;
882
883 ENTER();
884
885 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
886 return -ENODEV;
887
888 file->private_data = epfile;
889 ffs_data_opened(epfile->ffs);
890
891 return 0;
892}
893
894static int
895ffs_epfile_release(struct inode *inode, struct file *file)
896{
897 struct ffs_epfile *epfile = inode->i_private;
898
899 ENTER();
900
901 ffs_data_closed(epfile->ffs);
902
903 return 0;
904}
905
906static long ffs_epfile_ioctl(struct file *file, unsigned code,
907 unsigned long value)
908{
909 struct ffs_epfile *epfile = file->private_data;
910 int ret;
911
912 ENTER();
913
914 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
915 return -ENODEV;
916
917 spin_lock_irq(&epfile->ffs->eps_lock);
918 if (likely(epfile->ep)) {
919 switch (code) {
920 case FUNCTIONFS_FIFO_STATUS:
921 ret = usb_ep_fifo_status(epfile->ep->ep);
922 break;
923 case FUNCTIONFS_FIFO_FLUSH:
924 usb_ep_fifo_flush(epfile->ep->ep);
925 ret = 0;
926 break;
927 case FUNCTIONFS_CLEAR_HALT:
928 ret = usb_ep_clear_halt(epfile->ep->ep);
929 break;
930 case FUNCTIONFS_ENDPOINT_REVMAP:
931 ret = epfile->ep->num;
932 break;
933 default:
934 ret = -ENOTTY;
935 }
936 } else {
937 ret = -ENODEV;
938 }
939 spin_unlock_irq(&epfile->ffs->eps_lock);
940
941 return ret;
942}
943
944static const struct file_operations ffs_epfile_operations = {
945 .owner = THIS_MODULE,
946 .llseek = no_llseek,
947
948 .open = ffs_epfile_open,
949 .write = ffs_epfile_write,
950 .read = ffs_epfile_read,
951 .release = ffs_epfile_release,
952 .unlocked_ioctl = ffs_epfile_ioctl,
953};
954
955
956
957
958
959
960
961
962
963static struct inode *__must_check
964ffs_sb_make_inode(struct super_block *sb, void *data,
965 const struct file_operations *fops,
966 const struct inode_operations *iops,
967 struct ffs_file_perms *perms)
968{
969 struct inode *inode;
970
971 ENTER();
972
973 inode = new_inode(sb);
974
975 if (likely(inode)) {
976 struct timespec current_time = CURRENT_TIME;
977
978 inode->i_ino = get_next_ino();
979 inode->i_mode = perms->mode;
980 inode->i_uid = perms->uid;
981 inode->i_gid = perms->gid;
982 inode->i_atime = current_time;
983 inode->i_mtime = current_time;
984 inode->i_ctime = current_time;
985 inode->i_private = data;
986 if (fops)
987 inode->i_fop = fops;
988 if (iops)
989 inode->i_op = iops;
990 }
991
992 return inode;
993}
994
995
996static struct inode *ffs_sb_create_file(struct super_block *sb,
997 const char *name, void *data,
998 const struct file_operations *fops,
999 struct dentry **dentry_p)
1000{
1001 struct ffs_data *ffs = sb->s_fs_info;
1002 struct dentry *dentry;
1003 struct inode *inode;
1004
1005 ENTER();
1006
1007 dentry = d_alloc_name(sb->s_root, name);
1008 if (unlikely(!dentry))
1009 return NULL;
1010
1011 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1012 if (unlikely(!inode)) {
1013 dput(dentry);
1014 return NULL;
1015 }
1016
1017 d_add(dentry, inode);
1018 if (dentry_p)
1019 *dentry_p = dentry;
1020
1021 return inode;
1022}
1023
1024
1025static const struct super_operations ffs_sb_operations = {
1026 .statfs = simple_statfs,
1027 .drop_inode = generic_delete_inode,
1028};
1029
1030struct ffs_sb_fill_data {
1031 struct ffs_file_perms perms;
1032 umode_t root_mode;
1033 const char *dev_name;
1034 union {
1035
1036 void *private_data;
1037
1038 struct ffs_data *ffs_data;
1039 };
1040};
1041
1042static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1043{
1044 struct ffs_sb_fill_data *data = _data;
1045 struct inode *inode;
1046 struct ffs_data *ffs;
1047
1048 ENTER();
1049
1050
1051 ffs = ffs_data_new();
1052 if (unlikely(!ffs))
1053 goto Enomem;
1054
1055 ffs->sb = sb;
1056 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1057 if (unlikely(!ffs->dev_name))
1058 goto Enomem;
1059 ffs->file_perms = data->perms;
1060 ffs->private_data = data->private_data;
1061
1062
1063 data->ffs_data = ffs;
1064
1065 sb->s_fs_info = ffs;
1066 sb->s_blocksize = PAGE_CACHE_SIZE;
1067 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1068 sb->s_magic = FUNCTIONFS_MAGIC;
1069 sb->s_op = &ffs_sb_operations;
1070 sb->s_time_gran = 1;
1071
1072
1073 data->perms.mode = data->root_mode;
1074 inode = ffs_sb_make_inode(sb, NULL,
1075 &simple_dir_operations,
1076 &simple_dir_inode_operations,
1077 &data->perms);
1078 sb->s_root = d_make_root(inode);
1079 if (unlikely(!sb->s_root))
1080 goto Enomem;
1081
1082
1083 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1084 &ffs_ep0_operations, NULL)))
1085 goto Enomem;
1086
1087 return 0;
1088
1089Enomem:
1090 return -ENOMEM;
1091}
1092
1093static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1094{
1095 ENTER();
1096
1097 if (!opts || !*opts)
1098 return 0;
1099
1100 for (;;) {
1101 char *end, *eq, *comma;
1102 unsigned long value;
1103
1104
1105 comma = strchr(opts, ',');
1106 if (comma)
1107 *comma = 0;
1108
1109
1110 eq = strchr(opts, '=');
1111 if (unlikely(!eq)) {
1112 pr_err("'=' missing in %s\n", opts);
1113 return -EINVAL;
1114 }
1115 *eq = 0;
1116
1117
1118 value = simple_strtoul(eq + 1, &end, 0);
1119 if (unlikely(*end != ',' && *end != 0)) {
1120 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1121 return -EINVAL;
1122 }
1123
1124
1125 switch (eq - opts) {
1126 case 5:
1127 if (!memcmp(opts, "rmode", 5))
1128 data->root_mode = (value & 0555) | S_IFDIR;
1129 else if (!memcmp(opts, "fmode", 5))
1130 data->perms.mode = (value & 0666) | S_IFREG;
1131 else
1132 goto invalid;
1133 break;
1134
1135 case 4:
1136 if (!memcmp(opts, "mode", 4)) {
1137 data->root_mode = (value & 0555) | S_IFDIR;
1138 data->perms.mode = (value & 0666) | S_IFREG;
1139 } else {
1140 goto invalid;
1141 }
1142 break;
1143
1144 case 3:
1145 if (!memcmp(opts, "uid", 3))
1146 data->perms.uid = value;
1147 else if (!memcmp(opts, "gid", 3))
1148 data->perms.gid = value;
1149 else
1150 goto invalid;
1151 break;
1152
1153 default:
1154invalid:
1155 pr_err("%s: invalid option\n", opts);
1156 return -EINVAL;
1157 }
1158
1159
1160 if (!comma)
1161 break;
1162 opts = comma + 1;
1163 }
1164
1165 return 0;
1166}
1167
1168
1169
1170static struct dentry *
1171ffs_fs_mount(struct file_system_type *t, int flags,
1172 const char *dev_name, void *opts)
1173{
1174 struct ffs_sb_fill_data data = {
1175 .perms = {
1176 .mode = S_IFREG | 0600,
1177 .uid = 0,
1178 .gid = 0
1179 },
1180 .root_mode = S_IFDIR | 0500,
1181 };
1182 struct dentry *rv;
1183 int ret;
1184 void *ffs_dev;
1185
1186 ENTER();
1187
1188 ret = ffs_fs_parse_opts(&data, opts);
1189 if (unlikely(ret < 0))
1190 return ERR_PTR(ret);
1191
1192 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1193 if (IS_ERR(ffs_dev))
1194 return ffs_dev;
1195
1196 data.dev_name = dev_name;
1197 data.private_data = ffs_dev;
1198 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1199
1200
1201 if (IS_ERR(rv))
1202 functionfs_release_dev_callback(data.ffs_data);
1203
1204 return rv;
1205}
1206
1207static void
1208ffs_fs_kill_sb(struct super_block *sb)
1209{
1210 ENTER();
1211
1212 kill_litter_super(sb);
1213 if (sb->s_fs_info) {
1214 functionfs_release_dev_callback(sb->s_fs_info);
1215 ffs_data_put(sb->s_fs_info);
1216 }
1217}
1218
1219static struct file_system_type ffs_fs_type = {
1220 .owner = THIS_MODULE,
1221 .name = "functionfs",
1222 .mount = ffs_fs_mount,
1223 .kill_sb = ffs_fs_kill_sb,
1224};
1225
1226
1227
1228
1229static int functionfs_init(void)
1230{
1231 int ret;
1232
1233 ENTER();
1234
1235 ret = register_filesystem(&ffs_fs_type);
1236 if (likely(!ret))
1237 pr_info("file system registered\n");
1238 else
1239 pr_err("failed registering file system (%d)\n", ret);
1240
1241 return ret;
1242}
1243
1244static void functionfs_cleanup(void)
1245{
1246 ENTER();
1247
1248 pr_info("unloading\n");
1249 unregister_filesystem(&ffs_fs_type);
1250}
1251
1252
1253
1254
1255static void ffs_data_clear(struct ffs_data *ffs);
1256static void ffs_data_reset(struct ffs_data *ffs);
1257
1258static void ffs_data_get(struct ffs_data *ffs)
1259{
1260 ENTER();
1261
1262 atomic_inc(&ffs->ref);
1263}
1264
1265static void ffs_data_opened(struct ffs_data *ffs)
1266{
1267 ENTER();
1268
1269 atomic_inc(&ffs->ref);
1270 atomic_inc(&ffs->opened);
1271}
1272
1273static void ffs_data_put(struct ffs_data *ffs)
1274{
1275 ENTER();
1276
1277 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1278 pr_info("%s(): freeing\n", __func__);
1279 ffs_data_clear(ffs);
1280 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1281 waitqueue_active(&ffs->ep0req_completion.wait));
1282 kfree(ffs->dev_name);
1283 kfree(ffs);
1284 }
1285}
1286
1287static void ffs_data_closed(struct ffs_data *ffs)
1288{
1289 ENTER();
1290
1291 if (atomic_dec_and_test(&ffs->opened)) {
1292 ffs->state = FFS_CLOSING;
1293 ffs_data_reset(ffs);
1294 }
1295
1296 ffs_data_put(ffs);
1297}
1298
1299static struct ffs_data *ffs_data_new(void)
1300{
1301 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1302 if (unlikely(!ffs))
1303 return 0;
1304
1305 ENTER();
1306
1307 atomic_set(&ffs->ref, 1);
1308 atomic_set(&ffs->opened, 0);
1309 ffs->state = FFS_READ_DESCRIPTORS;
1310 mutex_init(&ffs->mutex);
1311 spin_lock_init(&ffs->eps_lock);
1312 init_waitqueue_head(&ffs->ev.waitq);
1313 init_completion(&ffs->ep0req_completion);
1314
1315
1316 ffs->ev.can_stall = 1;
1317
1318 return ffs;
1319}
1320
1321static void ffs_data_clear(struct ffs_data *ffs)
1322{
1323 ENTER();
1324
1325 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1326 functionfs_closed_callback(ffs);
1327
1328 BUG_ON(ffs->gadget);
1329
1330 if (ffs->epfiles)
1331 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1332
1333 kfree(ffs->raw_descs);
1334 kfree(ffs->raw_strings);
1335 kfree(ffs->stringtabs);
1336}
1337
1338static void ffs_data_reset(struct ffs_data *ffs)
1339{
1340 ENTER();
1341
1342 ffs_data_clear(ffs);
1343
1344 ffs->epfiles = NULL;
1345 ffs->raw_descs = NULL;
1346 ffs->raw_strings = NULL;
1347 ffs->stringtabs = NULL;
1348
1349 ffs->raw_descs_length = 0;
1350 ffs->raw_fs_descs_length = 0;
1351 ffs->fs_descs_count = 0;
1352 ffs->hs_descs_count = 0;
1353
1354 ffs->strings_count = 0;
1355 ffs->interfaces_count = 0;
1356 ffs->eps_count = 0;
1357
1358 ffs->ev.count = 0;
1359
1360 ffs->state = FFS_READ_DESCRIPTORS;
1361 ffs->setup_state = FFS_NO_SETUP;
1362 ffs->flags = 0;
1363}
1364
1365
1366static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1367{
1368 struct usb_gadget_strings **lang;
1369 int first_id;
1370
1371 ENTER();
1372
1373 if (WARN_ON(ffs->state != FFS_ACTIVE
1374 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1375 return -EBADFD;
1376
1377 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1378 if (unlikely(first_id < 0))
1379 return first_id;
1380
1381 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1382 if (unlikely(!ffs->ep0req))
1383 return -ENOMEM;
1384 ffs->ep0req->complete = ffs_ep0_complete;
1385 ffs->ep0req->context = ffs;
1386
1387 lang = ffs->stringtabs;
1388 for (lang = ffs->stringtabs; *lang; ++lang) {
1389 struct usb_string *str = (*lang)->strings;
1390 int id = first_id;
1391 for (; str->s; ++id, ++str)
1392 str->id = id;
1393 }
1394
1395 ffs->gadget = cdev->gadget;
1396 ffs_data_get(ffs);
1397 return 0;
1398}
1399
1400static void functionfs_unbind(struct ffs_data *ffs)
1401{
1402 ENTER();
1403
1404 if (!WARN_ON(!ffs->gadget)) {
1405 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1406 ffs->ep0req = NULL;
1407 ffs->gadget = NULL;
1408 ffs_data_put(ffs);
1409 clear_bit(FFS_FL_BOUND, &ffs->flags);
1410 }
1411}
1412
1413static int ffs_epfiles_create(struct ffs_data *ffs)
1414{
1415 struct ffs_epfile *epfile, *epfiles;
1416 unsigned i, count;
1417
1418 ENTER();
1419
1420 count = ffs->eps_count;
1421 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1422 if (!epfiles)
1423 return -ENOMEM;
1424
1425 epfile = epfiles;
1426 for (i = 1; i <= count; ++i, ++epfile) {
1427 epfile->ffs = ffs;
1428 mutex_init(&epfile->mutex);
1429 init_waitqueue_head(&epfile->wait);
1430 sprintf(epfiles->name, "ep%u", i);
1431 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1432 &ffs_epfile_operations,
1433 &epfile->dentry))) {
1434 ffs_epfiles_destroy(epfiles, i - 1);
1435 return -ENOMEM;
1436 }
1437 }
1438
1439 ffs->epfiles = epfiles;
1440 return 0;
1441}
1442
1443static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1444{
1445 struct ffs_epfile *epfile = epfiles;
1446
1447 ENTER();
1448
1449 for (; count; --count, ++epfile) {
1450 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1451 waitqueue_active(&epfile->wait));
1452 if (epfile->dentry) {
1453 d_delete(epfile->dentry);
1454 dput(epfile->dentry);
1455 epfile->dentry = NULL;
1456 }
1457 }
1458
1459 kfree(epfiles);
1460}
1461
1462static int functionfs_bind_config(struct usb_composite_dev *cdev,
1463 struct usb_configuration *c,
1464 struct ffs_data *ffs)
1465{
1466 struct ffs_function *func;
1467 int ret;
1468
1469 ENTER();
1470
1471 func = kzalloc(sizeof *func, GFP_KERNEL);
1472 if (unlikely(!func))
1473 return -ENOMEM;
1474
1475 func->function.name = "Function FS Gadget";
1476 func->function.strings = ffs->stringtabs;
1477
1478 func->function.bind = ffs_func_bind;
1479 func->function.unbind = ffs_func_unbind;
1480 func->function.set_alt = ffs_func_set_alt;
1481 func->function.disable = ffs_func_disable;
1482 func->function.setup = ffs_func_setup;
1483 func->function.suspend = ffs_func_suspend;
1484 func->function.resume = ffs_func_resume;
1485
1486 func->conf = c;
1487 func->gadget = cdev->gadget;
1488 func->ffs = ffs;
1489 ffs_data_get(ffs);
1490
1491 ret = usb_add_function(c, &func->function);
1492 if (unlikely(ret))
1493 ffs_func_free(func);
1494
1495 return ret;
1496}
1497
1498static void ffs_func_free(struct ffs_function *func)
1499{
1500 struct ffs_ep *ep = func->eps;
1501 unsigned count = func->ffs->eps_count;
1502 unsigned long flags;
1503
1504 ENTER();
1505
1506
1507 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1508 do {
1509 if (ep->ep && ep->req)
1510 usb_ep_free_request(ep->ep, ep->req);
1511 ep->req = NULL;
1512 ++ep;
1513 } while (--count);
1514 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1515
1516 ffs_data_put(func->ffs);
1517
1518 kfree(func->eps);
1519
1520
1521
1522
1523
1524
1525 kfree(func);
1526}
1527
1528static void ffs_func_eps_disable(struct ffs_function *func)
1529{
1530 struct ffs_ep *ep = func->eps;
1531 struct ffs_epfile *epfile = func->ffs->epfiles;
1532 unsigned count = func->ffs->eps_count;
1533 unsigned long flags;
1534
1535 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1536 do {
1537
1538 if (likely(ep->ep))
1539 usb_ep_disable(ep->ep);
1540 epfile->ep = NULL;
1541
1542 ++ep;
1543 ++epfile;
1544 } while (--count);
1545 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1546}
1547
1548static int ffs_func_eps_enable(struct ffs_function *func)
1549{
1550 struct ffs_data *ffs = func->ffs;
1551 struct ffs_ep *ep = func->eps;
1552 struct ffs_epfile *epfile = ffs->epfiles;
1553 unsigned count = ffs->eps_count;
1554 unsigned long flags;
1555 int ret = 0;
1556
1557 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1558 do {
1559 struct usb_endpoint_descriptor *ds;
1560 ds = ep->descs[ep->descs[1] ? 1 : 0];
1561
1562 ep->ep->driver_data = ep;
1563 ep->ep->desc = ds;
1564 ret = usb_ep_enable(ep->ep);
1565 if (likely(!ret)) {
1566 epfile->ep = ep;
1567 epfile->in = usb_endpoint_dir_in(ds);
1568 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1569 } else {
1570 break;
1571 }
1572
1573 wake_up(&epfile->wait);
1574
1575 ++ep;
1576 ++epfile;
1577 } while (--count);
1578 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1579
1580 return ret;
1581}
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593enum ffs_entity_type {
1594 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1595};
1596
1597typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1598 u8 *valuep,
1599 struct usb_descriptor_header *desc,
1600 void *priv);
1601
1602static int __must_check ffs_do_desc(char *data, unsigned len,
1603 ffs_entity_callback entity, void *priv)
1604{
1605 struct usb_descriptor_header *_ds = (void *)data;
1606 u8 length;
1607 int ret;
1608
1609 ENTER();
1610
1611
1612 if (len < 2) {
1613 pr_vdebug("descriptor too short\n");
1614 return -EINVAL;
1615 }
1616
1617
1618 length = _ds->bLength;
1619 if (len < length) {
1620 pr_vdebug("descriptor longer then available data\n");
1621 return -EINVAL;
1622 }
1623
1624#define __entity_check_INTERFACE(val) 1
1625#define __entity_check_STRING(val) (val)
1626#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1627#define __entity(type, val) do { \
1628 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1629 if (unlikely(!__entity_check_ ##type(val))) { \
1630 pr_vdebug("invalid entity's value\n"); \
1631 return -EINVAL; \
1632 } \
1633 ret = entity(FFS_ ##type, &val, _ds, priv); \
1634 if (unlikely(ret < 0)) { \
1635 pr_debug("entity " #type "(%02x); ret = %d\n", \
1636 (val), ret); \
1637 return ret; \
1638 } \
1639 } while (0)
1640
1641
1642 switch (_ds->bDescriptorType) {
1643 case USB_DT_DEVICE:
1644 case USB_DT_CONFIG:
1645 case USB_DT_STRING:
1646 case USB_DT_DEVICE_QUALIFIER:
1647
1648 pr_vdebug("descriptor reserved for gadget: %d\n",
1649 _ds->bDescriptorType);
1650 return -EINVAL;
1651
1652 case USB_DT_INTERFACE: {
1653 struct usb_interface_descriptor *ds = (void *)_ds;
1654 pr_vdebug("interface descriptor\n");
1655 if (length != sizeof *ds)
1656 goto inv_length;
1657
1658 __entity(INTERFACE, ds->bInterfaceNumber);
1659 if (ds->iInterface)
1660 __entity(STRING, ds->iInterface);
1661 }
1662 break;
1663
1664 case USB_DT_ENDPOINT: {
1665 struct usb_endpoint_descriptor *ds = (void *)_ds;
1666 pr_vdebug("endpoint descriptor\n");
1667 if (length != USB_DT_ENDPOINT_SIZE &&
1668 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1669 goto inv_length;
1670 __entity(ENDPOINT, ds->bEndpointAddress);
1671 }
1672 break;
1673
1674 case USB_DT_OTG:
1675 if (length != sizeof(struct usb_otg_descriptor))
1676 goto inv_length;
1677 break;
1678
1679 case USB_DT_INTERFACE_ASSOCIATION: {
1680 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1681 pr_vdebug("interface association descriptor\n");
1682 if (length != sizeof *ds)
1683 goto inv_length;
1684 if (ds->iFunction)
1685 __entity(STRING, ds->iFunction);
1686 }
1687 break;
1688
1689 case USB_DT_OTHER_SPEED_CONFIG:
1690 case USB_DT_INTERFACE_POWER:
1691 case USB_DT_DEBUG:
1692 case USB_DT_SECURITY:
1693 case USB_DT_CS_RADIO_CONTROL:
1694
1695 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1696 return -EINVAL;
1697
1698 default:
1699
1700 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1701 return -EINVAL;
1702
1703inv_length:
1704 pr_vdebug("invalid length: %d (descriptor %d)\n",
1705 _ds->bLength, _ds->bDescriptorType);
1706 return -EINVAL;
1707 }
1708
1709#undef __entity
1710#undef __entity_check_DESCRIPTOR
1711#undef __entity_check_INTERFACE
1712#undef __entity_check_STRING
1713#undef __entity_check_ENDPOINT
1714
1715 return length;
1716}
1717
1718static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1719 ffs_entity_callback entity, void *priv)
1720{
1721 const unsigned _len = len;
1722 unsigned long num = 0;
1723
1724 ENTER();
1725
1726 for (;;) {
1727 int ret;
1728
1729 if (num == count)
1730 data = NULL;
1731
1732
1733 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1734 if (unlikely(ret < 0)) {
1735 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1736 num, ret);
1737 return ret;
1738 }
1739
1740 if (!data)
1741 return _len - len;
1742
1743 ret = ffs_do_desc(data, len, entity, priv);
1744 if (unlikely(ret < 0)) {
1745 pr_debug("%s returns %d\n", __func__, ret);
1746 return ret;
1747 }
1748
1749 len -= ret;
1750 data += ret;
1751 ++num;
1752 }
1753}
1754
1755static int __ffs_data_do_entity(enum ffs_entity_type type,
1756 u8 *valuep, struct usb_descriptor_header *desc,
1757 void *priv)
1758{
1759 struct ffs_data *ffs = priv;
1760
1761 ENTER();
1762
1763 switch (type) {
1764 case FFS_DESCRIPTOR:
1765 break;
1766
1767 case FFS_INTERFACE:
1768
1769
1770
1771
1772
1773 if (*valuep >= ffs->interfaces_count)
1774 ffs->interfaces_count = *valuep + 1;
1775 break;
1776
1777 case FFS_STRING:
1778
1779
1780
1781
1782 if (*valuep > ffs->strings_count)
1783 ffs->strings_count = *valuep;
1784 break;
1785
1786 case FFS_ENDPOINT:
1787
1788 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1789 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1790 break;
1791 }
1792
1793 return 0;
1794}
1795
1796static int __ffs_data_got_descs(struct ffs_data *ffs,
1797 char *const _data, size_t len)
1798{
1799 unsigned fs_count, hs_count;
1800 int fs_len, ret = -EINVAL;
1801 char *data = _data;
1802
1803 ENTER();
1804
1805 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1806 get_unaligned_le32(data + 4) != len))
1807 goto error;
1808 fs_count = get_unaligned_le32(data + 8);
1809 hs_count = get_unaligned_le32(data + 12);
1810
1811 if (!fs_count && !hs_count)
1812 goto einval;
1813
1814 data += 16;
1815 len -= 16;
1816
1817 if (likely(fs_count)) {
1818 fs_len = ffs_do_descs(fs_count, data, len,
1819 __ffs_data_do_entity, ffs);
1820 if (unlikely(fs_len < 0)) {
1821 ret = fs_len;
1822 goto error;
1823 }
1824
1825 data += fs_len;
1826 len -= fs_len;
1827 } else {
1828 fs_len = 0;
1829 }
1830
1831 if (likely(hs_count)) {
1832 ret = ffs_do_descs(hs_count, data, len,
1833 __ffs_data_do_entity, ffs);
1834 if (unlikely(ret < 0))
1835 goto error;
1836 } else {
1837 ret = 0;
1838 }
1839
1840 if (unlikely(len != ret))
1841 goto einval;
1842
1843 ffs->raw_fs_descs_length = fs_len;
1844 ffs->raw_descs_length = fs_len + ret;
1845 ffs->raw_descs = _data;
1846 ffs->fs_descs_count = fs_count;
1847 ffs->hs_descs_count = hs_count;
1848
1849 return 0;
1850
1851einval:
1852 ret = -EINVAL;
1853error:
1854 kfree(_data);
1855 return ret;
1856}
1857
1858static int __ffs_data_got_strings(struct ffs_data *ffs,
1859 char *const _data, size_t len)
1860{
1861 u32 str_count, needed_count, lang_count;
1862 struct usb_gadget_strings **stringtabs, *t;
1863 struct usb_string *strings, *s;
1864 const char *data = _data;
1865
1866 ENTER();
1867
1868 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1869 get_unaligned_le32(data + 4) != len))
1870 goto error;
1871 str_count = get_unaligned_le32(data + 8);
1872 lang_count = get_unaligned_le32(data + 12);
1873
1874
1875 if (unlikely(!str_count != !lang_count))
1876 goto error;
1877
1878
1879 needed_count = ffs->strings_count;
1880 if (unlikely(str_count < needed_count))
1881 goto error;
1882
1883
1884
1885
1886
1887 if (!needed_count) {
1888 kfree(_data);
1889 return 0;
1890 }
1891
1892
1893 {
1894 struct {
1895 struct usb_gadget_strings *stringtabs[lang_count + 1];
1896 struct usb_gadget_strings stringtab[lang_count];
1897 struct usb_string strings[lang_count*(needed_count+1)];
1898 } *d;
1899 unsigned i = 0;
1900
1901 d = kmalloc(sizeof *d, GFP_KERNEL);
1902 if (unlikely(!d)) {
1903 kfree(_data);
1904 return -ENOMEM;
1905 }
1906
1907 stringtabs = d->stringtabs;
1908 t = d->stringtab;
1909 i = lang_count;
1910 do {
1911 *stringtabs++ = t++;
1912 } while (--i);
1913 *stringtabs = NULL;
1914
1915 stringtabs = d->stringtabs;
1916 t = d->stringtab;
1917 s = d->strings;
1918 strings = s;
1919 }
1920
1921
1922 data += 16;
1923 len -= 16;
1924
1925 do {
1926 unsigned needed = needed_count;
1927
1928 if (unlikely(len < 3))
1929 goto error_free;
1930 t->language = get_unaligned_le16(data);
1931 t->strings = s;
1932 ++t;
1933
1934 data += 2;
1935 len -= 2;
1936
1937
1938 do {
1939 size_t length = strnlen(data, len);
1940
1941 if (unlikely(length == len))
1942 goto error_free;
1943
1944
1945
1946
1947
1948
1949 if (likely(needed)) {
1950
1951
1952
1953
1954
1955 s->s = data;
1956 --needed;
1957 ++s;
1958 }
1959
1960 data += length + 1;
1961 len -= length + 1;
1962 } while (--str_count);
1963
1964 s->id = 0;
1965 s->s = NULL;
1966 ++s;
1967
1968 } while (--lang_count);
1969
1970
1971 if (unlikely(len))
1972 goto error_free;
1973
1974
1975 ffs->stringtabs = stringtabs;
1976 ffs->raw_strings = _data;
1977
1978 return 0;
1979
1980error_free:
1981 kfree(stringtabs);
1982error:
1983 kfree(_data);
1984 return -EINVAL;
1985}
1986
1987
1988
1989
1990static void __ffs_event_add(struct ffs_data *ffs,
1991 enum usb_functionfs_event_type type)
1992{
1993 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1994 int neg = 0;
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004 if (ffs->setup_state == FFS_SETUP_PENDING)
2005 ffs->setup_state = FFS_SETUP_CANCELED;
2006
2007 switch (type) {
2008 case FUNCTIONFS_RESUME:
2009 rem_type2 = FUNCTIONFS_SUSPEND;
2010
2011 case FUNCTIONFS_SUSPEND:
2012 case FUNCTIONFS_SETUP:
2013 rem_type1 = type;
2014
2015 break;
2016
2017 case FUNCTIONFS_BIND:
2018 case FUNCTIONFS_UNBIND:
2019 case FUNCTIONFS_DISABLE:
2020 case FUNCTIONFS_ENABLE:
2021
2022 rem_type1 = FUNCTIONFS_SUSPEND;
2023 rem_type2 = FUNCTIONFS_RESUME;
2024 neg = 1;
2025 break;
2026
2027 default:
2028 BUG();
2029 }
2030
2031 {
2032 u8 *ev = ffs->ev.types, *out = ev;
2033 unsigned n = ffs->ev.count;
2034 for (; n; --n, ++ev)
2035 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2036 *out++ = *ev;
2037 else
2038 pr_vdebug("purging event %d\n", *ev);
2039 ffs->ev.count = out - ffs->ev.types;
2040 }
2041
2042 pr_vdebug("adding event %d\n", type);
2043 ffs->ev.types[ffs->ev.count++] = type;
2044 wake_up_locked(&ffs->ev.waitq);
2045}
2046
2047static void ffs_event_add(struct ffs_data *ffs,
2048 enum usb_functionfs_event_type type)
2049{
2050 unsigned long flags;
2051 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2052 __ffs_event_add(ffs, type);
2053 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2054}
2055
2056
2057
2058
2059static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2060 struct usb_descriptor_header *desc,
2061 void *priv)
2062{
2063 struct usb_endpoint_descriptor *ds = (void *)desc;
2064 struct ffs_function *func = priv;
2065 struct ffs_ep *ffs_ep;
2066
2067
2068
2069
2070
2071 const int isHS = func->function.hs_descriptors != NULL;
2072 unsigned idx;
2073
2074 if (type != FFS_DESCRIPTOR)
2075 return 0;
2076
2077 if (isHS)
2078 func->function.hs_descriptors[(long)valuep] = desc;
2079 else
2080 func->function.descriptors[(long)valuep] = desc;
2081
2082 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2083 return 0;
2084
2085 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2086 ffs_ep = func->eps + idx;
2087
2088 if (unlikely(ffs_ep->descs[isHS])) {
2089 pr_vdebug("two %sspeed descriptors for EP %d\n",
2090 isHS ? "high" : "full",
2091 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2092 return -EINVAL;
2093 }
2094 ffs_ep->descs[isHS] = ds;
2095
2096 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2097 if (ffs_ep->ep) {
2098 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2099 if (!ds->wMaxPacketSize)
2100 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2101 } else {
2102 struct usb_request *req;
2103 struct usb_ep *ep;
2104
2105 pr_vdebug("autoconfig\n");
2106 ep = usb_ep_autoconfig(func->gadget, ds);
2107 if (unlikely(!ep))
2108 return -ENOTSUPP;
2109 ep->driver_data = func->eps + idx;
2110
2111 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2112 if (unlikely(!req))
2113 return -ENOMEM;
2114
2115 ffs_ep->ep = ep;
2116 ffs_ep->req = req;
2117 func->eps_revmap[ds->bEndpointAddress &
2118 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2119 }
2120 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2121
2122 return 0;
2123}
2124
2125static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2126 struct usb_descriptor_header *desc,
2127 void *priv)
2128{
2129 struct ffs_function *func = priv;
2130 unsigned idx;
2131 u8 newValue;
2132
2133 switch (type) {
2134 default:
2135 case FFS_DESCRIPTOR:
2136
2137 return 0;
2138
2139 case FFS_INTERFACE:
2140 idx = *valuep;
2141 if (func->interfaces_nums[idx] < 0) {
2142 int id = usb_interface_id(func->conf, &func->function);
2143 if (unlikely(id < 0))
2144 return id;
2145 func->interfaces_nums[idx] = id;
2146 }
2147 newValue = func->interfaces_nums[idx];
2148 break;
2149
2150 case FFS_STRING:
2151
2152 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2153 break;
2154
2155 case FFS_ENDPOINT:
2156
2157
2158
2159
2160 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2161 return 0;
2162
2163 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2164 if (unlikely(!func->eps[idx].ep))
2165 return -EINVAL;
2166
2167 {
2168 struct usb_endpoint_descriptor **descs;
2169 descs = func->eps[idx].descs;
2170 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2171 }
2172 break;
2173 }
2174
2175 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2176 *valuep = newValue;
2177 return 0;
2178}
2179
2180static int ffs_func_bind(struct usb_configuration *c,
2181 struct usb_function *f)
2182{
2183 struct ffs_function *func = ffs_func_from_usb(f);
2184 struct ffs_data *ffs = func->ffs;
2185
2186 const int full = !!func->ffs->fs_descs_count;
2187 const int high = gadget_is_dualspeed(func->gadget) &&
2188 func->ffs->hs_descs_count;
2189
2190 int ret;
2191
2192
2193 struct {
2194 struct ffs_ep eps[ffs->eps_count];
2195 struct usb_descriptor_header
2196 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2197 struct usb_descriptor_header
2198 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2199 short inums[ffs->interfaces_count];
2200 char raw_descs[high ? ffs->raw_descs_length
2201 : ffs->raw_fs_descs_length];
2202 } *data;
2203
2204 ENTER();
2205
2206
2207 if (unlikely(!(full | high)))
2208 return -ENOTSUPP;
2209
2210
2211 data = kmalloc(sizeof *data, GFP_KERNEL);
2212 if (unlikely(!data))
2213 return -ENOMEM;
2214
2215
2216 memset(data->eps, 0, sizeof data->eps);
2217 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2218 memset(data->inums, 0xff, sizeof data->inums);
2219 for (ret = ffs->eps_count; ret; --ret)
2220 data->eps[ret].num = -1;
2221
2222
2223 func->eps = data->eps;
2224 func->interfaces_nums = data->inums;
2225
2226
2227
2228
2229
2230
2231 if (likely(full)) {
2232 func->function.descriptors = data->fs_descs;
2233 ret = ffs_do_descs(ffs->fs_descs_count,
2234 data->raw_descs,
2235 sizeof data->raw_descs,
2236 __ffs_func_bind_do_descs, func);
2237 if (unlikely(ret < 0))
2238 goto error;
2239 } else {
2240 ret = 0;
2241 }
2242
2243 if (likely(high)) {
2244 func->function.hs_descriptors = data->hs_descs;
2245 ret = ffs_do_descs(ffs->hs_descs_count,
2246 data->raw_descs + ret,
2247 (sizeof data->raw_descs) - ret,
2248 __ffs_func_bind_do_descs, func);
2249 }
2250
2251
2252
2253
2254
2255
2256 ret = ffs_do_descs(ffs->fs_descs_count +
2257 (high ? ffs->hs_descs_count : 0),
2258 data->raw_descs, sizeof data->raw_descs,
2259 __ffs_func_bind_do_nums, func);
2260 if (unlikely(ret < 0))
2261 goto error;
2262
2263
2264 ffs_event_add(ffs, FUNCTIONFS_BIND);
2265 return 0;
2266
2267error:
2268
2269 return ret;
2270}
2271
2272
2273
2274
2275static void ffs_func_unbind(struct usb_configuration *c,
2276 struct usb_function *f)
2277{
2278 struct ffs_function *func = ffs_func_from_usb(f);
2279 struct ffs_data *ffs = func->ffs;
2280
2281 ENTER();
2282
2283 if (ffs->func == func) {
2284 ffs_func_eps_disable(func);
2285 ffs->func = NULL;
2286 }
2287
2288 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2289
2290 ffs_func_free(func);
2291}
2292
2293static int ffs_func_set_alt(struct usb_function *f,
2294 unsigned interface, unsigned alt)
2295{
2296 struct ffs_function *func = ffs_func_from_usb(f);
2297 struct ffs_data *ffs = func->ffs;
2298 int ret = 0, intf;
2299
2300 if (alt != (unsigned)-1) {
2301 intf = ffs_func_revmap_intf(func, interface);
2302 if (unlikely(intf < 0))
2303 return intf;
2304 }
2305
2306 if (ffs->func)
2307 ffs_func_eps_disable(ffs->func);
2308
2309 if (ffs->state != FFS_ACTIVE)
2310 return -ENODEV;
2311
2312 if (alt == (unsigned)-1) {
2313 ffs->func = NULL;
2314 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2315 return 0;
2316 }
2317
2318 ffs->func = func;
2319 ret = ffs_func_eps_enable(func);
2320 if (likely(ret >= 0))
2321 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2322 return ret;
2323}
2324
2325static void ffs_func_disable(struct usb_function *f)
2326{
2327 ffs_func_set_alt(f, 0, (unsigned)-1);
2328}
2329
2330static int ffs_func_setup(struct usb_function *f,
2331 const struct usb_ctrlrequest *creq)
2332{
2333 struct ffs_function *func = ffs_func_from_usb(f);
2334 struct ffs_data *ffs = func->ffs;
2335 unsigned long flags;
2336 int ret;
2337
2338 ENTER();
2339
2340 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2341 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2342 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2343 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2344 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355 if (ffs->state != FFS_ACTIVE)
2356 return -ENODEV;
2357
2358 switch (creq->bRequestType & USB_RECIP_MASK) {
2359 case USB_RECIP_INTERFACE:
2360 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2361 if (unlikely(ret < 0))
2362 return ret;
2363 break;
2364
2365 case USB_RECIP_ENDPOINT:
2366 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2367 if (unlikely(ret < 0))
2368 return ret;
2369 break;
2370
2371 default:
2372 return -EOPNOTSUPP;
2373 }
2374
2375 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2376 ffs->ev.setup = *creq;
2377 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2378 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2379 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2380
2381 return 0;
2382}
2383
2384static void ffs_func_suspend(struct usb_function *f)
2385{
2386 ENTER();
2387 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2388}
2389
2390static void ffs_func_resume(struct usb_function *f)
2391{
2392 ENTER();
2393 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2394}
2395
2396
2397
2398
2399static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2400{
2401 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2402 return num ? num : -EDOM;
2403}
2404
2405static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2406{
2407 short *nums = func->interfaces_nums;
2408 unsigned count = func->ffs->interfaces_count;
2409
2410 for (; count; --count, ++nums) {
2411 if (*nums >= 0 && *nums == intf)
2412 return nums - func->interfaces_nums;
2413 }
2414
2415 return -EDOM;
2416}
2417
2418
2419
2420
2421static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2422{
2423 return nonblock
2424 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2425 : mutex_lock_interruptible(mutex);
2426}
2427
2428static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2429{
2430 char *data;
2431
2432 if (unlikely(!len))
2433 return NULL;
2434
2435 data = kmalloc(len, GFP_KERNEL);
2436 if (unlikely(!data))
2437 return ERR_PTR(-ENOMEM);
2438
2439 if (unlikely(__copy_from_user(data, buf, len))) {
2440 kfree(data);
2441 return ERR_PTR(-EFAULT);
2442 }
2443
2444 pr_vdebug("Buffer from user space:\n");
2445 ffs_dump_mem("", data, len);
2446
2447 return data;
2448}
2449