From patchwork Mon Sep 28 00:27: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: 285687 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=-12.8 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 61659C4346E for ; Mon, 28 Sep 2020 00:28:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 28F97239A1 for ; Mon, 28 Sep 2020 00:28:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q926ccHh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgI1A2t (ORCPT ); Sun, 27 Sep 2020 20:28:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A2t (ORCPT ); Sun, 27 Sep 2020 20:28:49 -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 4F52CC0613CE; Sun, 27 Sep 2020 17:28:49 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mm21so2597328pjb.4; Sun, 27 Sep 2020 17:28:49 -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=ZdIya96dO2nmyvmHD/Tz+daovDXoyyBwC9BB/0lctoQ=; b=Q926ccHhke37xD4BrH8o0RP/iRfY3w6I7kuYYx0OG+94IKGNpvs1V2M7qih89IbFGs 6vWTh86UX2h2Bd7CQwwzhg0sEaxRoOYgoEg9J2mc0/Z7QM+g62o0tWSlDBxf7bx2t8kF PHM4llNT9I6Zkp5q+1XPCSiRFId0495UBJtlSRanEuVP0FBd6ZvgMGKf5ZOGMkzfTk77 0QzQKWglQGfmrcmbBOtXusRWc3whh/fhBR9qsDz0Lgba8CbRBy276B2mp9s60dP4j5Ky 3m7YK97Sms72m5EGDiBnTBa7262PDbCOuEVWdlKsX3fWcHgyIFA7Rk5hT+LhifvbVj8N ksPQ== 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=ZdIya96dO2nmyvmHD/Tz+daovDXoyyBwC9BB/0lctoQ=; b=MFK4xC61E+EUwuLBI+UzNfRc9mk8LumISVIgMAe7CAd/o8wuxODUEtxD5It/4EyOU2 MS82iCxa98GLIwNylbETtdeccaxHtw9WR8zDeAZHI7tAdEtGwVHDJ4SWk7DvPRTWeRGC uFv4uL1M7QG7JC8G4DwUyxhghPCYnW9+uMJmEQFPSUnOJHtLz6+0/7BY6fvQfTcfOIiS 1cHOnERIjkuI8VJcyYok+gwL+Vi9jgfD9avt3ar5WppxKlwbYC1YOPxJpz0FrwFBjLOe W/xyfWTYDXFBaBD12MsAVOvxc8of1/SUNAL2mmDWAmpz3UxYg7UQk5+IyrwLYxQPneq1 vkhw== X-Gm-Message-State: AOAM532NMhJTpJtaSTjpC63jNI8cj+DoiiUwhhb5I2VeAngtfsudgkus saGzkVh5bw/jarl454VAEubXKOg23A4ZwA== X-Google-Smtp-Source: ABdhPJxPDrqOMszqBtBrEHhMry22LuXTSbrreS4NQ/4YsEqvbbdcgRXURKYg8K5u5Z2fOuHlij2m/Q== X-Received: by 2002:a17:90b:30cd:: with SMTP id hi13mr7208071pjb.82.1601252928434; Sun, 27 Sep 2020 17:28:48 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:28:47 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 01/20] gpiolib: cdev: gpio_desc_to_lineinfo() should set info offset Date: Mon, 28 Sep 2020 08:27:48 +0800 Message-Id: <20200928002807.12146-2-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Set the value of the line info offset in gpio_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 Reviewed-by: Andy Shevchenko --- There are some instances where this results in the offset being set when it is already set in the info, but this is clearer especially considering that, as part of the replacement of strncpy with strscpy and to ensure kernel stack cannot be leaked to userspace, the info is initially zeroed in a subsequent patch. drivers/gpio/gpiolib-cdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e6c9b78adfc2..81ce2020f17b 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -752,6 +752,8 @@ 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 +935,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 Mon Sep 28 00:27:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254328 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=-12.8 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 D7B07C4346E for ; Mon, 28 Sep 2020 00:29:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 976A323977 for ; Mon, 28 Sep 2020 00:29:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="exruSH73" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726565AbgI1A3G (ORCPT ); Sun, 27 Sep 2020 20:29:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A3G (ORCPT ); Sun, 27 Sep 2020 20:29:06 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69757C0613CE; Sun, 27 Sep 2020 17:29:06 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id n14so7749500pff.6; Sun, 27 Sep 2020 17:29:06 -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=Eem0c+jPGt4h5O0FB7VOY7qfs4N3yUmzBZn+jIhhYvU=; b=exruSH73V21Bcg01fN9gjuKQFFSvKMOLtNaiTHl19ymM8Z9l6OM2CumZOtS4KyM0og gDyW/2MvffnXGalsV1I8oV91oAcWLgDWo4UplOmg4r8cCWaA4B6GhZIvfmj1PFVjq6jz I9eXjUlvTpoPAc9cJQZbNYOkIg3vIue6ZnYJOUD+aljCNkpi4mhJ6NUklaTOI0PkYdch EZmIuUsLg1TNIcTBE6J12SGc+2j3bN7qPzicfs0TSMqvH1rQZz6hDqLI1dgqAKNxjdQw Muw19yO+3+RePjjVHfyAMIdOUll/QSm/Ck4X3tkveqzUxSziatutVd5xABfpNQYecAuB z3aA== 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=Eem0c+jPGt4h5O0FB7VOY7qfs4N3yUmzBZn+jIhhYvU=; b=fci4zsC7bsdf18Vb6KUF+sy7nHn6fxc4YiBEnWRZUFrLhBvMAoTBGFob6ru0GXkVWj kUwnwtgGIiuniAABWLa6olYTjTo7TvJUloJqOz1WpDwfIX05plSEHuO/ZS5dxjMxR+/f RZG1ZMHINK3FMBSAmnkvVYZC9wowCVbqRXaM19R8BY8GWAlLDOjQEvw/gwC+emREsC2a kPHg1sWOoOs8jt4P7TVuUHLZtmTBlWvClksLM7Mt+PSIyU/W6FeD3QjYOtvaloxg9/uu JIat/lE83iY8cFrc07NTvBb/o2NAGvXYDM2zrW/2yeNZgB4+GTYMozj7jU66dGFYuznw yeMQ== X-Gm-Message-State: AOAM530d7TYCYbQhn46xhgzOaNVoxgBcPcWSV7cXm27bgSYCjnwOxSG1 IxFIZ4q2bd2WfsRHul+7HZ3wzfWFeLH3bw== X-Google-Smtp-Source: ABdhPJzkdXUqeCX+EwXwqnDmRInMmV7qZ2LxZBEwGMY2u8Dk54EegWjbOhCvgYpUqfAPEsp4OoFhJQ== X-Received: by 2002:a17:902:c213:b029:d2:564d:8352 with SMTP id 19-20020a170902c213b02900d2564d8352mr9123320pll.54.1601252945593; Sun, 27 Sep 2020 17:29:05 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:29:04 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson , kernel test robot Subject: [PATCH v10 02/20] gpiolib: cdev: replace strncpy() with strscpy() Date: Mon, 28 Sep 2020 08:27:49 +0800 Message-Id: <20200928002807.12146-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Replace usage of strncpy() with strscpy() to remove -Wstringop-truncation warnings. The structures being populated are zeroed, to prevent stack leakage as they are returned to userspace, so strscpy() performs the equivalent function without the warnings. Reported-by: kernel test robot Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- The memset in gpio_desc_to_lineinfo(), in conjunction with the strscpy, is necessary as strncpy zero pads the remainder of the destination. It also guarantees that the info cannot leak kernel stack to userspace. This is useful here, but is even more important for the v2 info, that this function is changed to generate in a subsequent patch, as that struct contains padding and attribute arrays that also need to be initialised. drivers/gpio/gpiolib-cdev.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 81ce2020f17b..86679397d09c 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; + memset(info, 0, sizeof(*info)); info->line_offset = gpio_chip_hwgpio(desc); /* @@ -766,19 +767,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, spin_lock_irqsave(&gpio_lock, flags); - if (desc->name) { - strncpy(info->name, desc->name, sizeof(info->name)); - info->name[sizeof(info->name) - 1] = '\0'; - } else { - info->name[0] = '\0'; - } + if (desc->name) + strscpy(info->name, desc->name, sizeof(info->name)); - if (desc->label) { - strncpy(info->consumer, desc->label, sizeof(info->consumer)); - info->consumer[sizeof(info->consumer) - 1] = '\0'; - } else { - info->consumer[0] = '\0'; - } + if (desc->label) + strscpy(info->consumer, desc->label, sizeof(info->consumer)); /* * Userspace only need to know that the kernel is using this GPIO so @@ -842,12 +835,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) memset(&chipinfo, 0, sizeof(chipinfo)); - strncpy(chipinfo.name, dev_name(&gdev->dev), + strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); - chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; - strncpy(chipinfo.label, gdev->label, + strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); - chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; chipinfo.lines = gdev->ngpio; if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; From patchwork Mon Sep 28 00:27: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: 285686 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=-12.8 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 4E9B1C4346E for ; Mon, 28 Sep 2020 00:29:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E72BA239A1 for ; Mon, 28 Sep 2020 00:29:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FnTt14dj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726589AbgI1A3O (ORCPT ); Sun, 27 Sep 2020 20:29:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A3O (ORCPT ); Sun, 27 Sep 2020 20:29:14 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AEFAC0613CE; Sun, 27 Sep 2020 17:29:14 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id n14so7749682pff.6; Sun, 27 Sep 2020 17:29:14 -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=7Xmm15LbmubgcaoAcN8p4xddZgeXjh2diqDOrXM5Y2w=; b=FnTt14djxHJAdf7yF1iDMqsEgp8UOZGjwgHBgjTFK2uhnrgOdPipcs1c3xLY7uoeau /k+IQq6tukbuuXf5uXk8rX+W47nMH+rSx695kbBGgcp6Q+UCUAZoDKnG1KDLZR8Lzo3q EZspUjDxBrQxll+H9Q+LbKobab57/J8OLEziwpr8YuN6syiEmk+Jjc4bo0peI5A/4WUt s0oLaNtR9KDtgR+ZJCaKQpuGCsFmZaCetPg5xPCg9jeDMLJxaVUlGdS8DqSPkMjjzN+5 yHlo3THul0K9jHbsk+l9apL8AsCTGIFSJ3heumR8GASbdXO+Q6PGDvckny+yNaq1kIso gJbw== 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=7Xmm15LbmubgcaoAcN8p4xddZgeXjh2diqDOrXM5Y2w=; b=owbTQEXjkFLW0/qSV03HPGRpQvhgz6nXo8SPyDUFwzjlBq+Vvtg3Dx5haOOzruwAK4 zGEz3xtXwu5hsylpk8h1fklhjVovlDJW6oXrTK0TPu0bEZIIfLxgqFBocEGFPx/svRNT 0Zshebs1EyLKEbaeIK0QUd1dZetoX0Zfbdstrk3t4HgiBSsNCO3pVpdiP1mLTKXOxtCD CFg/wOS4x5ysyXJSsf6UFmcAA1QaIEktwrXNrJ37YrhOKtKghwiHkQhW6bbuXgfbWzOr BO8TVyOthFwFYRTAzL9WRWwSH7El8Hm2vmi/1RPbUw+WQU+zbOaMI3xxfeK6+fqY8McP vfDw== X-Gm-Message-State: AOAM532h0kB4wnYjAbTBcFVbo/rcJ8kiCR0KudHpHiKJteONxT18RhoR C0sZ30W50njuvEELZrWzS/+kHaKrYFAYHg== X-Google-Smtp-Source: ABdhPJy0SL9NcxXndhVclm/2wuttmofXb1UJYvLYIjsZA702aSmwpHbPb801x9qVmTrbHYd7bW9Dig== X-Received: by 2002:aa7:96f1:0:b029:142:2501:39ed with SMTP id i17-20020aa796f10000b0290142250139edmr7893101pfq.60.1601252953289; Sun, 27 Sep 2020 17:29:13 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:29:12 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 03/20] gpio: uapi: define GPIO_MAX_NAME_SIZE for array sizes Date: Mon, 28 Sep 2020 08:27:50 +0800 Message-Id: <20200928002807.12146-4-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 Reviewed-by: Andy Shevchenko --- 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 Mon Sep 28 00:27: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: 254327 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=-12.8 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 CB1A5C4346E for ; Mon, 28 Sep 2020 00:29:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 85401239A1 for ; Mon, 28 Sep 2020 00:29:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CFZsM9YY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726603AbgI1A3X (ORCPT ); Sun, 27 Sep 2020 20:29:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A3W (ORCPT ); Sun, 27 Sep 2020 20:29:22 -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 97526C0613CE; Sun, 27 Sep 2020 17:29:22 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id m34so6778206pgl.9; Sun, 27 Sep 2020 17:29:22 -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=jFUNxTe1zCF4yzhYMkuNdZbQHhIK6b4WaolDbT/fMkQ=; b=CFZsM9YYpu2imKTR6VljERXnGvYt7NCpQF+ZWhKnZQGPNMEq7dJg+AFrrO27UGbB+4 0kJ6h9dQgx3fEB6V3o16He2fxmypW7vqTW+TbJGnrLW5+3XnhCYI46KsE/6KbDggNNIo uM9932gYs7z/Mzw6FyMIqrmKjo3G5U0UzgrsD227c7/gTh8C8maj+vTce78XdsWY28uO +xSTVwSTULIUl8R76sPXoeIvYXQtDbL7EJzj7FZ03JlGz98lWbEuLZ++c/fIVxsKZBKD dMTiNp+IQa2f+bVTvRLiR+F4MrYaWvrGWMu6VhgoW4DToZ5QCKLchYfwhKehS+ruQp2n Bttg== 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=jFUNxTe1zCF4yzhYMkuNdZbQHhIK6b4WaolDbT/fMkQ=; b=oIOSUw8mFohp5StWcJblBfOsgem2PTAX6X355JB4LtoDjbYqxmhY/VfmbY2L8u+SSh 1cAKGBSG7FwmYqKEvGlHETCwxiYeVI6btmlLG7qN2/F3yWB6dEvI6ACso4Ra3hdgu48J 0fp51SlTrIWOUPS8VKqv6rDiwyb06c3WtKxivZYgsu4ISBBFhp0Q2iwzHR+XysrpvTG7 OtNRXksQlcdvUZ2gDHlkeYHrfqh5JQXiWb+9oDfkr74NUI+BSyiMQv6JHlg9GoKs51MN WFUxWdRDHKNtuG3o4VRZ7uVNMoOzFjNkYIpH+21nqWETV5Yrzv+OThPGzMYW2u7nYWnh qosw== X-Gm-Message-State: AOAM53118TTEsheZj5vlSsMeQZCNVii/RJ5rKWTg0wPshScX/9lY72T9 A4YhqOI0g1GanH1rRj6BJvlaUeuQNSq9vg== X-Google-Smtp-Source: ABdhPJxYbBQhhjCvDrDTOWYdzaWWa03RHiFsYDeH3ZzVNlZsy9q2hCJES1LDYDV43OigE9KIYPmAGg== X-Received: by 2002:aa7:8ad8:0:b029:150:7d36:cddf with SMTP id b24-20020aa78ad80000b02901507d36cddfmr7864543pfd.55.1601252961444; Sun, 27 Sep 2020 17:29:21 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:29:20 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 04/20] gpio: uapi: define uAPI v2 Date: Mon, 28 Sep 2020 08:27:51 +0800 Message-Id: <20200928002807.12146-5-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a new version of the uAPI to address existing 32/64-bit alignment issues, add support for debounce and event sequence numbers, allow requested lines with different configurations, and provide some future proofing by adding padding reserved for future use. The alignment issue relates to the gpioevent_data, which packs to different sizes on 32-bit and 64-bit platforms. That creates problems for 32-bit apps running on 64-bit kernels. uAPI v2 addresses that particular issue, and the problem more generally, by adding pad fields that explicitly pad structs out to 64-bit boundaries, so they will pack to the same size now, and even if some of the reserved padding is used for __u64 fields in the future. The new structs have been analysed with pahole to ensure that they are sized as expected and contain no implicit padding. The lack of future proofing in v1 makes it impossible to, for example, add the debounce feature that is included in v2. The future proofing is addressed by providing configurable attributes in line config and reserved padding in all structs for future features. Specifically, the line request, config, info, info_changed and event structs receive updated versions and new ioctls. As the majority of the structs and ioctls were being replaced, it is opportune to rework some of the other aspects of the uAPI: v1 has three different flags fields, each with their own separate bit definitions. In v2 that is collapsed to one - gpio_v2_line_flag. The handle and event requests are merged into a single request, the line request, as the two requests were mostly the same other than the edge detection provided by event requests. As a byproduct, the v2 uAPI allows for multiple lines producing edge events on the same line handle. This is a new capability as v1 only supports a single line in an event request. As a consequence, there are now only two types of file handle to be concerned with, the chip and the line, and it is clearer which ioctls apply to which type of handle. There is also some minor renaming of fields for consistency compared to their v1 counterparts, e.g. offset rather than lineoffset or line_offset, and consumer rather than consumer_label. Additionally, v1 GPIOHANDLES_MAX becomes GPIO_V2_LINES_MAX in v2 for clarity, and the gpiohandle_data __u8 array becomes a bitmap in gpio_v2_line_values. The v2 uAPI is mostly a reorganisation and extension of v1, so userspace code, particularly libgpiod, should readily port to it. Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- Changes for v9: - use time scale suffixes on timestamp (_ns) and debounce period (_us) variables - document all enum values - comment wording tweaks - change some field orders to improve readability Changes for v7: - use _BITULL for flag constants Changes for v4: - clarify bitmap width in GPIO_V2_LINES_MAX description Changes for v3: - relocated commentary into commit description - hard limit max requested lines to 64 so bitmaps always fit in a single u64. - prefix all v2 symbols with GPIO_V2 - 64-bit flag values to ULL - use __aligned_u64 to ensure 64-bit fields are 64-bit aligned - support masked get values, as per set values. Changes for v2: - lower case V1 and V2, except in capitalized names - hyphenate 32/64-bit - rename bitmap field to bits - drop PAD_SIZE consts in favour of hard coded numbers - sort includes - change config flags to __u64 - increase padding of gpioline_event - relocate GPIOLINE_CHANGED enum into v2 section (is common with v1) - rework config to collapse direction, drive, bias and edge enums back into flags and add optional attributes that can be associated with a subset of the requested lines. Changes for v1 (since the RFC): - document the constraints on array sizes to maintain 32/64 alignment - add sequence numbers to gpioline_event - use bitmap for values instead of array of __u8 - gpioline_info_v2 contains gpioline_config instead of its composite fields - provide constants for all array sizes, especially padding - renamed "GPIOLINE_FLAG_V2_KERNEL" to "GPIOLINE_FLAG_V2_USED" - renamed "default_values" to "values" - made gpioline_direction zero based - document clock used in gpioline_event timestamp - add event_buffer_size to gpioline_request - rename debounce to debounce_period - rename lines to num_lines include/uapi/linux/gpio.h | 291 +++++++++++++++++++++++++++++++++++++- 1 file changed, 284 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 285cc10355b2..5904f49399de 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -11,11 +11,14 @@ #ifndef _UAPI_GPIO_H_ #define _UAPI_GPIO_H_ +#include #include #include /* * The maximum size of name and label arrays. + * + * Must be a multiple of 8 to ensure 32/64-bit alignment of structs. */ #define GPIO_MAX_NAME_SIZE 32 @@ -32,6 +35,265 @@ struct gpiochip_info { __u32 lines; }; +/* + * Maximum number of requested lines. + * + * Must be no greater than 64, as bitmaps are restricted here to 64-bits + * for simplicity, and a multiple of 2 to ensure 32/64-bit alignment of + * structs. + */ +#define GPIO_V2_LINES_MAX 64 + +/* + * The maximum number of configuration attributes associated with a line + * request. + */ +#define GPIO_V2_LINE_NUM_ATTRS_MAX 10 + +/** + * enum gpio_v2_line_flag - &struct gpio_v2_line_attribute.flags values + * @GPIO_V2_LINE_FLAG_USED: line is not available for request + * @GPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical low + * @GPIO_V2_LINE_FLAG_INPUT: line is an input + * @GPIO_V2_LINE_FLAG_OUTPUT: line is an output + * @GPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active) + * edges + * @GPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to + * inactive) edges + * @GPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain output + * @GPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source output + * @GPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabled + * @GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabled + * @GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled + */ +enum gpio_v2_line_flag { + GPIO_V2_LINE_FLAG_USED = _BITULL(0), + GPIO_V2_LINE_FLAG_ACTIVE_LOW = _BITULL(1), + GPIO_V2_LINE_FLAG_INPUT = _BITULL(2), + GPIO_V2_LINE_FLAG_OUTPUT = _BITULL(3), + GPIO_V2_LINE_FLAG_EDGE_RISING = _BITULL(4), + GPIO_V2_LINE_FLAG_EDGE_FALLING = _BITULL(5), + GPIO_V2_LINE_FLAG_OPEN_DRAIN = _BITULL(6), + GPIO_V2_LINE_FLAG_OPEN_SOURCE = _BITULL(7), + GPIO_V2_LINE_FLAG_BIAS_PULL_UP = _BITULL(8), + GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN = _BITULL(9), + GPIO_V2_LINE_FLAG_BIAS_DISABLED = _BITULL(10), +}; + +/** + * struct gpio_v2_line_values - Values of GPIO lines + * @bits: a bitmap containing the value of the lines, set to 1 for active + * and 0 for inactive. + * @mask: a bitmap identifying the lines to get or set, with each bit + * number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + */ +struct gpio_v2_line_values { + __aligned_u64 bits; + __aligned_u64 mask; +}; + +/** + * enum gpio_v2_line_attr_id - &struct gpio_v2_line_attribute.id values + * identifying which field of the attribute union is in use. + * @GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in use + * @GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in use + * @GPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us is in use + */ +enum gpio_v2_line_attr_id { + GPIO_V2_LINE_ATTR_ID_FLAGS = 1, + GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES = 2, + GPIO_V2_LINE_ATTR_ID_DEBOUNCE = 3, +}; + +/** + * struct gpio_v2_line_attribute - a configurable attribute of a line + * @id: attribute identifier with value from &enum gpio_v2_line_attr_id + * @padding: reserved for future use and must be zero filled + * @flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO + * line, with values from enum gpio_v2_line_flag, such as + * GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed + * together. This overrides the default flags contained in the &struct + * gpio_v2_line_config for the associated line. + * @values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap + * containing the values to which the lines will be set, with each bit + * number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + * @debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired + * debounce period, in microseconds + */ +struct gpio_v2_line_attribute { + __u32 id; + __u32 padding; + union { + __aligned_u64 flags; + __aligned_u64 values; + __u32 debounce_period_us; + }; +}; + +/** + * struct gpio_v2_line_config_attribute - a configuration attribute + * associated with one or more of the requested lines. + * @attr: the configurable attribute + * @mask: a bitmap identifying the lines to which the attribute applies, + * with each bit number corresponding to the index into &struct + * gpio_v2_line_request.offsets. + */ +struct gpio_v2_line_config_attribute { + struct gpio_v2_line_attribute attr; + __aligned_u64 mask; +}; + +/** + * struct gpio_v2_line_config - Configuration for GPIO lines + * @flags: flags for the GPIO lines, with values from enum + * gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, + * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together. This is the default for + * all requested lines but may be overridden for particular lines using + * attrs. + * @num_attrs: the number of attributes in attrs + * @padding: reserved for future use and must be zero filled + * @attrs: the configuration attributes associated with the requested + * lines. Any attribute should only be associated with a particular line + * once. If an attribute is associated with a line multiple times then the + * first occurrence (i.e. lowest index) has precedence. + */ +struct gpio_v2_line_config { + __aligned_u64 flags; + __u32 num_attrs; + /* Pad to fill implicit padding and reserve space for future use. */ + __u32 padding[5]; + struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; +}; + +/** + * struct gpio_v2_line_request - Information about a request for GPIO lines + * @offsets: an array of desired lines, specified by offset index for the + * associated GPIO chip + * @consumer: a desired consumer label for the selected GPIO lines such as + * "my-bitbanged-relay" + * @config: requested configuration for the lines. + * @num_lines: number of lines requested in this request, i.e. the number + * of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to + * request a single line + * @event_buffer_size: a suggested minimum number of line events that the + * kernel should buffer. This is only relevant if edge detection is + * enabled in the configuration. Note that this is only a suggested value + * and the kernel may allocate a larger buffer or cap the size of the + * buffer. If this field is zero then the buffer size defaults to a minimum + * of num_lines*16. + * @padding: reserved for future use and must be zero filled + * @fd: if successful this field will contain a valid anonymous file handle + * after a GPIO_GET_LINE_IOCTL operation, zero or negative value means + * error + */ +struct gpio_v2_line_request { + __u32 offsets[GPIO_V2_LINES_MAX]; + char consumer[GPIO_MAX_NAME_SIZE]; + struct gpio_v2_line_config config; + __u32 num_lines; + __u32 event_buffer_size; + /* Pad to fill implicit padding and reserve space for future use. */ + __u32 padding[5]; + __s32 fd; +}; + +/** + * struct gpio_v2_line_info - Information about a certain GPIO line + * @name: the name of this GPIO line, such as the output pin of the line on + * the chip, a rail or a pin header name on a board, as specified by the + * GPIO chip, may be empty + * @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 + * @flags: flags for the GPIO line, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, + * GPIO_V2_LINE_FLAG_OUTPUT etc, OR:ed together + * @offset: the local offset on this GPIO chip, fill this in when + * requesting the line information from the kernel + * @num_attrs: the number of attributes in attrs + * @attrs: the configuration attributes associated with the line + * @padding: reserved for future use + */ +struct gpio_v2_line_info { + char name[GPIO_MAX_NAME_SIZE]; + char consumer[GPIO_MAX_NAME_SIZE]; + __u32 offset; + __u32 num_attrs; + __aligned_u64 flags; + struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX]; + /* Space reserved for future use. */ + __u32 padding[4]; +}; + +/** + * enum gpio_v2_line_changed_type - &struct gpio_v2_line_changed.event_type + * values + * @GPIO_V2_LINE_CHANGED_REQUESTED: line has been requested + * @GPIO_V2_LINE_CHANGED_RELEASED: line has been released + * @GPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured + */ +enum gpio_v2_line_changed_type { + GPIO_V2_LINE_CHANGED_REQUESTED = 1, + GPIO_V2_LINE_CHANGED_RELEASED = 2, + GPIO_V2_LINE_CHANGED_CONFIG = 3, +}; + +/** + * struct gpio_v2_line_info_changed - Information about a change in status + * of a GPIO line + * @info: updated line information + * @timestamp_ns: estimate of time of status change occurrence, in nanoseconds + * @event_type: the type of change with a value from enum + * gpio_v2_line_changed_type + * @padding: reserved for future use + */ +struct gpio_v2_line_info_changed { + struct gpio_v2_line_info info; + __aligned_u64 timestamp_ns; + __u32 event_type; + /* Pad struct to 64-bit boundary and reserve space for future use. */ + __u32 padding[5]; +}; + +/** + * enum gpio_v2_line_event_id - &struct gpio_v2_line_event.id values + * @GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edge + * @GPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge + */ +enum gpio_v2_line_event_id { + GPIO_V2_LINE_EVENT_RISING_EDGE = 1, + GPIO_V2_LINE_EVENT_FALLING_EDGE = 2, +}; + +/** + * struct gpio_v2_line_event - The actual event being pushed to userspace + * @timestamp_ns: best estimate of time of event occurrence, in nanoseconds. + * The timestamp_ns is read from CLOCK_MONOTONIC and is intended to allow the + * accurate measurement of the time between events. It does not provide + * the wall-clock time. + * @id: event identifier with value from enum gpio_v2_line_event_id + * @offset: the offset of the line that triggered the event + * @seqno: the sequence number for this event in the sequence of events for + * all the lines in this line request + * @line_seqno: the sequence number for this event in the sequence of + * events on this particular line + * @padding: reserved for future use + */ +struct gpio_v2_line_event { + __aligned_u64 timestamp_ns; + __u32 id; + __u32 offset; + __u32 seqno; + __u32 line_seqno; + /* Space reserved for future use. */ + __u32 padding[6]; +}; + +/* + * ABI v1 + */ + /* Informational flags */ #define GPIOLINE_FLAG_KERNEL (1UL << 0) /* Line used by the kernel */ #define GPIOLINE_FLAG_IS_OUT (1UL << 1) @@ -149,8 +411,6 @@ struct gpiohandle_config { __u32 padding[4]; /* padding for future use */ }; -#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0a, struct gpiohandle_config) - /** * struct gpiohandle_data - Information of values on a GPIO handle * @values: when getting the state of lines this contains the current @@ -161,9 +421,6 @@ struct gpiohandle_data { __u8 values[GPIOHANDLES_MAX]; }; -#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) -#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) - /* Eventrequest flags */ #define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0) #define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1) @@ -207,11 +464,31 @@ struct gpioevent_data { __u32 id; }; +/* + * v1 and v2 ioctl()s + */ #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info) +#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0C, __u32) + +/* + * v2 ioctl()s + */ +#define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info) +#define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request) +#define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config) +#define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values) +#define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values) + +/* + * v1 ioctl()s + */ #define GPIO_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x02, struct gpioline_info) -#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0b, struct gpioline_info) -#define GPIO_GET_LINEINFO_UNWATCH_IOCTL _IOWR(0xB4, 0x0c, __u32) #define GPIO_GET_LINEHANDLE_IOCTL _IOWR(0xB4, 0x03, struct gpiohandle_request) #define GPIO_GET_LINEEVENT_IOCTL _IOWR(0xB4, 0x04, struct gpioevent_request) +#define GPIOHANDLE_GET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x08, struct gpiohandle_data) +#define GPIOHANDLE_SET_LINE_VALUES_IOCTL _IOWR(0xB4, 0x09, struct gpiohandle_data) +#define GPIOHANDLE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0A, struct gpiohandle_config) +#define GPIO_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x0B, struct gpioline_info) #endif /* _UAPI_GPIO_H_ */ From patchwork Mon Sep 28 00:27:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285685 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=-12.8 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 7D2F7C4346E for ; Mon, 28 Sep 2020 00:29:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49C62239A1 for ; Mon, 28 Sep 2020 00:29:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="klGlzmAO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726621AbgI1A3b (ORCPT ); Sun, 27 Sep 2020 20:29:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A3b (ORCPT ); Sun, 27 Sep 2020 20:29:31 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 806EBC0613CE; Sun, 27 Sep 2020 17:29:31 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id o25so6813976pgm.0; Sun, 27 Sep 2020 17:29:31 -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=yxFGP77mHktaAR7WX6vWj7VITnrOOL1DChoD0Wvv/jQ=; b=klGlzmAOCMbywlsF8JCt9Ubil5NBFhAqheKoKtbYrnKx84SnVTAv6yFEeLkCfLG38E nswiVPnnxwyxwmIMadSUAvVYoR/LZ+83JBg8AJGOq4VUeOcmnvhl7xYkCR1vzOc0m9cg zmHe5XPYJzahhSfv2Gy5KZy9RE/MR0fBWIVOAERIMgAcJqU4O3nY4Lj+IjXNBO0cB5dG LFp8RgZG7wc4PQ7L3BdFOgwu4TzDwuqPjR7tC+oGPaOQ5n64ukv3/o6agg/xdGwBEqWN Jslw+4ef37OhdwlzjzN+cdZglpDy6+Zt5+AsrrP+16CWOMXUBeQFFksOio/Mman7p6ee fDvw== 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=yxFGP77mHktaAR7WX6vWj7VITnrOOL1DChoD0Wvv/jQ=; b=OEw8XwQuxDujKjcliR75HIlKO4G1+iG83v6X1B9939IiXNIUEkDiPgsL+4gwHZu4fn sBd5pwQUylKCCRKUsht9PuVAS/K9jp8/j1qS88RT20kDtaWO1GAUBzonP4X3Oh+07CdU bLqmc7B2qRGiMlc+j7O9QwnKnU+YB+JmK20yC6Jun3OMhewBxgIab6Ea9AWF9LmRpfkA PSkLytrqiJ2NS5SA50UonsmA2pfhMYKd1UQl8Fu0T0IaUySvXGE2cCuwuwV+Rf7WceuF gIaoXKrox2seVGpav8h4s03PQRCCaqMxBulrQ0owH6xlutiWXErdeFcksoONvSi72LPg BP3g== X-Gm-Message-State: AOAM5308nda4X6LcY3GpNXtFjqeSoT2gXbrehkniJoTqloEHyjTVY48t KPkV1yYHPR4LqGatV7MUX0o39CMkqctavA== X-Google-Smtp-Source: ABdhPJytxyX1bIEMGDN9qRpgr1CFqg9gimUj/Hwe9sHJYupFGropqFr2Fr3kk2esqR07qKM04vi7XA== X-Received: by 2002:a63:e602:: with SMTP id g2mr7305948pgh.427.1601252970622; Sun, 27 Sep 2020 17:29:30 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:29:30 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 05/20] gpiolib: make cdev a build option Date: Mon, 28 Sep 2020 08:27:52 +0800 Message-Id: <20200928002807.12146-6-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 CDEV interface specific build options to follow. Suggested-by: Bartosz Golaszewski Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- 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 5cfdaf3b004d..e4debd66d71f 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 639275eb4e4d..6c3791a55a7b 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -6,8 +6,8 @@ ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG obj-$(CONFIG_GPIOLIB) += gpiolib.o obj-$(CONFIG_GPIOLIB) += gpiolib-devres.o obj-$(CONFIG_GPIOLIB) += gpiolib-legacy.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 Mon Sep 28 00:27: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: 254326 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=-12.8 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 64C95C4346E for ; Mon, 28 Sep 2020 00:29:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1FCFC239A1 for ; Mon, 28 Sep 2020 00:29:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lmcQKamd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726638AbgI1A3q (ORCPT ); Sun, 27 Sep 2020 20:29:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1A3q (ORCPT ); Sun, 27 Sep 2020 20:29:46 -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 5528BC0613CE; Sun, 27 Sep 2020 17:29:46 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id 197so6778327pge.8; Sun, 27 Sep 2020 17:29:46 -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=FaUFcogze2+V66PN/PuD4HqelSjChmCQWYPEe3k7KCw=; b=lmcQKamd3BmtPBY4CvR7ZeGhRsLypZhLk4bi4xJ0sIc4GGlb2zFFC732OQvolCDBD0 tuMKryTXqns/PdKtYK1hSONmJ9D8xu2HAuWzQX71TSYRw3f4PEshNmVHp4mnaPrreL8p jaOcnQCdIqNbcmpeoddG1jUmNyjTEmFhuYI/mZfaolddjaDrvAxvzj+2tLhuca/fA2HH EssFODXTpatme3rnKkbnLnZZENgzEAHH5tLbfHE5fKE4VTz7cm+C+b/XPCzD0DDm+BAt nXLKLgOiSXJeFAiOJI9ZV3bXaaE4fret3KPojWY39Bs5FlFRInCFPdNOLevCLHoEjYcm Ht+Q== 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=FaUFcogze2+V66PN/PuD4HqelSjChmCQWYPEe3k7KCw=; b=sH2zMy8HJJgsxfPOsq9KtAf3S/rvNitj3lQFKCLs9BC83Sweo0w6i2GTOz2q7eKpcW eaO7n9j9KnrQvsfKYeuIwLdBlnlnY/pHA6kTZH98ZirfucsnkaFJpmsncOC0gbiBeyBo 47dJBR3lgkvKY0R7nOSDSYsCyf1ap+qwsTgJAs+d0UHQlu11vvFK+2u3Zpq+5/VsNJ/J 4K2BlaVJTzX95al9jzBPMrZ57zLyJNZufZzCDAMqLCMgJo6ZT2JHE1RXWbmJKIiFUT6O ngXKh8qY9daYElztHdgsyWFE4olvhov/BB9+Xs2D28f3DOxHLNcB3HLsXmhsr714PPRn xZFw== X-Gm-Message-State: AOAM532ZGsm/+Qs8uDjJEejj/SZz+I1bNVdti3qdaBt/RUXJ7QbCIC1W kxblUgTj0HdNcCfBsRWkcQEp2a4YXUOnNQ== X-Google-Smtp-Source: ABdhPJx9UJK7KWA70L19GVWGaayldZcwasnt9C8DakqS5G/6Rl557SMZIj0EF5HiskuPjiZjy1ZqCg== X-Received: by 2002:a17:902:ee53:b029:d1:ece5:a1d1 with SMTP id 19-20020a170902ee53b02900d1ece5a1d1mr8833168plo.63.1601252985555; Sun, 27 Sep 2020 17:29:45 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:29:45 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 06/20] gpiolib: add build option for CDEV v1 ABI Date: Mon, 28 Sep 2020 08:27:53 +0800 Message-Id: <20200928002807.12146-7-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a build option to allow the removal of the CDEV v1 ABI. Suggested-by: Bartosz Golaszewski Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- This patch is before the v2 implementation, and is non-functional until that patch, as some parts of that patch would be written slightly differently if removing v1 was not considered. Adding this patch after that would necessitate revisiting the v2 changes, so this ordering results in two simpler patches. drivers/gpio/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index e4debd66d71f..d8d086635929 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -82,6 +82,18 @@ config GPIO_CDEV If unsure, say Y. +config GPIO_CDEV_V1 + bool "Support GPIO ABI Version 1" + default y + depends on GPIO_CDEV + help + Say Y here to support version 1 of the GPIO CDEV ABI. + + This ABI version is deprecated. + Please use the latest ABI for new developments. + + If unsure, say Y. + config GPIO_GENERIC depends on HAS_IOMEM # Only for IOMEM drivers tristate From patchwork Mon Sep 28 00:27:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285684 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=-12.8 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 F3793C4346E for ; Mon, 28 Sep 2020 00:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A5571239A1 for ; Mon, 28 Sep 2020 00:30:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Btaa6EyI" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726420AbgI1AaD (ORCPT ); Sun, 27 Sep 2020 20:30:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1AaC (ORCPT ); Sun, 27 Sep 2020 20:30:02 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D306CC0613CE; Sun, 27 Sep 2020 17:30:02 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id u3so2568075pjr.3; Sun, 27 Sep 2020 17:30:02 -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=UEKXscWrsgD0uXvgrfX9r7gxqnPNyPWVakiXV1xalPw=; b=Btaa6EyIT1wTWpJpl5FjfijwAnLCM1Wh0M20P1ujWBkKPgWK9+aYi5UR9dOqbrpfrv RX4Wh9MFW64YasJ4cFHEuWKADw+HXNB4SkHI4f5lu4k8KAcTHjQoiO0YlVDmpHkcLn8t 7wwHsZQMfoSkp+uaig6FLN2XTvqcHKN7jAfTUmpIphZo1D18ZvMQNRXF3zqGQ/rPxgeR ndguZ3YF5FMSVkAvYAk1VpxBxxISdKkrtLlubksuhDKx7HmEE4AzbVsbbRZX7YFjqd0e a/RON/LJA2Ew7smnZE+q/S+9swUEPVrLweBY3Kfo0tWHqFVonH1QbKkVZX/ebP0Hsp2E 5Q7A== 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=UEKXscWrsgD0uXvgrfX9r7gxqnPNyPWVakiXV1xalPw=; b=JnSyXVHKU9J8m1aohbyhg1tOpRxK7cjfSMS/qrConRTFhcYBI2lhrZfq7eWwHaWLbc 4uO3TNjxel5alsv5QVMJdGF3uLNjfDIqomkx5ZEOtyBG+2R89FJFYCVfZ0JSL/XJ5dN5 tUpLEDaodLKP7HVmQcPtYGzeBpwHX9LkgG4tEfSq8UZ87Bcj2QOTYQbURXbuC+PEbHAt F74R9DoRiscp7oU+Srr7S6cKlS3g7uoRuakhExk7ARyWTLJmlnLGYlgJrKQxSCvL+wMP GPcVv/qdI0RNx+LdpzEtSzPrIejX99xEMuveBH9YUA2bMtBusbbvLTSRASZAvaktFjK/ 8Pyw== X-Gm-Message-State: AOAM531Z/L8lgEX3zZTDBF23Jl3Ne6ennJH9cQgzL54sWhsxLKdUqSFd j/0duDwYTuqVqBjFUpDnkvY7hKiuli1hVg== X-Google-Smtp-Source: ABdhPJxg3P/bHSIDLI64cn9aoOiajxFxWGnbOlPVUi/gomqL+kG6yBpbuyXRq5PF8+o0KheBsnYvdw== X-Received: by 2002:a17:90a:b302:: with SMTP id d2mr6943036pjr.150.1601253001766; Sun, 27 Sep 2020 17:30:01 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.29.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:30:01 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 07/20] gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL Date: Mon, 28 Sep 2020 08:27:54 +0800 Message-Id: <20200928002807.12146-8-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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. The struct linereq implementation is based on the v1 struct linehandle implementation. Signed-off-by: Kent Gibson --- The linereq_ioctl() is a simple wrapper around linereq_get_values() here, but will be extended with other ioctls in subsequent patches. Similarly, the struct line only contains the desc here, but will receive the edge detector and debouncer fields in subsequent patches. Changes for v10: - as per cover letter Changes for v8: - fix BUILD_BUG_ON conditions and relocate them before the return in gpiolib_cdev_register() Changes for v7: - add check on kmalloc_array return value Changes for v5: - as per cover letter Changes for v4: - fix handling of mask in line_get_values drivers/gpio/gpiolib-cdev.c | 424 ++++++++++++++++++++++++++++++++++++ 1 file changed, 424 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 86679397d09c..a5f2256cdf8c 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 @@ -24,6 +26,25 @@ #include "gpiolib.h" #include "gpiolib-cdev.h" +/* + * Array sizes must ensure 64-bit alignment and not create holes in the + * struct packing. + */ +static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2)); +static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8)); + +/* + * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility + */ +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8)); +static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8)); + /* Character device interface to GPIO. * * The GPIO character device, /dev/gpiochipN, provides userspace an @@ -34,6 +55,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 +398,402 @@ 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 requested line + * @desc: the GPIO descriptor for this line. + */ +struct line { + struct gpio_desc *desc; +}; + +/** + * struct linereq - contains the state of a userspace line request + * @gdev: the GPIO device the line request pertains to + * @label: consumer label used to tag GPIO descriptors + * @num_lines: the number of lines in the lines array + * @lines: the lines held by this line request, with @num_lines elements. + */ +struct linereq { + struct gpio_device *gdev; + const char *label; + u32 num_lines; + struct line lines[]; +}; + +#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, + unsigned int line_idx) +{ + unsigned 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, + unsigned int line_idx) +{ + unsigned 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 and 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 and 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 requires 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, + unsigned int num_lines) +{ + unsigned int i; + u64 flags; + int ret; + + 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 linereq_get_values(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_values lv; + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + unsigned int i, didx, num_get; + int ret; + + /* NOTE: It's ok to read values of output lines. */ + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + for (num_get = 0, i = 0; i < lr->num_lines; i++) { + if (lv.mask & BIT_ULL(i)) { + num_get++; + descs = &lr->lines[i].desc; + } + } + + if (num_get == 0) + return -EINVAL; + + if (num_get != 1) { + descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); + if (!descs) + return -ENOMEM; + for (didx = 0, i = 0; i < lr->num_lines; i++) { + if (lv.mask & BIT_ULL(i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret = gpiod_get_array_value_complex(false, true, num_get, + descs, NULL, vals); + + if (num_get != 1) + kfree(descs); + if (ret) + return ret; + + lv.bits = 0; + for (didx = 0, i = 0; i < lr->num_lines; 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 linereq_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct linereq *lr = file->private_data; + void __user *ip = (void __user *)arg; + + if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) + return linereq_get_values(lr, ip); + + return -EINVAL; +} + +#ifdef CONFIG_COMPAT +static long linereq_ioctl_compat(struct file *file, unsigned int cmd, + unsigned long arg) +{ + return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); +} +#endif + +static void linereq_free(struct linereq *lr) +{ + unsigned int i; + + for (i = 0; i < lr->num_lines; i++) { + if (lr->lines[i].desc) + gpiod_free(lr->lines[i].desc); + } + kfree(lr->label); + put_device(&lr->gdev->dev); + kfree(lr); +} + +static int linereq_release(struct inode *inode, struct file *file) +{ + struct linereq *lr = file->private_data; + + linereq_free(lr); + return 0; +} + +static const struct file_operations line_fileops = { + .release = linereq_release, + .owner = THIS_MODULE, + .llseek = noop_llseek, + .unlocked_ioctl = linereq_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = linereq_ioctl_compat, +#endif +}; + +static int linereq_create(struct gpio_device *gdev, void __user *ip) +{ + struct gpio_v2_line_request ulr; + struct gpio_v2_line_config *lc; + struct linereq *lr; + struct file *file; + u64 flags; + unsigned int i; + int fd, ret; + + if (copy_from_user(&ulr, ip, sizeof(ulr))) + return -EFAULT; + + if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX)) + return -EINVAL; + + if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding))) + return -EINVAL; + + lc = &ulr.config; + ret = gpio_v2_line_config_validate(lc, ulr.num_lines); + if (ret) + return ret; + + lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); + if (!lr) + return -ENOMEM; + + lr->gdev = gdev; + get_device(&gdev->dev); + + /* Make sure this is terminated */ + ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; + if (strlen(ulr.consumer)) { + /* label is only initialized if consumer is set */ + lr->label = kstrdup(ulr.consumer, GFP_KERNEL); + if (!lr->label) { + ret = -ENOMEM; + goto out_free_linereq; + } + } + + lr->num_lines = ulr.num_lines; + + /* Request each GPIO */ + for (i = 0; i < ulr.num_lines; i++) { + u32 offset = ulr.offsets[i]; + struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); + + if (IS_ERR(desc)) { + ret = PTR_ERR(desc); + goto out_free_linereq; + } + + ret = gpiod_request(desc, lr->label); + if (ret) + goto out_free_linereq; + + lr->lines[i].desc = 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_linereq; + + /* + * 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_linereq; + } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { + ret = gpiod_direction_input(desc); + if (ret) + goto out_free_linereq; + } + + 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_linereq; + } + + file = anon_inode_getfile("gpio-line", &line_fileops, lr, + O_RDONLY | O_CLOEXEC); + if (IS_ERR(file)) { + ret = PTR_ERR(file); + goto out_put_unused_fd; + } + + ulr.fd = fd; + if (copy_to_user(ip, &ulr, sizeof(ulr))) { + /* + * 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", + lr->num_lines); + + return 0; + +out_put_unused_fd: + put_unused_fd(fd); +out_free_linereq: + linereq_free(lr); + return ret; +} + +#ifdef CONFIG_GPIO_CDEV_V1 /* * GPIO line event management @@ -745,6 +1163,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) { @@ -843,6 +1263,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; @@ -885,6 +1306,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 linereq_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { if (copy_from_user(&offset, ip, sizeof(offset))) return -EFAULT; From patchwork Mon Sep 28 00:27: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: 254325 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=-12.8 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 5F9C9C4346E for ; Mon, 28 Sep 2020 00:30:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00AA3239A1 for ; Mon, 28 Sep 2020 00:30:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="plxeWcxh" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726409AbgI1AaP (ORCPT ); Sun, 27 Sep 2020 20:30:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbgI1AaP (ORCPT ); Sun, 27 Sep 2020 20:30:15 -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 22527C0613CE; Sun, 27 Sep 2020 17:30:15 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id d6so7745933pfn.9; Sun, 27 Sep 2020 17:30: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=ADkrA+l4uzKnZImYbXPd+UqVMJaLEJwSfCB7v9VQ3qM=; b=plxeWcxhaoT/uXHcld0oOl33GyInyzeq43hMFAajvSi9hliE0cFSIcJ4aK8hkU7Xzn Sz93obOTTNgWXf9Nr6gdMHQsYuZ/aShxanF8O6CkUlV2De3AzAm1Y/3Lk8gUZCx8BVPl Cx8wUBs8j9XdYB9KFP3spLsz4oqJIG0A+oIvksZvgGzDTWegZGAw+l0ZvascJ4MmjVCD KrqxQXoZl/216TyBI7HSlGg++xTZYr8Fzn2N5u9gmNoFbYazWLIZmpg5yaZ8uNMRlvz3 MwGklkOxcBSPbP54Pwu+O33BIrryx0zDfg6j4cT7e47XOAX+xBwBDqDnJo82YZUQNueg zmKw== 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=ADkrA+l4uzKnZImYbXPd+UqVMJaLEJwSfCB7v9VQ3qM=; b=AAkOEFcI+PoMZsVLujLGLKVlH91xhexgNWgH9bAeIGZHwn46rD4qSXMEqfrQ9fjZT5 u5MqAubnT9pDDXlZbpXjtqZaI0QQxYdePsT6cSFW/Xv3+YhlrhlCtf6jKITAEFiOrN/Z wqORLAsDPSur2CoVkKdV2nOoZb5/GUEUdrpJ8ziko4hqUGCFMj4lZK54T6PA2G60Hv0s B5sl4ky50LDHrxsrGNNjF4Vxo87BIHi+2Vjgmi0nm2jJAyRwxT4JhOCbOxPAXK/VI44+ 3SuJXO7WEEb04MVZvqYsqo8wwkHUrU8R8Shtf9OlhHxnQkV21uWPAbeM6GPTc5T9y71F 3nsQ== X-Gm-Message-State: AOAM530ilyiRK0h2/q0NSQfz/qrj9JDj3x2TjQwx1RIzh/tJYsUfuDry cFF5YCmeKvcuAoS0G6ik8QoIcaj9PIQHrw== X-Google-Smtp-Source: ABdhPJwhcXXCHjsityuGkc/VqCS8tlWWVAgis61RdWCLo6w6M8rnV1Vi1eIOe1oJFvLjiFEHA0kIpQ== X-Received: by 2002:a17:902:6504:b029:d2:63a7:1737 with SMTP id b4-20020a1709026504b02900d263a71737mr8791287plk.74.1601253014094; Sun, 27 Sep 2020 17:30:14 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:30:13 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 08/20] gpiolib: cdev: support GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL Date: Mon, 28 Sep 2020 08:27:55 +0800 Message-Id: <20200928002807.12146-9-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for GPIO_V2_GET_LINEINFO_IOCTL and GPIO_V2_GET_LINEINFO_WATCH_IOCTL. The core of this change is the event kfifo switching to contain struct gpioline_info_changed_v2, instead of v1 as v2 is richer. The two uAPI versions are mostly independent - other than where they both provide line info changes via reads on the chip fd. As the info change structs differ between v1 and v2, the infowatch implementation tracks which version of the infowatch ioctl, either GPIO_GET_LINEINFO_WATCH_IOCTL or GPIO_V2_GET_LINEINFO_WATCH_IOCTL, initiates the initial watch and returns the corresponding info change struct to the read. The version supported on that fd locks to that version on the first watch request, so subsequent watches from that process must use the same uAPI version. Signed-off-by: Kent Gibson --- Changes for v10: - as per cover letter Changes for v5: - as per cover letter Changes for v4: - replace strncpy with memcpy in gpio_v2_line_info_to_v1 drivers/gpio/gpiolib-cdev.c | 196 ++++++++++++++++++++++++++++++------ 1 file changed, 168 insertions(+), 28 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index a5f2256cdf8c..786e667cdc85 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -181,7 +181,8 @@ static long linehandle_set_config(struct linehandle_state *lh, } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_CONFIG, desc); + GPIO_V2_LINE_CHANGED_CONFIG, + desc); } return 0; } @@ -353,7 +354,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", offset); @@ -749,7 +750,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", offset); @@ -1096,7 +1097,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) goto out_free_le; blocking_notifier_call_chain(&desc->gdev->notifier, - GPIOLINE_CHANGED_REQUESTED, desc); + GPIO_V2_LINE_CHANGED_REQUESTED, desc); irq = gpiod_to_irq(desc); if (irq <= 0) { @@ -1163,17 +1164,58 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2, + struct gpioline_info *info_v1) +{ + u64 flagsv2 = info_v2->flags; + + memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name)); + memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer)); + info_v1->line_offset = info_v2->offset; + info_v1->flags = 0; + + if (flagsv2 & GPIO_V2_LINE_FLAG_USED) + info_v1->flags |= GPIOLINE_FLAG_KERNEL; + + if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT) + info_v1->flags |= GPIOLINE_FLAG_IS_OUT; + + if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW) + info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW; + + if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN) + info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN; + if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE) + info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE; + + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) + info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) + info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; + if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED) + info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE; +} + +static void gpio_v2_line_info_changed_to_v1( + struct gpio_v2_line_info_changed *lic_v2, + struct gpioline_info_changed *lic_v1) +{ + gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info); + lic_v1->timestamp = lic_v2->timestamp_ns; + lic_v1->event_type = lic_v2->event_type; +} + #endif /* CONFIG_GPIO_CDEV_V1 */ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, - struct gpioline_info *info) + struct gpio_v2_line_info *info) { struct gpio_chip *gc = desc->gdev->chip; bool ok_for_pinctrl; unsigned long flags; memset(info, 0, sizeof(*info)); - info->line_offset = gpio_chip_hwgpio(desc); + info->offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking @@ -1183,7 +1225,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, * lock common to both frameworks? */ ok_for_pinctrl = - pinctrl_gpio_can_use_line(gc->base + info->line_offset); + pinctrl_gpio_can_use_line(gc->base + info->offset); spin_lock_irqsave(&gpio_lock, flags); @@ -1204,23 +1246,27 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, test_bit(FLAG_EXPORT, &desc->flags) || test_bit(FLAG_SYSFS, &desc->flags) || !ok_for_pinctrl) - info->flags |= GPIOLINE_FLAG_KERNEL; + info->flags |= GPIO_V2_LINE_FLAG_USED; + if (test_bit(FLAG_IS_OUT, &desc->flags)) - info->flags |= GPIOLINE_FLAG_IS_OUT; + info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; + else + info->flags |= GPIO_V2_LINE_FLAG_INPUT; + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) - info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; + info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | - GPIOLINE_FLAG_IS_OUT); + info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) - info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | - GPIOLINE_FLAG_IS_OUT); + info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; + if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; if (test_bit(FLAG_PULL_DOWN, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; if (test_bit(FLAG_PULL_UP, &desc->flags)) - info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; spin_unlock_irqrestore(&gpio_lock, flags); } @@ -1228,11 +1274,65 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_chardev_data { struct gpio_device *gdev; wait_queue_head_t wait; - DECLARE_KFIFO(events, struct gpioline_info_changed, 32); + DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32); struct notifier_block lineinfo_changed_nb; unsigned long *watched_lines; +#ifdef CONFIG_GPIO_CDEV_V1 + atomic_t watch_abi_version; +#endif }; +#ifdef CONFIG_GPIO_CDEV_V1 +/* + * returns 0 if the versions match, else the previously selected ABI version + */ +static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata, + unsigned int version) +{ + int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version); + + if (abiv == version) + return 0; + + return abiv; +} +#endif + +static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip, + bool watch) +{ + struct gpio_desc *desc; + struct gpio_v2_line_info lineinfo; + + if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) + return -EFAULT; + + if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding))) + return -EINVAL; + + desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + if (watch) { +#ifdef CONFIG_GPIO_CDEV_V1 + if (lineinfo_ensure_abi_version(cdev, 2)) + return -EPERM; +#endif + if (test_and_set_bit(lineinfo.offset, cdev->watched_lines)) + return -EBUSY; + } + gpio_desc_to_lineinfo(desc, &lineinfo); + + if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { + if (watch) + clear_bit(lineinfo.offset, cdev->watched_lines); + return -EFAULT; + } + + return 0; +} + /* * gpio_ioctl() - ioctl handler for the GPIO chardev */ @@ -1242,7 +1342,6 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) struct gpio_device *gdev = cdev->gdev; struct gpio_chip *gc = gdev->chip; void __user *ip = (void __user *)arg; - struct gpio_desc *desc; __u32 offset; /* We fail any subsequent ioctl():s when the chip is gone */ @@ -1265,7 +1364,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return 0; #ifdef CONFIG_GPIO_CDEV_V1 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { + struct gpio_desc *desc; struct gpioline_info lineinfo; + struct gpio_v2_line_info lineinfo_v2; if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; @@ -1275,7 +1376,8 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (IS_ERR(desc)) return PTR_ERR(desc); - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; @@ -1285,7 +1387,9 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { return lineevent_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { + struct gpio_desc *desc; struct gpioline_info lineinfo; + struct gpio_v2_line_info lineinfo_v2; if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; @@ -1295,10 +1399,14 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (IS_ERR(desc)) return PTR_ERR(desc); + if (lineinfo_ensure_abi_version(cdev, 1)) + return -EPERM; + if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines)) return -EBUSY; - gpio_desc_to_lineinfo(desc, &lineinfo); + gpio_desc_to_lineinfo(desc, &lineinfo_v2); + gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { clear_bit(lineinfo.line_offset, cdev->watched_lines); @@ -1307,6 +1415,10 @@ 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_LINEINFO_IOCTL || + cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL) { + return lineinfo_get(cdev, ip, + cmd == GPIO_V2_GET_LINEINFO_WATCH_IOCTL); } else if (cmd == GPIO_V2_GET_LINE_IOCTL) { return linereq_create(gdev, ip); } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { @@ -1342,7 +1454,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, unsigned long action, void *data) { struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb); - struct gpioline_info_changed chg; + struct gpio_v2_line_info_changed chg; struct gpio_desc *desc = data; int ret; @@ -1351,7 +1463,7 @@ static int lineinfo_changed_notify(struct notifier_block *nb, memset(&chg, 0, sizeof(chg)); chg.event_type = action; - chg.timestamp = ktime_get_ns(); + chg.timestamp_ns = ktime_get_ns(); gpio_desc_to_lineinfo(desc, &chg.info); ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); @@ -1382,12 +1494,16 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, size_t count, loff_t *off) { struct gpio_chardev_data *cdev = file->private_data; - struct gpioline_info_changed event; + struct gpio_v2_line_info_changed event; ssize_t bytes_read = 0; int ret; + size_t event_size; - if (count < sizeof(event)) +#ifndef CONFIG_GPIO_CDEV_V1 + event_size = sizeof(struct gpio_v2_line_info_changed); + if (count < event_size) return -EINVAL; +#endif do { spin_lock(&cdev->wait.lock); @@ -1409,7 +1525,17 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, return ret; } } - +#ifdef CONFIG_GPIO_CDEV_V1 + /* must be after kfifo check so watch_abi_version is set */ + if (atomic_read(&cdev->watch_abi_version) == 2) + event_size = sizeof(struct gpio_v2_line_info_changed); + else + event_size = sizeof(struct gpioline_info_changed); + if (count < event_size) { + spin_unlock(&cdev->wait.lock); + return -EINVAL; + } +#endif ret = kfifo_out(&cdev->events, &event, 1); spin_unlock(&cdev->wait.lock); if (ret != 1) { @@ -1418,9 +1544,23 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, /* We should never get here. See lineevent_read(). */ } - if (copy_to_user(buf + bytes_read, &event, sizeof(event))) +#ifdef CONFIG_GPIO_CDEV_V1 + if (event_size == sizeof(struct gpio_v2_line_info_changed)) { + if (copy_to_user(buf + bytes_read, &event, event_size)) + return -EFAULT; + } else { + struct gpioline_info_changed event_v1; + + gpio_v2_line_info_changed_to_v1(&event, &event_v1); + if (copy_to_user(buf + bytes_read, &event_v1, + event_size)) + return -EFAULT; + } +#else + if (copy_to_user(buf + bytes_read, &event, event_size)) return -EFAULT; - bytes_read += sizeof(event); +#endif + bytes_read += event_size; } while (count >= bytes_read + sizeof(event)); return bytes_read; From patchwork Mon Sep 28 00:27:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285683 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=-12.8 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 8DAB0C4346E for ; Mon, 28 Sep 2020 00:30:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4291A206B6 for ; Mon, 28 Sep 2020 00:30:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JAsSv2Q2" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726393AbgI1Aaa (ORCPT ); Sun, 27 Sep 2020 20:30:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1Aaa (ORCPT ); Sun, 27 Sep 2020 20:30:30 -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 86740C0613CE; Sun, 27 Sep 2020 17:30:30 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id 197so6779531pge.8; Sun, 27 Sep 2020 17:30:30 -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=KcLZE9rQtYwXc9OUPzRxMgUpzLQzUW6O7REATWf1g+s=; b=JAsSv2Q2eP/H2QWY1wDKY5n6ziJYqqJo3zrGhTCK9lJBuzQaLLeBlzhh6hSotL5Gux tIGAyrNxZpsxid+mTrSB8ImwwqDuGgBFAHhGHO+tX6hgFHBSDNRbrFiBDp6W2dBWlTZx 7RVdpHuuyUHIWbPxKnYd/8BW7OkGKDWw4C/9HKmmCLlzCOgBGFwsaSJ07nTxoVSxHK+1 e+LivVIBn85zpbQIbGrK0ZqWxabskj4W7+nW59jPNANey0ibmIgzP1Yr9rt0gyCk/+rC KLXHJ80YA1DYYBsEIKIrvmVkxnAXWchqyA869IqxDvYR7UyrOebqJkoP1GuP8kNboLos rdaw== 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=KcLZE9rQtYwXc9OUPzRxMgUpzLQzUW6O7REATWf1g+s=; b=pGeaw1JLIqPRAHGGnMUDTtcLRzAp9A0si0iA8AuTSu71zwack/T6bwTBFsKC+su2vH DELpQBKp/75ci1kNFZAvulYMwDtpjfpVkddmgGaR5WX5XgIN2vW7W9DRd4jUY7aL75oW 9LnzQ+dMYTA9Z2KQSB7ImKB0BCL7CMIGLOueoiztOdyKjjiv3SYzSsWG8JmIzB/BMiX6 0hcqiy9slTUZ/gm6Sy+PXCjqTNR8wU/fTs4HuGLAxFqxoSmOVoXgSecvkg9bcDa3Rjfw cL1L3+boxTZmXMEhCC6pGyXLZxej23cB9P6dSN6vWwpvjnhn1Ysc35Oh3q4cEc5C0EVZ HXvQ== X-Gm-Message-State: AOAM532bQtHg0uB74pgW5F7ETooKLWjBbF0QXe04v/Tp1ZkLeTM5CeUP CNWpFZJy4GU/ttDamlxoqIuNEQo8YKAj9w== X-Google-Smtp-Source: ABdhPJxJJKqfiGR4exbRZDb1WAd1JnxX7jikfCk34yONG8XDPQFvOAfUwmgdmi9MLuJjm/Rv6b5mzg== X-Received: by 2002:a17:902:6a88:b029:d2:254:c89c with SMTP id n8-20020a1709026a88b02900d20254c89cmr8850522plk.19.1601253029326; Sun, 27 Sep 2020 17:30:29 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:30:28 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 09/20] gpiolib: cdev: support edge detection for uAPI v2 Date: Mon, 28 Sep 2020 08:27:56 +0800 Message-Id: <20200928002807.12146-10-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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. The edge_detector implementation is based on the v1 lineevent implementation. Unlike the v1 implementation, an overflow of the event buffer results in discarding older events, rather than the most recent, so the final event in a burst will correspond to the current state of the line. Signed-off-by: Kent Gibson --- The linereq_put_event() helper is only used once here, but is re-used in subsequent patches, and so is pre-emptively split out. edge_detector_stop() is extended in subsequent patches, and is structured to suit those additions. drivers/gpio/gpiolib-cdev.c | 277 ++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.c | 2 + drivers/gpio/gpiolib.h | 2 + 3 files changed, 281 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 786e667cdc85..868fcf89478c 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -404,9 +404,33 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /** * struct line - contains the state of a requested line * @desc: the GPIO descriptor for this line. + * @req: the corresponding line request + * @irq: the interrupt triggered in response to events on this GPIO + * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or + * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied + * @timestamp_ns: cache for the timestamp storing it between hardirq and + * IRQ thread, used to bring the timestamp close to the actual event + * @req_seqno: the seqno for the current edge event in the sequence of + * events for the corresponding line request. This is drawn from the @req. + * @line_seqno: the seqno for the current edge event in the sequence of + * events for this line. */ struct line { struct gpio_desc *desc; + /* + * -- edge detector specific fields -- + */ + struct linereq *req; + unsigned int irq; + u64 eflags; + /* + * timestamp_ns and req_seqno are accessed only by + * edge_irq_handler() and edge_irq_thread(), which are themselves + * mutually exclusive, so no additional protection is necessary. + */ + u64 timestamp_ns; + u32 req_seqno; + u32 line_seqno; }; /** @@ -414,12 +438,22 @@ struct line { * @gdev: the GPIO device the line request pertains to * @label: consumer label used to tag GPIO descriptors * @num_lines: the number of lines in the lines array + * @wait: wait queue that handles blocking reads of events + * @event_buffer_size: the number of elements allocated in @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_lines is 1, as + * the line_seqno is then the same and is cheaper to calculate. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq { struct gpio_device *gdev; const char *label; u32 num_lines; + wait_queue_head_t wait; + u32 event_buffer_size; + DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); + atomic_t seqno; struct line lines[]; }; @@ -436,12 +470,151 @@ struct linereq { (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 void linereq_put_event(struct linereq *lr, + struct gpio_v2_line_event *le) +{ + bool overflow = false; + + spin_lock(&lr->wait.lock); + if (kfifo_is_full(&lr->events)) { + overflow = true; + kfifo_skip(&lr->events); + } + kfifo_in(&lr->events, le, 1); + spin_unlock(&lr->wait.lock); + if (!overflow) + wake_up_poll(&lr->wait, EPOLLIN); + else + pr_debug_ratelimited("event FIFO is full - event dropped\n"); +} + +static irqreturn_t edge_irq_thread(int irq, void *p) +{ + struct line *line = p; + struct linereq *lr = line->req; + struct gpio_v2_line_event le; + + /* Do not leak kernel stack to userspace */ + memset(&le, 0, sizeof(le)); + + if (line->timestamp_ns) { + le.timestamp_ns = line->timestamp_ns; + } else { + /* + * We may be running from a nested threaded interrupt in + * which case we didn't get the timestamp from + * edge_irq_handler(). + */ + le.timestamp_ns = ktime_get_ns(); + if (lr->num_lines != 1) + line->req_seqno = atomic_inc_return(&lr->seqno); + } + line->timestamp_ns = 0; + + if (line->eflags == (GPIO_V2_LINE_FLAG_EDGE_RISING | + GPIO_V2_LINE_FLAG_EDGE_FALLING)) { + int level = gpiod_get_value_cansleep(line->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 (line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) { + /* Emit low-to-high event */ + le.id = GPIO_V2_LINE_EVENT_RISING_EDGE; + } else if (line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) { + /* Emit high-to-low event */ + le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE; + } else { + return IRQ_NONE; + } + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno; + le.offset = gpio_chip_hwgpio(line->desc); + + linereq_put_event(lr, &le); + + return IRQ_HANDLED; +} + +static irqreturn_t edge_irq_handler(int irq, void *p) +{ + struct line *line = p; + struct linereq *lr = line->req; + + /* + * Just store the timestamp in hardirq context so we get it as + * close in time as possible to the actual event. + */ + line->timestamp_ns = ktime_get_ns(); + + if (lr->num_lines != 1) + line->req_seqno = atomic_inc_return(&lr->seqno); + + return IRQ_WAKE_THREAD; +} + +static void edge_detector_stop(struct line *line) +{ + if (line->irq) { + free_irq(line->irq, line); + line->irq = 0; + } +} + +static int edge_detector_setup(struct line *line, + u64 eflags) +{ + unsigned long irqflags = 0; + int irq, ret; + + if (eflags && !kfifo_initialized(&line->req->events)) { + ret = kfifo_alloc(&line->req->events, + line->req->event_buffer_size, GFP_KERNEL); + if (ret) + return ret; + } + line->eflags = eflags; + + if (!eflags) + return 0; + + irq = gpiod_to_irq(line->desc); + if (irq < 0) + return -ENXIO; + + if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ? + IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; + if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING) + irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->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, line->req->label, line); + if (ret) + return ret; + + line->irq = irq; + return 0; +} + static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, unsigned int line_idx) { @@ -484,6 +657,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 and OPEN_DRAIN flags in a single * request. If the hardware actually supports enabling both at the @@ -547,6 +725,11 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags, 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, @@ -636,14 +819,85 @@ static long linereq_ioctl_compat(struct file *file, unsigned int cmd, } #endif +static __poll_t linereq_poll(struct file *file, + struct poll_table_struct *wait) +{ + struct linereq *lr = file->private_data; + __poll_t events = 0; + + poll_wait(file, &lr->wait, wait); + + if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events, + &lr->wait.lock)) + events = EPOLLIN | EPOLLRDNORM; + + return events; +} + +static ssize_t linereq_read(struct file *file, + char __user *buf, + size_t count, + loff_t *f_ps) +{ + struct linereq *lr = 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(&lr->wait.lock); + if (kfifo_is_empty(&lr->events)) { + if (bytes_read) { + spin_unlock(&lr->wait.lock); + return bytes_read; + } + + if (file->f_flags & O_NONBLOCK) { + spin_unlock(&lr->wait.lock); + return -EAGAIN; + } + + ret = wait_event_interruptible_locked(lr->wait, + !kfifo_is_empty(&lr->events)); + if (ret) { + spin_unlock(&lr->wait.lock); + return ret; + } + } + + ret = kfifo_out(&lr->events, &le, 1); + spin_unlock(&lr->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 linereq_free(struct linereq *lr) { unsigned int i; for (i = 0; i < lr->num_lines; i++) { + edge_detector_stop(&lr->lines[i]); if (lr->lines[i].desc) gpiod_free(lr->lines[i].desc); } + kfifo_free(&lr->events); kfree(lr->label); put_device(&lr->gdev->dev); kfree(lr); @@ -659,6 +913,8 @@ static int linereq_release(struct inode *inode, struct file *file) static const struct file_operations line_fileops = { .release = linereq_release, + .read = linereq_read, + .poll = linereq_poll, .owner = THIS_MODULE, .llseek = noop_llseek, .unlocked_ioctl = linereq_ioctl, @@ -698,6 +954,9 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev; get_device(&gdev->dev); + for (i = 0; i < ulr.num_lines; i++) + lr->lines[i].req = lr; + /* Make sure this is terminated */ ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; if (strlen(ulr.consumer)) { @@ -709,6 +968,14 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } + init_waitqueue_head(&lr->wait); + lr->event_buffer_size = ulr.event_buffer_size; + if (lr->event_buffer_size == 0) + lr->event_buffer_size = ulr.num_lines * 16; + else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16) + lr->event_buffer_size = GPIO_V2_LINES_MAX * 16; + + atomic_set(&lr->seqno, 0); lr->num_lines = ulr.num_lines; /* Request each GPIO */ @@ -747,6 +1014,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) ret = gpiod_direction_input(desc); if (ret) goto out_free_linereq; + + ret = edge_detector_setup(&lr->lines[i], + flags & GPIO_V2_LINE_EDGE_FLAGS); + if (ret) + goto out_free_linereq; } blocking_notifier_call_chain(&desc->gdev->notifier, @@ -1268,6 +1540,11 @@ 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; + spin_unlock_irqrestore(&gpio_lock, flags); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index dfcff5d24b18..aa20481e9452 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2092,6 +2092,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 Mon Sep 28 00:27: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: 254324 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=-12.8 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 48F3BC4346E for ; Mon, 28 Sep 2020 00:30:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 012F0206B8 for ; Mon, 28 Sep 2020 00:30:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="i7Rn2dvb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726440AbgI1Aao (ORCPT ); Sun, 27 Sep 2020 20:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1Aao (ORCPT ); Sun, 27 Sep 2020 20:30:44 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31FADC0613CE; Sun, 27 Sep 2020 17:30:44 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id s14so1442901pju.1; Sun, 27 Sep 2020 17:30:44 -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=imYtZiSX5AvWbH/xY/JjjqLmyT8ivLM44f4A3aeUozs=; b=i7Rn2dvb++mzLSNbsHPhMTk0eBbBOGSUr0Q+b6IYgqimkBeGNaRyOppX51BfEGx9CY Ozg69Pj25EBuQe3YNF6DoDYycPcJicoQXaetr7a8X3NAMG4wMIk4j7zRZSALZQE3kylo uE19QjsbzPY2rizHxATxNU5RLDupVOoxiYDglO3NyD+MHr0JLWfxozfZe6p9gRiZoohs iUcnByVPXBZidLvaR74TTxe6W4LuCjx+M+2i15G/5dFOWqs556GEabQ2iPwlZxSgPMQ8 WzHl51h7eHkGst0PfJF4Nk+BFBOw+2UvburU9ZTLAM0SWlcLF3f19jKO22uekHRbzKgQ 7d6Q== 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=imYtZiSX5AvWbH/xY/JjjqLmyT8ivLM44f4A3aeUozs=; b=iLwGvSBBtzaufVbzp5O8ry7WbJhGWCZql47TvPZUpJ0Vtg7eKwfJ44KJSD/Z1Ij7Cj e3UKD6l0hUbceAFpW/jkRFMgLV3z50t/Hq6N8ZMkLTjtVhhyyrDnZ69BR7k88VDB18OI MPQx4YzT6jGjCnZlqi1X4TfKgbY57JTapXZZ1kohE2oxNjZuauMOoQKhWS5MTG3XGQPf RqxLYjzx0aq4EYYFX2d99Hx4n7j7NN30aBVBRtJtnHOn7zIgKCe4pRHv5nSj8NI0GubO hCgy7j5IS8rJ1/0jTHHOP3J7at/FTxryShRd+Ltw9joU4yPdRwMK7az27+SmAiJe9Xph EMUw== X-Gm-Message-State: AOAM533n8dC3ZSnLHgWpyz0LyW3ip9EztARV2vV2wJw53rwgJw0fCUq2 3wL3vWU2iyEoChXv1I3XGQZKhOGwVlDyPA== X-Google-Smtp-Source: ABdhPJwGk7XnXmlNvvpF9HREUQ2lVj76/fT/cR33UqDFbII82c4MckDAR3+xffUDGG3OQiq88oSXVQ== X-Received: by 2002:a17:90a:156:: with SMTP id z22mr7188799pje.140.1601253043313; Sun, 27 Sep 2020 17:30:43 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.30.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:30:42 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 10/20] gpiolib: cdev: support GPIO_V2_LINE_SET_CONFIG_IOCTL Date: Mon, 28 Sep 2020 08:27:57 +0800 Message-Id: <20200928002807.12146-11-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for GPIO_V2_LINE_SET_CONFIG_IOCTL, the uAPI v2 line set config ioctl. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 868fcf89478c..608cdbd1d579 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -444,6 +445,8 @@ struct line { * @seqno: the sequence number for edge events generated on all lines in * this line request. Note that this is not used when @num_lines is 1, as * the line_seqno is then the same and is cheaper to calculate. + * @config_mutex: mutex for serializing ioctl() calls to ensure consistency + * of configuration, particularly multi-step accesses to desc flags. * @lines: the lines held by this line request, with @num_lines elements. */ struct linereq { @@ -454,6 +457,7 @@ struct linereq { u32 event_buffer_size; DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event); atomic_t seqno; + struct mutex config_mutex; struct line lines[]; }; @@ -574,6 +578,8 @@ static void edge_detector_stop(struct line *line) free_irq(line->irq, line); line->irq = 0; } + + line->eflags = 0; } static int edge_detector_setup(struct line *line, @@ -615,6 +621,17 @@ static int edge_detector_setup(struct line *line, return 0; } +static int edge_detector_update(struct line *line, u64 eflags, + bool polarity_change) +{ + if ((line->eflags == eflags) && !polarity_change) + return 0; + + edge_detector_stop(line); + + return edge_detector_setup(line, eflags); +} + static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, unsigned int line_idx) { @@ -799,6 +816,74 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return 0; } +static long linereq_set_config_unlocked(struct linereq *lr, + struct gpio_v2_line_config *lc) +{ + struct gpio_desc *desc; + unsigned int i; + u64 flags; + bool polarity_change; + int ret; + + for (i = 0; i < lr->num_lines; i++) { + desc = lr->lines[i].desc; + flags = gpio_v2_line_config_flags(lc, i); + polarity_change = + (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) != + ((flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW) != 0)); + + gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); + /* + * 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); + + edge_detector_stop(&lr->lines[i]); + ret = gpiod_direction_output(desc, val); + if (ret) + return ret; + } else if (flags & GPIO_V2_LINE_FLAG_INPUT) { + ret = gpiod_direction_input(desc); + if (ret) + return ret; + + ret = edge_detector_update(&lr->lines[i], + flags & GPIO_V2_LINE_EDGE_FLAGS, + polarity_change); + if (ret) + return ret; + } + + blocking_notifier_call_chain(&desc->gdev->notifier, + GPIO_V2_LINE_CHANGED_CONFIG, + desc); + } + return 0; +} + +static long linereq_set_config(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_config lc; + int ret; + + if (copy_from_user(&lc, ip, sizeof(lc))) + return -EFAULT; + + ret = gpio_v2_line_config_validate(&lc, lr->num_lines); + if (ret) + return ret; + + mutex_lock(&lr->config_mutex); + + ret = linereq_set_config_unlocked(lr, &lc); + + mutex_unlock(&lr->config_mutex); + + return ret; +} + static long linereq_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -807,6 +892,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return linereq_get_values(lr, ip); + else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) + return linereq_set_config(lr, ip); return -EINVAL; } @@ -968,6 +1055,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) } } + mutex_init(&lr->config_mutex); init_waitqueue_head(&lr->wait); lr->event_buffer_size = ulr.event_buffer_size; if (lr->event_buffer_size == 0) From patchwork Mon Sep 28 00:27:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285682 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=-12.8 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 8FFF6C4346E for ; Mon, 28 Sep 2020 00:30:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4DDEC206B6 for ; Mon, 28 Sep 2020 00:30:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eTfUZzLv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726469AbgI1Aaz (ORCPT ); Sun, 27 Sep 2020 20:30:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1Aaz (ORCPT ); Sun, 27 Sep 2020 20:30:55 -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 88B49C0613CE; Sun, 27 Sep 2020 17:30:55 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id 5so6798224pgf.5; Sun, 27 Sep 2020 17:30:55 -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=AW1sSBKCVsXpYEbajwLasF/VVriD8AxM28/q4ZLpQUI=; b=eTfUZzLvj52P8rwZvTiMTdHAz0V/blgrPBwyen7HBkE1rTNaVTg03UGx/h68efQ7DV 4gTlB588wGjRREj89AJnGy5aPs0fYmeTg9OpVTz8bk5AKqt+L1kyuM27/X1ArDSDrAXr rtmyL8xwHX1Ea7e+VZtyRiM9KsXoOz+TLXZ7y2BsvQBVaWuPy4q3U+uuiJqbLQyQ1as9 5jNCZlIltC2zzeWP3mzpdNhF1lcMVxrriM/dhSpeoUJfRargDAc22dyXJIzyqxwdL0OT PeHOwje0a3cpe5JxGmXadt8FWstC8JZlQ/stNGrKpHNBQKdULk7M3hs//tey6Ocm5ShM i3IA== 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=AW1sSBKCVsXpYEbajwLasF/VVriD8AxM28/q4ZLpQUI=; b=nI6JbvKF4jq17uNVmTkU4gyoITo3U3korQk+o2bXgKkLlZRYY5hWUnBQlkcFtERjS9 JohSVQqJ5okgXXqqCYDPrTENWW3ojFFk0ZBzIZmvTgz6jHLAg53z6LDugn2xUC9gz6oP rua7j+WEP0EMudEZ7jNGxAnexTqke3y9Sm3VJ3PO6XiCeI+T9Luxf9a5egvp2sH1QZxT c+RfhZYRUnusl9Vn7dos/8+UEwYulBwiRsHS9eUJl2k1ztSZxJ2X9HpY5F2oXGsaLwGb oxRuIBY4bGgSmMvkJcNPttmUbE4U7hiRPHzXve1xjRDT7U4TfTe5CbdoGtsyEb7U+5Y7 SmGg== X-Gm-Message-State: AOAM531PNbvHMhm15vlPIddTu9vhrPNnqq+5NULQRJSrq5fQGxhOeKpy vqXjGgdm0GRFdZAP8DnOdLv0rTRwpIMoaQ== X-Google-Smtp-Source: ABdhPJyuqI4mhQC3TJaLtO8Wag4fTcIPoj3L/2nrfB0fii178MWZwmzl0TwGHkq3hMfwr3HQqGe20g== X-Received: by 2002:a62:1e07:0:b029:14e:d96c:9e04 with SMTP id e7-20020a621e070000b029014ed96c9e04mr7911448pfe.58.1601253054756; Sun, 27 Sep 2020 17:30:54 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:30:54 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 11/20] gpiolib: cdev: support GPIO_V2_LINE_SET_VALUES_IOCTL Date: Mon, 28 Sep 2020 08:27:58 +0800 Message-Id: <20200928002807.12146-12-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 608cdbd1d579..25536aae3e18 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -816,6 +816,65 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return 0; } +static long linereq_set_values_unlocked(struct linereq *lr, + struct gpio_v2_line_values *lv) +{ + DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); + struct gpio_desc **descs; + unsigned int i, didx, num_set; + int ret; + + bitmap_zero(vals, GPIO_V2_LINES_MAX); + for (num_set = 0, i = 0; i < lr->num_lines; i++) { + if (lv->mask & BIT_ULL(i)) { + if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) + return -EPERM; + if (lv->bits & BIT_ULL(i)) + __set_bit(num_set, vals); + num_set++; + descs = &lr->lines[i].desc; + } + } + if (num_set == 0) + return -EINVAL; + + if (num_set != 1) { + /* build compacted desc array and values */ + descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); + if (!descs) + return -ENOMEM; + for (didx = 0, i = 0; i < lr->num_lines; i++) { + if (lv->mask & BIT_ULL(i)) { + descs[didx] = lr->lines[i].desc; + didx++; + } + } + } + ret = gpiod_set_array_value_complex(false, true, num_set, + descs, NULL, vals); + + if (num_set != 1) + kfree(descs); + return ret; +} + +static long linereq_set_values(struct linereq *lr, void __user *ip) +{ + struct gpio_v2_line_values lv; + int ret; + + if (copy_from_user(&lv, ip, sizeof(lv))) + return -EFAULT; + + mutex_lock(&lr->config_mutex); + + ret = linereq_set_values_unlocked(lr, &lv); + + mutex_unlock(&lr->config_mutex); + + return ret; +} + static long linereq_set_config_unlocked(struct linereq *lr, struct gpio_v2_line_config *lc) { @@ -892,6 +951,8 @@ static long linereq_ioctl(struct file *file, unsigned int cmd, if (cmd == GPIO_V2_LINE_GET_VALUES_IOCTL) return linereq_get_values(lr, ip); + else if (cmd == GPIO_V2_LINE_SET_VALUES_IOCTL) + return linereq_set_values(lr, ip); else if (cmd == GPIO_V2_LINE_SET_CONFIG_IOCTL) return linereq_set_config(lr, ip); From patchwork Mon Sep 28 00:27:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254323 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=-12.8 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 0316FC4346E for ; Mon, 28 Sep 2020 00:31:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D1C5206B8 for ; Mon, 28 Sep 2020 00:31:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NSg+4Lnk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726691AbgI1AbF (ORCPT ); Sun, 27 Sep 2020 20:31:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1AbE (ORCPT ); Sun, 27 Sep 2020 20:31:04 -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 CD09EC0613CE; Sun, 27 Sep 2020 17:31:04 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id d6so7747404pfn.9; Sun, 27 Sep 2020 17:31:04 -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=xfjy4YEAEexPX8kqFdfDjo/3VVBQSCZeNT9dfotqO9s=; b=NSg+4LnkgUcZuNYpZ5KFnpFuIunc6Mhhm4ZnSxW6I7xC3+JVRuMTZgY2lP4KumL+Qt 54arH18ULG5WuIs0xA3hbmmTniFB5T3Iw+LwSrr7QV395Q9OrkS83uAqn/onL4lFtO8x FlQPeR+zcM2J4i9rrpnT3PeEqEGydwQFMiPTkV1PJTUeA/uKg0aTU1GSqlVKXhXNVyVD QXD8ZYnU/0Z+DZcvmAiHRH4In4g5YiEJDZDNXKsQuI7dcxmCm146GPK3e5eQAUBhVXGt XNZplLmzM3H5VJZCdHLR0DplfIBNBNym/dpK8utBWLTPPIsBWkpEV8H2AVPIVXrwkKTI UZ6w== 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=xfjy4YEAEexPX8kqFdfDjo/3VVBQSCZeNT9dfotqO9s=; b=k72CLB8CyUQ5t+TduoQYJoK3PPwuferzWeI4X6cjy7CIMEeg06EkuSGTQfxOkMGjqd acjuvEZ5OMvQst2hS/uk7N1mMMgutHXOxdNg39CmW7ncwyEA7mTyLE/NxC8CUqBZajzG dsESYpfOTJ/OgaYhDucsFG6U1HyL1ggcJAI9dH6VDXhsZHNpxHkdzIUDKtjhqqDeO0rS 1QM3aqiWLgoIzl4JQnvoNeoYFPVaB+BuakJAQD9ES4lFBJ2Om1Ey8lULYisZtbZbH0XX 9NGp+naLkE0OMGCQ3A9fUZlfoj1UU+oR+0U7XAy+TJkqS1UOhcnJNVb+w9zc903IDFAs D+GA== X-Gm-Message-State: AOAM530gp3uolmfM9UkDd7r+8XJpxAo+ntcI9XP2UHvETo/WzCzew7yN 5YoSHvghswm8+M+EkZqsKhghTDQp0ozXNw== X-Google-Smtp-Source: ABdhPJy3OnTdO+7uDaFhsLsGMaTUATPlroO7raEo2uoHYz209a5j0IDTHs6t8jFZUjSaiUn4wX/lDw== X-Received: by 2002:a63:2e42:: with SMTP id u63mr7158142pgu.292.1601253063745; Sun, 27 Sep 2020 17:31:03 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:03 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 12/20] gpiolib: cdev: support setting debounce Date: Mon, 28 Sep 2020 08:27:59 +0800 Message-Id: <20200928002807.12146-13-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add support for setting debounce on a line via the GPIO uAPI. Where debounce is not supported by hardware, a software debounce is provided. The implementation of the software debouncer waits for the line to be stable for the debounce period before determining if a level change, and a corresponding edge event, has occurred. This provides maximum protection against glitches, but also introduces a debounce_period latency to edge events. The software debouncer is integrated with the edge detection as it utilises the line interrupt, and integration is simpler than getting the two to interwork. Where software debounce AND edge detection is required, the debouncer provides both. Signed-off-by: Kent Gibson --- Changes for v10: - as per cover letter Changes for v6: - as per cover letter Changes for v5: - as per cover letter Changes for v4: - fix handling of mask in line_get_values Changes for v3: - only GPIO_V2 field renaming Changes for v2: - improve documentation on fields shared by threads. - use READ_ONCE/WRITE_ONCE for shared fields rather than atomic_t which was overkill. drivers/gpio/gpiolib-cdev.c | 247 ++++++++++++++++++++++++++++++++++-- drivers/gpio/gpiolib.c | 3 + drivers/gpio/gpiolib.h | 4 + 3 files changed, 244 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 25536aae3e18..73386fcc252d 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include #include "gpiolib.h" @@ -415,6 +417,9 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) * events for the corresponding line request. This is drawn from the @req. * @line_seqno: the seqno for the current edge event in the sequence of * events for this line. + * @work: the worker that implements software debouncing + * @sw_debounced: flag indicating if the software debouncer is active + * @level: the current debounced physical level of the line */ struct line { struct gpio_desc *desc; @@ -431,7 +436,28 @@ struct line { */ u64 timestamp_ns; u32 req_seqno; + /* + * line_seqno is accessed by either edge_irq_thread() or + * debounce_work_func(), which are themselves mutually exclusive, + * so no additional protection is necessary. + */ u32 line_seqno; + /* + * -- debouncer specific fields -- + */ + struct delayed_work work; + /* + * sw_debounce is accessed by linereq_set_config(), which is the + * only setter, and linereq_get_values(), which can live with a + * slightly stale value. + */ + unsigned int sw_debounced; + /* + * level is accessed by debounce_work_func(), which is the only + * setter, and linereq_get_values() which can live with a slightly + * stale value. + */ + unsigned int level; }; /** @@ -572,6 +598,154 @@ static irqreturn_t edge_irq_handler(int irq, void *p) return IRQ_WAKE_THREAD; } +/* + * returns the current debounced logical value. + */ +static bool debounced_value(struct line *line) +{ + bool value; + + /* + * minor race - debouncer may be stopped here, so edge_detector_stop() + * must leave the value unchanged so the following will read the level + * from when the debouncer was last running. + */ + value = READ_ONCE(line->level); + + if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) + value = !value; + + return value; +} + +static irqreturn_t debounce_irq_handler(int irq, void *p) +{ + struct line *line = p; + + mod_delayed_work(system_wq, &line->work, + usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); + + return IRQ_HANDLED; +} + +static void debounce_work_func(struct work_struct *work) +{ + struct gpio_v2_line_event le; + struct line *line = container_of(work, struct line, work.work); + struct linereq *lr; + int level; + + level = gpiod_get_raw_value_cansleep(line->desc); + if (level < 0) { + pr_debug_ratelimited("debouncer failed to read line value\n"); + return; + } + + if (READ_ONCE(line->level) == level) + return; + + WRITE_ONCE(line->level, level); + + /* -- edge detection -- */ + if (!line->eflags) + return; + + /* switch from physical level to logical - if they differ */ + if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags)) + level = !level; + + /* ignore edges that are not being monitored */ + if (((line->eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) || + ((line->eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level)) + return; + + /* Do not leak kernel stack to userspace */ + memset(&le, 0, sizeof(le)); + + lr = line->req; + le.timestamp_ns = ktime_get_ns(); + le.offset = gpio_chip_hwgpio(line->desc); + line->line_seqno++; + le.line_seqno = line->line_seqno; + le.seqno = (lr->num_lines == 1) ? + le.line_seqno : atomic_inc_return(&lr->seqno); + + 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; + + linereq_put_event(lr, &le); +} + +static int debounce_setup(struct line *line, + unsigned int debounce_period_us) +{ + unsigned long irqflags; + int ret, level, irq; + + /* try hardware */ + ret = gpiod_set_debounce(line->desc, debounce_period_us); + if (!ret) { + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + return ret; + } + if (ret != -ENOTSUPP) + return ret; + + if (debounce_period_us) { + /* setup software debounce */ + level = gpiod_get_raw_value_cansleep(line->desc); + if (level < 0) + return level; + + irq = gpiod_to_irq(line->desc); + if (irq < 0) + return -ENXIO; + + WRITE_ONCE(line->level, level); + irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING; + ret = request_irq(irq, debounce_irq_handler, irqflags, + line->req->label, line); + if (ret) + return ret; + + WRITE_ONCE(line->sw_debounced, 1); + line->irq = irq; + } + return 0; +} + +static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned 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_DEBOUNCE) && + (lc->attrs[i].mask & mask)) + return true; + } + return false; +} + +static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, + unsigned int line_idx) +{ + unsigned 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_DEBOUNCE) && + (lc->attrs[i].mask & mask)) + return lc->attrs[i].attr.debounce_period_us; + } + return 0; +} + static void edge_detector_stop(struct line *line) { if (line->irq) { @@ -579,12 +753,18 @@ static void edge_detector_stop(struct line *line) line->irq = 0; } + cancel_delayed_work_sync(&line->work); + WRITE_ONCE(line->sw_debounced, 0); line->eflags = 0; + /* do not change line->level - see comment in debounced_value() */ } static int edge_detector_setup(struct line *line, + struct gpio_v2_line_config *lc, + unsigned int line_idx, u64 eflags) { + u32 debounce_period_us; unsigned long irqflags = 0; int irq, ret; @@ -595,8 +775,16 @@ static int edge_detector_setup(struct line *line, return ret; } line->eflags = eflags; + if (gpio_v2_line_config_debounced(lc, line_idx)) { + debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx); + ret = debounce_setup(line, debounce_period_us); + if (ret) + return ret; + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + } - if (!eflags) + /* detection disabled or sw debouncer will provide edge detection */ + if (!eflags || READ_ONCE(line->sw_debounced)) return 0; irq = gpiod_to_irq(line->desc); @@ -621,15 +809,31 @@ static int edge_detector_setup(struct line *line, return 0; } -static int edge_detector_update(struct line *line, u64 eflags, - bool polarity_change) +static int edge_detector_update(struct line *line, + struct gpio_v2_line_config *lc, + unsigned int line_idx, + u64 eflags, bool polarity_change) { - if ((line->eflags == eflags) && !polarity_change) + unsigned int debounce_period_us = + gpio_v2_line_config_debounce_period(lc, line_idx); + + if ((line->eflags == eflags) && !polarity_change && + (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) return 0; - edge_detector_stop(line); + /* sw debounced and still will be...*/ + if (debounce_period_us && READ_ONCE(line->sw_debounced)) { + line->eflags = eflags; + WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); + return 0; + } + + /* reconfiguring edge detection or sw debounce being disabled */ + if ((line->irq && !READ_ONCE(line->sw_debounced)) || + (!debounce_period_us && READ_ONCE(line->sw_debounced))) + edge_detector_stop(line); - return edge_detector_setup(line, eflags); + return edge_detector_setup(line, lc, line_idx, eflags); } static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc, @@ -727,6 +931,11 @@ static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc, ret = gpio_v2_line_flags_validate(flags); if (ret) return ret; + + /* debounce requires explicit input */ + if (gpio_v2_line_config_debounced(lc, i) && + !(flags & GPIO_V2_LINE_FLAG_INPUT)) + return -EINVAL; } return 0; } @@ -766,6 +975,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); struct gpio_desc **descs; unsigned int i, didx, num_get; + bool val; int ret; /* NOTE: It's ok to read values of output lines. */ @@ -804,7 +1014,11 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) lv.bits = 0; for (didx = 0, i = 0; i < lr->num_lines; i++) { if (lv.mask & BIT_ULL(i)) { - if (test_bit(didx, vals)) + if (lr->lines[i].sw_debounced) + val = debounced_value(&lr->lines[i]); + else + val = test_bit(didx, vals); + if (val) lv.bits |= BIT_ULL(i); didx++; } @@ -908,7 +1122,7 @@ static long linereq_set_config_unlocked(struct linereq *lr, if (ret) return ret; - ret = edge_detector_update(&lr->lines[i], + ret = edge_detector_update(&lr->lines[i], lc, i, flags & GPIO_V2_LINE_EDGE_FLAGS, polarity_change); if (ret) @@ -1102,8 +1316,11 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) lr->gdev = gdev; get_device(&gdev->dev); - for (i = 0; i < ulr.num_lines; i++) + for (i = 0; i < ulr.num_lines; i++) { lr->lines[i].req = lr; + WRITE_ONCE(lr->lines[i].sw_debounced, 0); + INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func); + } /* Make sure this is terminated */ ulr.consumer[sizeof(ulr.consumer)-1] = '\0'; @@ -1164,7 +1381,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) if (ret) goto out_free_linereq; - ret = edge_detector_setup(&lr->lines[i], + ret = edge_detector_setup(&lr->lines[i], lc, i, flags & GPIO_V2_LINE_EDGE_FLAGS); if (ret) goto out_free_linereq; @@ -1634,6 +1851,8 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, struct gpio_chip *gc = desc->gdev->chip; bool ok_for_pinctrl; unsigned long flags; + u32 debounce_period_us; + unsigned int num_attrs = 0; memset(info, 0, sizeof(*info)); info->offset = gpio_chip_hwgpio(desc); @@ -1694,6 +1913,14 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; + debounce_period_us = READ_ONCE(desc->debounce_period_us); + if (debounce_period_us) { + info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; + info->attrs[num_attrs].debounce_period_us = debounce_period_us; + num_attrs++; + } + info->num_attrs = num_attrs; + spin_unlock_irqrestore(&gpio_lock, flags); } diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index aa20481e9452..3cdf9effc13a 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -2097,6 +2097,9 @@ static bool gpiod_free_commit(struct gpio_desc *desc) clear_bit(FLAG_IS_HOGGED, &desc->flags); #ifdef CONFIG_OF_DYNAMIC desc->hog = NULL; +#endif +#ifdef CONFIG_GPIO_CDEV + WRITE_ONCE(desc->debounce_period_us, 0); #endif ret = true; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 39b356160937..b674b5bb980e 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -124,6 +124,10 @@ struct gpio_desc { #ifdef CONFIG_OF_DYNAMIC struct device_node *hog; #endif +#ifdef CONFIG_GPIO_CDEV + /* debounce period in microseconds */ + unsigned int debounce_period_us; +#endif }; int gpiod_request(struct gpio_desc *desc, const char *label); From patchwork Mon Sep 28 00:28:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285681 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=-12.8 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 BA092C4346E for ; Mon, 28 Sep 2020 00:31:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 79652206B6 for ; Mon, 28 Sep 2020 00:31:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y6XxYkB6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726604AbgI1AbP (ORCPT ); Sun, 27 Sep 2020 20:31:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1AbO (ORCPT ); Sun, 27 Sep 2020 20:31:14 -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 AF5DAC0613CE; Sun, 27 Sep 2020 17:31:14 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id s31so2531812pga.7; Sun, 27 Sep 2020 17:31:14 -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=n/qd/Q8kwQyOh0/mMVICaic8kPdt+pWRcMGx87GUK6k=; b=Y6XxYkB6nz3ZbHr0k+NO+bS1nXF751ze2MjXXsVhPV4Dezdt/mUNliDi7kk1KvZZsx eaq3qizPbmThYvT0BYbSe7X4mB3anh8crdkjYu4uR4wsOdwlO8jGYlhsEk/PWV08f/f7 QMDfG++y4TnNsXgkooYcHgsmqGlSB+KSgkKFKVHELGeTLZbnEy3znQw2SSsOzJbS2m8V Wsz+kEN764rIO8PEK+Py8k+XW1jO8s7JeJVoUZMcG/mu+9FM1qMNBzYncojrvHiJdAXO gcn1gFTLgOGmy6D0LVTG+Mf9oFGYje5ffKoQru8BObRD9MHcMe3KInZ2v6eT+B0AL4yZ MCYw== 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=n/qd/Q8kwQyOh0/mMVICaic8kPdt+pWRcMGx87GUK6k=; b=Jahnx6asySbV3GOM+xXC1otsarXy+eMq2cEAE9ZgbOtx00O/YpFY2jZ+atnCzHvnil 3GQzVFZeG3un+J0bVnWv7TCVEV/1CXkdsJlZ/nbIuuNElG/26zg38YCbSg/pLOwg07fC 4iAtL1EMqxjEysWRjl55l12k6xothVlWNs2oygOHNVIOwxdoetEJeasKy3frPQDuF+BK ewatz66pa9otuW9ERHxBaLZClMjpVLQbDuAWh9wqCOSwRArtdCJQ4B89SEK9c1dMZ2Ke JsdbObJq3icYYUy4au8rALOWOjIgQdg/PhK42mOeLcEVBBb2Rzx9Mp94ffW+HQ/s4JSQ dNDw== X-Gm-Message-State: AOAM531K2iXbNtuJKgxD3qlNCawfflUEkwYliWLk7rHvsAB/W17kWYG6 7Lig945x44GWaoSiDznXnZq/WKcHZF/DgQ== X-Google-Smtp-Source: ABdhPJyFvtVxIXuqXOLghHG8XI8NwIrEtpG1e1LiGg0OIfafRdwLOLriwYcAObE8lvG5pxZ0hY+Ddw== X-Received: by 2002:aa7:85d4:0:b029:142:4339:42ca with SMTP id z20-20020aa785d40000b0290142433942camr7989215pfn.5.1601253073818; Sun, 27 Sep 2020 17:31:13 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:13 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 13/20] gpio: uapi: document uAPI v1 as deprecated Date: Mon, 28 Sep 2020 08:28:00 +0800 Message-Id: <20200928002807.12146-14-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 5904f49399de..07865c601099 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -292,6 +292,9 @@ struct gpio_v2_line_event { /* * ABI v1 + * + * This version of the ABI is deprecated. + * Use the latest version of the ABI, defined above, instead. */ /* Informational flags */ @@ -315,6 +318,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; @@ -346,6 +352,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; @@ -385,6 +394,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]; @@ -404,6 +416,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; @@ -416,6 +431,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]; @@ -439,6 +457,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; @@ -458,6 +479,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; @@ -482,6 +506,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 Mon Sep 28 00:28:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254322 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=-12.8 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 4C5AAC4346E for ; Mon, 28 Sep 2020 00:31:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 17ECD206B6 for ; Mon, 28 Sep 2020 00:31:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Yy9gkOH+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726522AbgI1Ab2 (ORCPT ); Sun, 27 Sep 2020 20:31:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbgI1Ab2 (ORCPT ); Sun, 27 Sep 2020 20:31:28 -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 565CEC0613CE; Sun, 27 Sep 2020 17:31:28 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id k13so7782709pfg.1; Sun, 27 Sep 2020 17:31:28 -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=Ys6wqSQYuQoXYJfJn/Rp0bUM9O0PtZUuVhMhadizOSw=; b=Yy9gkOH+K0pXLd6dtmxD+1HGK/QC5Z5JL18LOqnq2QHgB+dFfMv34JS8laS3rP52jn s9Xh9fpMz+xfGrw2SEXpO8+w8wZHeiHCqEIhzuXnwEqa5RqawLhB6XR33OIhdnn/Ctdl TWUpNCV6+glqmQtoEv+HbaNrgsNd3/+RyTRRwv1FV6iznvvpTDuM90wp8H6DjnUlwXOi sZsXkyvWx2abLRHw7G4ELki8mLtC8JfPdDvDTGS14roqaliNS0ADqPA6TMRSDBN3EUbJ KOs1Dws7TUMz5xC6pNv8hUiGGObLI+hQdKzqpe3WDItTEkiRh7+0f6oD8d0WfqeBPwPm zViQ== 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=Ys6wqSQYuQoXYJfJn/Rp0bUM9O0PtZUuVhMhadizOSw=; b=pcp9cZL72br2Iiir3onRvtIPT4FcqgOfZsrBD6GoaYUDZsR9yEtl1hKGFOm5/qgmPD MpALQOjIHm+3qHwaw/wEgMj/bjE7yWoTSeKH3peJ2TIXrS//LBz8kXfGQXw4GKALS7AV G5hp0HDJsshOSKGRH1unVoN9TNDdtjZoIyxKnze43sUbjZsN8XwT//r9fh+YuOFAfmzn bSHccb3gZCpG6GgCsZk8kHw5SKQxvD2YKmpIC/1ASShu5rJPpNTW4tyojzl5scItte2l dykXHbJkE9enNiK7jX2xuCQMMfuscwsbpgiv67i0kiHM1BOpMfWlyXs9VauhCaKnw20u aGvw== X-Gm-Message-State: AOAM531f/0W4IQfruKS6EpqbvC9VvvfM3fZ8v4Udj/kYydR16Ufw38B5 MW961lN7/bQ5WYTil1hkei/f4dtBnAiqxQ== X-Google-Smtp-Source: ABdhPJxpR9TQBQUblUMBSkleBJu0ly/sskdkEh75Zoyuf3JENUT0NyhgjXTbrD6rWWSDpzCh2+71sA== X-Received: by 2002:a62:8c88:0:b029:13e:d13d:a08b with SMTP id m130-20020a628c880000b029013ed13da08bmr8824851pfd.34.1601253087516; Sun, 27 Sep 2020 17:31:27 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:26 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 14/20] tools: gpio: port lsgpio to v2 uAPI Date: Mon, 28 Sep 2020 08:28:01 +0800 Message-Id: <20200928002807.12146-15-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 Reviewed-by: Andy Shevchenko --- 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..5a05a454d0c9 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_us); + } } 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 Mon Sep 28 00:28:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285680 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=-12.8 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 965B2C4346E for ; Mon, 28 Sep 2020 00:31:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5ABFA206B8 for ; Mon, 28 Sep 2020 00:31:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aAnV3h12" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726567AbgI1Abi (ORCPT ); Sun, 27 Sep 2020 20:31:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbgI1Abh (ORCPT ); Sun, 27 Sep 2020 20:31:37 -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 AF222C0613CE; Sun, 27 Sep 2020 17:31:37 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id w7so7774721pfi.4; Sun, 27 Sep 2020 17:31:37 -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=y5UkbSjxhZvdiPNHlP+8FifgoxfV/pq/MvdPKMHbdhE=; b=aAnV3h12QXrdwTQaMRHXUp1aYSt4fjPIV+lvS3lJKmkh6qpXFqOQGUrMKyOggkUtxR iZFjzWPtok3jfKiHNbzZb0S68nZB08yeCCQ98nXnFJgTlA63h2jElx10quGP4qBMItZy agdXe4ARBZ/kKEFlp4gAxpkMsb2iuJUbvj14iPr320cSUxnnsiBCH32+jlWVd5m8PTxc 5AUewgVyfeGCazo/5rPB0ImfmI6CQhUn2puehPLe5UU1Kxqd4QNJ6uXVXG/2qzrC09JA A0fjlIJ0Sy0d3HiikYHsy1HguDcJYs/cscdTIx+Ysv8eKm9Udvd7HhW95UIgMF2cbgNM rISg== 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=y5UkbSjxhZvdiPNHlP+8FifgoxfV/pq/MvdPKMHbdhE=; b=Eu9B03XlUsf+S3jxqHsoF2AX2B+ZpQuiJcftUg0UOY4IHBdGXT7flYrIrcOGMZmC75 t96WA6UlfVTfghqGUDzsjmnbjglwbj8WaxkT4flhZGYPGoGfq78tHWPyJuMRvNAjZEbR BVvk+2StQfQI/D1sPRKHdThGQqsqzAOc7EZOnmKl51MoD2zegCsdSu0vvS/iIYi6M562 D/4KlVJgJ4pw4PMkKoDVgi9AbVkSWRjm4K3XNxWWI2Wzo0jT403FFvt+Xp8aUcUi+y/F UBZQJ3oRVhptA14iV+PYdQZ/3aedW7uKe7QcvOq4CXyJRy56sgqHyA5yqNvDJWVLN4rl k0Jw== X-Gm-Message-State: AOAM532NQj5pYmKD3aoePHbiDH9CVmrcUtrUlfEUduWpQYfFbBS6ZmjM H5vBzC+PGZ8eGv50Wsq5Rc2CfGgmJFKA5A== X-Google-Smtp-Source: ABdhPJyBvJhd8kxwRMulvBLpAmgzvRUoG1GXFLWvVLPg9aMMtg389kJHnQMkmx2cKp4Me28+Hz7FjA== X-Received: by 2002:a63:338e:: with SMTP id z136mr6939391pgz.122.1601253096894; Sun, 27 Sep 2020 17:31:36 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:36 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 15/20] tools: gpio: port gpio-watch to v2 uAPI Date: Mon, 28 Sep 2020 08:28:02 +0800 Message-Id: <20200928002807.12146-16-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the gpio-watch tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- tools/gpio/gpio-watch.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/gpio/gpio-watch.c b/tools/gpio/gpio-watch.c index 5cea24fddfa7..f229ec62301b 100644 --- a/tools/gpio/gpio-watch.c +++ b/tools/gpio/gpio-watch.c @@ -21,8 +21,8 @@ int main(int argc, char **argv) { - struct gpioline_info_changed chg; - struct gpioline_info req; + struct gpio_v2_line_info_changed chg; + struct gpio_v2_line_info req; struct pollfd pfd; int fd, i, j, ret; char *event, *end; @@ -40,11 +40,11 @@ int main(int argc, char **argv) for (i = 0, j = 2; i < argc - 2; i++, j++) { memset(&req, 0, sizeof(req)); - req.line_offset = strtoul(argv[j], &end, 0); + req.offset = strtoul(argv[j], &end, 0); if (*end != '\0') goto err_usage; - ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req); if (ret) { perror("unable to set up line watch"); return EXIT_FAILURE; @@ -71,13 +71,13 @@ int main(int argc, char **argv) } switch (chg.event_type) { - case GPIOLINE_CHANGED_REQUESTED: + case GPIO_V2_LINE_CHANGED_REQUESTED: event = "requested"; break; - case GPIOLINE_CHANGED_RELEASED: + case GPIO_V2_LINE_CHANGED_RELEASED: event = "released"; break; - case GPIOLINE_CHANGED_CONFIG: + case GPIO_V2_LINE_CHANGED_CONFIG: event = "config changed"; break; default: @@ -87,7 +87,7 @@ int main(int argc, char **argv) } printf("line %u: %s at %llu\n", - chg.info.line_offset, event, chg.timestamp); + chg.info.offset, event, chg.timestamp_ns); } } From patchwork Mon Sep 28 00:28:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254321 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=-12.8 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 3A67AC4727D for ; Mon, 28 Sep 2020 00:31:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 040B9206B6 for ; Mon, 28 Sep 2020 00:31:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zz1Kcduo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726742AbgI1Abr (ORCPT ); Sun, 27 Sep 2020 20:31:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbgI1Abr (ORCPT ); Sun, 27 Sep 2020 20:31:47 -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 9058AC0613CE; Sun, 27 Sep 2020 17:31:47 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id f18so7754810pfa.10; Sun, 27 Sep 2020 17:31:47 -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=A82gHtX9RRF6VC0sYaiYCee1g1wYkGBCPnSQAb9jHJw=; b=Zz1KcduogGEPRd9z31H3JDGsAihnoOWrP9ywzHqQ6Uie7942C9nCVo+sSyu/TWbkll cJIOxQJG7DwL2eG+PiVE/DAxvZyJ+/pTF/BC04QuFWqV+XLeZ+dmZjjRGWYqCLWU/WAV VTEzsjNo0HCDh/amNPMQHQ5GCCXZIwpfnhL5IRINW7nSXaE/ELUH059//39jK0/rroUh 34vWZjr6sw2vYJ6hqGuGkD/WVCXa5Nd9QWUUGF/tBxPd7yVdQddf6IowemRo0ALca/t1 X6uqO1HZ/CRUDl2K1f0vA8Cu0eJpb43uxTgHSN3Wz9kAe7jN9GLqlBprlNA5OPHBGac6 7Jtw== 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=A82gHtX9RRF6VC0sYaiYCee1g1wYkGBCPnSQAb9jHJw=; b=ufufAUcN7hQy4/Tde8CH4T5rOCH6mAxLKh1AKLDoCKgyb8LiaNmYd2IPOkP3gWa1ys pnRK5k9yn6CI+UrmeexaU19DBQQoits4tIWsJPjTWcO/7pM0ea1xczAsS/WrQKLgnbKU lrAJRsgt2bmZY61X65kz6PXPa/3ZBkzNJp+dbzEtKN3vZg2RPsMHRlsymVIAsk2xQHSi qFAeKDYLcoNO4GTZM4feSbY54oe9Ncb9v6z4QBSq1HdjV0Riqmtc+EN2z9R0h41oVyk1 BIhP5WgBPiblsN801dOUpfomJCdJQthGRPWnFPVVm+SCbJnlWJi6ca98BUsojnDT6bcb aQmg== X-Gm-Message-State: AOAM531L70fZAZtQ+v3O2ykBpxCz546tMQLwfuctWksrXTxGks3N9Ubf yEYmT5wOp4IkAhET5GuoVRxah7ptBWJYGQ== X-Google-Smtp-Source: ABdhPJwxqzcLeyIse+oYaLIJukNbz3t4nhWuygc2PK64k7+vFHUNMnQ9qbdxaUCf42gb21SGs9MOTw== X-Received: by 2002:a63:ed4e:: with SMTP id m14mr7102176pgk.173.1601253106676; Sun, 27 Sep 2020 17:31:46 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:46 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 16/20] tools: gpio: rename nlines to num_lines Date: Mon, 28 Sep 2020 08:28:03 +0800 Message-Id: <20200928002807.12146-17-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 Reviewed-by: Andy Shevchenko --- 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 Mon Sep 28 00:28:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285679 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=-12.8 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 00727C4346E for ; Mon, 28 Sep 2020 00:31:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A54D6206B6 for ; Mon, 28 Sep 2020 00:31:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QXIhi8u4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726588AbgI1Ab4 (ORCPT ); Sun, 27 Sep 2020 20:31:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726497AbgI1Ab4 (ORCPT ); Sun, 27 Sep 2020 20:31:56 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EEAEC0613CE; Sun, 27 Sep 2020 17:31:56 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id t14so6779056pgl.10; Sun, 27 Sep 2020 17:31: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=hryeBx4bub+kQuV02Dr/UPuOptaQtzJ+uyEhZozusLU=; b=QXIhi8u4RvCu/NL9bDi0Y1PFkb9DKuvXUHk3yu0o5im/mXpDXvP4JVVi3hhr4eU80C eviuE4xgtEtEW67rzi9eB+EzxKgHh0qL6SLAWcfHkDdMAXOjntJuebDyw4NLi3X96pdi JcW9eI4wjXn2j4i/oOGZPGGCuFGPx1BsScYFDz47lR/psS2ItH2AzUxmsJ+NWTzu2eOH 2/iHY/Lmr9YiX9A2ef7EY6DPQ+KpeB0nFLSi7IUq62HCnqVHl9oxjOsOzwYzTge36pQd fitKVT6jIdGBXJTz3mMb5WiUKgYlpvZXhbOuNwvTjDZ2P92nxRpoizqAI/bu7/9J/mUs qW6A== 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=hryeBx4bub+kQuV02Dr/UPuOptaQtzJ+uyEhZozusLU=; b=rt8aQ9hU0hCEbwYJ8MAlyLKDdXnQR70BQdQofih+sKsg/4kbBFQbMUF9r3Qc0Sja1n TXThMBHIUAsmsE2WLHUwIojRzHBxAFcHW00A0LV3CyZ36SD9/JAtQDVg6gPzHKWNlfYM 6n8HLmed9znLPh7o8lYJP6qJcVmGkJ9ADq8ErsKgku5/vBtVxr7W6aE6e8zZr1a5WKxk H3tuWiEAKK4Zvi8JW9PDX0WfOaeMr5VP17bfFbdxtiGEply0BJadY0kCblMWUco0tFDG 2Bn38tXyuQfnRhWJojMSXplPZlo4tDseO8uXn3eqKT5onAGbrxnNlMLM+dr/Lapx5gzz kfRw== X-Gm-Message-State: AOAM533rDdHXuuDXV9JO77mGIIdhRkta6t6/0c4+bAEacrhNdxDTa7XQ Su3X/7/MA+HHBb1EzJv+ZJyio5rJl/XkpQ== X-Google-Smtp-Source: ABdhPJxdXTylwN2AfAuXlMKsFWjTRZbaepKyMrtuNaSmHa8BoQsXwZhuXEVs4KRQNqDeUcx7M5D23g== X-Received: by 2002:a62:d44e:0:b029:13c:1611:652f with SMTP id u14-20020a62d44e0000b029013c1611652fmr8686904pfl.15.1601253114973; Sun, 27 Sep 2020 17:31:54 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:31:54 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 17/20] tools: gpio: port gpio-hammer to v2 uAPI Date: Mon, 28 Sep 2020 08:28:04 +0800 Message-Id: <20200928002807.12146-18-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Port the gpio-hammer tool to the latest GPIO uAPI. Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- tools/gpio/gpio-hammer.c | 32 +++++--- tools/gpio/gpio-utils.c | 164 ++++++++++++++++++++++++++++++++------- tools/gpio/gpio-utils.h | 46 ++++++++++- 3 files changed, 197 insertions(+), 45 deletions(-) diff --git a/tools/gpio/gpio-hammer.c b/tools/gpio/gpio-hammer.c index a2c7577fad5c..54fdf59dd320 100644 --- a/tools/gpio/gpio-hammer.c +++ b/tools/gpio/gpio-hammer.c @@ -25,23 +25,30 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, unsigned int loops) { - struct gpiohandle_data data; + struct gpio_v2_line_values values; + struct gpio_v2_line_config config; char swirr[] = "-\\|/"; int fd; int ret; int i, j; unsigned int iteration = 0; - memset(&data.values, 0, sizeof(data.values)); - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_OUTPUT, &data, - "gpio-hammer"); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_OUTPUT; + + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, "gpio-hammer"); if (ret < 0) goto exit_error; else fd = ret; - ret = gpiotools_get_values(fd, &data); + values.mask = 0; + values.bits = 0; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&values.mask, i); + + ret = gpiotools_get_values(fd, &values); if (ret < 0) goto exit_close_error; @@ -53,7 +60,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, } fprintf(stdout, "] on %s, initial states: [", device_name); for (i = 0; i < num_lines; i++) { - fprintf(stdout, "%d", data.values[i]); + fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i)); if (i != (num_lines - 1)) fprintf(stdout, ", "); } @@ -64,14 +71,14 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, while (1) { /* Invert all lines so we blink */ for (i = 0; i < num_lines; i++) - data.values[i] = !data.values[i]; + gpiotools_change_bit(&values.bits, i); - ret = gpiotools_set_values(fd, &data); + ret = gpiotools_set_values(fd, &values); if (ret < 0) goto exit_close_error; /* Re-read values to get status */ - ret = gpiotools_get_values(fd, &data); + ret = gpiotools_get_values(fd, &values); if (ret < 0) goto exit_close_error; @@ -82,7 +89,8 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, fprintf(stdout, "["); for (i = 0; i < num_lines; i++) { - fprintf(stdout, "%d: %d", lines[i], data.values[i]); + fprintf(stdout, "%d: %d", lines[i], + gpiotools_test_bit(values.bits, i)); if (i != (num_lines - 1)) fprintf(stdout, ", "); } @@ -97,7 +105,7 @@ int hammer_device(const char *device_name, unsigned int *lines, int num_lines, ret = 0; exit_close_error: - gpiotools_release_linehandle(fd); + gpiotools_release_line(fd); exit_error: return ret; } diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c index d527980bcb94..37187e056c8b 100644 --- a/tools/gpio/gpio-utils.c +++ b/tools/gpio/gpio-utils.c @@ -100,20 +100,87 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, free(chrdev_name); return ret < 0 ? ret : req.fd; } + +/** + * gpiotools_request_line() - request gpio lines in a gpiochip + * @device_name: The name of gpiochip without prefix "/dev/", + * such as "gpiochip0" + * @lines: An array desired lines, specified by offset + * index for the associated GPIO device. + * @num_lines: The number of lines to request. + * @config: The new config for requested gpio. Reference + * "linux/gpio.h" for config details. + * @consumer: The name of consumer, such as "sysfs", + * "powerkey". This is useful for other users to + * know who is using. + * + * Request gpio lines through the ioctl provided by chardev. User + * could call gpiotools_set_values() and gpiotools_get_values() to + * read and write respectively through the returned fd. Call + * gpiotools_release_line() to release these lines after that. + * + * Return: On success return the fd; + * On failure return the errno. + */ +int gpiotools_request_line(const char *device_name, unsigned int *lines, + unsigned int num_lines, + struct gpio_v2_line_config *config, + const char *consumer) +{ + struct gpio_v2_line_request req; + char *chrdev_name; + int fd; + int i; + int ret; + + ret = asprintf(&chrdev_name, "/dev/%s", device_name); + if (ret < 0) + return -ENOMEM; + + fd = open(chrdev_name, 0); + if (fd == -1) { + ret = -errno; + fprintf(stderr, "Failed to open %s, %s\n", + chrdev_name, strerror(errno)); + goto exit_free_name; + } + + memset(&req, 0, sizeof(req)); + for (i = 0; i < num_lines; i++) + req.offsets[i] = lines[i]; + + req.config = *config; + strcpy(req.consumer, consumer); + req.num_lines = num_lines; + + ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); + if (ret == -1) { + ret = -errno; + fprintf(stderr, "Failed to issue %s (%d), %s\n", + "GPIO_GET_LINE_IOCTL", ret, strerror(errno)); + } + + if (close(fd) == -1) + perror("Failed to close GPIO character device file"); +exit_free_name: + free(chrdev_name); + return ret < 0 ? ret : req.fd; +} + /** * gpiotools_set_values(): Set the value of gpio(s) * @fd: The fd returned by - * gpiotools_request_linehandle(). - * @data: The array of values want to set. + * gpiotools_request_line(). + * @values: The array of values want to set. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_set_values(const int fd, struct gpiohandle_data *data) +int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values) { int ret; - ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data); + ret = ioctl(fd, GPIO_V2_LINE_SET_VALUES_IOCTL, values); if (ret == -1) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n", @@ -127,17 +194,17 @@ int gpiotools_set_values(const int fd, struct gpiohandle_data *data) /** * gpiotools_get_values(): Get the value of gpio(s) * @fd: The fd returned by - * gpiotools_request_linehandle(). - * @data: The array of values get from hardware. + * gpiotools_request_line(). + * @values: The array of values get from hardware. * * Return: On success return 0; * On failure return the errno. */ -int gpiotools_get_values(const int fd, struct gpiohandle_data *data) +int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values) { int ret; - ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data); + ret = ioctl(fd, GPIO_V2_LINE_GET_VALUES_IOCTL, values); if (ret == -1) { ret = -errno; fprintf(stderr, "Failed to issue %s (%d), %s\n", @@ -169,6 +236,27 @@ int gpiotools_release_linehandle(const int fd) return ret; } +/** + * gpiotools_release_line(): Release the line(s) of gpiochip + * @fd: The fd returned by + * gpiotools_request_line(). + * + * Return: On success return 0; + * On failure return the errno. + */ +int gpiotools_release_line(const int fd) +{ + int ret; + + ret = close(fd); + if (ret == -1) { + perror("Failed to close GPIO LINE device file"); + ret = -errno; + } + + return ret; +} + /** * gpiotools_get(): Get value from specific line * @device_name: The name of gpiochip without prefix "/dev/", @@ -180,11 +268,14 @@ int gpiotools_release_linehandle(const int fd) */ int gpiotools_get(const char *device_name, unsigned int line) { - struct gpiohandle_data data; + int ret; + unsigned int value; unsigned int lines[] = {line}; - gpiotools_gets(device_name, lines, 1, &data); - return data.values[0]; + ret = gpiotools_gets(device_name, lines, 1, &value); + if (ret) + return ret; + return value; } @@ -195,27 +286,35 @@ int gpiotools_get(const char *device_name, unsigned int line) * @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. - * @data: The array of values get from gpiochip. + * @values: 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 num_lines, struct gpiohandle_data *data) + unsigned int num_lines, unsigned int *values) { - int fd; + int fd, i; int ret; int ret_close; + struct gpio_v2_line_config config; + struct gpio_v2_line_values lv; - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_INPUT, data, - CONSUMER); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_INPUT; + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, CONSUMER); if (ret < 0) return ret; fd = ret; - ret = gpiotools_get_values(fd, data); - ret_close = gpiotools_release_linehandle(fd); + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&lv.mask, i); + ret = gpiotools_get_values(fd, &lv); + if (!ret) + for (i = 0; i < num_lines; i++) + values[i] = gpiotools_test_bit(lv.bits, i); + ret_close = gpiotools_release_line(fd); return ret < 0 ? ret : ret_close; } @@ -232,11 +331,9 @@ int gpiotools_gets(const char *device_name, unsigned int *lines, int gpiotools_set(const char *device_name, unsigned int line, unsigned int value) { - struct gpiohandle_data data; unsigned int lines[] = {line}; - data.values[0] = value; - return gpiotools_sets(device_name, lines, 1, &data); + return gpiotools_sets(device_name, lines, 1, &value); } /** @@ -246,22 +343,31 @@ int gpiotools_set(const char *device_name, unsigned int line, * @lines: An array desired lines, specified by offset * index for the associated GPIO device. * @num_lines: The number of lines to request. - * @data: The array of values set to gpiochip, must be + * @value: The array of values set to gpiochip, must be * 0(low) or 1(high). * * Return: On success return 0; * On failure return the errno. */ int gpiotools_sets(const char *device_name, unsigned int *lines, - unsigned int num_lines, struct gpiohandle_data *data) + unsigned int num_lines, unsigned int *values) { - int ret; + int ret, i; + struct gpio_v2_line_config config; - ret = gpiotools_request_linehandle(device_name, lines, num_lines, - GPIOHANDLE_REQUEST_OUTPUT, data, - CONSUMER); + memset(&config, 0, sizeof(config)); + config.flags = GPIO_V2_LINE_FLAG_OUTPUT; + config.num_attrs = 1; + config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES; + for (i = 0; i < num_lines; i++) { + gpiotools_set_bit(&config.attrs[0].mask, i); + gpiotools_assign_bit(&config.attrs[0].attr.values, + i, values[i]); + } + ret = gpiotools_request_line(device_name, lines, num_lines, + &config, CONSUMER); if (ret < 0) return ret; - return gpiotools_release_linehandle(ret); + return gpiotools_release_line(ret); } diff --git a/tools/gpio/gpio-utils.h b/tools/gpio/gpio-utils.h index 324729577865..6c69a9f1c253 100644 --- a/tools/gpio/gpio-utils.h +++ b/tools/gpio/gpio-utils.h @@ -12,7 +12,9 @@ #ifndef _GPIO_UTILS_H_ #define _GPIO_UTILS_H_ +#include #include +#include #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) @@ -26,16 +28,52 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines, 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); -int gpiotools_get_values(const int fd, struct gpiohandle_data *data); int gpiotools_release_linehandle(const int fd); +int gpiotools_request_line(const char *device_name, + unsigned int *lines, + unsigned int num_lines, + struct gpio_v2_line_config *config, + const char *consumer); +int gpiotools_set_values(const int fd, struct gpio_v2_line_values *values); +int gpiotools_get_values(const int fd, struct gpio_v2_line_values *values); +int gpiotools_release_line(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 num_lines, struct gpiohandle_data *data); + unsigned int num_lines, unsigned int *values); 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 num_lines, struct gpiohandle_data *data); + unsigned int num_lines, unsigned int *values); + +/* helper functions for gpio_v2_line_values bits */ +static inline void gpiotools_set_bit(__u64 *b, int n) +{ + *b |= _BITULL(n); +} + +static inline void gpiotools_change_bit(__u64 *b, int n) +{ + *b ^= _BITULL(n); +} + +static inline void gpiotools_clear_bit(__u64 *b, int n) +{ + *b &= ~_BITULL(n); +} + +static inline int gpiotools_test_bit(__u64 b, int n) +{ + return !!(b & _BITULL(n)); +} + +static inline void gpiotools_assign_bit(__u64 *b, int n, bool value) +{ + if (value) + gpiotools_set_bit(b, n); + else + gpiotools_clear_bit(b, n); +} #endif /* _GPIO_UTILS_H_ */ From patchwork Mon Sep 28 00:28:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254320 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=-12.8 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 1A15AC4346E for ; Mon, 28 Sep 2020 00:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5323206B6 for ; Mon, 28 Sep 2020 00:32:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QXLjzW44" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726460AbgI1AcE (ORCPT ); Sun, 27 Sep 2020 20:32:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbgI1AcE (ORCPT ); Sun, 27 Sep 2020 20:32:04 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17813C0613CE; Sun, 27 Sep 2020 17:32:04 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id b124so7735082pfg.13; Sun, 27 Sep 2020 17:32:04 -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=6GGRqqsz5wRVNByVt8NrnlXsZyXGJptYx7CCapwCwqk=; b=QXLjzW44CSWRKrbWbrzCAoMNG00RHtYJmSKeBQougut9ZoQZskxR3fSt2D+eE1ObKN BFpnglTL0mLcHugfgs2xrKWeyd3AAYMJP/m3lhziEiQAXSb+q5f1ukvQsMLetWkwNFaJ IOVJiPo+7yBYqSCaQFgH/a5lJgwZTSOA5NAxofANwFB90E7nlqpwQwnrHmYu3Yy+Njv4 5rXCWW7LRHap9+4iJThtC/lcdxzbSD6NrlYECWTVgFhUNr7s1w8t8NF4TH4xT4/YKC+R k3YuksdSUB/ca6n80WgrgFyHR+adBGvrFYXzIF+JBl8WSyrKkBKQbMljOwI1OGnr4IUF JwJQ== 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=6GGRqqsz5wRVNByVt8NrnlXsZyXGJptYx7CCapwCwqk=; b=J235b6KpafBPHjY+ibsLmU9qTch+dNxbxuFSYvlGFRC23e5xnim9BZLbBjjhF4BArM N1iFHzX9eSRaW5qPDCRRz16IfyRC6Yv3U0bfowXHKdEvBT4IMxg2w+hUVO2A4GIM+yUX Ix+soYtWMZJE+1jPdZ3z/CHD1GGMtZ/OD90lUAinJehePOTiNzWuNxjCvfS1a2hXEWRb v/sUYkabD3V/gF4InUbdrJlHaW5qjO96V6GMULYjyJgGPy/XiIObbJAiSXOTFU8uqPM1 kQs16+tyLUAZL5syxZmTx5eXVOzfBI343gQP0/EUgY5KXjT7hNb0UDHbUDRT7l59QZOz ElJA== X-Gm-Message-State: AOAM530+GBQwHQQWOqD956hbJlWiEdienr4pHsn/s2I/FzDf1A89dYvo HHxcmaIhY2vS7qeIhcaYl5FJHx384hADyw== X-Google-Smtp-Source: ABdhPJwpp1eCjFYtehK29ADKx9PyuYbajlYYBlZLxCUPHMshi1joBMeGbF6UIEBX++Axe/DjKohRTw== X-Received: by 2002:a63:4c46:: with SMTP id m6mr6936216pgl.127.1601253123233; Sun, 27 Sep 2020 17:32:03 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.31.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:32:02 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 18/20] tools: gpio: port gpio-event-mon to v2 uAPI Date: Mon, 28 Sep 2020 08:28:05 +0800 Message-Id: <20200928002807.12146-19-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 Reviewed-by: Andy Shevchenko --- 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..b230af889f26 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_ns, 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 Mon Sep 28 00:28:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 285678 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=-12.8 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 7A3A0C4346E for ; Mon, 28 Sep 2020 00:32:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 39824206B6 for ; Mon, 28 Sep 2020 00:32:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="vYVt7WrL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726421AbgI1AcN (ORCPT ); Sun, 27 Sep 2020 20:32:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbgI1AcN (ORCPT ); Sun, 27 Sep 2020 20:32:13 -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 3B0FCC0613CE; Sun, 27 Sep 2020 17:32:13 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id v14so2566124pjd.4; Sun, 27 Sep 2020 17:32:13 -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=/D6x+D3oSJHjRsNieXXDsh9in/3vu2HgqJk6KbWYET0=; b=vYVt7WrL8ugS0hXQzoRptnGiLc6GUI1hCnyMRvmZbq+ynzRPw16kwD9Rd7C6uY9TCA 7DVr9TyzWiK9FnchETicqBCxB17cAlwVatv9VSAXfPYNZj3ckc34QOL3s/UbIOf0Ze2G /MVWIwpNunuKQ4JHgQjAJSLxyUXN/KLtPhDCivwg6qEhff1A+Z0/q7hlVRxtCGlPX5Fl jQIGUibN9Bp61S+b5QxjEfcXYyImCsMNhB4p3fNinerV8iN+KMjm6mWRWp/mNmHs9H4s 1aRMqy6QjN0j09VYVmsFoRCUurmQdZnhuEAKo++h6CpeU0YGuM5hyZ6fvygDQ4WqWItf iSKA== 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=/D6x+D3oSJHjRsNieXXDsh9in/3vu2HgqJk6KbWYET0=; b=rqRHHt0AeY5YBdGHChOR4uNCyJwHe6MmmCAD+3SjDVravKZujoELBrHL4ospjSn4nK p9lEnudLYMZyVA0HCabuB26PY7KWQ10jXvSBVUMHzzibP1mAkN6PBCF8ZY2b80K5Czl0 Gg3y8ugLdcIWToHgEhJmHaUznXGxzf03hMzXoue9NurB6lRtueFOo41BIdnFmjIcBica P30Yn28X0rU5+6KDvqque8TPbgE/bdMok8P+jjo86AqaUOr1EOV75vyGOBw0MVXy7Jbh JPrrEACD4QzcvXqAjkUbMnpT9iR5ibcEhFAZ7jtptSfJL1XvTgsZhP+ka10BxRO5eZMm 6Plw== X-Gm-Message-State: AOAM5317fLshIlXc6ROLb2mxv8U9JmehnfTTMxpnXyR1568hz3/WdXve 9inry6dMD61WcDY0fe0Dzm4oUKmGgWxcRg== X-Google-Smtp-Source: ABdhPJzQxqGYrTiTfpjpvQAgrbGXF9NN/kXPv3TIzR4jkJuGwHLNsHBYln+vLSNBMKZ2JXigYiw62g== X-Received: by 2002:a17:90b:693:: with SMTP id m19mr6932328pjz.111.1601253132396; Sun, 27 Sep 2020 17:32:12 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.32.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:32:11 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 19/20] tools: gpio: add multi-line monitoring to gpio-event-mon Date: Mon, 28 Sep 2020 08:28:06 +0800 Message-Id: <20200928002807.12146-20-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Extend gpio-event-mon to support monitoring multiple lines. This would require multiple lineevent requests to implement using uAPI v1, but can be performed with a single line request using uAPI v2. Signed-off-by: Kent Gibson Reviewed-by: Andy Shevchenko --- tools/gpio/gpio-event-mon.c | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/tools/gpio/gpio-event-mon.c b/tools/gpio/gpio-event-mon.c index b230af889f26..0c34f18f511c 100644 --- a/tools/gpio/gpio-event-mon.c +++ b/tools/gpio/gpio-event-mon.c @@ -26,7 +26,8 @@ #include "gpio-utils.h" int monitor_device(const char *device_name, - unsigned int line, + unsigned int *lines, + unsigned int num_lines, struct gpio_v2_line_config *config, unsigned int loops) { @@ -47,7 +48,7 @@ int monitor_device(const char *device_name, goto exit_free_name; } - ret = gpiotools_request_line(device_name, &line, 1, config, + ret = gpiotools_request_line(device_name, lines, num_lines, config, "gpio-event-mon"); if (ret < 0) goto exit_device_close; @@ -55,8 +56,10 @@ int monitor_device(const char *device_name, lfd = ret; /* Read initial states */ - values.mask = 1; + values.mask = 0; values.bits = 0; + for (i = 0; i < num_lines; i++) + gpiotools_set_bit(&values.mask, i); ret = gpiotools_get_values(lfd, &values); if (ret < 0) { fprintf(stderr, @@ -65,9 +68,23 @@ int monitor_device(const char *device_name, goto exit_line_close; } - fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); - fprintf(stdout, "Initial line value: %d\n", - gpiotools_test_bit(values.bits, 0)); + if (num_lines == 1) { + fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name); + fprintf(stdout, "Initial line value: %d\n", + gpiotools_test_bit(values.bits, 0)); + } else { + fprintf(stdout, "Monitoring lines %d", lines[0]); + for (i = 1; i < num_lines - 1; i++) + fprintf(stdout, ", %d", lines[i]); + fprintf(stdout, " and %d on %s\n", lines[i], device_name); + fprintf(stdout, "Initial line values: %d", + gpiotools_test_bit(values.bits, 0)); + for (i = 1; i < num_lines - 1; i++) + fprintf(stdout, ", %d", + gpiotools_test_bit(values.bits, i)); + fprintf(stdout, " and %d\n", + gpiotools_test_bit(values.bits, i)); + } while (1) { struct gpio_v2_line_event event; @@ -126,7 +143,7 @@ void print_usage(void) fprintf(stderr, "Usage: gpio-event-mon [options]...\n" "Listen to events on GPIO lines, 0->1 1->0\n" " -n Listen on GPIOs on a named device (must be stated)\n" - " -o Offset to monitor\n" + " -o Offset of line to monitor (may be repeated)\n" " -d Set line as open drain\n" " -s Set line as open source\n" " -r Listen for rising edges\n" @@ -146,7 +163,8 @@ void print_usage(void) int main(int argc, char **argv) { const char *device_name = NULL; - unsigned int line = -1; + unsigned int lines[GPIO_V2_LINES_MAX]; + unsigned int num_lines = 0; unsigned int loops = 0; struct gpio_v2_line_config config; int c; @@ -162,7 +180,12 @@ int main(int argc, char **argv) device_name = optarg; break; case 'o': - line = strtoul(optarg, NULL, 10); + if (num_lines >= GPIO_V2_LINES_MAX) { + print_usage(); + return -1; + } + lines[num_lines] = strtoul(optarg, NULL, 10); + num_lines++; break; case 'd': config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; @@ -182,7 +205,7 @@ int main(int argc, char **argv) } } - if (!device_name || line == -1) { + if (!device_name || num_lines == 0) { print_usage(); return -1; } @@ -191,5 +214,5 @@ int main(int argc, char **argv) "falling edges\n"); config.flags |= EDGE_FLAGS; } - return monitor_device(device_name, line, &config, loops); + return monitor_device(device_name, lines, num_lines, &config, loops); } From patchwork Mon Sep 28 00:28:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 254319 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=-12.8 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 2B047C4346E for ; Mon, 28 Sep 2020 00:32:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E320D206B8 for ; Mon, 28 Sep 2020 00:32:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aB+u4b0I" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726600AbgI1AcX (ORCPT ); Sun, 27 Sep 2020 20:32:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbgI1AcX (ORCPT ); Sun, 27 Sep 2020 20:32:23 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3123DC0613CE; Sun, 27 Sep 2020 17:32:23 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id v14so2566255pjd.4; Sun, 27 Sep 2020 17:32:23 -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=orI2XdPNMcc9fk0dBNQocsoJ4tDxFbu6pi0+98Ky68Y=; b=aB+u4b0IizAbMXWGxnjGw9NoznuYjPDynjcZQl0ZJHjEL7lAag84A06BYgIcc0wNIW ThGb97ZyS1I7iQXhO3IY82dT+R3+N+lAabZWELHasKPtFlJRPB9tNePCHcTBQ3WAQduX f+V31uqJ87oufgIGDB0FAr8rSSWnWdgiq4uGDqCwQW+k9YlKSusPlED2CogQFhCypShv sxNtXS8k7m6Y0zIdTuNSmPPFdHMFX0Ri3+/CCIlMi5OPsqTmm528MgXbfeMXYLqd2Em6 Nd+MYo0X2GzeQA9WOziWhp6z97BrdvryCDg4JjOWfywjVdYvjc9200sCaa36AVLM1y7Z u8dw== 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=orI2XdPNMcc9fk0dBNQocsoJ4tDxFbu6pi0+98Ky68Y=; b=OXWjbqhkhMilNZHxOCrYkbiFdpkvic1XMDd8TxOC60rwPSQRLEYPoDhyCaEIhq8y1I Nhj2l6v4Acx/jPgdeE3bsQG8S3jIVlnz3GO7Ki+f4JnHPuwOx4c6x8cu4xpAUL+BAJ71 ymbFSEZBHwdy9xa++mgq9MVVH4GHsMRN+3Rt6LLfn4+vdJuGIFppL5VaojCmK139A5mL PMGCg8acxbCrAOCKGpSqXkFHhJghPFxxUbZYit8lE8g/x1kms2QZku9YM6Gh1G4XvIjQ VQm58dGSuUpV1ZuLfGY31yFoC6QGAtfCEM/SMM7YKO/1j8KDYrjRycgMVIdP33iuUn5d DKaw== X-Gm-Message-State: AOAM530UKtHCZETYdXVTnKSpqV/veXWKidjkOQGYUwxCzEaM8Zz341Oj 95JZeTcNZFpDE8kgbxM+fjKGWB7F4xVorg== X-Google-Smtp-Source: ABdhPJzo94Q6KL67/oOAwVreZ2P/nxuMceGKv0M5Amc3y3deBIrdJJDlZHMPfsAYh1ak/G2SPz9Cgg== X-Received: by 2002:a17:90b:889:: with SMTP id bj9mr7215964pjb.101.1601253142317; Sun, 27 Sep 2020 17:32:22 -0700 (PDT) Received: from sol.lan (106-69-171-132.dyn.iinet.net.au. [106.69.171.132]) by smtp.gmail.com with ESMTPSA id o20sm8443783pgh.63.2020.09.27.17.32.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 17:32:21 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com, arnd@arndb.de Cc: Kent Gibson Subject: [PATCH v10 20/20] tools: gpio: add debounce support to gpio-event-mon Date: Mon, 28 Sep 2020 08:28:07 +0800 Message-Id: <20200928002807.12146-21-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928002807.12146-1-warthog618@gmail.com> References: <20200928002807.12146-1-warthog618@gmail.com> MIME-Version: 1.0 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 Reviewed-by: Andy Shevchenko --- 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 0c34f18f511c..90c3155f05b1 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_us = 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_us = 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_us) { + 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_us = debounce_period_us; + } + if (!device_name || num_lines == 0) { print_usage(); return -1;