36
36
#include <linux/kernel.h>
37
37
#include <linux/slab.h>
38
38
#include <linux/module.h>
39
- #include <linux/rwsem .h>
39
+ #include <linux/mutex .h>
40
40
#include <linux/usb/input.h>
41
41
#include <linux/map_to_7segment.h>
42
42
@@ -103,6 +103,8 @@ struct yealink_dev {
103
103
u8 lcdMap [ARRAY_SIZE (lcdMap )]; /* state of LCD, LED ... */
104
104
int key_code ; /* last reported key */
105
105
106
+ struct mutex sysfs_mutex ;
107
+
106
108
unsigned int shutdown :1 ;
107
109
108
110
int stat_ix ;
@@ -548,8 +550,6 @@ static void input_close(struct input_dev *dev)
548
550
* sysfs interface
549
551
******************************************************************************/
550
552
551
- static DECLARE_RWSEM (sysfs_rwsema );
552
-
553
553
/* Interface to the 7-segments translation table aka. char set.
554
554
*/
555
555
static ssize_t show_map (struct device * dev , struct device_attribute * attr ,
@@ -580,15 +580,10 @@ static ssize_t store_map(struct device *dev, struct device_attribute *attr,
580
580
*/
581
581
static ssize_t show_line (struct device * dev , char * buf , int a , int b )
582
582
{
583
- struct yealink_dev * yld ;
583
+ struct yealink_dev * yld = dev_get_drvdata ( dev ) ;
584
584
int i ;
585
585
586
- down_read (& sysfs_rwsema );
587
- yld = dev_get_drvdata (dev );
588
- if (yld == NULL ) {
589
- up_read (& sysfs_rwsema );
590
- return - ENODEV ;
591
- }
586
+ guard (mutex )(& yld -> sysfs_mutex );
592
587
593
588
for (i = a ; i < b ; i ++ )
594
589
* buf ++ = lcdMap [i ].type ;
@@ -598,7 +593,6 @@ static ssize_t show_line(struct device *dev, char *buf, int a, int b)
598
593
* buf ++ = '\n' ;
599
594
* buf = 0 ;
600
595
601
- up_read (& sysfs_rwsema );
602
596
return 3 + ((b - a ) << 1 );
603
597
}
604
598
@@ -630,22 +624,16 @@ static ssize_t show_line3(struct device *dev, struct device_attribute *attr,
630
624
static ssize_t store_line (struct device * dev , const char * buf , size_t count ,
631
625
int el , size_t len )
632
626
{
633
- struct yealink_dev * yld ;
627
+ struct yealink_dev * yld = dev_get_drvdata ( dev ) ;
634
628
int i ;
635
629
636
- down_write (& sysfs_rwsema );
637
- yld = dev_get_drvdata (dev );
638
- if (yld == NULL ) {
639
- up_write (& sysfs_rwsema );
640
- return - ENODEV ;
641
- }
630
+ guard (mutex )(& yld -> sysfs_mutex );
642
631
643
632
if (len > count )
644
633
len = count ;
645
634
for (i = 0 ; i < len ; i ++ )
646
635
setChar (yld , el ++ , buf [i ]);
647
636
648
- up_write (& sysfs_rwsema );
649
637
return count ;
650
638
}
651
639
@@ -675,15 +663,10 @@ static ssize_t store_line3(struct device *dev, struct device_attribute *attr,
675
663
static ssize_t get_icons (struct device * dev , struct device_attribute * attr ,
676
664
char * buf )
677
665
{
678
- struct yealink_dev * yld ;
666
+ struct yealink_dev * yld = dev_get_drvdata ( dev ) ;
679
667
int i , ret = 1 ;
680
668
681
- down_read (& sysfs_rwsema );
682
- yld = dev_get_drvdata (dev );
683
- if (yld == NULL ) {
684
- up_read (& sysfs_rwsema );
685
- return - ENODEV ;
686
- }
669
+ guard (mutex )(& yld -> sysfs_mutex );
687
670
688
671
for (i = 0 ; i < ARRAY_SIZE (lcdMap ); i ++ ) {
689
672
if (lcdMap [i ].type != '.' )
@@ -692,23 +675,18 @@ static ssize_t get_icons(struct device *dev, struct device_attribute *attr,
692
675
yld -> lcdMap [i ] == ' ' ? " " : "on" ,
693
676
lcdMap [i ].u .p .name );
694
677
}
695
- up_read ( & sysfs_rwsema );
678
+
696
679
return ret ;
697
680
}
698
681
699
682
/* Change the visibility of a particular element. */
700
683
static ssize_t set_icon (struct device * dev , const char * buf , size_t count ,
701
684
int chr )
702
685
{
703
- struct yealink_dev * yld ;
686
+ struct yealink_dev * yld = dev_get_drvdata ( dev ) ;
704
687
int i ;
705
688
706
- down_write (& sysfs_rwsema );
707
- yld = dev_get_drvdata (dev );
708
- if (yld == NULL ) {
709
- up_write (& sysfs_rwsema );
710
- return - ENODEV ;
711
- }
689
+ guard (mutex )(& yld -> sysfs_mutex );
712
690
713
691
for (i = 0 ; i < ARRAY_SIZE (lcdMap ); i ++ ) {
714
692
if (lcdMap [i ].type != '.' )
@@ -719,7 +697,6 @@ static ssize_t set_icon(struct device *dev, const char *buf, size_t count,
719
697
}
720
698
}
721
699
722
- up_write (& sysfs_rwsema );
723
700
return count ;
724
701
}
725
702
@@ -739,22 +716,16 @@ static ssize_t hide_icon(struct device *dev, struct device_attribute *attr,
739
716
*/
740
717
741
718
/* Stores raw ringtone data in the phone */
742
- static ssize_t store_ringtone (struct device * dev ,
743
- struct device_attribute * attr ,
744
- const char * buf , size_t count )
719
+ static ssize_t store_ringtone (struct device * dev , struct device_attribute * attr ,
720
+ const char * buf , size_t count )
745
721
{
746
- struct yealink_dev * yld ;
722
+ struct yealink_dev * yld = dev_get_drvdata ( dev ) ;
747
723
748
- down_write (& sysfs_rwsema );
749
- yld = dev_get_drvdata (dev );
750
- if (yld == NULL ) {
751
- up_write (& sysfs_rwsema );
752
- return - ENODEV ;
753
- }
724
+ guard (mutex )(& yld -> sysfs_mutex );
754
725
755
726
/* TODO locking with async usb control interface??? */
756
727
yealink_set_ringtone (yld , (char * )buf , count );
757
- up_write ( & sysfs_rwsema );
728
+
758
729
return count ;
759
730
}
760
731
@@ -835,14 +806,10 @@ static int usb_cleanup(struct yealink_dev *yld, int err)
835
806
836
807
static void usb_disconnect (struct usb_interface * intf )
837
808
{
838
- struct yealink_dev * yld ;
839
-
840
- down_write (& sysfs_rwsema );
841
- yld = usb_get_intfdata (intf );
842
- usb_set_intfdata (intf , NULL );
843
- up_write (& sysfs_rwsema );
809
+ struct yealink_dev * yld = usb_get_intfdata (intf );
844
810
845
811
usb_cleanup (yld , 0 );
812
+ usb_set_intfdata (intf , NULL );
846
813
}
847
814
848
815
static int usb_probe (struct usb_interface * intf , const struct usb_device_id * id )
@@ -870,6 +837,7 @@ static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
870
837
871
838
yld -> udev = udev ;
872
839
yld -> intf = intf ;
840
+ mutex_init (& yld -> sysfs_mutex );
873
841
874
842
yld -> idev = input_dev = input_allocate_device ();
875
843
if (!input_dev )
0 commit comments