From patchwork Sat Sep 12 02:22:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1362749 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=XVngAXHK; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BpGdS5zgSz9sTH for ; Sat, 12 Sep 2020 12:23:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725768AbgILCXk (ORCPT ); Fri, 11 Sep 2020 22:23:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725765AbgILCXj (ORCPT ); Fri, 11 Sep 2020 22:23:39 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B7BAC061573 for ; Fri, 11 Sep 2020 19:23:39 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id l191so7766970pgd.5 for ; Fri, 11 Sep 2020 19:23:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=18Dm2iojGfO5+OYo1XLG6bTBD1jGVlwz09PNEWkVrDA=; b=XVngAXHKGMNEfxNcidcOjpZ6lmFxk0Qw063LZUvj7mlbO4Yjm5WsQLVq6IiO+To81S y4shgt/XQ2oCnV3V7LDcQf3dLZt7Pc7zewjQ0Uph2zshqQ84V7OGDp5/JUxYAvNdoEJR n0yg41zeG2vgz6W1Qz3/5MhSt4Vr3xw8KwHbA1rbJ+9DiNDDlov38yYAjd9tBvATisf3 oBLdaw+KZvS1kVrwNnjLacqiSNyjVVOh/RRy2D2R3Hu/ofGiv66KKlRwuZQ3eL1Um6sb tICCDRyjTrDQ12ekmUbm5W20Xhuf0vPkNR2cXyyQaOIE1A07vyJ2nkocYoyCU6MEqX1Y hFXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=18Dm2iojGfO5+OYo1XLG6bTBD1jGVlwz09PNEWkVrDA=; b=P6Jnds7tNhpfpUhAj9vfr+jyEO5F9iRIKpQhHZdoTIbTwBPYshwWvQuv1wbIhgU++c q3kJf+/K7dSa7/h6YAlLBGkucyZYxzM9fYODi3JJu+Qrw2H/xjS8LEgaW21LVvgs3Cnm t/dFHJ1zXBtIDZTe37XTq/pplUzt/pBd/dE3w92rDvbFZeTfBBbUX4q3kIjyymUKFPQZ uo8ojoRH84sW30ZuS7/wiUmoyxEvi2sR8nYWazko03y8qfWwdI7v1GRB5QbIWQ0iZGwj jTU19C6bAboaBZ8se2AFC5AqaWaDm32SLt9bpTmB8MwL5qSuQHttmbtIH9jmNNa2J/cg tGoA== X-Gm-Message-State: AOAM531dFHNkrl/WTdisbfGkQ3+DitdmIYewgfPqQ0sVO7fsFWUtgfbA aZ3hcWQ0KYWdh1Yx/EE37MB1enfx4Os= X-Google-Smtp-Source: ABdhPJwNM1ZJXDBuEBzPR2GYGcYnlga318GHO+qDohes4uFsqeTOLWkKaDWn8tlLXYC7YeevXUpHdg== X-Received: by 2002:a62:52ce:0:b029:13e:50c8:499b with SMTP id g197-20020a6252ce0000b029013e50c8499bmr4691378pfb.14.1599877418425; Fri, 11 Sep 2020 19:23:38 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id g21sm3545574pfh.30.2020.09.11.19.23.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2020 19:23:37 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com Cc: Kent Gibson Subject: [libgpiod][PATCH 1/2] tests: event: reading test coverage extended to cover reading a subset of available events Date: Sat, 12 Sep 2020 10:22:47 +0800 Message-Id: <20200912022248.16240-2-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200912022248.16240-1-warthog618@gmail.com> References: <20200912022248.16240-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add tests for gpiod_line_event_read(), including reading multiple entries from the kernel event kfifo, and extend the existing read_multiple_event tests to read a subset of the available events as well as all the available events. Signed-off-by: Kent Gibson --- I have removed the usleep()s between setting the pulls on the mockup as I don't believe they are necessary. Setting the pulls results in serial writes to the debugfs, which the kernel should be able to deal with. Any queuing in the kernel that results should come out in the wash. tests/tests-event.c | 144 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 124 insertions(+), 20 deletions(-) diff --git a/tests/tests-event.c b/tests/tests-event.c index e323060..c1f73d4 100644 --- a/tests/tests-event.c +++ b/tests/tests-event.c @@ -663,13 +663,74 @@ GPIOD_TEST_CASE(invalid_fd, 0, { 8 }) g_assert_cmpint(errno, ==, EINVAL); } +GPIOD_TEST_CASE(read_event, 0, { 8 }) +{ + g_autoptr(gpiod_chip_struct) chip = NULL; + struct timespec ts = { 1, 0 }; + struct gpiod_line_event ev; + struct gpiod_line *line; + gint ret; + unsigned int i; + + chip = gpiod_chip_open(gpiod_test_chip_path(0)); + g_assert_nonnull(chip); + gpiod_test_return_if_failed(); + + line = gpiod_chip_get_line(chip, 7); + g_assert_nonnull(line); + gpiod_test_return_if_failed(); + + ret = gpiod_line_request_both_edges_events_flags(line, + GPIOD_TEST_CONSUMER, GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP); + g_assert_cmpint(ret, ==, 0); + + /* generate multiple events */ + for (i = 0; i < 3; i++) + gpiod_test_chip_set_pull(0, 7, i & 1); + + /* read them individually... */ + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + + ret = gpiod_line_event_read(line, &ev); + g_assert_cmpint(ret, ==, 0); + + g_assert_cmpint(ev.event_type, ==, GPIOD_LINE_EVENT_FALLING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + + ret = gpiod_line_event_read(line, &ev); + g_assert_cmpint(ret, ==, 0); + + g_assert_cmpint(ev.event_type, ==, GPIOD_LINE_EVENT_RISING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + + ret = gpiod_line_event_read(line, &ev); + g_assert_cmpint(ret, ==, 0); + + g_assert_cmpint(ev.event_type, ==, GPIOD_LINE_EVENT_FALLING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 0); +} + GPIOD_TEST_CASE(read_multiple_events, 0, { 8 }) { g_autoptr(gpiod_chip_struct) chip = NULL; - struct gpiod_line_event events[3]; + struct gpiod_line_event events[5]; struct timespec ts = { 1, 0 }; struct gpiod_line *line; gint ret; + unsigned int i; chip = gpiod_chip_open(gpiod_test_chip_path(0)); g_assert_nonnull(chip); @@ -682,22 +743,16 @@ GPIOD_TEST_CASE(read_multiple_events, 0, { 8 }) ret = gpiod_line_request_both_edges_events(line, GPIOD_TEST_CONSUMER); g_assert_cmpint(ret, ==, 0); - gpiod_test_chip_set_pull(0, 4, 1); - /* - * We sleep for a short period of time here and in other test cases - * for multiple events to let the kernel service each simulated - * interrupt. Otherwise we'd risk triggering an interrupt while the - * previous one is still being handled. - */ - usleep(10000); - gpiod_test_chip_set_pull(0, 4, 0); - usleep(10000); - gpiod_test_chip_set_pull(0, 4, 1); - usleep(10000); + /* generate multiple events */ + for (i = 0; i < 7; i++) + gpiod_test_chip_set_pull(0, 4, !(i & 1)); ret = gpiod_line_event_wait(line, &ts); g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + /* read a chunk */ ret = gpiod_line_event_read_multiple(line, events, 3); g_assert_cmpint(ret, ==, 3); @@ -707,15 +762,40 @@ GPIOD_TEST_CASE(read_multiple_events, 0, { 8 }) GPIOD_LINE_EVENT_FALLING_EDGE); g_assert_cmpint(events[2].event_type, ==, GPIOD_LINE_EVENT_RISING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + + /* + * read the remainder + * - note the attempt to read more than are available + */ + ret = gpiod_line_event_read_multiple(line, events, 5); + g_assert_cmpint(ret, ==, 4); + + g_assert_cmpint(events[0].event_type, ==, + GPIOD_LINE_EVENT_FALLING_EDGE); + g_assert_cmpint(events[1].event_type, ==, + GPIOD_LINE_EVENT_RISING_EDGE); + g_assert_cmpint(events[2].event_type, ==, + GPIOD_LINE_EVENT_FALLING_EDGE); + g_assert_cmpint(events[3].event_type, ==, + GPIOD_LINE_EVENT_RISING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 0); } GPIOD_TEST_CASE(read_multiple_events_fd, 0, { 8 }) { g_autoptr(gpiod_chip_struct) chip = NULL; - struct gpiod_line_event events[3]; + struct gpiod_line_event events[5]; struct timespec ts = { 1, 0 }; struct gpiod_line *line; gint ret, fd; + unsigned int i; chip = gpiod_chip_open(gpiod_test_chip_path(0)); g_assert_nonnull(chip); @@ -728,19 +808,19 @@ GPIOD_TEST_CASE(read_multiple_events_fd, 0, { 8 }) ret = gpiod_line_request_both_edges_events(line, GPIOD_TEST_CONSUMER); g_assert_cmpint(ret, ==, 0); - gpiod_test_chip_set_pull(0, 4, 1); - usleep(10000); - gpiod_test_chip_set_pull(0, 4, 0); - usleep(10000); - gpiod_test_chip_set_pull(0, 4, 1); - usleep(10000); + /* generate multiple events */ + for (i = 0; i < 7; i++) + gpiod_test_chip_set_pull(0, 4, !(i & 1)); ret = gpiod_line_event_wait(line, &ts); g_assert_cmpint(ret, ==, 1); + if (!ret) + return; fd = gpiod_line_event_get_fd(line); g_assert_cmpint(fd, >=, 0); + /* read a chunk */ ret = gpiod_line_event_read_fd_multiple(fd, events, 3); g_assert_cmpint(ret, ==, 3); @@ -750,4 +830,28 @@ GPIOD_TEST_CASE(read_multiple_events_fd, 0, { 8 }) GPIOD_LINE_EVENT_FALLING_EDGE); g_assert_cmpint(events[2].event_type, ==, GPIOD_LINE_EVENT_RISING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 1); + if (!ret) + return; + + /* + * read the remainder + * - note the attempt to read more than are available + */ + ret = gpiod_line_event_read_fd_multiple(fd, events, 5); + g_assert_cmpint(ret, ==, 4); + + g_assert_cmpint(events[0].event_type, ==, + GPIOD_LINE_EVENT_FALLING_EDGE); + g_assert_cmpint(events[1].event_type, ==, + GPIOD_LINE_EVENT_RISING_EDGE); + g_assert_cmpint(events[2].event_type, ==, + GPIOD_LINE_EVENT_FALLING_EDGE); + g_assert_cmpint(events[3].event_type, ==, + GPIOD_LINE_EVENT_RISING_EDGE); + + ret = gpiod_line_event_wait(line, &ts); + g_assert_cmpint(ret, ==, 0); } From patchwork Sat Sep 12 02:22:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 1362750 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=aW5rk5Q4; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4BpGdZ2xSHz9sTH for ; Sat, 12 Sep 2020 12:23:46 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725769AbgILCXq (ORCPT ); Fri, 11 Sep 2020 22:23:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgILCXp (ORCPT ); Fri, 11 Sep 2020 22:23:45 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25E82C061573 for ; Fri, 11 Sep 2020 19:23:45 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id k13so1745400plk.3 for ; Fri, 11 Sep 2020 19:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uKxJ6l42Rr5bTxZ/Q1sZtoURjghjoZrwJA5CjXTKf5Q=; b=aW5rk5Q4IvXJTT+ZW7ew/dkS+MAJ0bhgjbtltAzqwRHkc6gNaq3XTUKwbORNmWl8eh S0AqCkwXps/Oct6HMBNOqW++OplFXb+tVGjjdnbM7eEhgXJj1IJ7deWOqvOyWEShSAz3 YXffSOoXsIfkJ5UvXD/zv4yh1WcBhXZF8nNCRcv5ySAlLoKmMWBsaSjc6WhreYzz881i GZEh/CNUKFKcaToef1T0xtIPgHdzPyTVIMzyrDwXS1tdr0LdEEt/wZgV0OQNzF38AeE1 x3/3yAcDrOVAkE07EgjRcor0gm1G3UnuBOIN048fJIWLHLjO4683B1EpjXLm7PeHZWlz 0KaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uKxJ6l42Rr5bTxZ/Q1sZtoURjghjoZrwJA5CjXTKf5Q=; b=tmanSLZf63fnTozk7igiWIkOZ6GnUorU7rcLGUh7Ie+a91aSvl5uQQ67JSM6LTa2Gk t+EDR+/Gkl4OqV69tts1Ci3mPCcQ4lu98Y/V3tWqkW7my0EbVniBkqZHlfLDGVvT/fbj Pe+7+nEXTRyqH9p4gDPH1y7aYxouDBPlTbl/tYbGuo3FQF40s8qvQHMWH+L3jFQ2y4XH we1c1/E0bcTg2ldrkxHnhXnjv1AtvVApL791GMtzXgt62BT65Ryi6EDW4NvnWGRlyVTV HUTsa6I3weyAVdHgzG5vsUBqU9xbvPB6zPusaLK4RqJlyeouZ+AdXB473MqvnrXjjifv Flmw== X-Gm-Message-State: AOAM530WrV7W26Mg6rkhcSlorCYFC7h37LpcNLPxJezcujqwO4NMHw7U F2lUiqCd7rkFngm46/WQDVwpx40KGrM= X-Google-Smtp-Source: ABdhPJyeyH/7fNsQfngGjICx2TqSouxhv5x/RAEFC2DoZUFqgOuhmLXCetw0PjeAu9pMCLrbmC+nRQ== X-Received: by 2002:a17:90a:f206:: with SMTP id bs6mr4821140pjb.196.1599877424324; Fri, 11 Sep 2020 19:23:44 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id g21sm3545574pfh.30.2020.09.11.19.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2020 19:23:43 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com Cc: Kent Gibson Subject: [libgpiod][PATCH 2/2] core: fix reading subset of available events Date: Sat, 12 Sep 2020 10:22:48 +0800 Message-Id: <20200912022248.16240-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200912022248.16240-1-warthog618@gmail.com> References: <20200912022248.16240-1-warthog618@gmail.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Only read the requested number of events from the kernel rather than reading up to 16 and quietly discarding any surplus. The previous behavour is particularly bad for reading single events as userspace must read the events as quickly as they arrive, effectively negating the presence of the kernel event kfifo. Fixes: 44921ecc9a00 (core: provide functions for reading multiple line events at once) Signed-off-by: Kent Gibson --- lib/core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/core.c b/lib/core.c index ad76051..b964272 100644 --- a/lib/core.c +++ b/lib/core.c @@ -1090,7 +1090,10 @@ int gpiod_line_event_read_fd_multiple(int fd, struct gpiod_line_event *events, memset(evdata, 0, sizeof(evdata)); - rd = read(fd, evdata, sizeof(evdata)); + if (num_events > 16) + num_events = 16; + + rd = read(fd, evdata, num_events * sizeof(*evdata)); if (rd < 0) { return -1; } else if ((unsigned int)rd < sizeof(*evdata)) {