From: Bartosz Golaszewski Date: Wed, 15 Nov 2017 13:39:58 +0000 (+0100) Subject: core: don't allow open-source and open-drain flags at the same time X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=1749610c31cb701fb4864278167ed531ea1c7b63;p=qemu-gpiodev%2Flibgpiod.git core: don't allow open-source and open-drain flags at the same time If the hardware actually allowed it, the electrical result would be disastrous. This has only recently been fixed in the kernel, so add a relevant check to the library as well. Signed-off-by: Bartosz Golaszewski --- diff --git a/src/lib/core.c b/src/lib/core.c index c17dffa..a1d6e0e 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -343,6 +343,12 @@ static int line_request_values(struct gpiod_line_bulk *bulk, return -1; } + if ((config->flags & GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN) && + (config->flags & GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE)) { + errno = EINVAL; + return -1; + } + memset(&req, 0, sizeof(req)); if (config->flags & GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN) diff --git a/tests/tests-line.c b/tests/tests-line.c index 2071e8e..bc15170 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -513,6 +513,28 @@ TEST_DEFINE(line_open_source_open_drain_input_invalid, "gpiod_line - open-source & open-drain for input mode (invalid)", 0, { 8 }); +static void line_open_source_open_drain_simultaneously(void) +{ + TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL; + struct gpiod_line *line; + int rv; + + chip = gpiod_chip_open(test_chip_path(0)); + TEST_ASSERT_NOT_NULL(chip); + + line = gpiod_chip_get_line(chip, 2); + TEST_ASSERT_NOT_NULL(line); + + rv = gpiod_line_request_output_flags(line, TEST_CONSUMER, + GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE | + GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN, 1); + TEST_ASSERT_EQ(rv, -1); + TEST_ASSERT_ERRNO_IS(EINVAL); +} +TEST_DEFINE(line_open_source_open_drain_simultaneously, + "gpiod_line - open-source & open-drain flags simultaneously", + 0, { 8 }); + static void line_null_consumer(void) { TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL;