From patchwork Fri Aug 14 03:02:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E87AC433DF for ; Fri, 14 Aug 2020 03:03:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0766220716 for ; Fri, 14 Aug 2020 03:03:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XGIbA/8K" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726583AbgHNDDZ (ORCPT ); Thu, 13 Aug 2020 23:03:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDDZ (ORCPT ); Thu, 13 Aug 2020 23:03:25 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30076C061757; Thu, 13 Aug 2020 20:03:25 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id f5so3535234plr.9; Thu, 13 Aug 2020 20:03:25 -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=7KLfIVMRwqpdC47NwuYFPGVwE5g0uXxr3Mm7EIshdF4=; b=XGIbA/8K9NTQaOueUYCKV8UkHhRRSZR91/IkYRMh5jiuvYuHA3yQK5/gfLjAPtn8iv C4nk0l52EE03npbsCcb++oZJcODW97Zpu3hrDRumyoQcPx8QF5W6HBRl1V+X2HK0Rk3Y YRvofG5uNysBGozU2Gwn1yykJ25p/fdme7dq3d2+mdqFLSQnhmnmNq+YQ0zRH94d+juw tl5M0dRB2fqlKWIEKguabnlHgxVa8+qXfa9q4J1NqEvRtcRxkXjd2Xm+VxgX+I6202ve 2nyBXsZh5b9iv1mSDxo2u8TMZ/6Dj1ro+rc3hTdSflfI9LCLDw/zYkw9jWpJjcZQ58If ucrQ== 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=7KLfIVMRwqpdC47NwuYFPGVwE5g0uXxr3Mm7EIshdF4=; b=FVJXqplufl77TwQWlz+n6XCxhtznkT3egWCedxBZlLUoRx3zD2gaLop4K6Kr2Rm/e6 bp+30j+QT80cZkgdz3/nrrJFd+gdMXnu5cNInkrFRg1S+RmNp/ckId/egeJtaNaSHcWM ESbKcjchyKXEmDVq431Cw647c4I/+Y3eFyhuY8WUMHMAQ6AgCtWdmiuQrwqV5YR2nStQ N8YGG8rIyVtEQDiNZ28gvwGkDFbhywdbeYgIasY3y2URdFOBAsvCpwOnU3B88sWW7AAN p6JmxIpUpSi9ojngSFbs2J8NsoeETdmJV1Wv7uWJVjAg3mRLkuQMOIDdB7puz793/hVI /9fg== X-Gm-Message-State: AOAM533rJ7qsclNwXPI+oVbIBXFbR6Tx0xO/HTUzy1bB/U+MxpP7EuO2 W4LqC94O532OTgsegyvnr3vrmQCS X-Google-Smtp-Source: ABdhPJwxAiJr9pBa5wp8QnZGMAkjK8Qnswfi0F6zKM5pJLv/04Jna5dwKD/dQIqs9bUTmyKvm+K1Sg== X-Received: by 2002:a17:90a:b88c:: with SMTP id o12mr551819pjr.187.1597374204406; Thu, 13 Aug 2020 20:03:24 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.03.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:03:23 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 01/20] gpiolib: cdev: desc_to_lineinfo should set info offset Date: Fri, 14 Aug 2020 11:02:38 +0800 Message-Id: <20200814030257.135463-2-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Set the value of the line info offset in desc_to_lineinfo, rather than relying on it being passed in the info. This makes the function behave as you would expect from the name - it generates the line info corresponding to a given GPIO desc. Signed-off-by: Kent Gibson --- There are some instances where this results in the offset being set when it is already set in the info, but I think this is clearer especially considering that, as part of the replacement of strncpy with strscpy and to to ensure kernel stack cannot be leaked to userspace, the info is zeroed in a subsequent patch. drivers/gpio/gpiolib-cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e6c9b78adfc2..e95e3eab9867 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -752,6 +752,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, bool ok_for_pinctrl; unsigned long flags; + info->line_offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking * the spinlock. @@ -933,7 +934,6 @@ static int lineinfo_changed_notify(struct notifier_block *nb, return NOTIFY_DONE; memset(&chg, 0, sizeof(chg)); - chg.info.line_offset = gpio_chip_hwgpio(desc); chg.event_type = action; chg.timestamp = ktime_get_ns(); gpio_desc_to_lineinfo(desc, &chg.info); From patchwork Fri Aug 14 03:02:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254618 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9BF5C433DF for ; Fri, 14 Aug 2020 03:03:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B644A20716 for ; Fri, 14 Aug 2020 03:03:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jE56/qNS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726727AbgHNDDl (ORCPT ); Thu, 13 Aug 2020 23:03:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDDk (ORCPT ); Thu, 13 Aug 2020 23:03:40 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BFF3C061757; Thu, 13 Aug 2020 20:03:40 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id v5so1381219plo.4; Thu, 13 Aug 2020 20:03:40 -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=s4z3c80rW1f77SHZ1DJfS7Y59f6hWqfux6WtIWEmMvU=; b=jE56/qNS2bR2eU/znoGV6CpLP/o/OxHAV7hxz27u+pQJgrjwR0rrwTLvEKS/buhcNp 0TYpM+FeHKK3knW1+S+DcLD7UcvslV2WSB8A6zrYREsGr5R8uyASKqg5i7Vm+D6cp55F KUxt+BVIYKnPqpN90JwU60FDmEpUdEfVSEZkYFame9ShRYi991AmZ7s4OaoG/OjivJHd Ph5lxUv/MhYDOp/zElspaZl7ROy1q2H5DM6MHkoBECCwdzAtnBV9rbkygk9K3xx9yQf0 1O+6xWcugPAoIl6FdjNw3bjCteX/JW3SWYRNEH7OqvHuHHSaTZ8HsWfl38xoL/81lzGz yHbg== 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=s4z3c80rW1f77SHZ1DJfS7Y59f6hWqfux6WtIWEmMvU=; b=Y1AgtAkXe8PwQXkB92Nt9c5OcCHks4VuBTNJdZ1eUol0x9JbUMIDHFZ7/pDUh1Adgu LZ0twysFM0Daqz7MA0QCrVSchWFil1Q8Ry2kr/KyZvkH0Uc176heqMyUVkUqpgC3uVhp mQ1X3xt3czDzZfLmk9ovw1Sub75rU4ajxVxM4etp4cuVXm+nqKNwKkX1euAYdOB4qVCV 0s/ZE7OEsORCP6KL8FU7oQLOJz68ZEdCdVEFyNiTlmu74komJ9gqSfrvhW8n3ngC9hiA SnLZlRRFZALYkpTbEhnwOguAS+YiVnd725qxEE3UPgFQq/3BUTy3xf48ot+utJSZUgsO LFEw== X-Gm-Message-State: AOAM533HOV/f0yil/rOVNjSUkhzNlxNKERe0juWUm2tCa6Rs/aZ+ulOS oKWHtBWzcJegqvvn8/CAJgrhGY1j X-Google-Smtp-Source: ABdhPJzgr/sNJhj12Zn2keI64vzZMXkJj/QXCSoZ+heE3pk5VkzrBYgkAclOw9nn0Q86QMy2aYAeeg== X-Received: by 2002:a17:90a:e50d:: with SMTP id t13mr571289pjy.33.1597374219770; Thu, 13 Aug 2020 20:03:39 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.03.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:03:39 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 03/20] gpio: uapi: define GPIO_MAX_NAME_SIZE for array sizes Date: Fri, 14 Aug 2020 11:02:40 +0800 Message-Id: <20200814030257.135463-4-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Replace constant array sizes with a macro constant to clarify the source of array sizes, provide a place to document any constraints on the size, and to simplify array sizing in userspace if constructing structs from their composite fields. Signed-off-by: Kent Gibson --- include/uapi/linux/gpio.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 9c27cecf406f..285cc10355b2 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -14,6 +14,11 @@ #include #include +/* + * The maximum size of name and label arrays. + */ +#define GPIO_MAX_NAME_SIZE 32 + /** * struct gpiochip_info - Information about a certain GPIO chip * @name: the Linux kernel name of this GPIO chip @@ -22,8 +27,8 @@ * @lines: number of GPIO lines on this chip */ struct gpiochip_info { - char name[32]; - char label[32]; + char name[GPIO_MAX_NAME_SIZE]; + char label[GPIO_MAX_NAME_SIZE]; __u32 lines; }; @@ -52,8 +57,8 @@ struct gpiochip_info { struct gpioline_info { __u32 line_offset; __u32 flags; - char name[32]; - char consumer[32]; + char name[GPIO_MAX_NAME_SIZE]; + char consumer[GPIO_MAX_NAME_SIZE]; }; /* Maximum number of requested handles */ @@ -123,7 +128,7 @@ struct gpiohandle_request { __u32 lineoffsets[GPIOHANDLES_MAX]; __u32 flags; __u8 default_values[GPIOHANDLES_MAX]; - char consumer_label[32]; + char consumer_label[GPIO_MAX_NAME_SIZE]; __u32 lines; int fd; }; @@ -182,7 +187,7 @@ struct gpioevent_request { __u32 lineoffset; __u32 handleflags; __u32 eventflags; - char consumer_label[32]; + char consumer_label[GPIO_MAX_NAME_SIZE]; int fd; }; From patchwork Fri Aug 14 03:02:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B346C433E3 for ; Fri, 14 Aug 2020 03:04:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48ACB2078B for ; Fri, 14 Aug 2020 03:04:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ywc50p8q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726777AbgHNDD7 (ORCPT ); Thu, 13 Aug 2020 23:03:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDD7 (ORCPT ); Thu, 13 Aug 2020 23:03:59 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C85BC061757; Thu, 13 Aug 2020 20:03:59 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id q19so3558117pll.0; Thu, 13 Aug 2020 20:03:59 -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=/NSQWGG4Dd/lyfNZqBtWl2adXGHauiXHc+m8VldHKpo=; b=Ywc50p8q2t0qU0e6/oegr6rKEKFA3JlQVb5hQYT3OatbH6pCT+Wi3Vmwt+J2lyqPrY 6p+XI8YCMnZh06kvAkXSXBVXLVPoUuuDuahRs9qk6W3qhmsP/ML+UI5gImEPvmeyHwsE xPQmR/zI4OK/6pGH5o2+ckZhsK4UV70/veorBo4j1nrkFXRU07ka0i8ke8LSI2Jr9rOM /9UjwmJAlH61foUtY7GnBxtoBt/NxjAMs61YftDbS1X4LYJKMRXZDR2sCKIq1/VcENXA 0aihl9Jb7FYU14a+8SbgU3UcyiggH2MmsqLx7d06RVb+6nOdbTsAoRi9asF7da6C5m9M Jw5A== 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=/NSQWGG4Dd/lyfNZqBtWl2adXGHauiXHc+m8VldHKpo=; b=Qlde+mJeKgOlQ+x5/Sjt4QoaHqMkiKFrBDDszdpOiv14eWMMghUnvc9xt4a6/FBUha HQq+YX5ygRT4T5QzlMsB0fOUywz0dKOJVISb3+tC6MquRWfWi17+NQweNdtEza+TJ6pz 6ru0Xu13QacU0pjazeioU7mqS8yCDDgObymbZJ7A8E8OjTueNccAl05s789rNzf37taf jKKcARR4kJw5nuMAQIm2P7bkuTnk+ntyMRkm6vdCM69H4vBrzJ2+qXNzRR6MzimWKwdC z4x801oiBI/1xsDtvRFvnAt9XpAInDMojMPCJ/9IgM+VOdT5sFqyJMd/N6KQltfsPv5S 2JrQ== X-Gm-Message-State: AOAM532hLCircdyADSvd25sd0vUnOpa+438iybeEuOqIByG2WJseKLny hu43Hq+c6R7N2hRpqhXbPAfZEA3+ X-Google-Smtp-Source: ABdhPJwyZIQwSUKaVlDKRond0uwUd1ZJP5aUtzmYiKalk9o5AhbJK37CN+A3/kBI5kFc+NhIs4nBPQ== X-Received: by 2002:a17:90a:fa92:: with SMTP id cu18mr545693pjb.215.1597374238221; Thu, 13 Aug 2020 20:03:58 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:03:57 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 05/20] gpiolib: make cdev a build option Date: Fri, 14 Aug 2020 11:02:42 +0800 Message-Id: <20200814030257.135463-6-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Make the gpiolib-cdev module a build option. This allows the CDEV interface to be removed from the kernel to reduce kernel size in applications where is it not required, and provides the parent for other other CDEV interface specific build options to follow. Suggested-by: Bartosz Golaszewski Signed-off-by: Kent Gibson --- drivers/gpio/Kconfig | 17 +++++++++++++++-- drivers/gpio/Makefile | 2 +- drivers/gpio/gpiolib-cdev.h | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 8030fd91a3cc..8e409b9c33dc 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -66,8 +66,21 @@ config GPIO_SYSFS This ABI is deprecated. If you want to use GPIO from userspace, use the character device /dev/gpiochipN with the appropriate - ioctl() operations instead. The character device is always - available. + ioctl() operations instead. + +config GPIO_CDEV + bool + prompt "Character device (/dev/gpiochipN) support" if EXPERT + default y + help + Say Y here to add the character device /dev/gpiochipN interface + for GPIOs. The character device allows userspace to control GPIOs + using ioctl() operations. + + Only say N if you are sure that the GPIO character device is not + required. + + If unsure, say Y. config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 4f9abff4f2dc..7c24c8d77068 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -7,8 +7,8 @@ obj-$(CONFIG_GPIOLIB) += gpiolib.o obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.o obj-$(CONFIG_GPIOLIB) += gpiolib-devprop.o -obj-$(CONFIG_GPIOLIB) += gpiolib-cdev.o obj-$(CONFIG_OF_GPIO) += gpiolib-of.o +obj-$(CONFIG_GPIO_CDEV) += gpiolib-cdev.o obj-$(CONFIG_GPIO_SYSFS) += gpiolib-sysfs.o obj-$(CONFIG_GPIO_ACPI) += gpiolib-acpi.o diff --git a/drivers/gpio/gpiolib-cdev.h b/drivers/gpio/gpiolib-cdev.h index 973578e7ad10..19a4e3d57120 100644 --- a/drivers/gpio/gpiolib-cdev.h +++ b/drivers/gpio/gpiolib-cdev.h @@ -5,7 +5,22 @@ #include +#ifdef CONFIG_GPIO_CDEV + int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt); void gpiolib_cdev_unregister(struct gpio_device *gdev); +#else + +static inline int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) +{ + return 0; +} + +static inline void gpiolib_cdev_unregister(struct gpio_device *gdev) +{ +} + +#endif /* CONFIG_GPIO_CDEV */ + #endif /* GPIOLIB_CDEV_H */ From patchwork Fri Aug 14 03:02:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254616 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2862C433E1 for ; Fri, 14 Aug 2020 03:04:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4DF220716 for ; Fri, 14 Aug 2020 03:04:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QWVx3ba1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726815AbgHNDEQ (ORCPT ); Thu, 13 Aug 2020 23:04:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDEP (ORCPT ); Thu, 13 Aug 2020 23:04:15 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61223C061757; Thu, 13 Aug 2020 20:04:15 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id d19so3831462pgl.10; Thu, 13 Aug 2020 20:04:15 -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=7NF47HewwH7DV/zOo/X1hDT1U7DvTSRmB73C31HJ3gQ=; b=QWVx3ba1o2CGaD+1DUt37uTQJGPa7S7DiSWentLZzaWdGYzC+SrJC+UF2YgedtVHs0 6p4ckxEvsocAkvgfVD9wrzPMnCZnWJcplkInYeojy7WkLS1yWiWNovoF3D4WOUxxHqKh CHzz4fK9VIHpeOSnSxEVwj4Da0a1rz9P7Ij+8Y/jEEa41b9lAo8bah0VHtMkD5ygJr2O Iky/NRFRXG8XFRue1z9ylr3pP64RHrQroVoLeNjtKWsmEwI4pYHvzdPXNtX4Bv7Va8yE L3klFFZYPzxE2C2CzfRBlOSJ+7jPnAh8VAYvCM1278n+WOg0/9Xh6zXjdBgmjsv2WGk0 nUZQ== 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=7NF47HewwH7DV/zOo/X1hDT1U7DvTSRmB73C31HJ3gQ=; b=TUPvxVacTG9/GutWp+gcC5t1FXMe4zsvd9pEAZsVrLngMkSFOBjGIjwPFxdpPDo/nJ +fu9Ktf0QOaLzEwK6KjhM4ggcBTov6mP4Q4EzgWOkUgICYn6saAH7+ClK2zyYQYwdOCp N7c3/eRusR7gWt/2T/5tB1+zTj0hBneSx7MMyUw/6W86PTbuThoCtlLFaXyQTHQErHll nYSRo9Nkx/O47P5s+TLVNdbMEE8C377cVnFOmWVzQgPlTO3ek+OqIoZhcXK52hz5hXtM ys8VkSXd2zjH5JpSZMMNSUxWzAhM4C+mjTUP4aSfQ5MNVpsH+518UA9yoCaNZBp8Nfop L9aQ== X-Gm-Message-State: AOAM532voviKQqaCVCrVWIPuGmMwWeRnE6bakgmyp0qDPO5fH90RQfd/ m6PcZbzWjdf7P6z/rBOteg0pVLAG X-Google-Smtp-Source: ABdhPJwL4UnhWwMNEQU60nhjb7g9X43t6cf0cZB1EMzEfB4VmHlH8oU1D40EZHMiXnm8Y+o6FD1q5w== X-Received: by 2002:a65:568b:: with SMTP id v11mr420383pgs.396.1597374254390; Thu, 13 Aug 2020 20:04:14 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:04:13 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL Date: Fri, 14 Aug 2020 11:02:44 +0800 Message-Id: <20200814030257.135463-8-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 support for requesting lines using the GPIO_V2_GET_LINE_IOCTL, and returning their current values using GPIO_V2_LINE_GET_VALUES_IOCTL. Signed-off-by: Kent Gibson --- The struct line implementation is based on the v1 struct linehandle implementation. The line_ioctl() is a simple wrapper around line_get_values() here, but will be extended with other ioctls in subsequent patches. Changes for v4: - fix handling of mask in line_get_values drivers/gpio/gpiolib-cdev.c | 413 ++++++++++++++++++++++++++++++++++++ 1 file changed, 413 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 8b012879fe3f..8671e04ff989 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include +#include #include #include #include @@ -34,6 +36,7 @@ * GPIO line handle management */ +#ifdef CONFIG_GPIO_CDEV_V1 /** * struct linehandle_state - contains the state of a userspace handle * @gdev: the GPIO device the handle pertains to @@ -376,6 +379,390 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) linehandle_free(lh); return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ + +/** + * struct line - contains the state of a userspace line request + * @gdev: the GPIO device the line request pertains to + * @label: consumer label used to tag descriptors + * @num_descs: the number of descriptors held in the descs array + * @descs: the GPIO descriptors held by this line request, with @num_descs + * elements. + */ +struct line { + struct gpio_device *gdev; + const char *label; + u32 num_descs; + struct gpio_desc *descs[]; +}; + +#define GPIO_V2_LINE_BIAS_FLAGS \ + (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ + GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ + GPIO_V2_LINE_FLAG_BIAS_DISABLED) + +#define GPIO_V2_LINE_DIRECTION_FLAGS \ + (GPIO_V2_LINE_FLAG_INPUT | \ + GPIO_V2_LINE_FLAG_OUTPUT) + +#define GPIO_V2_LINE_DRIVE_FLAGS \ + (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ + GPIO_V2_LINE_FLAG_OPEN_SOURCE) + +#define GPIO_V2_LINE_VALID_FLAGS \ + (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ + GPIO_V2_LINE_DIRECTION_FLAGS | \ + GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_BIAS_FLAGS) + +static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, + int line_idx) +{ + int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && + (lc->attrs[i].mask & mask)) + return lc->attrs[i].attr.flags; + } + return lc->flags; +} + +static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, + int line_idx) +{ + int i; + u64 mask = BIT_ULL(line_idx); + + for (i = 0; i < lc->num_attrs; i++) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) && + (lc->attrs[i].mask & mask)) + return !!(lc->attrs[i].attr.values & mask); + } + return 0; +} + +static int gpio_v2_line_flags_validate(u64 flags) +{ + /* Return an error if an unknown flag is set */ + if (flags & ~GPIO_V2_LINE_VALID_FLAGS) + return -EINVAL; + + /* + * Do not allow both INPUT & OUTPUT flags to be set as they are + * contradictory. + */ + if ((flags & GPIO_V2_LINE_FLAG_INPUT) && + (flags & GPIO_V2_LINE_FLAG_OUTPUT)) + return -EINVAL; + + /* + * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If + * the hardware actually supports enabling both at the same time the + * electrical result would be disastrous. + */ + if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) && + (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE)) + return -EINVAL; + + /* Drive requires explicit output direction. */ + if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) && + !(flags & GPIO_V2_LINE_FLAG_OUTPUT)) + return -EINVAL; + + /* Bias requies explicit direction. */ + if ((flags & GPIO_V2_LINE_BIAS_FLAGS) && + !(flags & GPIO_V2_LINE_DIRECTION_FLAGS)) + return -EINVAL; + + /* Only one bias flag can be set. */ + if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) && + (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | + GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) || + ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) && + (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) + return -EINVAL; + + return 0; +} + +static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, + int num_lines) +{ + int i, ret; + u64 flags; + + if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX) + return -EINVAL; + + if (memchr_inv(lc->padding, 0, sizeof(lc->padding))) + return -EINVAL; + + for (i = 0; i < num_lines; i++) { + flags = gpio_v2_line_config_flags(lc, i); + ret = gpio_v2_line_flags_validate(flags); + if (ret) + return ret; + } + return 0; +} + +static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, + unsigned long *flagsp) +{ + assign_bit(FLAG_ACTIVE_LOW, flagsp, + flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW); + if (flags & GPIO_V2_LINE_FLAG_OUTPUT) + set_bit(FLAG_IS_OUT, flagsp); + else if (flags & GPIO_V2_LINE_FLAG_INPUT) + clear_bit(FLAG_IS_OUT, flagsp); + assign_bit(FLAG_OPEN_DRAIN, flagsp, + flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); + assign_bit(FLAG_OPEN_SOURCE, flagsp, + flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE); + assign_bit(FLAG_PULL_UP, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP); + assign_bit(FLAG_PULL_DOWN, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN); + assign_bit(FLAG_BIAS_DISABLE, flagsp, + flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED); +} + +static long line_get_values(struct line *line, void __user *ip) +{ + struct gpio_v2_line_values lv; + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + int ret, i, didx, num_get = 0; + + /* NOTE: It's ok to read values of output lines. */ + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + if (lv.mask == 0) + return -EINVAL; + + for (i = 0; i < line->num_descs; i++) + if (lv.mask & BIT_ULL(i)) + num_get++; + if (num_get == line->num_descs) { + descs = line->descs; + } else { + descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); + for (didx = 0, i = 0; i < line->num_descs; i++) { + if (lv.mask & BIT_ULL(i)) { + descs[didx] = line->descs[i]; + didx++; + } + } + } + ret = gpiod_get_array_value_complex(false, + true, + num_get, + descs, + NULL, + vals); + + if (descs != line->descs) + kfree(descs); + if (ret) + return ret; + + lv.bits = 0; + for (didx = 0, i = 0; i < line->num_descs; i++) { + if (lv.mask & BIT_ULL(i)) { + if (test_bit(didx, vals)) + lv.bits |= BIT_ULL(i); + didx++; + } + } + + if (copy_to_user(ip, &lv, sizeof(lv))) + return -EFAULT; + + return 0; +} + +static long line_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct line *line = file->private_data; + void __user *ip = (void __user *)arg; + + if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) + return line_get_values(line, ip); + + return -EINVAL; +} + +#ifdef CONFIG_COMPAT +static long line_ioctl_compat(struct file *file, unsigned int cmd, + unsigned long arg) +{ + return line_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); +} +#endif + +static void line_free(struct line *line) +{ + int i; + + for (i = 0; i < line->num_descs; i++) { + if (line->descs[i]) + gpiod_free(line->descs[i]); + } + kfree(line->label); + put_device(&line->gdev->dev); + kfree(line); +} + +static int line_release(struct inode *inode, struct file *file) +{ + struct line *line = file->private_data; + + line_free(line); + return 0; +} + +static const struct file_operations line_fileops = { + .release = line_release, + .owner = THIS_MODULE, + .llseek = noop_llseek, + .unlocked_ioctl = line_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = line_ioctl_compat, +#endif +}; + +static int line_create(struct gpio_device *gdev, void __user *ip) +{ + struct gpio_v2_line_request lr; + struct line *line; + struct file *file; + int fd, i, ret; + struct gpio_v2_line_config *lc; + unsigned long flags; + + if (copy_from_user(&lr, ip, sizeof(lr))) + return -EFAULT; + if ((lr.num_lines == 0) || (lr.num_lines > GPIO_V2_LINES_MAX)) + return -EINVAL; + + if (memchr_inv(lr.padding, 0, sizeof(lr.padding))) + return -EINVAL; + + lc = &lr.config; + ret = gpio_v2_line_config_validate(lc, lr.num_lines); + if (ret) + return ret; + + line = kzalloc(struct_size(line, descs, lr.num_lines), + GFP_KERNEL); + if (!line) + return -ENOMEM; + + line->gdev = gdev; + get_device(&gdev->dev); + + /* Make sure this is terminated */ + lr.consumer[sizeof(lr.consumer)-1] = '\0'; + if (strlen(lr.consumer)) { + line->label = kstrdup(lr.consumer, GFP_KERNEL); + if (!line->label) { + ret = -ENOMEM; + goto out_free_line; + } + } + + line->num_descs = lr.num_lines; + + /* Request each GPIO */ + for (i = 0; i < lr.num_lines; i++) { + u32 offset = lr.offsets[i]; + struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); + + if (IS_ERR(desc)) { + ret = PTR_ERR(desc); + goto out_free_line; + } + + ret = gpiod_request(desc, line->label); + if (ret) + goto out_free_line; + + line->descs[i] = desc; + flags = gpio_v2_line_config_flags(lc, i); + gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); + + ret = gpiod_set_transitory(desc, false); + if (ret < 0) + goto out_free_line; + + /* + * Lines have to be requested explicitly for input + * or output, else the line will be treated "as is". + */ + if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { + int val = gpio_v2_line_config_output_value(lc, i); + + ret = gpiod_direction_output(desc, val); + if (ret) + goto out_free_line; + } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { + ret = gpiod_direction_input(desc); + if (ret) + goto out_free_line; + } + + blocking_notifier_call_chain(&desc->gdev->notifier, + GPIOLINE_CHANGED_REQUESTED, desc); + + dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", + offset); + } + + fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); + if (fd < 0) { + ret = fd; + goto out_free_line; + } + + file = anon_inode_getfile("gpio-line", + &line_fileops, + line, + O_RDONLY | O_CLOEXEC); + if (IS_ERR(file)) { + ret = PTR_ERR(file); + goto out_put_unused_fd; + } + + lr.fd = fd; + if (copy_to_user(ip, &lr, sizeof(lr))) { + /* + * fput() will trigger the release() callback, so do not go onto + * the regular error cleanup path here. + */ + fput(file); + put_unused_fd(fd); + return -EFAULT; + } + + fd_install(fd, file); + + dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", + line->num_descs); + + return 0; + +out_put_unused_fd: + put_unused_fd(fd); +out_free_line: + line_free(line); + return ret; +} + +#ifdef CONFIG_GPIO_CDEV_V1 /* * GPIO line event management @@ -745,6 +1132,8 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +#endif /* CONFIG_GPIO_CDEV_V1 */ + static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpioline_info *info) { @@ -842,6 +1231,7 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; return 0; +#ifdef CONFIG_GPIO_CDEV_V1 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { struct gpioline_info lineinfo; @@ -884,6 +1274,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } return 0; +#endif /* CONFIG_GPIO_CDEV_V1 */ + } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { + return line_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { if (copy_from_user(&offset, ip, sizeof(offset))) return -EFAULT; @@ -1109,4 +1502,24 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt) void gpiolib_cdev_unregister(struct gpio_device *gdev) { cdev_device_del(&gdev->chrdev, &gdev->dev); + + /* + * array sizes must ensure 64-bit alignment and not create holes in + * the struct packing. + */ + BUILD_BUG_ON(GPIO_V2_LINES_MAX % 2); + BUILD_BUG_ON(GPIO_MAX_NAME_SIZE % 8); + + /* + * check that uAPI structs are 64-bit aligned for 32/64-bit + * compatibility + */ + BUILD_BUG_ON(sizeof(struct gpio_v2_line_attribute) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_config_attribute) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_config) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_request) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_info) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_info_changed) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_event) % 8); + BUILD_BUG_ON(sizeof(struct gpio_v2_line_values) % 8); } From patchwork Fri Aug 14 03:02:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 407BBC433E1 for ; Fri, 14 Aug 2020 03:04:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1228820716 for ; Fri, 14 Aug 2020 03:04:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JjDF9AHt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726854AbgHNDEd (ORCPT ); Thu, 13 Aug 2020 23:04:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDEd (ORCPT ); Thu, 13 Aug 2020 23:04:33 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D53C061757; Thu, 13 Aug 2020 20:04:33 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id l60so3738552pjb.3; Thu, 13 Aug 2020 20:04:32 -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=0lkfS04wTAwxyVJLfeLESo1LkiYHB4fDEphiDr+5RhA=; b=JjDF9AHtPCUeHy/MpXfn2BrnRe9sBW8jLMeTeCGk83fY69GOzxDG595nqD+EJVWJJD g1n8PMjz44Jlq4F/KnezKng2hkr5bcu1n/dtvz0VB+Ld0Z+gH5hxkK3A6mmTC6xI/LJc m/G+LNHbpwz0qCZBudtSNviicSJ0BZyi/M+zFbtp+QGkKx7gMm+XnuZxrMvEv4tvPUox dbFlD9vdXzCrJwzGtETN/3IsWG6dd6BW40FuGITpFepmBv1MOxeCjCW1ARFKdb82dirf FidJtxqawgUnmE2SytbmFFf3aRf/mNlUC45dcmlcg1MmC7PLfcBRQ+2vojzUjdEOAwj0 PkDA== 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=0lkfS04wTAwxyVJLfeLESo1LkiYHB4fDEphiDr+5RhA=; b=fvI4NiL+kmbSMxX50dflbc1y021Iyvo52Ast91cbF19VaJ58LusT+3tmc2eFHCVHI8 pH5KFWXwJ0wlEWSmyoHjxAcBXheOoNAUlwptPFElgmBYMceaLiJEkMThTP+vN9R1PLKJ NAFlpX4tbU0db/RXf2/mft5y54Vz1sCjozrDpbWPpnBKY7K8bfoPrGRMBZlMaDoGxNKi JYnNN08yoOt37sqkFTl8TNt41TcoG1jBfDsLWWeqsFpdEeUpefVFK3sx3q9DvcCFq9qQ KP4erHR3N78Fb9bbogxbXmt0Gczbl+EFuiuYwjPGDp+pH/daVtwdJECWFfAynZCASPbC 66tA== X-Gm-Message-State: AOAM532512zBxYTK3iUfywn2VnOx7nmoCiyYwd2KEQsdz8R81ec9sadC I189mRwp6xiszc2yr0C1OhEHZjNF X-Google-Smtp-Source: ABdhPJz2+r1X8/zMt+exRGfHXw49smtJhkqiJv1jFmVz9UF1U4eQarZW3y7ygLnzN87QUNV7gikf3g== X-Received: by 2002:a17:902:6f01:: with SMTP id w1mr602779plk.49.1597374271872; Thu, 13 Aug 2020 20:04:31 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.04.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:04:31 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 09/20] gpiolib: cdev: support edge detection for uAPI v2 Date: Fri, 14 Aug 2020 11:02:46 +0800 Message-Id: <20200814030257.135463-10-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 support for edge detection to lines requested using GPIO_V2_GET_LINE_IOCTL. Signed-off-by: Kent Gibson --- The edge_detector implementation is based on the v1 lineevent implementation. drivers/gpio/gpiolib-cdev.c | 316 +++++++++++++++++++++++++++++++++++- drivers/gpio/gpiolib.c | 2 + drivers/gpio/gpiolib.h | 2 + 3 files changed, 319 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 28273f3105d3..1d42a01f5414 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -382,11 +382,43 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) } #endif /* CONFIG_GPIO_CDEV_V1 */ +/** + * struct edge_detector - contains the state of a line edge detector + * @line: the corresponding line request + * @irq: the interrupt triggered in response to events on this GPIO + * @flags: the flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or + * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied + * @timestamp: cache for the timestamp storing it between hardirq and IRQ + * thread, used to bring the timestamp close to the actual event + * @seqno: the seqno for the current edge event in the sequence of events + * for the corresponding line request. Ths is drawn from the @line. + * @line_seqno: the seqno for the current edge event in the sequence of + * events for this line. + */ +struct edge_detector { + struct line *line; + unsigned int irq; + u64 flags; + /* + * timestamp and seqno are shared by edge_irq_handler() and + * edge_irq_thread() which are themselves mutually exclusive. + */ + u64 timestamp; + u32 seqno; + u32 line_seqno; +}; + /** * struct line - contains the state of a userspace line request * @gdev: the GPIO device the line request pertains to * @label: consumer label used to tag descriptors * @num_descs: the number of descriptors held in the descs array + * @wait: wait queue that handles blocking reads of events + * @events: KFIFO for the GPIO events + * @seqno: the sequence number for edge events generated on all lines in + * this line request. Note that this is not used when @num_descs is 1, as + * the line_seqno is then the same and is cheaper to calculate. + * @edets: an array of edge detectors, of size @num_descs * @descs: the GPIO descriptors held by this line request, with @num_descs * elements. */ @@ -394,9 +426,146 @@ struct line { struct gpio_device *gdev; const char *label; u32 num_descs; + wait_queue_head_t wait; + DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); + atomic_t seqno; + struct edge_detector *edets; struct gpio_desc *descs[]; }; +static inline struct gpio_desc *edge_detector_desc( + const struct edge_detector *edet) +{ + return edet->line->descs[edet - &edet->line->edets[0]]; +} + +static irqreturn_t edge_irq_thread(int irq, void *p) +{ + struct edge_detector *edet = p; + struct line *line = edet->line; + struct gpio_desc *desc = edge_detector_desc(edet); + struct gpio_v2_line_event le; + int ret; + + /* Do not leak kernel stack to userspace */ + memset(&le, 0, sizeof(le)); + + /* + * We may be running from a nested threaded interrupt in which case + * we didn't get the timestamp from edge_irq_handler(). + */ + if (!edet->timestamp) { + le.timestamp = ktime_get_ns(); + if (line->num_descs != 1) + edet->seqno = atomic_inc_return(&line->seqno); + } else { + le.timestamp = edet->timestamp; + } + edet->timestamp = 0; + + if (edet->flags == (GPIO_V2_LINE_FLAG_EDGE_RISING | + GPIO_V2_LINE_FLAG_EDGE_FALLING)) { + int level = gpiod_get_value_cansleep(desc); + + if (level) + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + else + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + } else if (edet->flags == GPIO_V2_LINE_FLAG_EDGE_RISING) { + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + } else if (edet->flags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + } else { + return IRQ_NONE; + } + edet->line_seqno++; + le.line_seqno = edet->line_seqno; + le.seqno = (line->num_descs == 1) ? le.line_seqno : edet->seqno; + le.offset = gpio_chip_hwgpio(desc); + + ret = kfifo_in_spinlocked_noirqsave(&line->events, &le, + 1, &line->wait.lock); + if (ret) + wake_up_poll(&line->wait, EPOLLIN); + else + pr_debug_ratelimited("event FIFO is full - event dropped\n"); + + return IRQ_HANDLED; +} + +static irqreturn_t edge_irq_handler(int irq, void *p) +{ + struct edge_detector *edet = p; + struct line *line = edet->line; + + /* + * Just store the timestamp in hardirq context so we get it as + * close in time as possible to the actual event. + */ + edet->timestamp = ktime_get_ns(); + + if (line->num_descs != 1) + edet->seqno = atomic_inc_return(&line->seqno); + + return IRQ_WAKE_THREAD; +} + +static int edge_detector_start(struct edge_detector *edet) +{ + int ret, irq, irqflags = 0; + struct gpio_desc *desc; + + desc = edge_detector_desc(edet); + irq = gpiod_to_irq(desc); + + if (irq <= 0) + return -ENODEV; + + edet->seqno = 0; + edet->line_seqno = 0; + + if (edet->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? + IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; + if (edet->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? + IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; + irqflags |= IRQF_ONESHOT; + + /* Request a thread to read the events */ + ret = request_threaded_irq(irq, + edge_irq_handler, + edge_irq_thread, + irqflags, + edet->line->label, + edet); + if (ret) + return ret; + + edet->irq = irq; + return 0; +} + +static void edge_detector_stop(struct edge_detector *edet) +{ + if (edet->irq) { + free_irq(edet->irq, edet); + edet->irq = 0; + } +} + +static int edge_detector_setup(struct edge_detector *edet, + struct gpio_v2_line_config *lc) +{ + if (edet->flags) + return edge_detector_start(edet); + return 0; +} + #define GPIO_V2_LINE_BIAS_FLAGS \ (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \ @@ -410,10 +579,15 @@ struct line { (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \ GPIO_V2_LINE_FLAG_OPEN_SOURCE) +#define GPIO_V2_LINE_EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) + #define GPIO_V2_LINE_VALID_FLAGS \ (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \ GPIO_V2_LINE_DIRECTION_FLAGS | \ GPIO_V2_LINE_DRIVE_FLAGS | \ + GPIO_V2_LINE_EDGE_FLAGS | \ GPIO_V2_LINE_BIAS_FLAGS) static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, @@ -430,6 +604,21 @@ static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, return lc->flags; } +static int gpio_v2_line_config_has_edge_detection(struct gpio_v2_line_config *lc) +{ + int i; + + if (lc->flags & GPIO_V2_LINE_EDGE_FLAGS) + return 1; + + for (i = 0; i < lc->num_attrs; i--) { + if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) && + (lc->attrs[i].attr.flags & GPIO_V2_LINE_EDGE_FLAGS)) + return 1; + } + return 0; +} + static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc, int line_idx) { @@ -458,6 +647,11 @@ static int gpio_v2_line_flags_validate(u64 flags) (flags & GPIO_V2_LINE_FLAG_OUTPUT)) return -EINVAL; + /* Edge detection requires explicit input. */ + if ((flags & GPIO_V2_LINE_EDGE_FLAGS) && + !(flags & GPIO_V2_LINE_FLAG_INPUT)) + return -EINVAL; + /* * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If * the hardware actually supports enabling both at the same time the @@ -518,6 +712,10 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, set_bit(FLAG_IS_OUT, flagsp); else if (flags & GPIO_V2_LINE_FLAG_INPUT) clear_bit(FLAG_IS_OUT, flagsp); + assign_bit(FLAG_EDGE_RISING, flagsp, + flags & GPIO_V2_LINE_FLAG_EDGE_RISING); + assign_bit(FLAG_EDGE_FALLING, flagsp, + flags & GPIO_V2_LINE_FLAG_EDGE_FALLING); assign_bit(FLAG_OPEN_DRAIN, flagsp, flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN); assign_bit(FLAG_OPEN_SOURCE, flagsp, @@ -605,14 +803,87 @@ static long line_ioctl_compat(struct file *file, unsigned int cmd, } #endif +static __poll_t line_poll(struct file *file, + struct poll_table_struct *wait) +{ + struct line *line = file->private_data; + __poll_t events = 0; + + poll_wait(file, &line->wait, wait); + + if (!kfifo_is_empty_spinlocked_noirqsave(&line->events, + &line->wait.lock)) + events = EPOLLIN | EPOLLRDNORM; + + return events; +} + +static ssize_t line_read(struct file *file, + char __user *buf, + size_t count, + loff_t *f_ps) +{ + struct line *line = file->private_data; + struct gpio_v2_line_event le; + ssize_t bytes_read = 0; + int ret; + + if (count < sizeof(le)) + return -EINVAL; + + do { + spin_lock(&line->wait.lock); + if (kfifo_is_empty(&line->events)) { + if (bytes_read) { + spin_unlock(&line->wait.lock); + return bytes_read; + } + + if (file->f_flags & O_NONBLOCK) { + spin_unlock(&line->wait.lock); + return -EAGAIN; + } + + ret = wait_event_interruptible_locked(line->wait, + !kfifo_is_empty(&line->events)); + if (ret) { + spin_unlock(&line->wait.lock); + return ret; + } + } + + ret = kfifo_out(&line->events, &le, 1); + spin_unlock(&line->wait.lock); + if (ret != 1) { + /* + * This should never happen - we were holding the + * lock from the moment we learned the fifo is no + * longer empty until now. + */ + ret = -EIO; + break; + } + + if (copy_to_user(buf + bytes_read, &le, sizeof(le))) + return -EFAULT; + bytes_read += sizeof(le); + } while (count >= bytes_read + sizeof(le)); + + return bytes_read; +} + static void line_free(struct line *line) { int i; for (i = 0; i < line->num_descs; i++) { + if (line->edets) + edge_detector_stop(&line->edets[i]); if (line->descs[i]) gpiod_free(line->descs[i]); } + kfifo_free(&line->events); + kfree(line->edets); kfree(line->label); put_device(&line->gdev->dev); kfree(line); @@ -628,6 +899,8 @@ static int line_release(struct inode *inode, struct file *file) static const struct file_operations line_fileops = { .release = line_release, + .read = line_read, + .poll = line_poll, .owner = THIS_MODULE, .llseek = noop_llseek, .unlocked_ioctl = line_ioctl, @@ -641,7 +914,7 @@ static int line_create(struct gpio_device *gdev, void __user *ip) struct gpio_v2_line_request lr; struct line *line; struct file *file; - int fd, i, ret; + int fd, i, ret, size, has_edge_detection; struct gpio_v2_line_config *lc; unsigned long flags; @@ -658,6 +931,11 @@ static int line_create(struct gpio_device *gdev, void __user *ip) if (ret) return ret; + /* event_buffer_size only valid with edge detection */ + has_edge_detection = gpio_v2_line_config_has_edge_detection(lc); + if (lr.event_buffer_size && !has_edge_detection) + return -EINVAL; + line = kzalloc(struct_size(line, descs, lr.num_lines), GFP_KERNEL); if (!line) @@ -666,6 +944,16 @@ static int line_create(struct gpio_device *gdev, void __user *ip) line->gdev = gdev; get_device(&gdev->dev); + line->edets = kcalloc(lr.num_lines, sizeof(*line->edets), + GFP_KERNEL); + if (!line->edets) { + ret = -ENOMEM; + goto out_free_line; + } + + for (i = 0; i < lr.num_lines; i++) + line->edets[i].line = line; + /* Make sure this is terminated */ lr.consumer[sizeof(lr.consumer)-1] = '\0'; if (strlen(lr.consumer)) { @@ -676,6 +964,21 @@ static int line_create(struct gpio_device *gdev, void __user *ip) } } + init_waitqueue_head(&line->wait); + if (has_edge_detection) { + size = lr.event_buffer_size; + + if (size > GPIO_V2_LINES_MAX*16) + size = GPIO_V2_LINES_MAX*16; + else if (size == 0) + size = lr.num_lines*16; + + ret = kfifo_alloc(&line->events, size, GFP_KERNEL); + if (ret) + goto out_free_line; + } + + atomic_set(&line->seqno, 0); line->num_descs = lr.num_lines; /* Request each GPIO */ @@ -714,6 +1017,10 @@ static int line_create(struct gpio_device *gdev, void __user *ip) ret = gpiod_direction_input(desc); if (ret) goto out_free_line; + line->edets[i].flags = flags & GPIO_V2_LINE_EDGE_FLAGS; + ret = edge_detector_setup(&line->edets[i], lc); + if (ret) + goto out_free_line; } blocking_notifier_call_chain(&desc->gdev->notifier, @@ -1237,6 +1544,13 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (test_bit(FLAG_PULL_UP, &desc->flags)) info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; + if (test_bit(FLAG_EDGE_RISING, &desc->flags)) + info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; + if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) + info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; + + info->num_attrs = 0; + spin_unlock_irqrestore(&gpio_lock, flags); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 80137c1b3cdc..e4c81dca7f8b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2041,6 +2041,8 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_PULL_UP, &desc->flags); clear_bit(FLAG_PULL_DOWN, &desc->flags); clear_bit(FLAG_BIAS_DISABLE, &desc->flags); + clear_bit(FLAG_EDGE_RISING, &desc->flags); + clear_bit(FLAG_EDGE_FALLING, &desc->flags); clear_bit(FLAG_IS_HOGGED, &desc->flags); #ifdef CONFIG_OF_DYNAMIC desc->hog = NULL; diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 6709f79c02dd..39b356160937 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -114,6 +114,8 @@ struct gpio_desc { #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ +#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ +#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ /* Connection label */ const char *label; From patchwork Fri Aug 14 03:02: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: 254614 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EC64C433DF for ; Fri, 14 Aug 2020 03:04:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4894620716 for ; Fri, 14 Aug 2020 03:04:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Xd1iMaRP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726681AbgHNDEx (ORCPT ); Thu, 13 Aug 2020 23:04:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726583AbgHNDEx (ORCPT ); Thu, 13 Aug 2020 23:04:53 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4316AC061757; Thu, 13 Aug 2020 20:04:53 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id m34so3832344pgl.11; Thu, 13 Aug 2020 20:04:53 -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=id2yCOcY/kLWgNpSgBrFBrYhr1/oEj38W9XI4BIRcLQ=; b=Xd1iMaRPMDmisbYJcR9JA8EgrlXLkXHq5g+FRXUw7FxNsvP4k1/PJZH3WS8LHeasuV x2NA98+5wNiWUHl95hmiMYYgt+Sf6XWAcN6f7zfH1YW7G3KBwmKZ1BfPv2FMDxvW/ayy LoJd/r9+Hwk0piSPyoRY0AdOj5Aeorposnp8Kmo9C/J6ep9aTmLD+c0NVI8QT9JWTEBd emI5qTCLANFEQ8oT7qHOdBT0DYc02VkmBDuEqKa2zw2XDQgM+eVuZZKRITzoJUxyhHtL I21bQJScSSh5jM2CnqA54+x6yqg8WcduX4cWK4p43gWS0KAyqrIZAuhH33dGb5OJMLH6 EIQw== 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=id2yCOcY/kLWgNpSgBrFBrYhr1/oEj38W9XI4BIRcLQ=; b=VImJY9pQsz2DaJ1aZZ9hBnwoM7ti1FstsfkyYaP5sriwdO2sDd7zq6b2QjWzuvfFYq eVfdPwJOeSzpNpe7E3s37Qyd3jJFtipWIc985HdzPNzuqQNefr8bT8BN9lp1BMN/W+s7 aIS+XnpXCsyuNKwC3BRy7PwM8BCEx5+6yatNfV00QMhhFNg+1wSsMynvNSqIqKGr7N7s UYoKxuKyJRbN4WqFKQsQfgI/D3T4YnVTPTzaFP+BLqALGOkUkUckKa9UbwepxG6JGDZJ xgPHT577uWyFm9/Tktsk4f2A79+GemJlAa5cZOSmmvUDO9ZUC2YA5cHZ3G1JnA9ZlYUL 0iwg== X-Gm-Message-State: AOAM532DV20pUMTOYXWap/rJRV2KzDjEO+zESsaoYrtrAwkZIoIg3I1z jUF5eOoG+ESe6EilHGL7nGImFm8r X-Google-Smtp-Source: ABdhPJwgGxpU9hYbuXpqcIykFyRwBa4hmiFAzXBHiUj8zO5ijlEjMyrI9EUWHT5Kw/YKXXKM2oetoA== X-Received: by 2002:a63:1f04:: with SMTP id f4mr439564pgf.34.1597374292434; Thu, 13 Aug 2020 20:04:52 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:04:51 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL Date: Fri, 14 Aug 2020 11:02:48 +0800 Message-Id: <20200814030257.135463-12-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 support for the GPIO_V2_LINE_SET_VALUES_IOCTL. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 04472c2b6678..de88b7a5ba0f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -811,6 +811,71 @@ static long line_get_values(struct line *line, void __user *ip) return 0; } +static long line_set_values_locked(struct line *line, + struct gpio_v2_line_values *lv) +{ + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + int ret, i, didx, num_set = 0; + + bitmap_zero(vals, GPIO_V2_LINES_MAX); + for (i = 0; i < line->num_descs; i++) { + if (lv->mask & BIT_ULL(i)) { + if (!test_bit(FLAG_IS_OUT, &line->descs[i]->flags)) + return -EPERM; + if (lv->bits & BIT_ULL(i)) + __set_bit(num_set, vals); + num_set++; + } + } + if (num_set == 0) + return -EINVAL; + + if (num_set == line->num_descs) + /* Reuse the array setting function */ + return gpiod_set_array_value_complex(false, + true, + line->num_descs, + line->descs, + NULL, + vals); + + /* build compacted desc array and values */ + descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); + for (didx = 0, i = 0; i < line->num_descs; i++) { + if (lv->mask & BIT_ULL(i)) { + descs[didx] = line->descs[i]; + didx++; + } + } + ret = gpiod_set_array_value_complex(false, + true, + num_set, + descs, + NULL, + vals); + + kfree(descs); + return ret; +} + +static long line_set_values(struct line *line, void __user *ip) +{ + struct gpio_v2_line_values lv; + int ret; + + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + mutex_lock(&line->config_mutex); + + ret = line_set_values_locked(line, &lv); + + mutex_unlock(&line->config_mutex); + + return ret; +} + static long line_set_config_locked(struct line *line, struct gpio_v2_line_config *lc) { @@ -880,6 +945,8 @@ static long line_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return line_get_values(line, ip); + else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) + return line_set_values(line, ip); else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) return line_set_config(line, ip); From patchwork Fri Aug 14 03:02:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254613 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B58A2C433DF for ; Fri, 14 Aug 2020 03:05:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8BAE12078B for ; Fri, 14 Aug 2020 03:05:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tWE/vFQh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726938AbgHNDFN (ORCPT ); Thu, 13 Aug 2020 23:05:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726690AbgHNDFM (ORCPT ); Thu, 13 Aug 2020 23:05:12 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1557C061383; Thu, 13 Aug 2020 20:05:11 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id k13so3529593plk.13; Thu, 13 Aug 2020 20:05:11 -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=9daGc+bQBQhtyKnIGABCoe3Sraaytqyp+0NjDqFhrAU=; b=tWE/vFQhF4JVkq0UAtlrFfLFuE0m1EAIbkz6Sqg3ea7NaJf1BR+TVpN0W3PhNV8PJF Tr+JvKQXEXFfY34Q1aJ/km84LQDXNJNcEfbLfcUuCld+FChPcFN93/fg+JHrL+R88q6Q yOL7jAar5fi6Px5Y6PBC/lDU+X3C2Mgf3d5iFWW1d5w8VRS4pJlqtJzIJI/upCQDW2Zn kze3EiTNb4lw0Jlq9LxyjUAbNSdD7xZCdLiROUA4P9Iqe6eVlZqD8GGVA2c3paIdUUlT KklNKRclIdoSpzytXqI1oPhWc+hUyAUm0rlge9fub+jdh/tpa/d65w2Hw2mli7eGXt0U yyBg== 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=9daGc+bQBQhtyKnIGABCoe3Sraaytqyp+0NjDqFhrAU=; b=YXedpbd7c5lNe+j5EBOMrMkkQD9hXgIwNOEtcpLc2IHzEvBH2qjg3FO1cdvrFWdCY4 O2PnH5YfBPFUTDx3xX00XMtECikMi7V2A+P0ouDrmJru926T4M1jwUcc1jTJDbU/m+K/ xXmhiwIljuYcpYNMDL0/BnnzNwE7Xyx80tn0XGRB/berfn9ck8ka1MSFC7c90e5cT/f8 RlQNu2Jy4GQBjZuQRYX4aSPFUZww9ds91t3MFe50opi7RhmJHxY5KyhtzEFhFA4If4Ph 0rBAtHrsj1EIkgrWw7/hay2R5QaywfhLdit34VsRVMjWhpkVlYiXhSaImTJ9AChsyZ4w esag== X-Gm-Message-State: AOAM531fPWafKE6T0luoBCabZutssk1Dxb8CwxVWj6GJgfokloUw4MLb CTi+XkYesrVel7lZ3mvorGZ+K0v5 X-Google-Smtp-Source: ABdhPJxOETVuvb0Lqh4wKmNHKkF2SNyOPIB2lNBPt9if8kaL+cqulsR9lpghZUUUFUt5c7Q1n1eAQA== X-Received: by 2002:a17:90a:256b:: with SMTP id j98mr598491pje.83.1597374310653; Thu, 13 Aug 2020 20:05:10 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:05:10 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 13/20] gpio: uapi: document uAPI v1 as deprecated Date: Fri, 14 Aug 2020 11:02:50 +0800 Message-Id: <20200814030257.135463-14-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Update uAPI documentation to deprecate v1 structs and ioctls. Signed-off-by: Kent Gibson --- include/uapi/linux/gpio.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 0eb1f53b47e0..4af67415d73e 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -274,6 +274,9 @@ struct gpio_v2_line_event { /* * ABI v1 + * + * This version of the ABI is deprecated and will be removed in the future. + * Use the latest version of the ABI, defined above, instead. */ /* Informational flags */ @@ -297,6 +300,9 @@ struct gpio_v2_line_event { * @consumer: a functional name for the consumer of this GPIO line as set by * whatever is using it, will be empty if there is no current user but may * also be empty if the consumer doesn't set this up + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_info instead. */ struct gpioline_info { __u32 line_offset; @@ -328,6 +334,9 @@ enum { * guarantee there are no implicit holes between it and subsequent members. * The 20-byte padding at the end makes sure we don't add any implicit padding * at the end of the structure on 64-bit architectures. + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_info_changed instead. */ struct gpioline_info_changed { struct gpioline_info info; @@ -367,6 +376,9 @@ struct gpioline_info_changed { * @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEHANDLE_IOCTL operation, zero or negative value * means error + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_request instead. */ struct gpiohandle_request { __u32 lineoffsets[GPIOHANDLES_MAX]; @@ -386,6 +398,9 @@ struct gpiohandle_request { * this specifies the default output value, should be 0 (low) or * 1 (high), anything else than 0 or 1 will be interpreted as 1 (high) * @padding: reserved for future use and should be zero filled + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_config instead. */ struct gpiohandle_config { __u32 flags; @@ -398,6 +413,9 @@ struct gpiohandle_config { * @values: when getting the state of lines this contains the current * state of a line, when setting the state of lines these should contain * the desired target state + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_values instead. */ struct gpiohandle_data { __u8 values[GPIOHANDLES_MAX]; @@ -421,6 +439,9 @@ struct gpiohandle_data { * @fd: if successful this field will contain a valid anonymous file handle * after a GPIO_GET_LINEEVENT_IOCTL operation, zero or negative value * means error + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_request instead. */ struct gpioevent_request { __u32 lineoffset; @@ -440,6 +461,9 @@ struct gpioevent_request { * struct gpioevent_data - The actual event being pushed to userspace * @timestamp: best estimate of time of event occurrence, in nanoseconds * @id: event identifier + * + * This struct is part of ABI v1 and is deprecated. + * Use struct gpio_v2_line_event instead. */ struct gpioevent_data { __u64 timestamp; @@ -464,6 +488,8 @@ struct gpioevent_data { /* * v1 ioctl()s + * + * These ioctl()s are deprecated. Use the v2 equivalent instead. */ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info) #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request) From patchwork Fri Aug 14 03:02:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254612 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9E05C433DF for ; Fri, 14 Aug 2020 03:05:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4CC220716 for ; Fri, 14 Aug 2020 03:05:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mRKdgiqs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbgHNDFV (ORCPT ); Thu, 13 Aug 2020 23:05:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726576AbgHNDFV (ORCPT ); Thu, 13 Aug 2020 23:05:21 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D222C061757; Thu, 13 Aug 2020 20:05:21 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mw10so3723931pjb.2; Thu, 13 Aug 2020 20:05:21 -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=ADP2aGUDRbAJ2fzGJ4u2MgmoEkak0G6DX7jqH1kdmdo=; b=mRKdgiqs07vkxxjTqsAjH/48JLLsLSlgnk86cInqgmM2qtR3OFBEIImGHr7cnPmOWo bH98bVfbHz3ZEmpoiSkgX9pCscgED2r5H7SKU6axAQ+5g/1HaYWSaeZDNB7FD5JlgFqw FCyLt6VkzxW+4yHrbJ6OnXWTV9DhrF5cJywLHuToFVjpjDhczNCqGlPBFMb3nF8baeFc ymKJu3V5r/awDaDY8pTojRjd/A1d2xyj2mea54AZcPG8E7TnSxzyThEaZu1Kdc0u4lTU Op3E/C/xuHu4X59/M3bqHHCS90P25Ox3E5j6axdMB7oWbIrBTDmi/TIC6Z5F16NrV7QM ihBg== 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=ADP2aGUDRbAJ2fzGJ4u2MgmoEkak0G6DX7jqH1kdmdo=; b=hPAqdLROZkdyFBRDbO2ITSk6zky1EVPkYQ2938qvjvUjfSMnrEKKCF2KB0/HTuTsp9 UTAxz0adzTB1FhFq60VIoZ86QbOd+62QFLaDzWQxN51KRPcpmDR+NpvPxcQZwIAEdbVD KFlbcU05RQqoDwI5ykzPgfzciJBFltKRvi+DC5vpuE1nlgaER2kdhTZCXqsaMEMrm0dr GBLfuaFJV+ypMTTjHDnCkntcyTQyldSlP9vasRy/TAbE3cQvlztNekMGLmboyJA51XPQ puqdGIugILetnuuKm+FkPdUSwbiZ/sRoMOtK2AV1A5dE+y2/pZ0h+wuwpFSuP5Pcu7SJ xzlQ== X-Gm-Message-State: AOAM531XGMmWlyOkN+IL6lFhwtfcdDc4ADgzqY1x3PwiilZiJjoPrB5b 5Qgu810tx2ER0yaYsjlOI1POPSda X-Google-Smtp-Source: ABdhPJyBfyqQYElJZFfdYEuxQXJmg/JEp305/WNS5U1OvbEIKg7mYWMBZW5bpPPuv97c87bvSBAOXw== X-Received: by 2002:a17:902:30d:: with SMTP id 13mr563880pld.251.1597374320266; Thu, 13 Aug 2020 20:05:20 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.05.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:05:19 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 14/20] tools: gpio: port lsgpio to v2 uAPI Date: Fri, 14 Aug 2020 11:02:51 +0800 Message-Id: <20200814030257.135463-15-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Port the lsgpio tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/lsgpio.c | 60 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/tools/gpio/lsgpio.c b/tools/gpio/lsgpio.c index b08d7a5e779b..deda38244026 100644 --- a/tools/gpio/lsgpio.c +++ b/tools/gpio/lsgpio.c @@ -25,57 +25,73 @@ struct gpio_flag { char *name; - unsigned long mask; + unsigned long long mask; }; struct gpio_flag flagnames[] = { { - .name = "kernel", - .mask = GPIOLINE_FLAG_KERNEL, + .name = "used", + .mask = GPIO_V2_LINE_FLAG_USED, + }, + { + .name = "input", + .mask = GPIO_V2_LINE_FLAG_INPUT, }, { .name = "output", - .mask = GPIOLINE_FLAG_IS_OUT, + .mask = GPIO_V2_LINE_FLAG_OUTPUT, }, { .name = "active-low", - .mask = GPIOLINE_FLAG_ACTIVE_LOW, + .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW, }, { .name = "open-drain", - .mask = GPIOLINE_FLAG_OPEN_DRAIN, + .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN, }, { .name = "open-source", - .mask = GPIOLINE_FLAG_OPEN_SOURCE, + .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE, }, { .name = "pull-up", - .mask = GPIOLINE_FLAG_BIAS_PULL_UP, + .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP, }, { .name = "pull-down", - .mask = GPIOLINE_FLAG_BIAS_PULL_DOWN, + .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN, }, { .name = "bias-disabled", - .mask = GPIOLINE_FLAG_BIAS_DISABLE, + .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED, }, }; -void print_flags(unsigned long flags) +static void print_attributes(struct gpio_v2_line_info *info) { int i; - int printed = 0; + const char *field_format = "%s"; for (i = 0; i < ARRAY_SIZE(flagnames); i++) { - if (flags & flagnames[i].mask) { - if (printed) - fprintf(stdout, " "); - fprintf(stdout, "%s", flagnames[i].name); - printed++; + if (info->flags & flagnames[i].mask) { + fprintf(stdout, field_format, flagnames[i].name); + field_format = ", %s"; } } + + if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) && + (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)) + fprintf(stdout, field_format, "both-edges"); + else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) + fprintf(stdout, field_format, "rising-edge"); + else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING) + fprintf(stdout, field_format, "falling-edge"); + + for (i = 0; i < info->num_attrs; i++) { + if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) + fprintf(stdout, ", debounce_period=%dusec", + info->attrs[0].debounce_period); + } } int list_device(const char *device_name) @@ -109,18 +125,18 @@ int list_device(const char *device_name) /* Loop over the lines and print info */ for (i = 0; i < cinfo.lines; i++) { - struct gpioline_info linfo; + struct gpio_v2_line_info linfo; memset(&linfo, 0, sizeof(linfo)); - linfo.line_offset = i; + linfo.offset = i; - ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo); + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo); if (ret == -1) { ret = -errno; perror("Failed to issue LINEINFO IOCTL\n"); goto exit_close_error; } - fprintf(stdout, "\tline %2d:", linfo.line_offset); + fprintf(stdout, "\tline %2d:", linfo.offset); if (linfo.name[0]) fprintf(stdout, " \"%s\"", linfo.name); else @@ -131,7 +147,7 @@ int list_device(const char *device_name) fprintf(stdout, " unused"); if (linfo.flags) { fprintf(stdout, " ["); - print_flags(linfo.flags); + print_attributes(&linfo); fprintf(stdout, "]"); } fprintf(stdout, "\n"); From patchwork Fri Aug 14 03:02:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6A58C433E1 for ; Fri, 14 Aug 2020 03:05:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7CE61205CB for ; Fri, 14 Aug 2020 03:05:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W/dA8/hb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726568AbgHNDFk (ORCPT ); Thu, 13 Aug 2020 23:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgHNDFk (ORCPT ); Thu, 13 Aug 2020 23:05:40 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26C28C061757; Thu, 13 Aug 2020 20:05:40 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id l60so3739565pjb.3; Thu, 13 Aug 2020 20:05:40 -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=b4qnERya2lAXwivmnc6QYVo0ihib6LVkWcetGyot3Rg=; b=W/dA8/hb1hG9Ltmv4qAYHWaULe7h0awxt20ohIbdHDSXogtnXNweAohATElA0n/hPi p5nLtmITfmoZl9F2SVO6IfR+C5jofEODu1D3pqzrT5uIM8hSI8NLzyMaTeOBb+HKYX8Y vieIR+zbGsxXjKsZX1xIQenGviQBNyLCfrgL7KDelU8LzZ3Jw9c+hB/m8T8S3HYMOwzK EndmEPQWDAJ7qMAcAEuZACVs9nMvxXATSIHNkLW17m9I3m6qARdFiCTYE4Pb3HWIVzmh CrjHF2nLFlhB8KZenr9E4ZURJCfT9XUwG1VnPyN5Pd55IDYMeiocmlSEzgr8LIoAOHDz fn7Q== 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=b4qnERya2lAXwivmnc6QYVo0ihib6LVkWcetGyot3Rg=; b=h+kJCsOGtWCS6W1yNRMsEnFFF/Amj7YjpDBYYQnNP71ghw/mf83pZpwcrT/Wc6/LAQ NgJlZJRvU34X+njZbAGlfo4N/apRQWbwAbvFPHLHFztYcvoTyE0XtLvYU81SPe5OvmHS khB5akHIl77tsrtT3rWw4+k2LAKhpG0+61CYYiI2W78PfEuObnYHn03VlUmMVrnysZ6u M0nTt2HBdNEp8hYSsYCeIzZ+PRplHw656eENIZX6dh6MIEPNft3x0+6/h5yFqMQC/5XL 2cKWGLyR5pIsDnF6Bl0pTf2yQFQvPTaxJ/xklyOgdn0dXenfx2fxVq98BGmxUniTW9B6 kb9A== X-Gm-Message-State: AOAM533Yu5Xp19vULh4pG3Bgn52mXpLTlvrFK6vYeuzjio3cMxV17ofE hUGJTaqcF7LImPNEH80pqyIG/YuQ X-Google-Smtp-Source: ABdhPJzuvyoCJpKfAQTcryiXweATysl7ZZCrXeXFdL/1++TDqfYKUylRRXRrhjidhy/XqUggycyyow== X-Received: by 2002:a17:90a:24ed:: with SMTP id i100mr587647pje.126.1597374339137; Thu, 13 Aug 2020 20:05:39 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.05.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:05:38 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 16/20] tools: gpio: rename nlines to num_lines Date: Fri, 14 Aug 2020 11:02:53 +0800 Message-Id: <20200814030257.135463-17-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Rename nlines to num_lines to be consistent with other usage for fields describing the number of entries in an array. Signed-off-by: Kent Gibson --- tools/gpio/gpio-hammer.c | 26 +++++++++++++------------- tools/gpio/gpio-utils.c | 20 ++++++++++---------- tools/gpio/gpio-utils.h | 6 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index 9fd926e8cb52..a2c7577fad5c 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -22,7 +22,7 @@ #include #include "gpio-utils.h" -int hammer_device(const char *device_name, unsigned int *lines, int nlines, +int hammer_device(const char *device_name, unsigned int *lines, int num_lines, unsigned int loops) { struct gpiohandle_data data; @@ -33,7 +33,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, unsigned int iteration = 0; memset(&data.values, 0, sizeof(data.values)); - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_OUTPUT, &data, "gpio-hammer"); if (ret < 0) @@ -46,15 +46,15 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, goto exit_close_error; fprintf(stdout, "Hammer lines ["); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d", lines[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "] on %s, initial states: [", device_name); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d", data.values[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "]\n"); @@ -63,7 +63,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j = 0; while (1) { /* Invert all lines so we blink */ - for (i = 0; i < nlines; i++) + for (i = 0; i < num_lines; i++) data.values[i] = !data.values[i]; ret = gpiotools_set_values(fd, &data); @@ -81,9 +81,9 @@ int hammer_device(const char *device_name, unsigned int *lines, int nlines, j = 0; fprintf(stdout, "["); - for (i = 0; i < nlines; i++) { + for (i = 0; i < num_lines; i++) { fprintf(stdout, "%d: %d", lines[i], data.values[i]); - if (i != (nlines - 1)) + if (i != (num_lines - 1)) fprintf(stdout, ", "); } fprintf(stdout, "]\r"); @@ -121,7 +121,7 @@ int main(int argc, char **argv) const char *device_name = NULL; unsigned int lines[GPIOHANDLES_MAX]; unsigned int loops = 0; - int nlines; + int num_lines; int c; int i; @@ -158,11 +158,11 @@ int main(int argc, char **argv) return -1; } - nlines = i; + num_lines = i; - if (!device_name || !nlines) { + if (!device_name || !num_lines) { print_usage(); return -1; } - return hammer_device(device_name, lines, nlines, loops); + return hammer_device(device_name, lines, num_lines, loops); } diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c index 16a5d9cb9da2..d527980bcb94 100644 --- a/tools/gpio/gpio-utils.c +++ b/tools/gpio/gpio-utils.c @@ -38,7 +38,7 @@ * such as "gpiochip0" * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @flag: The new flag for requsted gpio. Reference * "linux/gpio.h" for the meaning of flag. * @data: Default value will be set to gpio when flag is @@ -56,7 +56,7 @@ * On failure return the errno. */ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int nlines, unsigned int flag, + unsigned int num_lines, unsigned int flag, struct gpiohandle_data *data, const char *consumer_label) { @@ -78,12 +78,12 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, goto exit_free_name; } - for (i = 0; i < nlines; i++) + for (i = 0; i < num_lines; i++) req.lineoffsets[i] = lines[i]; req.flags = flag; strcpy(req.consumer_label, consumer_label); - req.lines = nlines; + req.lines = num_lines; if (flag & GPIOHANDLE_REQUEST_OUTPUT) memcpy(req.default_values, data, sizeof(req.default_values)); @@ -194,20 +194,20 @@ int gpiotools_get(const char *device_name, unsigned int line) * such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @data: The array of values get from gpiochip. * * Return: On success return 0; * On failure return the errno. */ int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data) + unsigned int num_lines, struct gpiohandle_data *data) { int fd; int ret; int ret_close; - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_INPUT, data, CONSUMER); if (ret < 0) @@ -245,7 +245,7 @@ int gpiotools_set(const char *device_name, unsigned int line, * such as "gpiochip0". * @lines: An array desired lines, specified by offset * index for the associated GPIO device. - * @nline: The number of lines to request. + * @num_lines: The number of lines to request. * @data: The array of values set to gpiochip, must be * 0(low) or 1(high). * @@ -253,11 +253,11 @@ int gpiotools_set(const char *device_name, unsigned int line, * On failure return the errno. */ int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data) + unsigned int num_lines, struct gpiohandle_data *data) { int ret; - ret = gpiotools_request_linehandle(device_name, lines, nlines, + ret = gpiotools_request_linehandle(device_name, lines, num_lines, GPIOHANDLE_REQUEST_OUTPUT, data, CONSUMER); if (ret < 0) diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h index cf37f13f3dcb..324729577865 100644 --- a/tools/gpio/gpio-utils.h +++ b/tools/gpio/gpio-utils.h @@ -23,7 +23,7 @@ static inline int check_prefix(const char *str, const char *prefix) } int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, - unsigned int nlines, unsigned int flag, + unsigned int num_lines, unsigned int flag, struct gpiohandle_data *data, const char *consumer_label); int gpiotools_set_values(const int fd, struct gpiohandle_data *data); @@ -32,10 +32,10 @@ int gpiotools_release_linehandle(const int fd); int gpiotools_get(const char *device_name, unsigned int line); int gpiotools_gets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data); + unsigned int num_lines, struct gpiohandle_data *data); int gpiotools_set(const char *device_name, unsigned int line, unsigned int value); int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int nlines, struct gpiohandle_data *data); + unsigned int num_lines, struct gpiohandle_data *data); #endif /* _GPIO_UTILS_H_ */ From patchwork Fri Aug 14 03:02:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86AF5C433DF for ; Fri, 14 Aug 2020 03:05:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4449E205CB for ; Fri, 14 Aug 2020 03:05:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="rh3ATi/J" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726684AbgHNDF4 (ORCPT ); Thu, 13 Aug 2020 23:05:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgHNDF4 (ORCPT ); Thu, 13 Aug 2020 23:05:56 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43AF5C061757; Thu, 13 Aug 2020 20:05:56 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id d188so3868329pfd.2; Thu, 13 Aug 2020 20:05:56 -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=NHFesHMh8urOzDGLF3BG3WK3USIsA8bYlNdGJPsk5Ng=; b=rh3ATi/Jsrq1HxJBRguZO4kmqL2V/TdhJk5favgLPL7HWvNQUmR2Ni0y3IVZwJrfAX ejo33Bk7e/UdSPuHFHYM+o6cbwJI7tyUbdS6mYzLuB0/3qRQ4gJ2gpE2NXvUwtMz3+Xc dmxZg5cgQLpWWFR5IVqTCAFyRM+tvNdw2zV5Pa9+4cBG6rcjMs6HXbqM96z2ULIH0fHC REdG7ln3zGff2azyxYnauJN1oYvId9w2fXidz4Yf8joXQWI5bx8o083sLBi5X0Zl+mqe ZeswtBmxzIT2IwVmSK954hkoGsg37eVw1xYa5Vo4d5mt9nEYhWB9lBr1p+S8R6J3v8Ii FDAQ== 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=NHFesHMh8urOzDGLF3BG3WK3USIsA8bYlNdGJPsk5Ng=; b=hod3Wq+FyhwYPktMfCnFRMsbDBwoKs8lS4kIcPG3iGEMrJRugazZHG5oQWHNhExMx1 16Zx8PIRzkrHkvhAv8c6qcwUKzor7owrGf+82UZfemCNZZDHXiL04ISd5Nsi01G9Ina5 YI64McT1mTS/jFhR19BeWO/0jn6vBjrhv61flGv+34jhqGWLzqdsUs7spABceOSfEVcG 1keEJC6vienjXAScMdZ/jPs8TugPlMb5oBxvsNLcbvtiIivWdVlv6ojp7jZszaBcP9jz XWRKzimM1+l1WEiPWoDzfsM+ZNkcXForrLYPoFqAkrcFOJGKoHRWjBx+U+oLhiR/iThI n0wQ== X-Gm-Message-State: AOAM532UHIteOfTB9cvswqtKwwOizDYDLCnIqxOrW+h84InFcKVOvV04 hKBiKxMmQ2/g7RlCfD6UmcE/2d2P X-Google-Smtp-Source: ABdhPJxKGsLhJoS2br14WvSENwWWh79Elosr8vbI96Q0m1DuRcVa4xJXr2rg3Rmj5wJX5NvAXnALzA== X-Received: by 2002:a63:1d23:: with SMTP id d35mr406907pgd.291.1597374355314; Thu, 13 Aug 2020 20:05:55 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:05:54 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 18/20] tools: gpio: port gpio-event-mon to v2 uAPI Date: Fri, 14 Aug 2020 11:02:55 +0800 Message-Id: <20200814030257.135463-19-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 Port the gpio-event-mon tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson --- tools/gpio/gpio-event-mon.c | 91 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index 1a303a81aeef..d6a831200c18 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -23,17 +23,16 @@ #include #include #include +#include "gpio-utils.h" int monitor_device(const char *device_name, unsigned int line, - uint32_t handleflags, - uint32_t eventflags, + struct gpio_v2_line_config *config, unsigned int loops) { - struct gpioevent_request req; - struct gpiohandle_data data; + struct gpio_v2_line_values values; char *chrdev_name; - int fd; + int cfd, lfd; int ret; int i = 0; @@ -41,44 +40,39 @@ int monitor_device(const char *device_name, if (ret < 0) return -ENOMEM; - fd = open(chrdev_name, 0); - if (fd == -1) { + cfd = open(chrdev_name, 0); + if (cfd == -1) { ret = -errno; fprintf(stderr, "Failed to open %s\n", chrdev_name); goto exit_free_name; } - req.lineoffset = line; - req.handleflags = handleflags; - req.eventflags = eventflags; - strcpy(req.consumer_label, "gpio-event-mon"); - - ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req); - if (ret == -1) { - ret = -errno; - fprintf(stderr, "Failed to issue GET EVENT " - "IOCTL (%d)\n", - ret); - goto exit_close_error; - } + ret = gpiotools_request_line(device_name, &line, 1, config, + "gpio-event-mon"); + if (ret < 0) + goto exit_device_close; + else + lfd = ret; /* Read initial states */ - ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); - if (ret == -1) { - ret = -errno; - fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE " - "VALUES IOCTL (%d)\n", + values.mask = 1; + values.bits = 0; + ret = gpiotools_get_values(lfd, &values); + if (ret < 0) { + fprintf(stderr, + "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n", ret); - goto exit_close_error; + goto exit_line_close; } fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); - fprintf(stdout, "Initial line value: %d\n", data.values[0]); + fprintf(stdout, "Initial line value: %d\n", + gpiotools_test_bit(values.bits, 0)); while (1) { - struct gpioevent_data event; + struct gpio_v2_line_event event; - ret = read(req.fd, &event, sizeof(event)); + ret = read(lfd, &event, sizeof(event)); if (ret == -1) { if (errno == -EAGAIN) { fprintf(stderr, "nothing available\n"); @@ -96,12 +90,14 @@ int monitor_device(const char *device_name, ret = -EIO; break; } - fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp); + fprintf(stdout, "GPIO EVENT at %llu on line %d (%d|%d) ", + event.timestamp, event.offset, event.line_seqno, + event.seqno); switch (event.id) { - case GPIOEVENT_EVENT_RISING_EDGE: + case GPIO_V2_LINE_EVENT_RISING_EDGE: fprintf(stdout, "rising edge"); break; - case GPIOEVENT_EVENT_FALLING_EDGE: + case GPIO_V2_LINE_EVENT_FALLING_EDGE: fprintf(stdout, "falling edge"); break; default: @@ -114,8 +110,11 @@ int monitor_device(const char *device_name, break; } -exit_close_error: - if (close(fd) == -1) +exit_line_close: + if (close(lfd) == -1) + perror("Failed to close line file"); +exit_device_close: + if (close(cfd) == -1) perror("Failed to close GPIO character device file"); exit_free_name: free(chrdev_name); @@ -140,15 +139,20 @@ void print_usage(void) ); } +#define EDGE_FLAGS \ + (GPIO_V2_LINE_FLAG_EDGE_RISING | \ + GPIO_V2_LINE_FLAG_EDGE_FALLING) + int main(int argc, char **argv) { const char *device_name = NULL; unsigned int line = -1; unsigned int loops = 0; - uint32_t handleflags = GPIOHANDLE_REQUEST_INPUT; - uint32_t eventflags = 0; + struct gpio_v2_line_config config; int c; + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_INPUT; while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) { switch (c) { case 'c': @@ -161,16 +165,16 @@ int main(int argc, char **argv) line = strtoul(optarg, NULL, 10); break; case 'd': - handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN; + config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; break; case 's': - handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE; + config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; break; case 'r': - eventflags |= GPIOEVENT_REQUEST_RISING_EDGE; + config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; break; case 'f': - eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE; + config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; break; case '?': print_usage(); @@ -182,11 +186,10 @@ int main(int argc, char **argv) print_usage(); return -1; } - if (!eventflags) { + if (!(config.flags & EDGE_FLAGS)) { printf("No flags specified, listening on both rising and " "falling edges\n"); - eventflags = GPIOEVENT_REQUEST_BOTH_EDGES; + config.flags |= EDGE_FLAGS; } - return monitor_device(device_name, line, handleflags, - eventflags, loops); + return monitor_device(device_name, line, &config, loops); } From patchwork Fri Aug 14 03:02:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254609 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B12A9C433E1 for ; Fri, 14 Aug 2020 03:06:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8CD6C20716 for ; Fri, 14 Aug 2020 03:06:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jLR45Gl2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726691AbgHNDGM (ORCPT ); Thu, 13 Aug 2020 23:06:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgHNDGL (ORCPT ); Thu, 13 Aug 2020 23:06:11 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53AB0C061757; Thu, 13 Aug 2020 20:06:11 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id u128so3863477pfb.6; Thu, 13 Aug 2020 20:06:11 -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=HaME3Z5uLAfT5ND31CqQyrNIC7MORGaQXez8ygNaWV4=; b=jLR45Gl2DA2rRti+GMEvcjlEkZd1G7TuZhTfwWhzE3Btu5YqhkcgLUPpyCGdLywjPa H8IDLJsaUlE2XRq3ZIWT8rWakHVFkOPn0PLs9w96njhg44kwwDJgM9zaN6tar1oRBdlW ocoPeppftFJ1v3pzC7J89x8POwTNDrlCHoxdgJW+OzM+ctn3TgnGRZeCZWW5xIt6q7O8 5CyQ/U4dwcAKCHiMhFOdGssaU4sErtGYKjva1d3Q3TkspQgsQ7L186yhXL0hOLhtOLdT DHqcU5nF1ZjQeRZS6f62yIAFqi7UhMmaIg0G0YFo0fa8qhGiOuLoJuiTlCIOavEX7NFh P09Q== 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=HaME3Z5uLAfT5ND31CqQyrNIC7MORGaQXez8ygNaWV4=; b=rv6U0CDrEgmPK9bm+XDYriq9kMT9+ZhbFToXlgxjit4JL8cOPfs9lIcGjf3oVZKqcz hcyBpx/3YgRQqwvm3C3Rqdn7thP6/uakHNcKL+RHOIb6HHADekZ2eJLsgepV/FOohcD8 mDFsj5u4Xd3GMjy1a/Pry6+OqUUL3RxrHlSd7eOw/13ncK6/YqNvN5FiwmNhzHg7Gj6/ gFpa0nNwB5dtGow9MB/vbUIKv73DWx3gqy0Lk9DyGF7zQLdSa3UXxlMqt4u/zyGnLX0D dGocfBR1L0pxROQ1wsrO6uwZiyqkibXjfJ4ciSx9P0dft3l6DcUmWoKuuH/MTVuUElW5 MQIw== X-Gm-Message-State: AOAM533mWDxVv/fZbhRYPETGVxlPVdaswmO2ErgJgT3h29MKkCBnROqD IBH7BxSwzjuFVCnNpxg/VpJnrKpf X-Google-Smtp-Source: ABdhPJz8DjcUkVYhxph2GyoOQXwR3+ubl84dJguupmyyxcpV9poz45BszY08LNYRQZ4Qu3DAfqRy7g== X-Received: by 2002:a63:2324:: with SMTP id j36mr437494pgj.221.1597374370373; Thu, 13 Aug 2020 20:06:10 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.06.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:06:09 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson Subject: [PATCH v4 20/20] tools: gpio: add debounce support to gpio-event-mon Date: Fri, 14 Aug 2020 11:02:57 +0800 Message-Id: <20200814030257.135463-21-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-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 support for debouncing monitored lines to gpio-event-mon. Signed-off-by: Kent Gibson --- tools/gpio/gpio-event-mon.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index e50bb107ea3a..bd5ea3cc6e85 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -148,11 +148,12 @@ void print_usage(void) " -s Set line as open source\n" " -r Listen for rising edges\n" " -f Listen for falling edges\n" + " -b Debounce the line with period n microseconds\n" " [-c ] Do loops (optional, infinite loop if not stated)\n" " -? This helptext\n" "\n" "Example:\n" - "gpio-event-mon -n gpiochip0 -o 4 -r -f\n" + "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n" ); } @@ -167,11 +168,12 @@ int main(int argc, char **argv) unsigned int num_lines = 0; unsigned int loops = 0; struct gpio_v2_line_config config; - int c; + int c, attr, i; + unsigned long debounce_period = 0; memset(&config, 0, sizeof(config)); config.flags = GPIO_V2_LINE_FLAG_INPUT; - while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) { + while ((c = getopt(argc, argv, "c:n:o:b:dsrf?")) != -1) { switch (c) { case 'c': loops = strtoul(optarg, NULL, 10); @@ -187,6 +189,9 @@ int main(int argc, char **argv) lines[num_lines] = strtoul(optarg, NULL, 10); num_lines++; break; + case 'b': + debounce_period = strtoul(optarg, NULL, 10); + break; case 'd': config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; break; @@ -205,6 +210,15 @@ int main(int argc, char **argv) } } + if (debounce_period) { + attr = config.num_attrs; + config.num_attrs++; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&config.attrs[attr].mask, i); + config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + config.attrs[attr].attr.debounce_period = debounce_period; + } + if (!device_name || num_lines == 0) { print_usage(); return -1;