diff mbox series

[3/3] core: fix gpiod_line_get_value_bulk for events

Message ID 20200617030639.27690-4-warthog618@gmail.com
State New
Headers show
Series [1/3] bindings: cxx: tests: add tests for bulk events get_values | expand

Commit Message

Kent Gibson June 17, 2020, 3:06 a.m. UTC
Extend gpiod_line_get_value_bulk so that it works for bulks of
lineevents, not only linehandles.

Reported-by: Gerrit Wyen <ml@ionscale.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>

---
 lib/core.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/lib/core.c b/lib/core.c
index f704b44..ad76051 100644
--- a/lib/core.c
+++ b/lib/core.c
@@ -773,26 +773,41 @@  int gpiod_line_get_value(struct gpiod_line *line)
 int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, int *values)
 {
 	struct gpiohandle_data data;
-	struct gpiod_line *first;
+	struct gpiod_line *line;
 	unsigned int i;
 	int rv, fd;
 
 	if (!line_bulk_same_chip(bulk) || !line_bulk_all_requested(bulk))
 		return -1;
 
-	first = gpiod_line_bulk_get_line(bulk, 0);
+	line = gpiod_line_bulk_get_line(bulk, 0);
 
-	memset(&data, 0, sizeof(data));
+	if (line->state == LINE_REQUESTED_VALUES) {
+		memset(&data, 0, sizeof(data));
 
-	fd = line_get_fd(first);
+		fd = line_get_fd(line);
 
-	rv = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
-	if (rv < 0)
-		return -1;
+		rv = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
+		if (rv < 0)
+			return -1;
 
-	for (i = 0; i < gpiod_line_bulk_num_lines(bulk); i++)
-		values[i] = data.values[i];
+		for (i = 0; i < gpiod_line_bulk_num_lines(bulk); i++)
+			values[i] = data.values[i];
 
+	} else if (line->state == LINE_REQUESTED_EVENTS) {
+		for (i = 0; i < gpiod_line_bulk_num_lines(bulk); i++) {
+			line = gpiod_line_bulk_get_line(bulk, i);
+
+			fd = line_get_fd(line);
+			rv = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
+			if (rv < 0)
+				return -1;
+			values[i] = data.values[0];
+		}
+	} else {
+		errno = EINVAL;
+		return -1;
+	}
 	return 0;
 }