@@ -365,28 +365,28 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365365 struct linehandle_state * lh = filep -> private_data ;
366366 void __user * ip = (void __user * )arg ;
367367 struct gpiohandle_data ghd ;
368+ int vals [GPIOHANDLES_MAX ];
368369 int i ;
369370
370371 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL ) {
371- int val ;
372+ /* TODO: check if descriptors are really input */
373+ int ret = gpiod_get_array_value_complex (false,
374+ true,
375+ lh -> numdescs ,
376+ lh -> descs ,
377+ vals );
378+ if (ret )
379+ return ret ;
372380
373381 memset (& ghd , 0 , sizeof (ghd ));
374-
375- /* TODO: check if descriptors are really input */
376- for (i = 0 ; i < lh -> numdescs ; i ++ ) {
377- val = gpiod_get_value_cansleep (lh -> descs [i ]);
378- if (val < 0 )
379- return val ;
380- ghd .values [i ] = val ;
381- }
382+ for (i = 0 ; i < lh -> numdescs ; i ++ )
383+ ghd .values [i ] = vals [i ];
382384
383385 if (copy_to_user (ip , & ghd , sizeof (ghd )))
384386 return - EFAULT ;
385387
386388 return 0 ;
387389 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL ) {
388- int vals [GPIOHANDLES_MAX ];
389-
390390 /* TODO: check if descriptors are really output */
391391 if (copy_from_user (& ghd , ip , sizeof (ghd )))
392392 return - EFAULT ;
@@ -2466,6 +2466,71 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
24662466 return value ;
24672467}
24682468
2469+ static int gpio_chip_get_multiple (struct gpio_chip * chip ,
2470+ unsigned long * mask , unsigned long * bits )
2471+ {
2472+ if (chip -> get_multiple ) {
2473+ return chip -> get_multiple (chip , mask , bits );
2474+ } else if (chip -> get ) {
2475+ int i , value ;
2476+
2477+ for_each_set_bit (i , mask , chip -> ngpio ) {
2478+ value = chip -> get (chip , i );
2479+ if (value < 0 )
2480+ return value ;
2481+ __assign_bit (i , bits , value );
2482+ }
2483+ return 0 ;
2484+ }
2485+ return - EIO ;
2486+ }
2487+
2488+ int gpiod_get_array_value_complex (bool raw , bool can_sleep ,
2489+ unsigned int array_size ,
2490+ struct gpio_desc * * desc_array ,
2491+ int * value_array )
2492+ {
2493+ int i = 0 ;
2494+
2495+ while (i < array_size ) {
2496+ struct gpio_chip * chip = desc_array [i ]-> gdev -> chip ;
2497+ unsigned long mask [BITS_TO_LONGS (chip -> ngpio )];
2498+ unsigned long bits [BITS_TO_LONGS (chip -> ngpio )];
2499+ int first , j , ret ;
2500+
2501+ if (!can_sleep )
2502+ WARN_ON (chip -> can_sleep );
2503+
2504+ /* collect all inputs belonging to the same chip */
2505+ first = i ;
2506+ memset (mask , 0 , sizeof (mask ));
2507+ do {
2508+ const struct gpio_desc * desc = desc_array [i ];
2509+ int hwgpio = gpio_chip_hwgpio (desc );
2510+
2511+ __set_bit (hwgpio , mask );
2512+ i ++ ;
2513+ } while ((i < array_size ) &&
2514+ (desc_array [i ]-> gdev -> chip == chip ));
2515+
2516+ ret = gpio_chip_get_multiple (chip , mask , bits );
2517+ if (ret )
2518+ return ret ;
2519+
2520+ for (j = first ; j < i ; j ++ ) {
2521+ const struct gpio_desc * desc = desc_array [j ];
2522+ int hwgpio = gpio_chip_hwgpio (desc );
2523+ int value = test_bit (hwgpio , bits );
2524+
2525+ if (!raw && test_bit (FLAG_ACTIVE_LOW , & desc -> flags ))
2526+ value = !value ;
2527+ value_array [j ] = value ;
2528+ trace_gpio_value (desc_to_gpio (desc ), 1 , value );
2529+ }
2530+ }
2531+ return 0 ;
2532+ }
2533+
24692534/**
24702535 * gpiod_get_raw_value() - return a gpio's raw value
24712536 * @desc: gpio whose value will be returned
@@ -2514,6 +2579,51 @@ int gpiod_get_value(const struct gpio_desc *desc)
25142579}
25152580EXPORT_SYMBOL_GPL (gpiod_get_value );
25162581
2582+ /**
2583+ * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2584+ * @array_size: number of elements in the descriptor / value arrays
2585+ * @desc_array: array of GPIO descriptors whose values will be read
2586+ * @value_array: array to store the read values
2587+ *
2588+ * Read the raw values of the GPIOs, i.e. the values of the physical lines
2589+ * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2590+ * else an error code.
2591+ *
2592+ * This function should be called from contexts where we cannot sleep,
2593+ * and it will complain if the GPIO chip functions potentially sleep.
2594+ */
2595+ int gpiod_get_raw_array_value (unsigned int array_size ,
2596+ struct gpio_desc * * desc_array , int * value_array )
2597+ {
2598+ if (!desc_array )
2599+ return - EINVAL ;
2600+ return gpiod_get_array_value_complex (true, false, array_size ,
2601+ desc_array , value_array );
2602+ }
2603+ EXPORT_SYMBOL_GPL (gpiod_get_raw_array_value );
2604+
2605+ /**
2606+ * gpiod_get_array_value() - read values from an array of GPIOs
2607+ * @array_size: number of elements in the descriptor / value arrays
2608+ * @desc_array: array of GPIO descriptors whose values will be read
2609+ * @value_array: array to store the read values
2610+ *
2611+ * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2612+ * into account. Return 0 in case of success, else an error code.
2613+ *
2614+ * This function should be called from contexts where we cannot sleep,
2615+ * and it will complain if the GPIO chip functions potentially sleep.
2616+ */
2617+ int gpiod_get_array_value (unsigned int array_size ,
2618+ struct gpio_desc * * desc_array , int * value_array )
2619+ {
2620+ if (!desc_array )
2621+ return - EINVAL ;
2622+ return gpiod_get_array_value_complex (false, false, array_size ,
2623+ desc_array , value_array );
2624+ }
2625+ EXPORT_SYMBOL_GPL (gpiod_get_array_value );
2626+
25172627/*
25182628 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
25192629 * @desc: gpio descriptor whose state need to be set.
@@ -2942,6 +3052,53 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc)
29423052}
29433053EXPORT_SYMBOL_GPL (gpiod_get_value_cansleep );
29443054
3055+ /**
3056+ * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3057+ * @array_size: number of elements in the descriptor / value arrays
3058+ * @desc_array: array of GPIO descriptors whose values will be read
3059+ * @value_array: array to store the read values
3060+ *
3061+ * Read the raw values of the GPIOs, i.e. the values of the physical lines
3062+ * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3063+ * else an error code.
3064+ *
3065+ * This function is to be called from contexts that can sleep.
3066+ */
3067+ int gpiod_get_raw_array_value_cansleep (unsigned int array_size ,
3068+ struct gpio_desc * * desc_array ,
3069+ int * value_array )
3070+ {
3071+ might_sleep_if (extra_checks );
3072+ if (!desc_array )
3073+ return - EINVAL ;
3074+ return gpiod_get_array_value_complex (true, true, array_size ,
3075+ desc_array , value_array );
3076+ }
3077+ EXPORT_SYMBOL_GPL (gpiod_get_raw_array_value_cansleep );
3078+
3079+ /**
3080+ * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3081+ * @array_size: number of elements in the descriptor / value arrays
3082+ * @desc_array: array of GPIO descriptors whose values will be read
3083+ * @value_array: array to store the read values
3084+ *
3085+ * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3086+ * into account. Return 0 in case of success, else an error code.
3087+ *
3088+ * This function is to be called from contexts that can sleep.
3089+ */
3090+ int gpiod_get_array_value_cansleep (unsigned int array_size ,
3091+ struct gpio_desc * * desc_array ,
3092+ int * value_array )
3093+ {
3094+ might_sleep_if (extra_checks );
3095+ if (!desc_array )
3096+ return - EINVAL ;
3097+ return gpiod_get_array_value_complex (false, true, array_size ,
3098+ desc_array , value_array );
3099+ }
3100+ EXPORT_SYMBOL_GPL (gpiod_get_array_value_cansleep );
3101+
29453102/**
29463103 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
29473104 * @desc: gpio whose value will be assigned
0 commit comments