@@ -1057,8 +1057,19 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
1057
1057
desc -> gdev = gdev ;
1058
1058
1059
1059
if (gc -> get_direction && gpiochip_line_is_valid (gc , desc_index )) {
1060
- assign_bit (FLAG_IS_OUT ,
1061
- & desc -> flags , !gc -> get_direction (gc , desc_index ));
1060
+ ret = gc -> get_direction (gc , desc_index );
1061
+ if (ret < 0 )
1062
+ /*
1063
+ * FIXME: Bail-out here once all GPIO drivers
1064
+ * are updated to not return errors in
1065
+ * situations that can be considered normal
1066
+ * operation.
1067
+ */
1068
+ dev_warn (& gdev -> dev ,
1069
+ "%s: get_direction failed: %d\n" ,
1070
+ __func__ , ret );
1071
+
1072
+ assign_bit (FLAG_IS_OUT , & desc -> flags , !ret );
1062
1073
} else {
1063
1074
assign_bit (FLAG_IS_OUT ,
1064
1075
& desc -> flags , !gc -> direction_input );
@@ -2728,13 +2739,18 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
2728
2739
if (guard .gc -> direction_input ) {
2729
2740
ret = guard .gc -> direction_input (guard .gc ,
2730
2741
gpio_chip_hwgpio (desc ));
2731
- } else if (guard .gc -> get_direction &&
2732
- (guard .gc -> get_direction (guard .gc ,
2733
- gpio_chip_hwgpio (desc )) != 1 )) {
2734
- gpiod_warn (desc ,
2735
- "%s: missing direction_input() operation and line is output\n" ,
2736
- __func__ );
2737
- return - EIO ;
2742
+ } else if (guard .gc -> get_direction ) {
2743
+ ret = guard .gc -> get_direction (guard .gc ,
2744
+ gpio_chip_hwgpio (desc ));
2745
+ if (ret < 0 )
2746
+ return ret ;
2747
+
2748
+ if (ret != GPIO_LINE_DIRECTION_IN ) {
2749
+ gpiod_warn (desc ,
2750
+ "%s: missing direction_input() operation and line is output\n" ,
2751
+ __func__ );
2752
+ return - EIO ;
2753
+ }
2738
2754
}
2739
2755
if (ret == 0 ) {
2740
2756
clear_bit (FLAG_IS_OUT , & desc -> flags );
@@ -2771,12 +2787,18 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2771
2787
gpio_chip_hwgpio (desc ), val );
2772
2788
} else {
2773
2789
/* Check that we are in output mode if we can */
2774
- if (guard .gc -> get_direction &&
2775
- guard .gc -> get_direction (guard .gc , gpio_chip_hwgpio (desc ))) {
2776
- gpiod_warn (desc ,
2777
- "%s: missing direction_output() operation\n" ,
2778
- __func__ );
2779
- return - EIO ;
2790
+ if (guard .gc -> get_direction ) {
2791
+ ret = guard .gc -> get_direction (guard .gc ,
2792
+ gpio_chip_hwgpio (desc ));
2793
+ if (ret < 0 )
2794
+ return ret ;
2795
+
2796
+ if (ret != GPIO_LINE_DIRECTION_OUT ) {
2797
+ gpiod_warn (desc ,
2798
+ "%s: missing direction_output() operation\n" ,
2799
+ __func__ );
2800
+ return - EIO ;
2801
+ }
2780
2802
}
2781
2803
/*
2782
2804
* If we can't actively set the direction, we are some
@@ -3129,6 +3151,8 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
3129
3151
static int gpio_chip_get_multiple (struct gpio_chip * gc ,
3130
3152
unsigned long * mask , unsigned long * bits )
3131
3153
{
3154
+ lockdep_assert_held (& gc -> gpiodev -> srcu );
3155
+
3132
3156
if (gc -> get_multiple )
3133
3157
return gc -> get_multiple (gc , mask , bits );
3134
3158
if (gc -> get ) {
@@ -3159,6 +3183,7 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3159
3183
struct gpio_array * array_info ,
3160
3184
unsigned long * value_bitmap )
3161
3185
{
3186
+ struct gpio_chip * gc ;
3162
3187
int ret , i = 0 ;
3163
3188
3164
3189
/*
@@ -3170,10 +3195,15 @@ int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3170
3195
array_size <= array_info -> size &&
3171
3196
(void * )array_info == desc_array + array_info -> size ) {
3172
3197
if (!can_sleep )
3173
- WARN_ON (array_info -> chip -> can_sleep );
3198
+ WARN_ON (array_info -> gdev -> can_sleep );
3199
+
3200
+ guard (srcu )(& array_info -> gdev -> srcu );
3201
+ gc = srcu_dereference (array_info -> gdev -> chip ,
3202
+ & array_info -> gdev -> srcu );
3203
+ if (!gc )
3204
+ return - ENODEV ;
3174
3205
3175
- ret = gpio_chip_get_multiple (array_info -> chip ,
3176
- array_info -> get_mask ,
3206
+ ret = gpio_chip_get_multiple (gc , array_info -> get_mask ,
3177
3207
value_bitmap );
3178
3208
if (ret )
3179
3209
return ret ;
@@ -3454,6 +3484,8 @@ static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
3454
3484
static void gpio_chip_set_multiple (struct gpio_chip * gc ,
3455
3485
unsigned long * mask , unsigned long * bits )
3456
3486
{
3487
+ lockdep_assert_held (& gc -> gpiodev -> srcu );
3488
+
3457
3489
if (gc -> set_multiple ) {
3458
3490
gc -> set_multiple (gc , mask , bits );
3459
3491
} else {
@@ -3471,6 +3503,7 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3471
3503
struct gpio_array * array_info ,
3472
3504
unsigned long * value_bitmap )
3473
3505
{
3506
+ struct gpio_chip * gc ;
3474
3507
int i = 0 ;
3475
3508
3476
3509
/*
@@ -3482,14 +3515,19 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3482
3515
array_size <= array_info -> size &&
3483
3516
(void * )array_info == desc_array + array_info -> size ) {
3484
3517
if (!can_sleep )
3485
- WARN_ON (array_info -> chip -> can_sleep );
3518
+ WARN_ON (array_info -> gdev -> can_sleep );
3519
+
3520
+ guard (srcu )(& array_info -> gdev -> srcu );
3521
+ gc = srcu_dereference (array_info -> gdev -> chip ,
3522
+ & array_info -> gdev -> srcu );
3523
+ if (!gc )
3524
+ return - ENODEV ;
3486
3525
3487
3526
if (!raw && !bitmap_empty (array_info -> invert_mask , array_size ))
3488
3527
bitmap_xor (value_bitmap , value_bitmap ,
3489
3528
array_info -> invert_mask , array_size );
3490
3529
3491
- gpio_chip_set_multiple (array_info -> chip , array_info -> set_mask ,
3492
- value_bitmap );
3530
+ gpio_chip_set_multiple (gc , array_info -> set_mask , value_bitmap );
3493
3531
3494
3532
i = find_first_zero_bit (array_info -> set_mask , array_size );
3495
3533
if (i == array_size )
@@ -4751,9 +4789,10 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4751
4789
{
4752
4790
struct gpio_desc * desc ;
4753
4791
struct gpio_descs * descs ;
4792
+ struct gpio_device * gdev ;
4754
4793
struct gpio_array * array_info = NULL ;
4755
- struct gpio_chip * gc ;
4756
4794
int count , bitmap_size ;
4795
+ unsigned long dflags ;
4757
4796
size_t descs_size ;
4758
4797
4759
4798
count = gpiod_count (dev , con_id );
@@ -4774,16 +4813,16 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4774
4813
4775
4814
descs -> desc [descs -> ndescs ] = desc ;
4776
4815
4777
- gc = gpiod_to_chip (desc );
4816
+ gdev = gpiod_to_gpio_device (desc );
4778
4817
/*
4779
4818
* If pin hardware number of array member 0 is also 0, select
4780
4819
* its chip as a candidate for fast bitmap processing path.
4781
4820
*/
4782
4821
if (descs -> ndescs == 0 && gpio_chip_hwgpio (desc ) == 0 ) {
4783
4822
struct gpio_descs * array ;
4784
4823
4785
- bitmap_size = BITS_TO_LONGS (gc -> ngpio > count ?
4786
- gc -> ngpio : count );
4824
+ bitmap_size = BITS_TO_LONGS (gdev -> ngpio > count ?
4825
+ gdev -> ngpio : count );
4787
4826
4788
4827
array = krealloc (descs , descs_size +
4789
4828
struct_size (array_info , invert_mask , 3 * bitmap_size ),
@@ -4803,7 +4842,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4803
4842
4804
4843
array_info -> desc = descs -> desc ;
4805
4844
array_info -> size = count ;
4806
- array_info -> chip = gc ;
4845
+ array_info -> gdev = gdev ;
4807
4846
bitmap_set (array_info -> get_mask , descs -> ndescs ,
4808
4847
count - descs -> ndescs );
4809
4848
bitmap_set (array_info -> set_mask , descs -> ndescs ,
@@ -4816,7 +4855,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4816
4855
continue ;
4817
4856
4818
4857
/* Unmark array members which don't belong to the 'fast' chip */
4819
- if (array_info -> chip != gc ) {
4858
+ if (array_info -> gdev != gdev ) {
4820
4859
__clear_bit (descs -> ndescs , array_info -> get_mask );
4821
4860
__clear_bit (descs -> ndescs , array_info -> set_mask );
4822
4861
}
@@ -4839,9 +4878,10 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4839
4878
array_info -> set_mask );
4840
4879
}
4841
4880
} else {
4881
+ dflags = READ_ONCE (desc -> flags );
4842
4882
/* Exclude open drain or open source from fast output */
4843
- if (gpiochip_line_is_open_drain ( gc , descs -> ndescs ) ||
4844
- gpiochip_line_is_open_source ( gc , descs -> ndescs ))
4883
+ if (test_bit ( FLAG_OPEN_DRAIN , & dflags ) ||
4884
+ test_bit ( FLAG_OPEN_SOURCE , & dflags ))
4845
4885
__clear_bit (descs -> ndescs ,
4846
4886
array_info -> set_mask );
4847
4887
/* Identify 'fast' pins which require invertion */
@@ -4853,7 +4893,7 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4853
4893
if (array_info )
4854
4894
dev_dbg (dev ,
4855
4895
"GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n" ,
4856
- array_info -> chip -> label , array_info -> size ,
4896
+ array_info -> gdev -> label , array_info -> size ,
4857
4897
* array_info -> get_mask , * array_info -> set_mask ,
4858
4898
* array_info -> invert_mask );
4859
4899
return descs ;
0 commit comments