From patchwork Thu Dec 19 17:15:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213564 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="ZJkxoUwb"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6N1YgGz9sPh for ; Fri, 20 Dec 2019 04:15:39 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726963AbfLSRPi (ORCPT ); Thu, 19 Dec 2019 12:15:38 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:55621 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726858AbfLSRPh (ORCPT ); Thu, 19 Dec 2019 12:15:37 -0500 Received: by mail-wm1-f67.google.com with SMTP id q9so6241603wmj.5 for ; Thu, 19 Dec 2019 09:15:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3OFnQ2ohkFOq1yFTbDsXqXVWgfUM3/3VsiEYW/xfV9E=; b=ZJkxoUwb4DVcfMAileD3Rrt4x+EKOgSLdMnpFC1TEp6dU/JTboLmO7y4DuO85qIzmi VEFQbLqdzuO7KfCN4KG1ONhGaXoEwmWmUNfy1bSBFJaAZcHCIAOzx1Vzdk7/W5CquaXS Zq/6bIUxmH5KsvpZdoxvMiO8zHrcP+DRQDGQFYUXnIfsSKgjeJskckgeEdKakLql5e0v nU93JZ3aaSBt7b3PF0rpIQYEoy7aGtfoF3jx4r1bVrrKi5QS0qZr+NHQC5fu7dZOQpj6 YLr9juvDhwUb+Ul0uUAH8uzK1U2LnShS7wtPI/h8P0IdEsncXnOLN7VRKJcUd1o/M9Sw VWLw== 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=3OFnQ2ohkFOq1yFTbDsXqXVWgfUM3/3VsiEYW/xfV9E=; b=TO9ZyCdWHLUlbUQkgJcG2FEHKi9/a5k7S0MhQJjUrVTVcDcOkmXTOnd3tDWgYHG67y 0G1GnDAuOR6JPUKWWjFw6KKeiDXpWDG4aPJBc7x/rnoLijdKfrVXOsN/l6VIQn2bf2Ul AAEUm7486nO5dDL/EL9bFYPv230Jg+WTAIA4WKQbSgTgZzdYHYwJ8UCJh8f9XMzb1ayf 0np6ViO0PcoGneBtoHLnxx7sKY247bGy9CiaWXBn3VXHkeI/zahVLHU17gTfp5HqrBBv tiubRsOAPxommZgOBcHqfnbTiHE91i5nv/TrVdPS58IjkDzXsMovEtIEpnChvekfLXtT TtdQ== X-Gm-Message-State: APjAAAX6aNFDayjjiDP/rg6ZyIslOkN1jk776Ly3ouebPSJidUZhU0WC Jz9VE6MulCEKth40NCiqmZZAmQ== X-Google-Smtp-Source: APXvYqx9BtOSw9b/9nstUr0iQlAvNQJd9cEDr2v+n60w9iwCiyxGVsOgz2izpl/1jAsmTzYtP3PDXA== X-Received: by 2002:a7b:c956:: with SMTP id i22mr11184767wml.67.1576775736179; Thu, 19 Dec 2019 09:15:36 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:35 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 01/13] gpiolib: use 'unsigned int' instead of 'unsigned' in gpio_set_config() Date: Thu, 19 Dec 2019 18:15:16 +0100 Message-Id: <20191219171528.6348-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Checkpatch complains about using 'unsigned' instead of 'unsigned int'. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 21b02a0064f8..a31797fe78fa 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -3042,7 +3042,7 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); * rely on gpio_request() having been called beforehand. */ -static int gpio_set_config(struct gpio_chip *gc, unsigned offset, +static int gpio_set_config(struct gpio_chip *gc, unsigned int offset, enum pin_config_param mode) { unsigned long config; From patchwork Thu Dec 19 17:15:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213575 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="fJGiH31V"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz7F6BmHz9sPc for ; Fri, 20 Dec 2019 04:16:25 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726982AbfLSRPj (ORCPT ); Thu, 19 Dec 2019 12:15:39 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:38120 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726933AbfLSRPj (ORCPT ); Thu, 19 Dec 2019 12:15:39 -0500 Received: by mail-wm1-f67.google.com with SMTP id u2so6446088wmc.3 for ; Thu, 19 Dec 2019 09:15:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=evGr6fE++6g6GFFfppzZpmdFKXc03gQbrsBnrRpXkt4=; b=fJGiH31Vt13rJnJqTXwPwU+EtINEb85MW7y7grTb0ZaBou/agTnZVxjXD2Cypieuxu LJ/eLk+xT+uYj1+JMTShUVFgXKOYPzx7PvysYSqC2NMnQTBRmkODTp3536+Cas6F385z akTEC/muN+Ftaw2mIcXd7wRET1KlWQJ4RunrYHpyGMBNsRTnbLTYzbnidXQrJJIfWsVx ppN9h5wYG4eKW3kA1Ecx37f6ggPWqdhbD6W8rLoejke5CaXxjP318dSfH+KSsmD8+HoD jDcbsG9Adnhi9Ofr3bkZyek/WiqGrXI39CnMFqtM4jZpZYAK+nhqzz0lzvKcSuiqoCop mf3Q== 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=evGr6fE++6g6GFFfppzZpmdFKXc03gQbrsBnrRpXkt4=; b=pXcZfrvud33WuFwHRx5kdO4UwfuzAPbK9DTimz2TzVl9I2h69Y2Le4rXsWHBNwMCoC rmFuYLYKmWkBHkyBPLOu0uoetQABnMmqV2O4rtVWYO7Sc2J+2LCg2C6xNI+wlQmRO/vE JiYs1EzVgH8p9fAmpeZqiSVoFUA3wYEWr6hg43i0E44nJL30tPQYUXoe6DaSvgfDN/Dq yabeYRHXlqtQ8A4QcK5wMS/8Vv/kD18VctiQsDDECrxt1+T32yrNKUwtzmKB/ePA1nP0 qK6RKDAoA+V++Tm2wF3K2eAbCSfapvRUR8JCb6vDMPhu4K0G0csSXNas0+R1ucaXkm+F mNGQ== X-Gm-Message-State: APjAAAV7+CLey5JT8ohFZMHyynRUgK8ecwYUC5pGgiyoO6rvO2G+jjRA SBJhVUPCrmuobvlPoC90BkjJLQ== X-Google-Smtp-Source: APXvYqxS6MMzWyPCAcmckrozGtdlIRco9JhBrCYeknCV2Mx6N+bMCxx10LvlimrWqswtG2UTKJaozw== X-Received: by 2002:a05:600c:2148:: with SMTP id v8mr11810672wml.111.1576775737398; Thu, 19 Dec 2019 09:15:37 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:36 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 02/13] gpiolib: have a single place of calling set_config() Date: Thu, 19 Dec 2019 18:15:17 +0100 Message-Id: <20191219171528.6348-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Instead of calling the gpiochip's set_config() callback directly and checking its existence every time - just add a new routine that performs this check internally. Call it in gpio_set_config() and gpiod_set_transitory(). Also call it in gpiod_set_debounce() and drop the check for chip->set() as it's irrelevant to this config option. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index a31797fe78fa..72211407469f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -3042,6 +3042,15 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); * rely on gpio_request() having been called beforehand. */ +static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, + enum pin_config_param mode) +{ + if (!gc->set_config) + return -ENOTSUPP; + + return gc->set_config(gc, offset, mode); +} + static int gpio_set_config(struct gpio_chip *gc, unsigned int offset, enum pin_config_param mode) { @@ -3060,7 +3069,7 @@ static int gpio_set_config(struct gpio_chip *gc, unsigned int offset, } config = PIN_CONF_PACKED(mode, arg); - return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; + return gpio_do_set_config(gc, offset, mode); } static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc) @@ -3294,15 +3303,9 @@ int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) VALIDATE_DESC(desc); chip = desc->gdev->chip; - if (!chip->set || !chip->set_config) { - gpiod_dbg(desc, - "%s: missing set() or set_config() operations\n", - __func__); - return -ENOTSUPP; - } config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); - return chip->set_config(chip, gpio_chip_hwgpio(desc), config); + return gpio_do_set_config(chip, gpio_chip_hwgpio(desc), config); } EXPORT_SYMBOL_GPL(gpiod_set_debounce); @@ -3339,7 +3342,7 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE, !transitory); gpio = gpio_chip_hwgpio(desc); - rc = chip->set_config(chip, gpio, packed); + rc = gpio_do_set_config(chip, gpio, packed); if (rc == -ENOTSUPP) { dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", gpio); From patchwork Thu Dec 19 17:15:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213576 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="SWPQN6U5"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz7G35dcz9sPh for ; Fri, 20 Dec 2019 04:16:26 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727279AbfLSRQZ (ORCPT ); Thu, 19 Dec 2019 12:16:25 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:55624 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726976AbfLSRPk (ORCPT ); Thu, 19 Dec 2019 12:15:40 -0500 Received: by mail-wm1-f65.google.com with SMTP id q9so6241739wmj.5 for ; Thu, 19 Dec 2019 09:15:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XhiSom0iVO5AHRS5ZUjMyBcC2AAQ3b6I6WYo2HMzPrk=; b=SWPQN6U52q64ZPmqFofySJ402F+H0ckaDHl99RXhe/e2LryuutBlW6t+dABXiDAhFe QJPJx/w5IjIIhfP2oEGkB//h2+TWYEcLNBOcOUOU4Jz12ml54NanVvwcsNHLXITUzMSP m049gzgohL4S579+RX136l3jd/9UwjBewrdlLOn/o+KbFwasTlukpH8xERZoXQPcuQ6+ +TU7TF53TVQ03wpSSJf08n0PkvV1p9/qUOddg4uvymTXTKis3AD256c5VBlKFD63KmHS d8Qnhh5rWpQmmbT5rzyFfi4tC2eYLR5PbIosb+0q3JzVknLsdcU8WE9KlqxhhfWFkU0E Pe5w== 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=XhiSom0iVO5AHRS5ZUjMyBcC2AAQ3b6I6WYo2HMzPrk=; b=BKVP5H2w5mBG8yEXO6gkme0McKFrpC4kpZ8KBNgKBSL7HMmCZ+cJCHpEmqV6nf4/oh lPBxats4QZWTdp2BSqZXYqwzNi1fCSzrULhPNNy1W1F3onmwKleFo+ZBeEZcGa1jpbKJ Axl7oJDm17xIAR2e8RkwDxbRGHW/OdxuCvQU31cTnveyJK9TU3pmyXaICnaLpFm94/mX lukwCDp+yWMZNFEFf/ZkvyCUOeru/4dsahhWcYHKe+mfhFFM9QGKW8FHviXk1ehB4rdx XqP4wUtUB+dmvinrpD5Fk/pM2XSNSKHdPSM/RSRMkCtjH+md6YUU4jSbReloXtftN/LG Sb5A== X-Gm-Message-State: APjAAAWbeQUpOajH8XWZCGM5cJ2YBc1zSCidovzmVIWlIUHqefDihYsW KGT6Y8II4JbUCkbtJPzCnlLgKg== X-Google-Smtp-Source: APXvYqwE6tJDR5YKpKGyKSmR7DJqhpeUc9tFwjGGXDRfmjIokbxHVco+zkNTNvsbYfDHAgfdyiDTwA== X-Received: by 2002:a05:600c:2218:: with SMTP id z24mr11302607wml.50.1576775738540; Thu, 19 Dec 2019 09:15:38 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:38 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 03/13] gpiolib: convert the type of hwnum to unsigned int in gpiochip_get_desc() Date: Thu, 19 Dec 2019 18:15:18 +0100 Message-Id: <20191219171528.6348-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski gpiochip_get_desc() takes a u16 hwnum, but it turns out most users don't respect that and usually pass an unsigned int. Since implicit casting to a smaller type is dangerous - let's change the type of hwnum to unsigned int in gpiochip_get_desc() and in gpiochip_request_own_desc() where the size of hwnum is not respected either and who's a user of the former. This is safe as we then check the hwnum against the number of lines before proceeding in gpiochip_get_desc(). Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 5 +++-- drivers/gpio/gpiolib.h | 3 ++- include/linux/gpio/driver.h | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 72211407469f..b3ffb079e323 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -140,7 +140,7 @@ EXPORT_SYMBOL_GPL(gpio_to_desc); * in the given chip for the specified hardware number. */ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, - u16 hwnum) + unsigned int hwnum) { struct gpio_device *gdev = chip->gpiodev; @@ -2990,7 +2990,8 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested); * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error * code on failure. */ -struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, +struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, + unsigned int hwnum, const char *label, enum gpio_lookup_flags lflags, enum gpiod_flags dflags) diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index ca9bc1e4803c..a1cbeabadc69 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -78,7 +78,8 @@ struct gpio_array { unsigned long invert_mask[]; }; -struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); +struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, + unsigned int hwnum); int gpiod_get_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array, diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index e2480ef94c55..4f032de10bae 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -715,7 +715,8 @@ gpiochip_remove_pin_ranges(struct gpio_chip *chip) #endif /* CONFIG_PINCTRL */ -struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, +struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, + unsigned int hwnum, const char *label, enum gpio_lookup_flags lflags, enum gpiod_flags dflags); From patchwork Thu Dec 19 17:15:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213574 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="oOE+JHQM"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz793v6gz9sR8 for ; Fri, 20 Dec 2019 04:16:21 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727024AbfLSRPm (ORCPT ); Thu, 19 Dec 2019 12:15:42 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:54833 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726986AbfLSRPl (ORCPT ); Thu, 19 Dec 2019 12:15:41 -0500 Received: by mail-wm1-f68.google.com with SMTP id b19so6230290wmj.4 for ; Thu, 19 Dec 2019 09:15:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZKnof9NnGnWaz5Wg5PNLvyOb/wEdh4vzWoDXW3CwsIs=; b=oOE+JHQMOaM2l1QZYogpzJH6M7Wn3Dp2n5HfS+vV9Nr7K85412tv8ZdYFI1dhaCtDD ++YFBXokMGS6KEFagBjki2uJesGJIgREumCwOdU9e2+v21zYxDYi2U8oPDVG9OcEm7/i xZbd3W+OlbjSLaOzwMM2rg3rETgaiRzWxf9onnmMPPKOYf+Fe3x2+WcKN39U/41wTmpN ZkTueFGigG84uc7joqvVY9tmyOgWWcddHiPhSXHcJ81j3fA/cY7nD4CWzX8cGrIWM/Y7 XOxgbm7YkMD+lLRoLBtBdyIuSKEUDAOf64BBogI3nIhsGUCAWX4zLuZHfvutISOK+xBT fTlg== 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=ZKnof9NnGnWaz5Wg5PNLvyOb/wEdh4vzWoDXW3CwsIs=; b=ZVOUySxI0KlfKq21d7zieRz7NAqXnoomw0Np60CaVZArZMO+jrftxiJ0qLepG7va4f W2pE63hqiKgeXSq3OUSoVcQVOSy84Pf9qgq0VsTcYVfECLTc/L8FBQ9XZ36EPqG1QeYT a8vbDaRfQOaLrqYcW9K8sRoFN1muDR2SlCyaxic00gbp8qKW+qeRMVbPUNk32LlB386d uC3IFtVKBdv631haAx65cdH4klwWAKPTyMJde20lz5ShQMftxl9DC+WdoLPA9182c/hO UqIdaLIZ7TYlJ7IzIzAesGHowC3jYfJNMJGX5WJhpFp+c1yOorPsNpjaeamW6TqoKpgU nGzQ== X-Gm-Message-State: APjAAAUZEJ6N2DX19VPVh+9wbe+UHY9h9UGF8YbXdPzGG2fySXVvTUdD nYpfQ50Wbt5KdR++cEkROsWAKg== X-Google-Smtp-Source: APXvYqxyed9XQDWxCA7f59327N+KYa5GAs0nDM+2pUGQ5XO2gXAyJSlmK01+XGMkzke8l8r38BZI+w== X-Received: by 2002:a1c:6404:: with SMTP id y4mr11340453wmb.143.1576775739740; Thu, 19 Dec 2019 09:15:39 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:39 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 04/13] gpiolib: use gpiochip_get_desc() in linehandle_create() Date: Thu, 19 Dec 2019 18:15:19 +0100 Message-Id: <20191219171528.6348-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Unduplicate the ngpio check by simply calling gpiochip_get_desc() and checking its return value. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b3ffb079e323..6ef55cc1188b 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -678,14 +678,13 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) /* Request each GPIO */ for (i = 0; i < handlereq.lines; i++) { u32 offset = handlereq.lineoffsets[i]; - struct gpio_desc *desc; + struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset); - if (offset >= gdev->ngpio) { - ret = -EINVAL; + if (IS_ERR(desc)) { + ret = PTR_ERR(desc); goto out_free_descs; } - desc = &gdev->descs[offset]; ret = gpiod_request(desc, lh->label); if (ret) goto out_free_descs; From patchwork Thu Dec 19 17:15:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213565 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="BnDEMX+a"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6T29Pqz9sPn for ; Fri, 20 Dec 2019 04:15:45 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727031AbfLSRPn (ORCPT ); Thu, 19 Dec 2019 12:15:43 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52885 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727014AbfLSRPm (ORCPT ); Thu, 19 Dec 2019 12:15:42 -0500 Received: by mail-wm1-f65.google.com with SMTP id p9so6238115wmc.2 for ; Thu, 19 Dec 2019 09:15:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Srugj812bEad7lTKigN1hZIzPJdNdOMlpYaAJOfYAF4=; b=BnDEMX+afx9fDJ+i/VCutbb8AFolX5d7d9pqZ41uk9qRHnoY91mJh4l4JDsNZVvsPC 4wUTIz3Wcci1tRsQMdRzDp+3ZX5UvXXgUN3dhWN2F6UV2KXG4MLet6JX9UtunslNOfck xEFcIOBjfx1a5+xdwb/ZMftjszOTd06KorkIx4lzzIkkrICHB8XwgJOqz4K89bpXiqCL k3f7nKuetBXHNLEwLLvffqZUnBuuLWx/kYsxjw048Xv4FT67Q7psPUM/xnxUyVwz/NTn 6sOkjcdBFk4bCbWO5I/MuG5LFcVGM4L64x8rvg/rOU96xlC2fq67mvYUHUKHpTp9+31s 6fTA== 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=Srugj812bEad7lTKigN1hZIzPJdNdOMlpYaAJOfYAF4=; b=KCQsjO2TRmNLFZeg/OMy/nquuKBbfxTAunmu/bn1twwGyrsAzYa+KvvLjHu293Do2y gyYJW9/XvOZD6CJlhcyyXxW71sP3xxiD7RxN3XIKi4ql4c1QUUj0yT5BGCR2OoM36fBK 867KdrEWZVgmIeD8tbfsfeyFj2/AjynwKC4XbdKenqSSTOZSVRL1yPXxPShqgwJO8aAv nam31tJmKlPzEtyZ4KBiKXVVg4iDu2Lb45UTVcmqfnjivUbXBoLBo8ZaezvPHK07WpMG BHsm/b2OCXhQ5KO+4P7wA8l5uI2ci/U3L4BNz/YFXgqh0rqT/nXzlDScwC0vXV89xDqy bnpw== X-Gm-Message-State: APjAAAUV+I38LqEyISR193FC5kNqqxwzpHvK5KncUmgR8iVSrDE3k2Ha MoLAFbaUVu5Jbesm4lfiT66qvQ== X-Google-Smtp-Source: APXvYqw9BKPU3MwZXjSWVFbnLdLXiP4QsXclsAm4IhCn5elS0bX/IO0FaokjhPdH92FkGCV0lPANdw== X-Received: by 2002:a1c:48c1:: with SMTP id v184mr11583568wma.5.1576775740982; Thu, 19 Dec 2019 09:15:40 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:40 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 05/13] gpiolib: use gpiochip_get_desc() in lineevent_create() Date: Thu, 19 Dec 2019 18:15:20 +0100 Message-Id: <20191219171528.6348-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Unduplicate the ngpio check by simply calling gpiochip_get_desc() and checking its return value. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 6ef55cc1188b..17796437d7be 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1009,8 +1009,9 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) lflags = eventreq.handleflags; eflags = eventreq.eventflags; - if (offset >= gdev->ngpio) - return -EINVAL; + desc = gpiochip_get_desc(gdev->chip, offset); + if (IS_ERR(desc)) + return PTR_ERR(desc); /* Return an error if a unknown flag is set */ if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || @@ -1048,7 +1049,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) } } - desc = &gdev->descs[offset]; ret = gpiod_request(desc, le->label); if (ret) goto out_free_label; From patchwork Thu Dec 19 17:15:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213573 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="bQmZEgC+"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz7840rDz9sR4 for ; Fri, 20 Dec 2019 04:16:20 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727066AbfLSRPo (ORCPT ); Thu, 19 Dec 2019 12:15:44 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:37228 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726986AbfLSRPn (ORCPT ); Thu, 19 Dec 2019 12:15:43 -0500 Received: by mail-wm1-f65.google.com with SMTP id f129so6448824wmf.2 for ; Thu, 19 Dec 2019 09:15:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=95Vj6BCj4He2jTRmy6pbRhSQcUrTJuyZcD7xpHhCyRw=; b=bQmZEgC+HJNTCZAgnsFcEGPlB/Le6fAJuzEwDRF9QjXihH2f13xdRuCkGTdJGYRPE6 ZpAmEJ/VqDXobWYGUrfiJuR7DWTrX8RQ76QG+5uemZEHGLeT9VHlEiapqYJFFDxgp49n RlgcE4N9ICvr8mt36oHhcQ6RcP7sVL+y8XGOB5KvwwRpaPRC6ovaNmx3ExbSDL/z43Gs zp9XFVDRkimWHsurLky6fYG/lQTala6wTca83xAbbAe2BAjxX0s3cUDTWGqeSiEEB0Wv fCJk39i2UMATx8p07LuL4NHP1BWwsamutqFcs59gop0KYcBx2jqWVMjOPQXtuWF46usp 0E6w== 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=95Vj6BCj4He2jTRmy6pbRhSQcUrTJuyZcD7xpHhCyRw=; b=hMbgAhlmUcIG/HAdsgrqPZlZvdWETunHm9EtVTu82Zk8CI58h/7VCW18H+9DJJ29Wq e3IzXUWz/WcG6HVfW5F5QFMNqHdmg7dC32woWyIY2dBgZMUyaEoh+tXl4qeYKekwdWYo qKv2/vQF8UlgCY2CA2e2lq4ZHpVcNJc3V1vRULTy4bDQnqItI4R7X8TMfHXPYrn2HIrv IIXts5BCMrE1PXy+1cqiLjhDSYm5EpJNtIktrd4Bfc+QhgNAmTWkP2GUEKacQu+Uo7hy jZrdkep27SLe+X1V3ejRTkdH7+/nvDXHhJfxmL9gWQMu5m72oJB591gDyZQ93WQOMLB8 cnlQ== X-Gm-Message-State: APjAAAUYr5YMSrtmov8vXVlzLn0tCkaYbdpoubWEX66AVHzBKd7sI2SQ i6UW7jB36dao3sFJC5BlPME9dg== X-Google-Smtp-Source: APXvYqxm0zvCVaIjH2Ioute5ihjTo1ftEw4hS33yLMQ+MkcoURrq1G3nOVBcP+SznGiIzIGVk5lvyg== X-Received: by 2002:a05:600c:290f:: with SMTP id i15mr11517982wmd.115.1576775742039; Thu, 19 Dec 2019 09:15:42 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:41 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , Andy Shevchenko Subject: [PATCH v3 06/13] gpiolib: use gpiochip_get_desc() in gpio_ioctl() Date: Thu, 19 Dec 2019 18:15:21 +0100 Message-Id: <20191219171528.6348-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Unduplicate the offset check by simply calling gpiochip_get_desc() and checking its return value. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpiolib.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 17796437d7be..b7043946c029 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1175,10 +1175,11 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; - if (lineinfo.line_offset >= gdev->ngpio) - return -EINVAL; - desc = &gdev->descs[lineinfo.line_offset]; + desc = gpiochip_get_desc(chip, lineinfo.line_offset); + if (IS_ERR(desc)) + return PTR_ERR(desc); + if (desc->name) { strncpy(lineinfo.name, desc->name, sizeof(lineinfo.name)); From patchwork Thu Dec 19 17:15:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213571 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="yvCbBMgs"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz724Dntz9sPT for ; Fri, 20 Dec 2019 04:16:14 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727097AbfLSRPq (ORCPT ); Thu, 19 Dec 2019 12:15:46 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:52894 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727049AbfLSRPp (ORCPT ); Thu, 19 Dec 2019 12:15:45 -0500 Received: by mail-wm1-f67.google.com with SMTP id p9so6238272wmc.2 for ; Thu, 19 Dec 2019 09:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K3tKF/sG1li0TNgHDm/zaOCBiB+0gN/tDFvNC2uEQ+I=; b=yvCbBMgsXygGH3FpIEPNHsa/VXxazpj/KKkweqXvtiwPAGQGvioH+U4zhS4FEmBe2J 3bcwHbePdFYkv53xANQ07yko9f1EpaEr06XKEiGQfmhzVB+KP/bm5qc4/qMY36kFNMa/ An7xjv4KBFJgqalgay1+pIpHbpovJVofNQK1nUAzV4DChKHO1sOyRvVge6MdEKhIav9J XfBN1rrrif8DB15FRkaW/sYyvboMW6CIm5xx65HaoItIainM6I0Z6HuYLXsb90I8IJTM znXgVy2wze2oBNhtr70aExe9Z6UEqj/ha8WrO3msi1bx41fkBu68d2pgssY4yX8VGKA0 RKmg== 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=K3tKF/sG1li0TNgHDm/zaOCBiB+0gN/tDFvNC2uEQ+I=; b=aptyJ3erMvC0mfjDhkotY6bw0G5zEXteE14UEipdDlzbK7A0Bdlh8pvuRAjvGNfuMh 65ol7mqfmq9UqPN+XgfdQWCjHaoO0PjddpM7LdqgUVTPxtL94TvXXQJmtupP2f17/xR+ ntVumTHesXeYXBiCRTO+5MccQY/LkgzOecT69yWuPJkdaEImIFy87k1vqV2uzxrkuHEX QVyRTuGgMX5PEdK+D8tRZJhV/yCF9kPFJklHNomYpBzHpgrReJcj5IHDxUlqa+AGsqe9 CJcGaF0wC9AS1bz0TPPQioWlbTGJROrdDejjlAWrbX7AEsG/FykFYMELEvRLhDyQhbkd aARA== X-Gm-Message-State: APjAAAWdn7BAiygL+kQQ9+GNP9b74jNMQOu9DQNXXI5GL1aHlF3peTqF ecvFRcm5pIBtCfxrlN7NWf6JNg== X-Google-Smtp-Source: APXvYqzi/6ikWTdHrJtfEWndoMXEyahi/G3Kdtq/Ybn6ZO7teucGMeyvx1jbrI/xqlhXvEYWp5KGVw== X-Received: by 2002:a05:600c:2050:: with SMTP id p16mr10682400wmg.176.1576775743241; Thu, 19 Dec 2019 09:15:43 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:42 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 07/13] kfifo: provide noirqsave variants of spinlocked in and out helpers Date: Thu, 19 Dec 2019 18:15:22 +0100 Message-Id: <20191219171528.6348-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide variants of spinlocked kfifo_in() and kfifo_out() routines which don't disable interrupts. Signed-off-by: Bartosz Golaszewski --- include/linux/kfifo.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h index fc4b0b10210f..123c200ed7cb 100644 --- a/include/linux/kfifo.h +++ b/include/linux/kfifo.h @@ -517,6 +517,26 @@ __kfifo_uint_must_check_helper( \ __ret; \ }) +/** + * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for + * locking, don't disable interrupts + * @fifo: address of the fifo to be used + * @buf: the data to be added + * @n: number of elements to be added + * @lock: pointer to the spinlock to use for locking + * + * This is a variant of kfifo_in_spinlocked() but uses spin_lock/unlock() + * for locking and doesn't disable interrupts. + */ +#define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \ +({ \ + unsigned int __ret; \ + spin_lock(lock); \ + __ret = kfifo_in(fifo, buf, n); \ + spin_unlock(lock); \ + __ret; \ +}) + /* alias for kfifo_in_spinlocked, will be removed in a future release */ #define kfifo_in_locked(fifo, buf, n, lock) \ kfifo_in_spinlocked(fifo, buf, n, lock) @@ -569,6 +589,28 @@ __kfifo_uint_must_check_helper( \ }) \ ) +/** + * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock + * for locking, don't disable interrupts + * @fifo: address of the fifo to be used + * @buf: pointer to the storage buffer + * @n: max. number of elements to get + * @lock: pointer to the spinlock to use for locking + * + * This is a variant of kfifo_out_spinlocked() which uses spin_lock/unlock() + * for locking and doesn't disable interrupts. + */ +#define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \ +__kfifo_uint_must_check_helper( \ +({ \ + unsigned int __ret; \ + spin_lock(lock); \ + __ret = kfifo_out(fifo, buf, n); \ + spin_unlock(lock); \ + __ret; \ +}) \ +) + /* alias for kfifo_out_spinlocked, will be removed in a future release */ #define kfifo_out_locked(fifo, buf, n, lock) \ kfifo_out_spinlocked(fifo, buf, n, lock) From patchwork Thu Dec 19 17:15:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="g6TRXLzQ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz766THkz9sR0 for ; Fri, 20 Dec 2019 04:16:18 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727252AbfLSRQN (ORCPT ); Thu, 19 Dec 2019 12:16:13 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:35797 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727070AbfLSRPq (ORCPT ); Thu, 19 Dec 2019 12:15:46 -0500 Received: by mail-wr1-f65.google.com with SMTP id g17so6786150wro.2 for ; Thu, 19 Dec 2019 09:15:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3GsK7s8K6DIS0JIxs6gZ15L0MgMAbrzcUa2gzsoGKiM=; b=g6TRXLzQnGjOPWvH2HyZO/buusAWwoTEGg4ebhf8w94RAYzxtqUm6yVvhCIw8TKnTw NIW3bDN4U3M9AHMZEeG7YVVEB8H58W7zaTbFpsPH3MVS8CAqyDOOYTLrKiEYom7AQQI8 DmfAxe6M0vOJClNDCjPu2QxNK1XF2xDVYE+fJv3uwKyHlB1ODq0Wdi/hQfsOwyRTpSwZ sKS8UHmC+O+6E7BLZk2ze+aGJlb2VTxMMKifzz5mSt3y10yxnpSgUhIki9GhbmywIlDh jL6sAwvJIxhQ0wgchhfdMoxucMlNFpPt/qryPKpRjRPVFSg+5sU2NZGVgT6RFaMH8nIc WSVA== 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=3GsK7s8K6DIS0JIxs6gZ15L0MgMAbrzcUa2gzsoGKiM=; b=QrtT//olGe/iPBpErv6C1kM0IzC1qHs5RDKEiQT3ZLQse0ZtJMhDiOv0ZesIY8hSWf eYRfvRTJLdzXOz7/jD2cAZN0hwv2HM+QaIHAdZvotph8KgVdGe++ZUaIdjkmlylXDX7S gGZj9xP4ZTaurZ426AC/puipZjJNy6NMPjwjDZuoo8L4f+wuVxk5rDw+REQsk5lElKmz KOEetfksnUI7vz6Pa6+GAjow1mZYU5fwJEk5Ol42ehopAcIiQcjMf3Xvd2CyZO/cY1OI XoiWyO1ZbqfwqdLq1+UwdBnunaCQNjkSB5+asu65Llh22pMzcdi5CtrSyj0rU5I8mZqn yjrA== X-Gm-Message-State: APjAAAVS/FNurV/zFUGB7PruPI5A/6Rghs0heM8aQf9F0Q/JXR3FuJmW bBmRjh4r4Ryugx/DsoRnCGC8Iw== X-Google-Smtp-Source: APXvYqxpvJtV3K+218KyMYeyCIIlGfxC5RkmAu0mSlC24RiXlLDUIaN7rxKG11+WOceIDd+3Gez3FQ== X-Received: by 2002:a5d:43c7:: with SMTP id v7mr9974423wrr.32.1576775744375; Thu, 19 Dec 2019 09:15:44 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:43 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 08/13] kfifo: provide kfifo_is_empty_spinlocked() Date: Thu, 19 Dec 2019 18:15:23 +0100 Message-Id: <20191219171528.6348-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide two spinlocked versions of kfifo_is_empty() to be used with spinlocked variants of kfifo_in() and kfifo_out(). Signed-off-by: Bartosz Golaszewski --- include/linux/kfifo.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h index 123c200ed7cb..86249476b57f 100644 --- a/include/linux/kfifo.h +++ b/include/linux/kfifo.h @@ -246,6 +246,37 @@ __kfifo_int_must_check_helper(int val) __tmpq->kfifo.in == __tmpq->kfifo.out; \ }) +/** + * kfifo_is_empty_spinlocked - returns true if the fifo is empty using + * a spinlock for locking + * @fifo: address of the fifo to be used + * @lock: spinlock to be used for locking + */ +#define kfifo_is_empty_spinlocked(fifo, lock) \ +({ \ + unsigned long __flags; \ + bool __ret; \ + spin_lock_irqsave(lock, __flags); \ + __ret = kfifo_is_empty(fifo); \ + spin_unlock_irqrestore(lock, __flags); \ + __ret; \ +}) + +/** + * kfifo_is_empty_spinlocked_noirqsave - returns true if the fifo is empty + * using a spinlock for locking, doesn't disable interrupts + * @fifo: address of the fifo to be used + * @lock: spinlock to be used for locking + */ +#define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \ +({ \ + bool __ret; \ + spin_lock(lock); \ + __ret = kfifo_is_empty(fifo); \ + spin_unlock(lock); \ + __ret; \ +}) + /** * kfifo_is_full - returns true if the fifo is full * @fifo: address of the fifo to be used From patchwork Thu Dec 19 17:15:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213569 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="mIRpvHTY"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6w4Rmtz9sQp for ; Fri, 20 Dec 2019 04:16:08 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727031AbfLSRQH (ORCPT ); Thu, 19 Dec 2019 12:16:07 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:37956 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727092AbfLSRPr (ORCPT ); Thu, 19 Dec 2019 12:15:47 -0500 Received: by mail-wr1-f65.google.com with SMTP id y17so6794018wrh.5 for ; Thu, 19 Dec 2019 09:15:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0OkuyO6pI87nxwHDFAFros/G0Asl1CJacpmgH90eRXU=; b=mIRpvHTYI7yAj5njue7Coqe3+OX2ef3/eQsemEjW3Tf8bheoac9PHSyIieoqXsN1dD +OPKwvUkTwqf5dxGcDMMiZtOHsYBt5sIo410CW9+GOp7FFTidnsJAPAOnZNWhm6PZZ3v znmFCRs6Bm4r98mcVp6NJuL02Umh+wVYIAAT9uGqb8yz0HchFc2Ffck7xwKphiX26afD aNoBH9blvg4zn1ul7cwjnhMfel2qp7a1hXX/6i4B+yTt7RDD/KtzpWIuJ5laCu0VgMsx uOHWEvtwWzHbMMOKxa49CYqGGm/KbhntI2SWT80NlOhBUWFq8nVKLbuN8kNY8ChD+s+g 0bMw== 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=0OkuyO6pI87nxwHDFAFros/G0Asl1CJacpmgH90eRXU=; b=Y2D7zquDTt2BWFyHjspNeKxN/E4ZwUB3jr95xD+gOYz88DXyRcr4GsNUbeS2iUB5gA MxrKx/gx0R9lODzWBxNxaETAmWOWuFoPLzgQUO5uoSckuFKak+CJe8NnsKzX20bX2hVI STudw2bOFdHqJBP5PrHcPy5u58NjP0eXDf+bs7v5AWL7FFmJPUJlBp4FPCO8dbBsGwcL E3b67inKsVVtfDE/6UBT4YgJT11tbCxhEWsEp72HoTtsL8ZtJnyp1ogYBXzU2iAFyZsz R+Dw4gkRBUppe1yUYxcQV49XKUkutD5aJ8+MzpTDi02vQP+pjhMDxmsD/ddzh39bymy+ O07Q== X-Gm-Message-State: APjAAAV7G3k4adUdjK4EfpTdoAKN2CoF6g7is80EyX3TZXgBo7Xq65W/ Im4VguZYPfc+MzA6ZEI9OMQdog== X-Google-Smtp-Source: APXvYqxqTF4ScmikfQcnwxQTw5v6wzqEIRbOYS7a5w7X9UkGcQKfBf5JB2VbDrsC81TKmIlklKUjog== X-Received: by 2002:a5d:4847:: with SMTP id n7mr10683978wrs.30.1576775745354; Thu, 19 Dec 2019 09:15:45 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:44 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 09/13] gpiolib: rework the locking mechanism for lineevent kfifo Date: Thu, 19 Dec 2019 18:15:24 +0100 Message-Id: <20191219171528.6348-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski The read_lock mutex is supposed to prevent collisions between reading and writing to the line event kfifo but it's actually only taken when the events are being read from it. Drop the mutex entirely and reuse the spinlock made available to us in the waitqueue struct. Take the lock whenever the fifo is modified or inspected. Drop the call to kfifo_to_user() and instead first extract the new element from kfifo when the lock is taken and only then pass it on to the user after the spinlock is released. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 64 +++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b7043946c029..073bc75b3d45 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -788,8 +788,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) * @irq: the interrupt that trigger in response to events on this GPIO * @wait: wait queue that handles blocking reads of events * @events: KFIFO for the GPIO events - * @read_lock: mutex lock to protect reads from colliding with adding - * new events to the FIFO * @timestamp: cache for the timestamp storing it between hardirq * and IRQ thread, used to bring the timestamp close to the actual * event @@ -802,7 +800,6 @@ struct lineevent_state { int irq; wait_queue_head_t wait; DECLARE_KFIFO(events, struct gpioevent_data, 16); - struct mutex read_lock; u64 timestamp; }; @@ -818,7 +815,7 @@ static __poll_t lineevent_poll(struct file *filep, poll_wait(filep, &le->wait, wait); - if (!kfifo_is_empty(&le->events)) + if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock)) events = EPOLLIN | EPOLLRDNORM; return events; @@ -831,43 +828,52 @@ static ssize_t lineevent_read(struct file *filep, loff_t *f_ps) { struct lineevent_state *le = filep->private_data; - unsigned int copied; + struct gpioevent_data event; + ssize_t bytes_read = 0; int ret; - if (count < sizeof(struct gpioevent_data)) + if (count < sizeof(event)) return -EINVAL; do { + spin_lock(&le->wait.lock); if (kfifo_is_empty(&le->events)) { - if (filep->f_flags & O_NONBLOCK) + if (bytes_read) { + spin_unlock(&le->wait.lock); + return bytes_read; + } + + if (filep->f_flags & O_NONBLOCK) { + spin_unlock(&le->wait.lock); return -EAGAIN; + } - ret = wait_event_interruptible(le->wait, + ret = wait_event_interruptible_locked(le->wait, !kfifo_is_empty(&le->events)); - if (ret) + if (ret) { + spin_unlock(&le->wait.lock); return ret; + } } - if (mutex_lock_interruptible(&le->read_lock)) - return -ERESTARTSYS; - ret = kfifo_to_user(&le->events, buf, count, &copied); - mutex_unlock(&le->read_lock); - - if (ret) - return ret; - - /* - * If we couldn't read anything from the fifo (a different - * thread might have been faster) we either return -EAGAIN if - * the file descriptor is non-blocking, otherwise we go back to - * sleep and wait for more data to arrive. - */ - if (copied == 0 && (filep->f_flags & O_NONBLOCK)) - return -EAGAIN; + ret = kfifo_out(&le->events, &event, 1); + spin_unlock(&le->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; + } - } while (copied == 0); + if (copy_to_user(buf + bytes_read, &event, sizeof(event))) + return -EFAULT; + bytes_read += sizeof(event); + } while (count >= bytes_read + sizeof(event)); - return copied; + return bytes_read; } static int lineevent_release(struct inode *inode, struct file *filep) @@ -969,7 +975,8 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p) return IRQ_NONE; } - ret = kfifo_put(&le->events, ge); + ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge, + 1, &le->wait.lock); if (ret) wake_up_poll(&le->wait, EPOLLIN); @@ -1084,7 +1091,6 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) INIT_KFIFO(le->events); init_waitqueue_head(&le->wait); - mutex_init(&le->read_lock); /* Request a thread to read the events */ ret = request_threaded_irq(le->irq, From patchwork Thu Dec 19 17:15:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213570 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="rBzzEYfy"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6x3KDNz9sR4 for ; Fri, 20 Dec 2019 04:16:09 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727092AbfLSRQH (ORCPT ); Thu, 19 Dec 2019 12:16:07 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:39734 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727106AbfLSRPs (ORCPT ); Thu, 19 Dec 2019 12:15:48 -0500 Received: by mail-wm1-f66.google.com with SMTP id 20so6331389wmj.4 for ; Thu, 19 Dec 2019 09:15:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uycbaS0g1vE0Cuob7CqLcnjNvgH80U0c2b2m+7/3Ktw=; b=rBzzEYfyA1MtQaHuNuBZq+0ETt+FtIFBvw4RFKKOcpSVbBFxol0tf56M85YAhphsr5 a/HmMFRQKK/20smBrmLQA4rAUYEhkJKpwC1Sft2mP16nyeCkBG76iQr9pcTYfyTib+bY LOhO5AWg6DkMc1VUTfA5F7UbE8rVtwLCnyPT7zK9+jP+9iv77TX1eRVoYYLi+Z4X0V/a 4TJ6xwHag5hCVsmXg0LdXyKCyzsU1aOVDB+eECTV35zGRLzWJjXJ95Xgtbh4Y8HONcEY 0wyoBWzcCOXk+7lYfiUqoX2Agfn+cFBFqqi4C+CmqCjJjGvZgDxaOHpKJOQlbUOruwOP MQ+A== 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=uycbaS0g1vE0Cuob7CqLcnjNvgH80U0c2b2m+7/3Ktw=; b=LWyxUF+fj5xBDIjuVB8hztrj4P77Nn4QB/NSmYUsPVdKo9wuyrjNp3moiuVETsOqV2 9W2bDNBkLWNT78DQbW/keZbLCJVW8+PbapjFlEKKQHK65J/AHb8pesHYKqh2y8cQBWVn ZgHW2+IkPAdAxUvXeMqpHhlPSUFB0pOD5DYwQczRKncG1tRwEccjuVnibabZjKlhggIE 0qmDk5MOhnvo/XukwkJA6FZB4ktxZ2tZrp34bD7kfgrWCAJ6R+uxW72eoM99jrBAyLa4 jWgILlinb7rU222tqCN+13j5ibWrNR5hz6SlaSfBIzh9QF5mEv0muodZ6SwNu7nBCPpE uKiQ== X-Gm-Message-State: APjAAAVN9uoBpzJRxsAZ5UnoEn9YmTzDS4NAzKrV1cyPE2wgQLa9EnAn Ji/rN+ntwC4fzHA/FE62NP2yqg== X-Google-Smtp-Source: APXvYqwsGU6Wo1uPHcriBaY8UhilFJcnLEteqy07AwUJ7zq340qlG3Ixhg1A+rJmWNmKh/4+fligKQ== X-Received: by 2002:a05:600c:108a:: with SMTP id e10mr10817499wmd.38.1576775746351; Thu, 19 Dec 2019 09:15:46 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:45 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 10/13] gpiolib: emit a debug message when adding events to a full kfifo Date: Thu, 19 Dec 2019 18:15:25 +0100 Message-Id: <20191219171528.6348-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Currently if the line-event kfifo is full, we just silently drop any new events. Add a ratelimited debug message so that we at least have some trace in the kernel log of event overflow. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 073bc75b3d45..e27cf2113fed 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -979,6 +979,9 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p) 1, &le->wait.lock); if (ret) wake_up_poll(&le->wait, EPOLLIN); + else + pr_debug_ratelimited( + "%s: event FIFO is full - event dropped\n", __func__); return IRQ_HANDLED; } From patchwork Thu Dec 19 17:15:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213568 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="lhPHcab9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6p3mPrz9sR8 for ; Fri, 20 Dec 2019 04:16:02 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727161AbfLSRPt (ORCPT ); Thu, 19 Dec 2019 12:15:49 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:52902 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727137AbfLSRPt (ORCPT ); Thu, 19 Dec 2019 12:15:49 -0500 Received: by mail-wm1-f68.google.com with SMTP id p9so6238498wmc.2 for ; Thu, 19 Dec 2019 09:15:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=izVdkb3B6fuklcEw/hV1XUazbmANjGDXpj//MuY0JEw=; b=lhPHcab9tWEUpr5zez2HKjQULIQKo5Si2e1x7Ov5RzMbFYiz2ld3ou8VTSix91i5UX IDWTvCrSzIXZMaOzLI52k9Kgl9pk6obwrzpD/1ep0EqDKZK5Xs0ZM3nuE2F407PI/0qb QH1NCPnFgNRIRVLwkHZPzSEUyTOPfIZpew1PFSmh3ZLFxuGIk79cOo2PjhUza9Du4b96 WUcirheee76Zp4T19y53VKSGb1iKmBeuNInZXEqCsNJRVcPabXN0gTSAJDM289TJll6G mcgr9KW8H8W8f3bWSkfSpro7M7Bm2RioYzmlI+xB3pii+B86eLxRGxo2V/GPDeKZEN3o Q4Pw== 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=izVdkb3B6fuklcEw/hV1XUazbmANjGDXpj//MuY0JEw=; b=otEGnftGSPueyjnCXZ+yvvsEgJwnoMwyIDhBTAHLXFY+UGu9IO5XlK4BZbznHFa5H5 BVTxtBb2uElwvpWBxvVe+QCljpjMB2iIVvBwGZoV4IKQ1pxz8VmW9oEGRgtuKmysUj+L PpS0UZT+vq/7YfYI8CsHytIlgATPf2HBmwsQdX2/YtXDoeD3glodG0Jcc6wRSEp4LnLn AXaCOUL2+IQM4cKMyme0ui4rIwsMdQ7Hv54eLmKow9SL/5bI5HBsqDR2YAdu7IgpzqR+ tr5H/L41mOeTdT+iXxJPjCtRXcnVOtQt3Dwp2m800eQQ8HS5ihrBdBGWO2uNsXBDIdkv QKyg== X-Gm-Message-State: APjAAAVIegECNQ+Jl+ne6gTdc4Jlj9sVtZplRnswRThzInYbSRFfIsvE Sg1m7qKmJNtTMhmnXy01Sv4eGJUrDGk= X-Google-Smtp-Source: APXvYqwwdo1gvGGraneMABLw6o9frhqNZr1r88X2HztSEReoffhx/278goQehYKrJ8cm1UbA7+AIlA== X-Received: by 2002:a7b:c183:: with SMTP id y3mr11451806wmi.0.1576775747685; Thu, 19 Dec 2019 09:15:47 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:46 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 11/13] gpiolib: provide a dedicated function for setting lineinfo Date: Thu, 19 Dec 2019 18:15:26 +0100 Message-Id: <20191219171528.6348-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski We'll soon be filling out the gpioline_info structure in multiple places. Add a separate function that given a gpio_desc sets all relevant fields. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 98 ++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index e27cf2113fed..41abf17640de 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1149,6 +1149,60 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) return ret; } +static void gpio_desc_to_lineinfo(struct gpio_desc *desc, + struct gpioline_info *info) +{ + struct gpio_chip *chip = desc->gdev->chip; + unsigned long flags; + + 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->label) { + strncpy(info->consumer, desc->label, sizeof(info->consumer)); + info->consumer[sizeof(info->consumer) - 1] = '\0'; + } else { + info->consumer[0] = '\0'; + } + + /* + * Userspace only need to know that the kernel is using this GPIO so + * it can't use it. + */ + info->flags = 0; + if (test_bit(FLAG_REQUESTED, &desc->flags) || + test_bit(FLAG_IS_HOGGED, &desc->flags) || + test_bit(FLAG_USED_AS_IRQ, &desc->flags) || + test_bit(FLAG_EXPORT, &desc->flags) || + test_bit(FLAG_SYSFS, &desc->flags) || + !pinctrl_gpio_can_use_line(chip->base + info->line_offset)) + info->flags |= GPIOLINE_FLAG_KERNEL; + if (test_bit(FLAG_IS_OUT, &desc->flags)) + info->flags |= GPIOLINE_FLAG_IS_OUT; + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) + info->flags |= GPIOLINE_FLAG_ACTIVE_LOW; + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) + info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN | + GPIOLINE_FLAG_IS_OUT); + if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) + info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE | + GPIOLINE_FLAG_IS_OUT); + if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) + info->flags |= GPIOLINE_FLAG_BIAS_DISABLE; + if (test_bit(FLAG_PULL_DOWN, &desc->flags)) + info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; + if (test_bit(FLAG_PULL_UP, &desc->flags)) + info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + + spin_unlock_irqrestore(&gpio_lock, flags); +} + /* * gpio_ioctl() - ioctl handler for the GPIO chardev */ @@ -1189,49 +1243,7 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (IS_ERR(desc)) return PTR_ERR(desc); - if (desc->name) { - strncpy(lineinfo.name, desc->name, - sizeof(lineinfo.name)); - lineinfo.name[sizeof(lineinfo.name)-1] = '\0'; - } else { - lineinfo.name[0] = '\0'; - } - if (desc->label) { - strncpy(lineinfo.consumer, desc->label, - sizeof(lineinfo.consumer)); - lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0'; - } else { - lineinfo.consumer[0] = '\0'; - } - - /* - * Userspace only need to know that the kernel is using - * this GPIO so it can't use it. - */ - lineinfo.flags = 0; - if (test_bit(FLAG_REQUESTED, &desc->flags) || - test_bit(FLAG_IS_HOGGED, &desc->flags) || - test_bit(FLAG_USED_AS_IRQ, &desc->flags) || - test_bit(FLAG_EXPORT, &desc->flags) || - test_bit(FLAG_SYSFS, &desc->flags) || - !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset)) - lineinfo.flags |= GPIOLINE_FLAG_KERNEL; - if (test_bit(FLAG_IS_OUT, &desc->flags)) - lineinfo.flags |= GPIOLINE_FLAG_IS_OUT; - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) - lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW; - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN | - GPIOLINE_FLAG_IS_OUT); - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE | - GPIOLINE_FLAG_IS_OUT); - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) - lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE; - if (test_bit(FLAG_PULL_DOWN, &desc->flags)) - lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; - if (test_bit(FLAG_PULL_UP, &desc->flags)) - lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP; + gpio_desc_to_lineinfo(desc, &lineinfo); if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; From patchwork Thu Dec 19 17:15:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213567 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="nyAuIVTa"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6n2Dk7z9sR4 for ; Fri, 20 Dec 2019 04:16:01 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727198AbfLSRP4 (ORCPT ); Thu, 19 Dec 2019 12:15:56 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:54852 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727166AbfLSRPv (ORCPT ); Thu, 19 Dec 2019 12:15:51 -0500 Received: by mail-wm1-f65.google.com with SMTP id b19so6230917wmj.4 for ; Thu, 19 Dec 2019 09:15:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C2M6sI+GaPDwiZ86mCdlGOwzavWJLidzR+bx2oQQ/mo=; b=nyAuIVTayku53ZNj5rodyH5uhOx51b/6Hg/TN5OKhAHxkfmt4IJxcaBubRU7mKoSkS rHUdPnIY3YgE9y6YQU5IErBzQSLrGhAQoC254UNCGRp2/nsM2ovOtATx4zq6EEUeqpYe S6hl8c9D8DhIWWEUdbDqdSKDwdQl1DejKZH91KmGb9cdB9tMw6bkR2P2uvhpVs5rE8Cd EeJXQXQfzTMILAQdk4PgDcBDmQul/I56SMoFzHIeDS9snWo3ACFgjQqiP2G6k0gK0UEf pf65ppBcu53OPNeoi60SGXWTydImHpAQjJxh20Wm6ltU/Df12pHY/VwOfJCgm7BGBaeK VQog== 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=C2M6sI+GaPDwiZ86mCdlGOwzavWJLidzR+bx2oQQ/mo=; b=iQ1tIVrttlkNbSduUyXE39LK3FGpqnQikZjqSxA8loPLmMUCnv7CMln8Xz9UsiQ3Wk XkwtdLNcqg2SaDiJpRVYndnDOzQKGyQqJLNBTkruK3mp4YSeP27KN0iX08RLO/Q6M7Mk TGL2p7oe5Flptf1o5RbB+7I/XYDOk5TZxrsNeKGtVzHPBLxibAyMQ57r0HWWh8FHM7GA 2Oe9PfCKq4lB5yk5ApGTUg7RjyW15Hg+ZwUkqj0U/+qlsV0nfMJDLctavj6435je5odB sJeI1Zqk0AVM8UwvJ5fnX6p9bDdwdEH8Xu0wp77mjHU34sHJcmYJ6v0eowaBIFFxynf4 Em1w== X-Gm-Message-State: APjAAAWOXdS1NP2UJptYAUgeoF/yNBx9JVJQLQ0UnsBCUpFONmilt91N fWkUkcui6vUQ1SocnuqSelwaeQ== X-Google-Smtp-Source: APXvYqzv+7PBu/6hraYa2jFmjgRRClahKNcDeLbEpmS1C8tpgpAf8AeWLJqxaIEuIUf3H0RdG/+EEg== X-Received: by 2002:a1c:a543:: with SMTP id o64mr10740519wme.73.1576775748762; Thu, 19 Dec 2019 09:15:48 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:48 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 12/13] gpiolib: add new ioctl() for monitoring changes in line info Date: Thu, 19 Dec 2019 18:15:27 +0100 Message-Id: <20191219171528.6348-13-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Currently there is no way for user-space to be informed about changes in status of GPIO lines e.g. when someone else requests the line or its config changes. We can only periodically re-read the line-info. This is fine for simple one-off user-space tools, but any daemon that provides a centralized access to GPIO chips would benefit hugely from an event driven line info synchronization. This patch adds a new ioctl() that allows user-space processes to reuse the file descriptor associated with the character device for watching any changes in line properties. Every such event contains the updated line information. Currently the events are generated on three types of status changes: when a line is requested, when it's released and when its config is changed. The first two are self-explanatory. For the third one: this will only happen when another user-space process calls the new SET_CONFIG ioctl() as any changes that can happen from within the kernel (i.e. set_transitory() or set_debounce()) are of no interest to user-space. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 191 ++++++++++++++++++++++++++++++++++++-- drivers/gpio/gpiolib.h | 1 + include/uapi/linux/gpio.h | 24 +++++ tools/gpio/gpio-watch | Bin 0 -> 26528 bytes 4 files changed, 208 insertions(+), 8 deletions(-) create mode 100755 tools/gpio/gpio-watch GIT binary patch literal 26528 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⪻gd?Nc&?5gJsR}) z&5B!rP)u3>VOV~UtRw@wf=T3$G`VrSgwz!HU5JdaQ^+jW_iH_d_m?XJuQD$_T%|vw1sHOD*$mSRf2&HLpH~?6R=qcK`E2i#Rr>sX z$*`UUHLR}xr?ft|pYUrz4V$Yv9Q&=M#2*165!UDTRfbzwP{Zo-zgVTu@3jm!{gm?Q zJ%JVkbC~$~pZ|YBwZBX=r0311GJSqeekA}V63Z#7L^J=NkU@Qq%jfrL{{IlqYyY`_ zvL3hNz0f5Q*5~)`=V(n3(mDl-XVz!<5^~D*MOce=DLAZ22CT>MHDr|Q4~LcHu-0ck ztM!?F9SGTC|4)ZiPIdu(zsfG2-vUPEYw;qV^_LVm3L#la4BbE&S2^l@P8;m1%B-$W z>jL^SdXDhQhH|U(%erI%{qJ9|^s*cnZKlc{LH4;hSk`0t0}JTS>{k-^v!I66@h5vcc9e?_A5hd&+W$*I qUWgdev->notifier, + GPIOLINE_CHANGED_CONFIG, desc); } return 0; } @@ -1203,14 +1206,25 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, spin_unlock_irqrestore(&gpio_lock, flags); } +struct gpio_chardev_data { + struct gpio_device *gdev; + wait_queue_head_t wait; + DECLARE_KFIFO(events, struct gpioline_info_changed, 32); + struct notifier_block lineinfo_changed_nb; + unsigned long *watched_lines; +}; + /* * gpio_ioctl() - ioctl handler for the GPIO chardev */ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { - struct gpio_device *gdev = filp->private_data; + struct gpio_chardev_data *priv = filp->private_data; + struct gpio_device *gdev = priv->gdev; struct gpio_chip *chip = 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 */ if (!chip) @@ -1232,9 +1246,9 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; return 0; - } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { + } else if (cmd == GPIO_GET_LINEINFO_IOCTL || + cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) { struct gpioline_info lineinfo; - struct gpio_desc *desc; if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) return -EFAULT; @@ -1247,11 +1261,25 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) return -EFAULT; + + if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) + set_bit(desc_to_gpio(desc), priv->watched_lines); + return 0; } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { return linehandle_create(gdev, ip); } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { return lineevent_create(gdev, ip); + } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) { + if (copy_from_user(&offset, ip, sizeof(offset))) + return -EFAULT; + + desc = gpiochip_get_desc(chip, offset); + if (IS_ERR(desc)) + return PTR_ERR(desc); + + clear_bit(desc_to_gpio(desc), &desc->flags); + return 0; } return -EINVAL; } @@ -1264,6 +1292,105 @@ static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, } #endif +static struct gpio_chardev_data * +to_gpio_chardev_data(struct notifier_block *nb) +{ + return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb); +} + +static int lineinfo_changed_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct gpio_chardev_data *priv = to_gpio_chardev_data(nb); + struct gpioline_info_changed chg; + struct gpio_desc *desc = data; + int ret = NOTIFY_DONE; + + if (test_bit(desc_to_gpio(desc), priv->watched_lines)) { + memset(&chg, 0, sizeof(chg)); + chg.info.line_offset = gpio_chip_hwgpio(desc); + chg.event_type = action; + chg.timestamp = ktime_get_real_ns(); + gpio_desc_to_lineinfo(desc, &chg.info); + + ret = kfifo_in_spinlocked(&priv->events, &chg, + 1, &priv->wait.lock); + if (ret) + wake_up_poll(&priv->wait, EPOLLIN); + else + pr_debug_ratelimited( + "%s: lineinfo event FIFO is full - event dropped\n", + __func__); + + ret = NOTIFY_OK; + } + + return ret; +} + +static __poll_t lineinfo_watch_poll(struct file *filep, + struct poll_table_struct *pollt) +{ + struct gpio_chardev_data *priv = filep->private_data; + __poll_t events = 0; + + poll_wait(filep, &priv->wait, pollt); + + if (!kfifo_is_empty_spinlocked_noirqsave(&priv->events, + &priv->wait.lock)) + events = EPOLLIN | EPOLLRDNORM; + + return events; +} + +static ssize_t lineinfo_watch_read(struct file *filep, char __user *buf, + size_t count, loff_t *off) +{ + struct gpio_chardev_data *priv = filep->private_data; + struct gpioline_info_changed event; + ssize_t bytes_read = 0; + int ret; + + if (count < sizeof(event)) + return -EINVAL; + + do { + spin_lock(&priv->wait.lock); + if (kfifo_is_empty(&priv->events)) { + if (bytes_read) { + spin_unlock(&priv->wait.lock); + return bytes_read; + } + + if (filep->f_flags & O_NONBLOCK) { + spin_unlock(&priv->wait.lock); + return -EAGAIN; + } + + ret = wait_event_interruptible_locked(priv->wait, + !kfifo_is_empty(&priv->events)); + if (ret) { + spin_unlock(&priv->wait.lock); + return ret; + } + } + + ret = kfifo_out(&priv->events, &event, 1); + spin_unlock(&priv->wait.lock); + if (ret != 1) { + ret = -EIO; + break; + /* We should never get here. See lineevent_read(). */ + } + + if (copy_to_user(buf + bytes_read, &event, sizeof(event))) + return -EFAULT; + bytes_read += sizeof(event); + } while (count >= bytes_read + sizeof(event)); + + return bytes_read; +} + /** * gpio_chrdev_open() - open the chardev for ioctl operations * @inode: inode for this chardev @@ -1274,14 +1401,48 @@ static int gpio_chrdev_open(struct inode *inode, struct file *filp) { struct gpio_device *gdev = container_of(inode->i_cdev, struct gpio_device, chrdev); + struct gpio_chardev_data *priv; + int ret = -ENOMEM; /* Fail on open if the backing gpiochip is gone */ if (!gdev->chip) return -ENODEV; + + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); + if (!priv->watched_lines) + goto out_free_priv; + + init_waitqueue_head(&priv->wait); + INIT_KFIFO(priv->events); + priv->gdev = gdev; + + priv->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify; + ret = atomic_notifier_chain_register(&gdev->notifier, + &priv->lineinfo_changed_nb); + if (ret) + goto out_free_bitmap; + get_device(&gdev->dev); - filp->private_data = gdev; + filp->private_data = priv; + + ret = nonseekable_open(inode, filp); + if (ret) + goto out_unregister_notifier; + + return ret; - return nonseekable_open(inode, filp); +out_unregister_notifier: + atomic_notifier_chain_unregister(&gdev->notifier, + &priv->lineinfo_changed_nb); +out_free_bitmap: + bitmap_free(priv->watched_lines); +out_free_priv: + kfree(priv); + return ret; } /** @@ -1292,17 +1453,23 @@ static int gpio_chrdev_open(struct inode *inode, struct file *filp) */ static int gpio_chrdev_release(struct inode *inode, struct file *filp) { - struct gpio_device *gdev = container_of(inode->i_cdev, - struct gpio_device, chrdev); + struct gpio_chardev_data *priv = filp->private_data; + struct gpio_device *gdev = priv->gdev; + bitmap_free(priv->watched_lines); + atomic_notifier_chain_unregister(&gdev->notifier, + &priv->lineinfo_changed_nb); put_device(&gdev->dev); + kfree(priv); + return 0; } - static const struct file_operations gpio_fileops = { .release = gpio_chrdev_release, .open = gpio_chrdev_open, + .poll = lineinfo_watch_poll, + .read = lineinfo_watch_read, .owner = THIS_MODULE, .llseek = no_llseek, .unlocked_ioctl = gpio_ioctl, @@ -1513,6 +1680,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, for (i = 0; i < chip->ngpio; i++) gdev->descs[i].gdev = gdev; + ATOMIC_INIT_NOTIFIER_HEAD(&gdev->notifier); + #ifdef CONFIG_PINCTRL INIT_LIST_HEAD(&gdev->pin_ranges); #endif @@ -2852,6 +3021,8 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label) } done: spin_unlock_irqrestore(&gpio_lock, flags); + atomic_notifier_call_chain(&desc->gdev->notifier, + GPIOLINE_CHANGED_REQUESTED, desc); return ret; } @@ -2949,6 +3120,9 @@ static bool gpiod_free_commit(struct gpio_desc *desc) } spin_unlock_irqrestore(&gpio_lock, flags); + atomic_notifier_call_chain(&desc->gdev->notifier, + GPIOLINE_CHANGED_RELEASED, desc); + return ret; } @@ -3111,6 +3285,7 @@ static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc) if (ret != -ENOTSUPP) return ret; } + return 0; } diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index a1cbeabadc69..8e3969616cfe 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -54,6 +54,7 @@ struct gpio_device { const char *label; void *data; struct list_head list; + struct atomic_notifier_head notifier; #ifdef CONFIG_PINCTRL /* diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 799cf823d493..a37ad3d97c8d 100644 --- a/include/uapi/linux/gpio.h +++ b/include/uapi/linux/gpio.h @@ -59,6 +59,28 @@ struct gpioline_info { /* Maximum number of requested handles */ #define GPIOHANDLES_MAX 64 +/* Possible line status change events */ +enum { + GPIOLINE_CHANGED_REQUESTED = 1, + GPIOLINE_CHANGED_RELEASED, + GPIOLINE_CHANGED_CONFIG, +}; + +/** + * struct gpioline_info_changed - Information about a change in status + * of a GPIO line + * @info: updated line information + * @timestamp: estimate of time of status change occurrence, in nanoseconds + * and GPIOLINE_CHANGED_CONFIG + * @event_type: one of GPIOLINE_CHANGED_REQUESTED, GPIOLINE_CHANGED_RELEASED + */ +struct gpioline_info_changed { + struct gpioline_info info; + __u64 timestamp; + __u32 event_type; + __u32 padding[5]; /* for future use */ +}; + /* Linerequest flags */ #define GPIOHANDLE_REQUEST_INPUT (1UL << 0) #define GPIOHANDLE_REQUEST_OUTPUT (1UL << 1) @@ -176,6 +198,8 @@ struct gpioevent_data { #define GPIO_GET_CHIPINFO_IOCTL _IOR(0xB4, 0x01, struct gpiochip_info) #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) diff --git a/tools/gpio/gpio-watch b/tools/gpio/gpio-watch new file mode 100755 index 0000000000000000000000000000000000000000..d67bf021afad6b6eec40cf10b673c5127e55b0da From patchwork Thu Dec 19 17:15:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1213566 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="aSHjLTFS"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47dz6h1dyJz9sR4 for ; Fri, 20 Dec 2019 04:15:56 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727191AbfLSRPx (ORCPT ); Thu, 19 Dec 2019 12:15:53 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52903 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727181AbfLSRPw (ORCPT ); Thu, 19 Dec 2019 12:15:52 -0500 Received: by mail-wm1-f65.google.com with SMTP id p9so6238610wmc.2 for ; Thu, 19 Dec 2019 09:15:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=v5x4fAlkEMznzVftaw8KRmxYt8mx3j16Z+4NeG2Skcw=; b=aSHjLTFSDYBJLT4PzQIvewXR3v8S7vek5JhZlt0HKFpRbOHDEsBarazWmtVn+C3t7w j5RSN5OUc1YL6zDqp8Wi8RwZOlUo58QUdgt8hK6zTXE7m1Ryt7eiBDsBcG3miIKRAWwQ d1YZp4bjzODvS7DQMBs2dkPBs3lSWB57QjvaffXtU2FgfpsiQ25z5n0VjlVTKjck9pRE Aj5n8dmIqfBqjlY/e8dRQAjuFK/YXtEZ6UfRPJnpDal+zbe3CWMAk34RmcXMSJ0s2bnP zaMH+Mj8RmEksjNfe4hQzlAcR2ByiNfNwLaQ9V91n3scwK73fuNIskiYLlkqTSGnJlYB amSw== 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=v5x4fAlkEMznzVftaw8KRmxYt8mx3j16Z+4NeG2Skcw=; b=Gs0SKS0B8lasmRn0Tp/zTVOENp9L+JLahYtuxRJYsWoH5j10T3XwczgOoU0uSXpcVP 0QGtfMOQC2PAwXHIv16ALeT3peibP5hHLjOanfFmxU9xz9jt986Gsrc1pfyRsfYz8ptO QeW1TRJ7jn8OlBn2+PORuVOFE/ga2ZsLNYVG3TnJ4EGCrkY9mhUryZacyA4frCFPZxXq ax0sGTrFShzp/AcHWKof5nZ8exv4NHrtKOSAef26E8I5rOr3mMpKYpxKIm3l6MY+jAH0 DV0sIU0WRBGph5Wi6F4UAk695O302DGlHXC39rQxoqzFebUz1Wvek8Mveamz6zSVsWBN 24DQ== X-Gm-Message-State: APjAAAWEebPMWR8McbRusXJoiRCbvbdoETjBPcM7J4zmE96SqQv1Vdqy 50T5trplNwhZBOOLDUdQ0sM+SA== X-Google-Smtp-Source: APXvYqyPvNjyc3CJ00iQM5C3A5G3FIWmtY9FTqHMEOxl2cT3ojLyyCEX6D8ecnQZ1EN3hIxoe/PhIg== X-Received: by 2002:a1c:5a0a:: with SMTP id o10mr10945123wmb.114.1576775749893; Thu, 19 Dec 2019 09:15:49 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id q6sm7401428wrx.72.2019.12.19.09.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2019 09:15:49 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Greg Kroah-Hartman Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v3 13/13] tools: gpio: implement gpio-watch Date: Thu, 19 Dec 2019 18:15:28 +0100 Message-Id: <20191219171528.6348-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191219171528.6348-1-brgl@bgdev.pl> References: <20191219171528.6348-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Add a simple program that allows to test the new LINECHANGED_FD ioctl(). Signed-off-by: Bartosz Golaszewski --- tools/gpio/.gitignore | 1 + tools/gpio/Build | 1 + tools/gpio/Makefile | 11 ++++- tools/gpio/gpio-watch.c | 99 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tools/gpio/gpio-watch.c diff --git a/tools/gpio/.gitignore b/tools/gpio/.gitignore index a94c0e83b209..eab36c6d7751 100644 --- a/tools/gpio/.gitignore +++ b/tools/gpio/.gitignore @@ -1,4 +1,5 @@ gpio-event-mon gpio-hammer +gpio-watch lsgpio include/linux/gpio.h diff --git a/tools/gpio/Build b/tools/gpio/Build index 4141f35837db..67c7b7f6a717 100644 --- a/tools/gpio/Build +++ b/tools/gpio/Build @@ -2,3 +2,4 @@ gpio-utils-y += gpio-utils.o lsgpio-y += lsgpio.o gpio-utils.o gpio-hammer-y += gpio-hammer.o gpio-utils.o gpio-event-mon-y += gpio-event-mon.o gpio-utils.o +gpio-watch-y += gpio-watch.o diff --git a/tools/gpio/Makefile b/tools/gpio/Makefile index 6080de58861f..842287e42c83 100644 --- a/tools/gpio/Makefile +++ b/tools/gpio/Makefile @@ -18,7 +18,7 @@ MAKEFLAGS += -r override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon +ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon gpio-watch ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) all: $(ALL_PROGRAMS) @@ -66,6 +66,15 @@ $(GPIO_EVENT_MON_IN): prepare FORCE $(OUTPUT)gpio-utils-in.o $(OUTPUT)gpio-event-mon: $(GPIO_EVENT_MON_IN) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +# +# gpio-watch +# +GPIO_WATCH_IN := $(OUTPUT)gpio-watch-in.o +$(GPIO_WATCH_IN): prepare FORCE + $(Q)$(MAKE) $(build)=gpio-watch +$(OUTPUT)gpio-watch: $(GPIO_WATCH_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + clean: rm -f $(ALL_PROGRAMS) rm -f $(OUTPUT)include/linux/gpio.h diff --git a/tools/gpio/gpio-watch.c b/tools/gpio/gpio-watch.c new file mode 100644 index 000000000000..5cea24fddfa7 --- /dev/null +++ b/tools/gpio/gpio-watch.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * gpio-watch - monitor unrequested lines for property changes using the + * character device + * + * Copyright (C) 2019 BayLibre SAS + * Author: Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + struct gpioline_info_changed chg; + struct gpioline_info req; + struct pollfd pfd; + int fd, i, j, ret; + char *event, *end; + ssize_t rd; + + if (argc < 3) + goto err_usage; + + fd = open(argv[1], O_RDWR | O_CLOEXEC); + if (fd < 0) { + perror("unable to open gpiochip"); + return EXIT_FAILURE; + } + + for (i = 0, j = 2; i < argc - 2; i++, j++) { + memset(&req, 0, sizeof(req)); + + req.line_offset = strtoul(argv[j], &end, 0); + if (*end != '\0') + goto err_usage; + + ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); + if (ret) { + perror("unable to set up line watch"); + return EXIT_FAILURE; + } + } + + pfd.fd = fd; + pfd.events = POLLIN | POLLPRI; + + for (;;) { + ret = poll(&pfd, 1, 5000); + if (ret < 0) { + perror("error polling the linechanged fd"); + return EXIT_FAILURE; + } else if (ret > 0) { + memset(&chg, 0, sizeof(chg)); + rd = read(pfd.fd, &chg, sizeof(chg)); + if (rd < 0 || rd != sizeof(chg)) { + if (rd != sizeof(chg)) + errno = EIO; + + perror("error reading line change event"); + return EXIT_FAILURE; + } + + switch (chg.event_type) { + case GPIOLINE_CHANGED_REQUESTED: + event = "requested"; + break; + case GPIOLINE_CHANGED_RELEASED: + event = "released"; + break; + case GPIOLINE_CHANGED_CONFIG: + event = "config changed"; + break; + default: + fprintf(stderr, + "invalid event type received from the kernel\n"); + return EXIT_FAILURE; + } + + printf("line %u: %s at %llu\n", + chg.info.line_offset, event, chg.timestamp); + } + } + + return 0; + +err_usage: + printf("%s: ...\n", argv[0]); + return EXIT_FAILURE; +}