Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp691377ybl; Wed, 4 Dec 2019 09:18:29 -0800 (PST) X-Google-Smtp-Source: APXvYqxMFvFXqKmF5msaUJT/YzHrbAzaGXy7cGPIJHgKj4lVF8J7Utgd61do2XBfw/kVSERomGzz X-Received: by 2002:aca:5e04:: with SMTP id s4mr3611720oib.159.1575479909631; Wed, 04 Dec 2019 09:18:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575479909; cv=none; d=google.com; s=arc-20160816; b=Z/63Xe7Fj0oyMleFCQ0obWyaBTwM8ngjXPluxnhXNUCU2SxMnQlmnxixKV77UnAMm1 t2Nufarbhc5s7lWcPAKJ1mokpQy7QBLu4p5+1U0QpMUq4jeInq13wt3sS+HVgV7E2VgJ BrnLOL+QPN7ZE5KXeRcEY1hqBvUsZHCeVy3qO1iOUVTsDwDqR8qOSYLlvPiWTmN1P4uE zWfzev9OccDUaqjj4biaWmWTyK0ZVr6Jluo94AxP2g4QEBfetZ71mp7ImZP9Owgy2+rm IrASGgluIQBVv3WsX85g3t87VO45Z6qdgowI0YtR8Wnc8QcMbclgCmAH6X/bJevKGL7r cXCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3vOhxME4oM+fCXvGw9SeZO8ZtZ/Y7PIxuNLc7TEy6Us=; b=QeSXzLN/ucHQv9/EYb6Puz8pRxAhxjQQ8v+GeuG1cLZhfD4H7fXlZXEr4DQ+jgGF8v A4FjDMb4bhFI4O1jOqCpM+ftVaHOYwmc+h32779zd1LzsnCOuw9phRmtzzOfzgxKIsQw tnTJFD9QalZjW8XR834NNcpeDSYzRKmj5Znt6s3qC+Mswle/47j0r+ZwV4WdBh52zx7t HgmauaqG3Dmjr1VE76fzkROUjmKi2+CVlgizeWvheBiPG3jMPLjXOYcFCk03h/3lT2Pa UFNf7sNeQOwnT7oQW0m5CrXwcbL+rTcjh1daFfDh1I5k+sFoIw6wZyPQ731V1SXIG97r ORFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=BBMUbwY6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x21si3581140oif.250.2019.12.04.09.18.16; Wed, 04 Dec 2019 09:18:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=BBMUbwY6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728721AbfLDP7x (ORCPT + 99 others); Wed, 4 Dec 2019 10:59:53 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:37184 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728652AbfLDP7t (ORCPT ); Wed, 4 Dec 2019 10:59:49 -0500 Received: by mail-wr1-f66.google.com with SMTP id w15so9268530wru.4 for ; Wed, 04 Dec 2019 07:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3vOhxME4oM+fCXvGw9SeZO8ZtZ/Y7PIxuNLc7TEy6Us=; b=BBMUbwY67rlDFQzYeKhhMrURjvq+SV00mi/aBQLu2XUm4f46pIt+flDS2ZdL34ILWA DzDRxfz3Yse3rm5oSdZzFW6HbJOmk/562LaDsoSkah9MDpnNm6FirJxpYFPL19eTT5XO cYgZWpYU/4cQcK2Vyv9QkBmwzwB3XE6JXTh9hU5vQ2WV1i6ExBsOUVUUh5Z9j+uOHmh4 f+Kvl8kbcALtFF3IEXsIDRhybsXftUPA+95fNvPhs/VZsBUY9y8HXjmpAUiWUbWxVJT8 7cMQILfswsNcQuk1QkzOzoHcVacLpetKWDxSOH2geHB+fbAOo159sxgUmbSf1Qjljkq6 kcGA== 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=3vOhxME4oM+fCXvGw9SeZO8ZtZ/Y7PIxuNLc7TEy6Us=; b=DMITyOhujZ43xPmQhX0WNPz0qtW2cn2FaIYkPVTn8vff6w807exg9XC6wEM4BuBTRz TnoAEV3uPhDilYoeDb9ekjZZ6dqqMp9M7aq6seETjIasaQKbtnNyhU+JnSyTum1TDawD fIKk15T9rJt2a2sNsZFFEDUASc6aguuzWNOJg7GedOSx2JN1YXjkMqwMHuX8hchvV5tU z0OXgDXdBBViKZsgAPUi5KJvgK0Q/wL/9FpzshZ1gJi7nDOznwdX3TxSUd4IAyF4P6OV SFgyzRIcl4PLVHqUwG8KSeJ7EIEINRGMNeohH7ISRF46JfewtwoFsOK+nruBqeyw8h9A xpEQ== X-Gm-Message-State: APjAAAX6JnRFp743gUB67O1aMu951kKdiNYCq8cBrAYtt+iUDnhxX/0h IDRGJIeYgb8XQ2kVbN3UDICKkw== X-Received: by 2002:adf:dcc2:: with SMTP id x2mr4555036wrm.24.1575475186930; Wed, 04 Dec 2019 07:59:46 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id s10sm8769070wrw.12.2019.12.04.07.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:45 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 11/11] tools: gpio: implement gpio-watch Date: Wed, 4 Dec 2019 16:59:41 +0100 Message-Id: <20191204155941.17814-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155941.17814-1-brgl@bgdev.pl> References: <20191204155941.17814-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski Add a simple program that allows to test the new LINECHANGED_FD ioctl(). Signed-off-by: Bartosz Golaszewski --- tools/gpio/.gitignore | 1 + tools/gpio/Build | 1 + tools/gpio/Makefile | 11 +++- tools/gpio/gpio-watch.c | 112 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tools/gpio/gpio-watch.c diff --git a/tools/gpio/.gitignore b/tools/gpio/.gitignore index a94c0e83b209..fffd32969d62 100644 --- a/tools/gpio/.gitignore +++ b/tools/gpio/.gitignore @@ -1,4 +1,5 @@ gpio-event-mon gpio-hammer lsgpio +gpio-watch include/linux/gpio.h diff --git a/tools/gpio/Build b/tools/gpio/Build index 4141f35837db..67c7b7f6a717 100644 --- a/tools/gpio/Build +++ b/tools/gpio/Build @@ -2,3 +2,4 @@ gpio-utils-y += gpio-utils.o lsgpio-y += lsgpio.o gpio-utils.o gpio-hammer-y += gpio-hammer.o gpio-utils.o gpio-event-mon-y += gpio-event-mon.o gpio-utils.o +gpio-watch-y += gpio-watch.o diff --git a/tools/gpio/Makefile b/tools/gpio/Makefile index 6080de58861f..842287e42c83 100644 --- a/tools/gpio/Makefile +++ b/tools/gpio/Makefile @@ -18,7 +18,7 @@ MAKEFLAGS += -r override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon +ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon gpio-watch ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) all: $(ALL_PROGRAMS) @@ -66,6 +66,15 @@ $(GPIO_EVENT_MON_IN): prepare FORCE $(OUTPUT)gpio-utils-in.o $(OUTPUT)gpio-event-mon: $(GPIO_EVENT_MON_IN) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +# +# gpio-watch +# +GPIO_WATCH_IN := $(OUTPUT)gpio-watch-in.o +$(GPIO_WATCH_IN): prepare FORCE + $(Q)$(MAKE) $(build)=gpio-watch +$(OUTPUT)gpio-watch: $(GPIO_WATCH_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + clean: rm -f $(ALL_PROGRAMS) rm -f $(OUTPUT)include/linux/gpio.h diff --git a/tools/gpio/gpio-watch.c b/tools/gpio/gpio-watch.c new file mode 100644 index 000000000000..69aee43655ae --- /dev/null +++ b/tools/gpio/gpio-watch.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * gpio-watch - monitor unrequested lines for property changes using the + * character device + * + * Copyright (C) 2019 BayLibre SAS + * Author: Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool isnumber(const char *str) +{ + size_t sz = strlen(str); + int i; + + for (i = 0; i < sz; i++) { + if (!isdigit(str[i])) + return false; + } + + return true; +} + +int main(int argc, char **argv) +{ + struct gpioline_info_changed chg; + struct gpioline_info req; + struct pollfd pfd; + int fd, i, j, ret; + char *event; + ssize_t rd; + + if (argc < 3) + goto err_usage; + + fd = open(argv[1], O_RDWR | O_CLOEXEC); + if (fd < 0) { + perror("unable to open gpiochip"); + return EXIT_FAILURE; + } + + for (i = 0, j = 2; i < argc - 2; i++, j++) { + if (!isnumber(argv[j])) + goto err_usage; + + memset(&req, 0, sizeof(req)); + req.line_offset = atoi(argv[j]); + + ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); + if (ret) { + perror("unable to set up line watch"); + return EXIT_FAILURE; + } + } + + pfd.fd = fd; + pfd.events = POLLIN | POLLPRI; + + for (;;) { + ret = poll(&pfd, 1, 5000); + if (ret < 0) { + perror("error polling the linechanged fd"); + return EXIT_FAILURE; + } else if (ret > 0) { + memset(&chg, 0, sizeof(chg)); + rd = read(pfd.fd, &chg, sizeof(chg)); + if (rd < 0 || rd != sizeof(chg)) { + if (rd != sizeof(chg)) + errno = EIO; + + perror("error reading line change event"); + return EXIT_FAILURE; + } + + switch (chg.event_type) { + case GPIOLINE_CHANGED_REQUESTED: + event = "requested"; + break; + case GPIOLINE_CHANGED_RELEASED: + event = "released"; + break; + case GPIOLINE_CHANGED_CONFIG: + event = "config changed"; + break; + default: + fprintf(stderr, + "invalid event type received from the kernel\n"); + return EXIT_FAILURE; + } + + printf("line %u: %s at %llu\n", + chg.info.line_offset, event, chg.timestamp); + } + } + + return 0; + +err_usage: + printf("%s: ...\n", argv[0]); + return EXIT_FAILURE; +} -- 2.23.0