@@ -749,6 +749,9 @@ static struct {
749
749
* it is trigged by reading /sys/class/rc/rc?/protocols.
750
750
* It returns the protocol names of supported protocols.
751
751
* Enabled protocols are printed in brackets.
752
+ *
753
+ * dev->lock is taken to guard against races between device
754
+ * registration, store_protocols and show_protocols.
752
755
*/
753
756
static ssize_t show_protocols (struct device * device ,
754
757
struct device_attribute * mattr , char * buf )
@@ -762,6 +765,8 @@ static ssize_t show_protocols(struct device *device,
762
765
if (!dev )
763
766
return - EINVAL ;
764
767
768
+ mutex_lock (& dev -> lock );
769
+
765
770
if (dev -> driver_type == RC_DRIVER_SCANCODE ) {
766
771
enabled = dev -> rc_map .rc_type ;
767
772
allowed = dev -> allowed_protos ;
@@ -784,6 +789,9 @@ static ssize_t show_protocols(struct device *device,
784
789
if (tmp != buf )
785
790
tmp -- ;
786
791
* tmp = '\n' ;
792
+
793
+ mutex_unlock (& dev -> lock );
794
+
787
795
return tmp + 1 - buf ;
788
796
}
789
797
@@ -802,6 +810,9 @@ static ssize_t show_protocols(struct device *device,
802
810
* Writing "none" will disable all protocols.
803
811
* Returns -EINVAL if an invalid protocol combination or unknown protocol name
804
812
* is used, otherwise @len.
813
+ *
814
+ * dev->lock is taken to guard against races between device
815
+ * registration, store_protocols and show_protocols.
805
816
*/
806
817
static ssize_t store_protocols (struct device * device ,
807
818
struct device_attribute * mattr ,
@@ -815,18 +826,22 @@ static ssize_t store_protocols(struct device *device,
815
826
u64 mask ;
816
827
int rc , i , count = 0 ;
817
828
unsigned long flags ;
829
+ ssize_t ret ;
818
830
819
831
/* Device is being removed */
820
832
if (!dev )
821
833
return - EINVAL ;
822
834
835
+ mutex_lock (& dev -> lock );
836
+
823
837
if (dev -> driver_type == RC_DRIVER_SCANCODE )
824
838
type = dev -> rc_map .rc_type ;
825
839
else if (dev -> raw )
826
840
type = dev -> raw -> enabled_protocols ;
827
841
else {
828
842
IR_dprintk (1 , "Protocol switching not supported\n" );
829
- return - EINVAL ;
843
+ ret = - EINVAL ;
844
+ goto out ;
830
845
}
831
846
832
847
while ((tmp = strsep ((char * * ) & data , " \n" )) != NULL ) {
@@ -860,7 +875,8 @@ static ssize_t store_protocols(struct device *device,
860
875
}
861
876
if (i == ARRAY_SIZE (proto_names )) {
862
877
IR_dprintk (1 , "Unknown protocol: '%s'\n" , tmp );
863
- return - EINVAL ;
878
+ ret = - EINVAL ;
879
+ goto out ;
864
880
}
865
881
count ++ ;
866
882
}
@@ -875,15 +891,17 @@ static ssize_t store_protocols(struct device *device,
875
891
876
892
if (!count ) {
877
893
IR_dprintk (1 , "Protocol not specified\n" );
878
- return - EINVAL ;
894
+ ret = - EINVAL ;
895
+ goto out ;
879
896
}
880
897
881
898
if (dev -> change_protocol ) {
882
899
rc = dev -> change_protocol (dev , type );
883
900
if (rc < 0 ) {
884
901
IR_dprintk (1 , "Error setting protocols to 0x%llx\n" ,
885
902
(long long )type );
886
- return - EINVAL ;
903
+ ret = - EINVAL ;
904
+ goto out ;
887
905
}
888
906
}
889
907
@@ -898,7 +916,11 @@ static ssize_t store_protocols(struct device *device,
898
916
IR_dprintk (1 , "Current protocol(s): 0x%llx\n" ,
899
917
(long long )type );
900
918
901
- return len ;
919
+ ret = len ;
920
+
921
+ out :
922
+ mutex_unlock (& dev -> lock );
923
+ return ret ;
902
924
}
903
925
904
926
static void rc_dev_release (struct device * device )
@@ -974,6 +996,7 @@ struct rc_dev *rc_allocate_device(void)
974
996
975
997
spin_lock_init (& dev -> rc_map .lock );
976
998
spin_lock_init (& dev -> keylock );
999
+ mutex_init (& dev -> lock );
977
1000
setup_timer (& dev -> timer_keyup , ir_timer_keyup , (unsigned long )dev );
978
1001
979
1002
dev -> dev .type = & rc_dev_type ;
@@ -1019,12 +1042,21 @@ int rc_register_device(struct rc_dev *dev)
1019
1042
if (dev -> close )
1020
1043
dev -> input_dev -> close = ir_close ;
1021
1044
1045
+ /*
1046
+ * Take the lock here, as the device sysfs node will appear
1047
+ * when device_add() is called, which may trigger an ir-keytable udev
1048
+ * rule, which will in turn call show_protocols and access either
1049
+ * dev->rc_map.rc_type or dev->raw->enabled_protocols before it has
1050
+ * been initialized.
1051
+ */
1052
+ mutex_lock (& dev -> lock );
1053
+
1022
1054
dev -> devno = (unsigned long )(atomic_inc_return (& devno ) - 1 );
1023
1055
dev_set_name (& dev -> dev , "rc%ld" , dev -> devno );
1024
1056
dev_set_drvdata (& dev -> dev , dev );
1025
1057
rc = device_add (& dev -> dev );
1026
1058
if (rc )
1027
- return rc ;
1059
+ goto out_unlock ;
1028
1060
1029
1061
rc = ir_setkeytable (dev , rc_map );
1030
1062
if (rc )
@@ -1058,6 +1090,7 @@ int rc_register_device(struct rc_dev *dev)
1058
1090
if (rc < 0 )
1059
1091
goto out_input ;
1060
1092
}
1093
+ mutex_unlock (& dev -> lock );
1061
1094
1062
1095
if (dev -> change_protocol ) {
1063
1096
rc = dev -> change_protocol (dev , rc_map -> rc_type );
@@ -1083,6 +1116,8 @@ int rc_register_device(struct rc_dev *dev)
1083
1116
ir_free_table (& dev -> rc_map );
1084
1117
out_dev :
1085
1118
device_del (& dev -> dev );
1119
+ out_unlock :
1120
+ mutex_unlock (& dev -> lock );
1086
1121
return rc ;
1087
1122
}
1088
1123
EXPORT_SYMBOL_GPL (rc_register_device );
0 commit comments