@@ -74,6 +74,8 @@ MODULE_DESCRIPTION (DRIVER_DESC);
74
74
MODULE_AUTHOR ("David Brownell" );
75
75
MODULE_LICENSE ("GPL" );
76
76
77
+ static int ep_open (struct inode * , struct file * );
78
+
77
79
78
80
/*----------------------------------------------------------------------*/
79
81
@@ -283,14 +285,15 @@ static void epio_complete (struct usb_ep *ep, struct usb_request *req)
283
285
* still need dev->lock to use epdata->ep.
284
286
*/
285
287
static int
286
- get_ready_ep (unsigned f_flags , struct ep_data * epdata )
288
+ get_ready_ep (unsigned f_flags , struct ep_data * epdata , bool is_write )
287
289
{
288
290
int val ;
289
291
290
292
if (f_flags & O_NONBLOCK ) {
291
293
if (!mutex_trylock (& epdata -> lock ))
292
294
goto nonblock ;
293
- if (epdata -> state != STATE_EP_ENABLED ) {
295
+ if (epdata -> state != STATE_EP_ENABLED &&
296
+ (!is_write || epdata -> state != STATE_EP_READY )) {
294
297
mutex_unlock (& epdata -> lock );
295
298
nonblock :
296
299
val = - EAGAIN ;
@@ -305,18 +308,20 @@ get_ready_ep (unsigned f_flags, struct ep_data *epdata)
305
308
306
309
switch (epdata -> state ) {
307
310
case STATE_EP_ENABLED :
311
+ return 0 ;
312
+ case STATE_EP_READY : /* not configured yet */
313
+ if (is_write )
314
+ return 0 ;
315
+ // FALLTHRU
316
+ case STATE_EP_UNBOUND : /* clean disconnect */
308
317
break ;
309
318
// case STATE_EP_DISABLED: /* "can't happen" */
310
- // case STATE_EP_READY: /* "can't happen" */
311
319
default : /* error! */
312
320
pr_debug ("%s: ep %p not available, state %d\n" ,
313
321
shortname , epdata , epdata -> state );
314
- // FALLTHROUGH
315
- case STATE_EP_UNBOUND : /* clean disconnect */
316
- val = - ENODEV ;
317
- mutex_unlock (& epdata -> lock );
318
322
}
319
- return val ;
323
+ mutex_unlock (& epdata -> lock );
324
+ return - ENODEV ;
320
325
}
321
326
322
327
static ssize_t
@@ -390,7 +395,7 @@ static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
390
395
struct ep_data * data = fd -> private_data ;
391
396
int status ;
392
397
393
- if ((status = get_ready_ep (fd -> f_flags , data )) < 0 )
398
+ if ((status = get_ready_ep (fd -> f_flags , data , false )) < 0 )
394
399
return status ;
395
400
396
401
spin_lock_irq (& data -> dev -> lock );
@@ -572,7 +577,7 @@ ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
572
577
ssize_t value ;
573
578
char * buf ;
574
579
575
- if ((value = get_ready_ep (file -> f_flags , epdata )) < 0 )
580
+ if ((value = get_ready_ep (file -> f_flags , epdata , false )) < 0 )
576
581
return value ;
577
582
578
583
/* halt any endpoint by doing a "wrong direction" i/o call */
@@ -620,20 +625,25 @@ ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
620
625
return value ;
621
626
}
622
627
628
+ static ssize_t ep_config (struct ep_data * , const char * , size_t );
629
+
623
630
static ssize_t
624
631
ep_write_iter (struct kiocb * iocb , struct iov_iter * from )
625
632
{
626
633
struct file * file = iocb -> ki_filp ;
627
634
struct ep_data * epdata = file -> private_data ;
628
635
size_t len = iov_iter_count (from );
636
+ bool configured ;
629
637
ssize_t value ;
630
638
char * buf ;
631
639
632
- if ((value = get_ready_ep (file -> f_flags , epdata )) < 0 )
640
+ if ((value = get_ready_ep (file -> f_flags , epdata , true )) < 0 )
633
641
return value ;
634
642
643
+ configured = epdata -> state == STATE_EP_ENABLED ;
644
+
635
645
/* halt any endpoint by doing a "wrong direction" i/o call */
636
- if (!usb_endpoint_dir_in (& epdata -> desc )) {
646
+ if (configured && !usb_endpoint_dir_in (& epdata -> desc )) {
637
647
if (usb_endpoint_xfer_isoc (& epdata -> desc ) ||
638
648
!is_sync_kiocb (iocb )) {
639
649
mutex_unlock (& epdata -> lock );
@@ -659,7 +669,9 @@ ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
659
669
goto out ;
660
670
}
661
671
662
- if (is_sync_kiocb (iocb )) {
672
+ if (unlikely (!configured )) {
673
+ value = ep_config (epdata , buf , len );
674
+ } else if (is_sync_kiocb (iocb )) {
663
675
value = ep_io (epdata , buf , len );
664
676
} else {
665
677
struct kiocb_priv * priv = kzalloc (sizeof * priv , GFP_KERNEL );
@@ -681,13 +693,13 @@ ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
681
693
/* used after endpoint configuration */
682
694
static const struct file_operations ep_io_operations = {
683
695
.owner = THIS_MODULE ,
684
- .llseek = no_llseek ,
685
696
697
+ .open = ep_open ,
698
+ .release = ep_release ,
699
+ .llseek = no_llseek ,
686
700
.read = new_sync_read ,
687
701
.write = new_sync_write ,
688
702
.unlocked_ioctl = ep_ioctl ,
689
- .release = ep_release ,
690
-
691
703
.read_iter = ep_read_iter ,
692
704
.write_iter = ep_write_iter ,
693
705
};
@@ -706,17 +718,12 @@ static const struct file_operations ep_io_operations = {
706
718
* speed descriptor, then optional high speed descriptor.
707
719
*/
708
720
static ssize_t
709
- ep_config (struct file * fd , const char __user * buf , size_t len , loff_t * ptr )
721
+ ep_config (struct ep_data * data , const char * buf , size_t len )
710
722
{
711
- struct ep_data * data = fd -> private_data ;
712
723
struct usb_ep * ep ;
713
724
u32 tag ;
714
725
int value , length = len ;
715
726
716
- value = mutex_lock_interruptible (& data -> lock );
717
- if (value < 0 )
718
- return value ;
719
-
720
727
if (data -> state != STATE_EP_READY ) {
721
728
value = - EL2HLT ;
722
729
goto fail ;
@@ -727,9 +734,7 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
727
734
goto fail0 ;
728
735
729
736
/* we might need to change message format someday */
730
- if (copy_from_user (& tag , buf , 4 )) {
731
- goto fail1 ;
732
- }
737
+ memcpy (& tag , buf , 4 );
733
738
if (tag != 1 ) {
734
739
DBG (data -> dev , "config %s, bad tag %d\n" , data -> name , tag );
735
740
goto fail0 ;
@@ -742,19 +747,15 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
742
747
*/
743
748
744
749
/* full/low speed descriptor, then high speed */
745
- if (copy_from_user (& data -> desc , buf , USB_DT_ENDPOINT_SIZE )) {
746
- goto fail1 ;
747
- }
750
+ memcpy (& data -> desc , buf , USB_DT_ENDPOINT_SIZE );
748
751
if (data -> desc .bLength != USB_DT_ENDPOINT_SIZE
749
752
|| data -> desc .bDescriptorType != USB_DT_ENDPOINT )
750
753
goto fail0 ;
751
754
if (len != USB_DT_ENDPOINT_SIZE ) {
752
755
if (len != 2 * USB_DT_ENDPOINT_SIZE )
753
756
goto fail0 ;
754
- if (copy_from_user (& data -> hs_desc , buf + USB_DT_ENDPOINT_SIZE ,
755
- USB_DT_ENDPOINT_SIZE )) {
756
- goto fail1 ;
757
- }
757
+ memcpy (& data -> hs_desc , buf + USB_DT_ENDPOINT_SIZE ,
758
+ USB_DT_ENDPOINT_SIZE );
758
759
if (data -> hs_desc .bLength != USB_DT_ENDPOINT_SIZE
759
760
|| data -> hs_desc .bDescriptorType
760
761
!= USB_DT_ENDPOINT ) {
@@ -776,24 +777,20 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
776
777
case USB_SPEED_LOW :
777
778
case USB_SPEED_FULL :
778
779
ep -> desc = & data -> desc ;
779
- value = usb_ep_enable (ep );
780
- if (value == 0 )
781
- data -> state = STATE_EP_ENABLED ;
782
780
break ;
783
781
case USB_SPEED_HIGH :
784
782
/* fails if caller didn't provide that descriptor... */
785
783
ep -> desc = & data -> hs_desc ;
786
- value = usb_ep_enable (ep );
787
- if (value == 0 )
788
- data -> state = STATE_EP_ENABLED ;
789
784
break ;
790
785
default :
791
786
DBG (data -> dev , "unconnected, %s init abandoned\n" ,
792
787
data -> name );
793
788
value = - EINVAL ;
789
+ goto gone ;
794
790
}
791
+ value = usb_ep_enable (ep );
795
792
if (value == 0 ) {
796
- fd -> f_op = & ep_io_operations ;
793
+ data -> state = STATE_EP_ENABLED ;
797
794
value = length ;
798
795
}
799
796
gone :
@@ -803,14 +800,10 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
803
800
data -> desc .bDescriptorType = 0 ;
804
801
data -> hs_desc .bDescriptorType = 0 ;
805
802
}
806
- mutex_unlock (& data -> lock );
807
803
return value ;
808
804
fail0 :
809
805
value = - EINVAL ;
810
806
goto fail ;
811
- fail1 :
812
- value = - EFAULT ;
813
- goto fail ;
814
807
}
815
808
816
809
static int
@@ -838,15 +831,6 @@ ep_open (struct inode *inode, struct file *fd)
838
831
return value ;
839
832
}
840
833
841
- /* used before endpoint configuration */
842
- static const struct file_operations ep_config_operations = {
843
- .llseek = no_llseek ,
844
-
845
- .open = ep_open ,
846
- .write = ep_config ,
847
- .release = ep_release ,
848
- };
849
-
850
834
/*----------------------------------------------------------------------*/
851
835
852
836
/* EP0 IMPLEMENTATION can be partly in userspace.
@@ -1586,7 +1570,7 @@ static int activate_ep_files (struct dev_data *dev)
1586
1570
goto enomem1 ;
1587
1571
1588
1572
data -> dentry = gadgetfs_create_file (dev -> sb , data -> name ,
1589
- data , & ep_config_operations );
1573
+ data , & ep_io_operations );
1590
1574
if (!data -> dentry )
1591
1575
goto enomem2 ;
1592
1576
list_add_tail (& data -> epfiles , & dev -> epfiles );
0 commit comments