From patchwork Wed Dec 4 15:59:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204254 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="sp0X1oV1"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkBK5g6Bz9sRR for ; Thu, 5 Dec 2019 03:02:01 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728536AbfLDP7U (ORCPT ); Wed, 4 Dec 2019 10:59:20 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:34405 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728344AbfLDP7T (ORCPT ); Wed, 4 Dec 2019 10:59:19 -0500 Received: by mail-wm1-f68.google.com with SMTP id f4so5163210wmj.1 for ; Wed, 04 Dec 2019 07:59:18 -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=mslRHHSp/cuNnBW29h3HN5M5/Ygh6sAx6mKJrlS5yNM=; b=sp0X1oV1/HpAXBOrL6SJD4ucAhA5yp4me4sYOvskJPG+WgtnSicq1AE7f3IRNCR+nz ubKCiY+s0tqSxNQgr6Mrh0OVhZHGO8Po836TRyl/IIoU/AeuRAO3GZc+ygoozhvY27GD ni8goZVEGJLE+3itAVgt76mf4jek0kJtsugsSgBJJ/AvlY5pb0WhvwEorznNM8AQ29yd 6XGuKGzQKKuI2/f1H5aZmT28BB+UDMVzdbqU9DgeidzeWF/xGh41zroMDp+8GRszlS7r b7QoL8lwNWAys6rB8vF3nOXMPWLMdJcimM/56HDYoF+AjAdgIQqeQpXPO7GXxjY4sXiE t28A== 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=mslRHHSp/cuNnBW29h3HN5M5/Ygh6sAx6mKJrlS5yNM=; b=TS604HgiQtUxWNfPyWcK38cbiBt0d8krVQd7LEjHQorgqiDDO7KESBOxsLKWghFcCz AYdzI8ZEzwbv/4CAqAjfWaH2JyWkpFDiZ04QkrRddjy583v90/X5VVVEEhw4Lw2szGeq RAr4BsFBxaAl04X3+1aMeJTszs34xNWq3YKmwKPQWivEYPPNseQpySc3Uo/sAy8hRXec mXZQf5AlFle94zuNpVqnw+sblnnX5GhQkSTcbw6hc3Bm5Y1SpL/gEf+W6X+WpX/fnz+E yZJrDgg4jhxfl4gM38AbyJXFG6uVjgSj25LEg1uSsb2w0iQi42Y2YCap2hRDSqcj35Vv slog== X-Gm-Message-State: APjAAAXQQ34vhmr6qUhkwR75OXQpbPXPzpQDPzUdjXW11cebmDaubg5g LHxEBmhGRSV1DBTQu1vllDbzDw== X-Google-Smtp-Source: APXvYqxUV3B6D/kjwYQmFXK7hNpMMPQEwApjdkfCje2gxgeJX1iREqHUEw1ZPy6Y5EnCjGzk2JiU7A== X-Received: by 2002:a7b:c392:: with SMTP id s18mr153127wmj.169.1575475157805; Wed, 04 Dec 2019 07:59:17 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:17 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 01/11] gpiolib: use 'unsigned int' instead of 'unsigned' in gpio_set_config() Date: Wed, 4 Dec 2019 16:59:04 +0100 Message-Id: <20191204155912.17590-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204253 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="S/O8YBZS"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkBJ2gn2z9sSH for ; Thu, 5 Dec 2019 03:02:00 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728546AbfLDP7V (ORCPT ); Wed, 4 Dec 2019 10:59:21 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:32948 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728529AbfLDP7V (ORCPT ); Wed, 4 Dec 2019 10:59:21 -0500 Received: by mail-wm1-f66.google.com with SMTP id y23so5175244wma.0 for ; Wed, 04 Dec 2019 07:59:19 -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=jMGWdghs7WZhxPpptxQGrZdPfirAzMk9gNZC1ILuwUo=; b=S/O8YBZSycJTpuK10zc7KAvf+7aQqZEnGPXRzEEiiuUw5Wzr52xZY/r78S32x546zu gyD2vxX9IXv0QpI7uHztbp7HdiWF2hrw41GsPU9TsPtKAfqo/MsQP3HPFtUC7YijfsGI 3JnrAGd2oOD5hl9rvSB6cftgCIedWRloaiv0yXh7uAn8xul6jzbQn+AB49M4bwHFcQ+a wn11gb5YpmliAQLjZMEYwwe9HeNLYBRFDQzh+PRzty6DKIizeZGSKGOhqwNqnqHpD7di 0jkihd7k2DeAF9ozyyQeYeRBPCjIvmt9+FSiUdc5fXArTgaGhjtyKKCA2oqQmhwXBcgW G3Sw== 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=jMGWdghs7WZhxPpptxQGrZdPfirAzMk9gNZC1ILuwUo=; b=YvpkudCgnYT9lN3tEVIntw5nPu0awh3KDzXGIKt8tRJVw7Rh+1c+Lwi1qhmQMhkVyc 8k4gt0sqYMRFR+UpgYKnh4M74DCWMdL0ZMCm7Ou4Thd0sBZj5dEqfRH9h79H/s8dBMPj 9mGQ4721BSPp0iULouy++vJ56Wl+Q8aSZuEQi/qSSet6qd4oqYvm9tWcrRgdAmf7ovgY m3DyT0eFo/tC4RGUszT3nZhPWWVnsYnlSwGoMeMgJABDjgHy/PtUnZ272Mo4N0Zhz4em l+svyII52WpNSNiyKntqywnsDtSQuj+QZ4shwJkgZWAp3h0oRZn5ImwIn699GTuAhbYZ m8ng== X-Gm-Message-State: APjAAAXnPD7RTe8UrZkYek3wrwtYFMo7xvchZMUXsnKUsjf9yVHPmt0G KDNwXEFGzPDgy7LlicLFxSy6KQ== X-Google-Smtp-Source: APXvYqy2aG07PCeTt9lhhbHZJTbfVLQo5zbfEX+vCJcSWPicgFnX+mDn6emK2yNDyTl0uoIYiyytsA== X-Received: by 2002:a7b:ca57:: with SMTP id m23mr297179wml.65.1575475159138; Wed, 04 Dec 2019 07:59:19 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:18 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 02/11] gpiolib: have a single place of calling set_config() Date: Wed, 4 Dec 2019 16:59:05 +0100 Message-Id: <20191204155912.17590-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204240 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="B32Qb469"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Sk7K3hxFz9sPf for ; Thu, 5 Dec 2019 02:59:25 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728558AbfLDP7X (ORCPT ); Wed, 4 Dec 2019 10:59:23 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:34408 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728538AbfLDP7W (ORCPT ); Wed, 4 Dec 2019 10:59:22 -0500 Received: by mail-wm1-f67.google.com with SMTP id f4so5163314wmj.1 for ; Wed, 04 Dec 2019 07:59:21 -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=2P46jLQH53L0bSs7wrWGmrID6nlk6qTfagoHAkQM3lw=; b=B32Qb469tJrUvZwxSz+ciTPGZSLALv4cnX8nNY+HzhvzW0q7h+Swub3sIFplbjTfDX 94FApbkAPC0h2aTaIivPXrhTdCn9J4+Zg4FZ8v4ykMIbXA43JlIZyW+NZh85tOO28/oC zkgtv5DErDQW4ABnU9P7bqVQslSaYxYrGCTWWJtpTzRz2i46dIVXut9E5XXDKL/mFR49 2y0HXQ2zJMacf+2uEmIN9DZjwKHj7Yik3VkfDao/3sGs+8g6Wv7tGDIDNFEYSRryIgU6 yhwD5+UTNAe36sgaOoufE3JFsmxebSYyB7wPxkkGPHbteFQdx1eKPzR/HJlp8BGiPSFJ H3PA== 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=2P46jLQH53L0bSs7wrWGmrID6nlk6qTfagoHAkQM3lw=; b=HB8IZWgj9IYxfogQ/RCNHO04PiUOThU8k75gJrBYjGeg024hheiYx6Im40o+6fyEBD aO8NsHJK7+RPXNcaPy6ul+Y/Su4hydAdRILEVqxAC6a9qC4yFTeUrIaYjKH1mAM3TxV2 Fs0/qki+spuWUfd0WvZoN5Kjs4+LAy0X8zeXjl4CQ6nYtZOkPqPferQ80z8PhZyolNBC ooSWlZjkNKYGTmc7mPEeOfTCv85XNW4N4NDEQpeml7OSzKcu0v3ckXx6XWt1q7NGfvqc EBqzXG3hXhlx8729TRgSjcWRuyJi8UPwVcb8PAy4hiGP8+817aWre1JHDOxFu+h3u+AE rq0g== X-Gm-Message-State: APjAAAVX9gAEIzifId52sDd9H3uwNRwXrgdXxuTO6ZeKD3u13jT3k0+4 KD8bbfTeHbQAQ37bqaIWY/MztQ== X-Google-Smtp-Source: APXvYqxcnyVANW5BbUe14kf5FOQUIsuf/S8nNeBiuLjBJCSwpKbiIUIJW5ycBV758qAz1GFH6Oy7/w== X-Received: by 2002:a7b:c3cc:: with SMTP id t12mr250863wmj.121.1575475160462; Wed, 04 Dec 2019 07:59:20 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:19 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 03/11] gpiolib: convert the type of hwnum to unsigned int in gpiochip_get_desc() Date: Wed, 4 Dec 2019 16:59:06 +0100 Message-Id: <20191204155912.17590-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204252 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="mmTkweyB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkBH6JV5z9sRR for ; Thu, 5 Dec 2019 03:01:59 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727989AbfLDQB6 (ORCPT ); Wed, 4 Dec 2019 11:01:58 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:53826 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728482AbfLDP7Z (ORCPT ); Wed, 4 Dec 2019 10:59:25 -0500 Received: by mail-wm1-f66.google.com with SMTP id u18so247446wmc.3 for ; Wed, 04 Dec 2019 07:59:23 -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=90rmEcHqIRhcxzdKSYRA7QCcxU0JzYE9Vo2kuWKmjpQ=; b=mmTkweyBNsjrZtAgXPBok8OBhgsfAz3TFza+xoR1riA3xSrtbkIcz4fwWBJ/evT10f /poQc8k9UAEHBjWrIG0kZDOEq2fAvw3NG25cCuJWNSBepJOfMLXyiHt9Ef6VKw7lzHZY 7wbf4NnnNz+RhwliSS/B5oFDjr0QxZxPyZ47zxk9+bxAzQaYTks0BfCWjHRAQ6KOq1WN 6iML7GugNGjIRV3a8Rk8a8cJ3SkvwPrvDoimoncTzbeti5BFPQq82SpmrUzN+6iWQmpj g7MKCUPuE6o78C+t6DtYIt20enxGcn9BOIZ/Z5LEZfI8ZlVMlY7iFjyFtu3vvWxbuNOG AepA== 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=90rmEcHqIRhcxzdKSYRA7QCcxU0JzYE9Vo2kuWKmjpQ=; b=GhlDubej3/CrUfR2z1EJ0CQDNyEAqOykdd1eORJHFDR1aBmsOrnwZ57UWxzFxe+Eir RLpuptxe5yOOB5bouJsRtl0cpaqTy/h1dZhcb91sjdXFEld6fJHscdFkElyukrFbSplx pQah+sSG3h7xos3ZZCE+jPOFfvy5YnGiFlsz9f3Hi4tgFc9UW/sAx1jJRBMniTyvKusn /2n9JNoR4cTY7rXqJ5NZq5gRUr5PeHoHib6u7M6NsPaEkfa5ekPh1RviJRQHMGpdQ8R+ CIa7ZCnhpqGjKyDfNUhzSXV97GLKMy+3+qu2H5LSPX+RElRYRl8a78okTU9NSYgnD+BE +EAw== X-Gm-Message-State: APjAAAXxB5ue3883atX2/f/FBsRYv/V+zsKJuq3X+yIMkhTAkvCN7MRY L0m9uEdB5G6n1kmsSGhKcIbNHA== X-Google-Smtp-Source: APXvYqxDar3z8bnynnE1xVqsig1swJfibpNd7XiIpnAOj0XSqrF4GPSvryLVKQdkUNIUq44PQyRyaw== X-Received: by 2002:a1c:3d8a:: with SMTP id k132mr193843wma.144.1575475163325; Wed, 04 Dec 2019 07:59:23 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:22 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 04/11] gpiolib: use gpiochip_get_desc() in linehandle_create() Date: Wed, 4 Dec 2019 16:59:07 +0100 Message-Id: <20191204155912.17590-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204251 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="T6VXt8MI"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkBG0fDqz9sRR for ; Thu, 5 Dec 2019 03:01:58 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728572AbfLDP71 (ORCPT ); Wed, 4 Dec 2019 10:59:27 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:54277 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728564AbfLDP70 (ORCPT ); Wed, 4 Dec 2019 10:59:26 -0500 Received: by mail-wm1-f67.google.com with SMTP id b11so240294wmj.4 for ; Wed, 04 Dec 2019 07:59:25 -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=PwQtKdAUQY+SBl5OsHs80mv2U9tLV35lkOjHDH8uz/U=; b=T6VXt8MIgYcU7MsY3TdXMqG4/4HRWYESABPYgT0VZoaE8H5/6QTNNqzLD5Fongh5Tb b5c6LbaV+5bgyTMxcNS6MkTu6OfOzoVpNioIMUXWSWd90IWYsnDEpqZfY/23mJoRMli7 JIk+qU5sKkCujWkOd5jh43Xg3azL//AlEYWP7TzpLe7pvRmELIiukwSk/bxGz4HuAdfe 6dkSfCEPXYp5Z8FfDp+FUAu4y8/065sYSdQbVu2Q0+Rk8Xdxi6rvgiv9el7E0+qH+Y8I lkBW1FXom8XEsWCOoity9VRoOjQxd0/TZ4qcBcuS2TIzgZIzvCfP0o4iZ1ourYKA0Ux0 IvMQ== 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=PwQtKdAUQY+SBl5OsHs80mv2U9tLV35lkOjHDH8uz/U=; b=YAYkHyKLZObMyHUsI+zZ3PFLN9cTGuo8ckboaYxrmafxlxeCcHnYt6eCE0BN1OpCy1 9Tu1nstgfKHQw//qsmcnt5tVAO/wswm3rTqZf5J17TMwdqAA9dp5z9+N/9GbZqxpAJo2 NnE6jyU+Q2YhCe4FbLpmdpaoXkK/D0lrZfvQz0wDI2wZz49VvESaZVOoqR9Zl2/QujWA BY+6fjGQjXPLnrsFJFfEczTVjph9N+QlkzmS3gpogbsK0O5IMBquUH+y7xQBsl6uQtqs IW150QuGW/4JRxKMeIgaddh5Pont/dtFxWKfrL7YRnBod05gnLOu2F2tlJduojy97/DI x7QA== X-Gm-Message-State: APjAAAWGCQ0SrJ4Aa4xcmWtBHIaEByEiQp8azKvbmOuBKzc9ZNva6/y/ DG54SwQHNHZh7xTfT/PXplD2dw== X-Google-Smtp-Source: APXvYqw3j9t+sv27rxv6BsQ+49qDiWvcgmeoGIG7o/TNn3GxBxvIPxAHFn+65IIuWJboL7cqmVfOJw== X-Received: by 2002:a1c:8055:: with SMTP id b82mr159701wmd.127.1575475164550; Wed, 04 Dec 2019 07:59:24 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:23 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 05/11] gpiolib: use gpiochip_get_desc() in lineevent_create() Date: Wed, 4 Dec 2019 16:59:08 +0100 Message-Id: <20191204155912.17590-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204250 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="kDu4Z9b9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkBD65gKz9sSH for ; Thu, 5 Dec 2019 03:01:56 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728598AbfLDQBv (ORCPT ); Wed, 4 Dec 2019 11:01:51 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43026 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728565AbfLDP71 (ORCPT ); Wed, 4 Dec 2019 10:59:27 -0500 Received: by mail-wr1-f67.google.com with SMTP id d16so4603407wre.10 for ; Wed, 04 Dec 2019 07:59:26 -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=mx3ZyJkzQPPgrO9SEUEcMUEUo3OWsFykUUjXQ0ufMTY=; b=kDu4Z9b9TDdsQYYk9Aqa2AQIyrhUX8gwTrtcLtcSwl0pJqKg20tfOsdEPijAgMn5+z nY/4Nfgt91kwHD1aGecwkRT5WdBHqkxj+s1RvC+ZtbtSLKkGuV9HjV9lG7Q1YrDhpI9o oVRiSevz9IxfxKgKJDlzybR18/LNWQKvUNUdGFjJwEpiu9AlXo1nOz+aGNfIbXzFacCg DKjwnNc4qvfGZuXuJl6CEdV3m9VvX4wRUy+2X9p+bOECsdUDpnHeViZWGGSMPzoGC7N3 wPvEeEvy/FYGjjc9b8o/Mf7vAZzBUEbMx8P/7b8fF2jwRHHIDHeGVfsvIUqeC4nQWOw2 pYwA== 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=mx3ZyJkzQPPgrO9SEUEcMUEUo3OWsFykUUjXQ0ufMTY=; b=odLskaFqvfnExwMlvqMv68OwsYKmOdcSwJQJJVvEKjihQAI485F/BmTVR4DzCbxiQc PiG/Cj7ISKsSU2hQl8ey7FcQzN3fiQHVsxGD+i5RMQ9+uz+HPmJjlxkobW761k/PsDkl wxK1SzN4u5UZOVGxHyWd99JjhIACKEYFChUHDJCevXJd/U5cZX9ESDreZKsC+TJY4EvP G/P3sB8HTbKZlOLi81Su0eGZpzAxePU3SmUhMGS9s7Im3uzGOVNGUBpFF7qV3Ty1053U wdmYgrrJxF0PucDK0zUxPisuWzi3mxPqhFF2Fo0Ei2Jhjs7GvHwUpwWvDF0RdpiV1iAq BA0w== X-Gm-Message-State: APjAAAW546SXh5VkTE6UAsXBGiKpTOCHdHoTKx7KuXPIQiCuZv/GEinH aR4mu2E3VnwS/aCyW3sc+NlgFw== X-Google-Smtp-Source: APXvYqzBZmJRWvmbxNfjw0aGh+a1bvW1bU9MEAlgciuDoB8pzU/15i1PHt+f+DRqxgnI4+LF3onsxg== X-Received: by 2002:a5d:5708:: with SMTP id a8mr5111893wrv.79.1575475165950; Wed, 04 Dec 2019 07:59:25 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:25 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 06/11] gpiolib: use gpiochip_get_desc() in gpio_ioctl() Date: Wed, 4 Dec 2019 16:59:09 +0100 Message-Id: <20191204155912.17590-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 Wed Dec 4 15:59:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204249 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="rmBYXtdu"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47SkB33txYz9sRd for ; Thu, 5 Dec 2019 03:01:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728803AbfLDQBq (ORCPT ); Wed, 4 Dec 2019 11:01:46 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:37990 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728587AbfLDP73 (ORCPT ); Wed, 4 Dec 2019 10:59:29 -0500 Received: by mail-wr1-f68.google.com with SMTP id y17so9238469wrh.5 for ; Wed, 04 Dec 2019 07:59:28 -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=CdsiFTfek5xLI09RRog+58+WxPNQjw5a0l6zl3ADLU8=; b=rmBYXtduNWBjbJdBB+q6r8IPnN9IU+yx+mQDtMjrL6H4bTPot1sIMDnhXMoAJg8zR1 MWtQXlxfH1Lo+XbbhWyVrezj+c8MS5InDMMXyMEkMFKiwiZ3jxn214vTKImaqO814FHS ML5WqrnkJaDMVU9EvxqemBXs7quBnEfMdz5rpNlXNxspdd7N4Zhtx0W5Gl4Vl9iTlrzr aBB+6xVLC+Rg2FJcTlkkgXyzMHveem+THF9aBXQo0fvoq/AKIQQzXLt0DGSaP7mnrVHf qs4X4sIz7Ylf3KCih22Pl+za7oHFWJuoNuREYki9j/GxBoQ9+9uuthx2b2ud7hAR9iyI bRMQ== 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=CdsiFTfek5xLI09RRog+58+WxPNQjw5a0l6zl3ADLU8=; b=IJd5b4duzACMUOU0RY29E9jNa26FQJoA+f4SF0oNpiXF53Psfp3sXT+eGDVVrLAZr4 HY/bo7CU/oGJ0kZLZEYfjFe8ZeyiJhwb7A/A4fYHjzmLrMmQT56h47GDsxzW+eGLX6Ti 49EQBnFef3mM5w0mZoCprqO6sCQqJO22JKDpwymx6VP3A59vhjTMARgqHishstJR1xMY bRRAogTHiVKyix+JsivgP+i74pU7plmYuNHCIvfTdy0LcY2bZvP0yb9AMJhkbrTfkn7m tjLnRuk9/ZS8yNKOIOpdfFS0qbYmB3BC+faIo+cRbFdkg6dEey4edVzsUe9VpxW95O3t uXHA== X-Gm-Message-State: APjAAAXh0KNJeERVKA8W1HEQJR6I+BaFLSttEUr+G0nXL8YoBrv8aITc iCt8ZHCxB998oFN5VpgyN9Ys/Q== X-Google-Smtp-Source: APXvYqzgSN35FsKw+z4ACL3rPKUl4UEmfcPcuA9exxMvkFs/K/t3RLZn3dchtV+0t9ZL9bpN7xdjUg== X-Received: by 2002:adf:ec48:: with SMTP id w8mr4976853wrn.19.1575475167360; Wed, 04 Dec 2019 07:59:27 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:26 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 07/11] gpiolib: rework the locking mechanism for lineevent kfifo Date: Wed, 4 Dec 2019 16:59:10 +0100 Message-Id: <20191204155912.17590-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 | 54 ++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b7043946c029..43f90eca6d45 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,8 +815,10 @@ static __poll_t lineevent_poll(struct file *filep, poll_wait(filep, &le->wait, wait); + spin_lock(&le->wait.lock); if (!kfifo_is_empty(&le->events)) events = EPOLLIN | EPOLLRDNORM; + spin_unlock(&le->wait.lock); return events; } @@ -831,43 +830,47 @@ 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; int ret; - if (count < sizeof(struct gpioevent_data)) + if (count < sizeof(event)) return -EINVAL; - do { + for (;;) { + spin_lock(&le->wait.lock); if (kfifo_is_empty(&le->events)) { - if (filep->f_flags & O_NONBLOCK) + 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; + ret = kfifo_out(&le->events, &event, 1); + spin_unlock(&le->wait.lock); + if (ret == 1) + break; /* - * 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. + * We should never get here since we're holding the lock from + * the moment we noticed a new event until calling kfifo_out() + * but we must check the return value. On the off-chance - just + * go back to waiting. */ - if (copied == 0 && (filep->f_flags & O_NONBLOCK)) - return -EAGAIN; + } - } while (copied == 0); + ret = copy_to_user(buf, &event, sizeof(event)); + if (ret) + return -EFAULT; - return copied; + return sizeof(event); } static int lineevent_release(struct inode *inode, struct file *filep) @@ -969,7 +972,7 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p) return IRQ_NONE; } - ret = kfifo_put(&le->events, ge); + ret = kfifo_in_spinlocked(&le->events, &ge, 1, &le->wait.lock); if (ret) wake_up_poll(&le->wait, EPOLLIN); @@ -1084,7 +1087,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 Wed Dec 4 15:59:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204243 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="Vt/6D1lS"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Sk7j0DfVz9sPn for ; Thu, 5 Dec 2019 02:59:45 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728539AbfLDP7a (ORCPT ); Wed, 4 Dec 2019 10:59:30 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:46711 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728595AbfLDP73 (ORCPT ); Wed, 4 Dec 2019 10:59:29 -0500 Received: by mail-wr1-f68.google.com with SMTP id z7so9166467wrl.13 for ; Wed, 04 Dec 2019 07:59:28 -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=0HHgoLgFHf2KTo5tf1P+Sd12YWIyt1YcyhSjGYkb4R8=; b=Vt/6D1lS08tFWOArLQbOP8E/WEJ+/7lyTXHQCXBXklKYAc1dmvCjy97Idok02qrp+G /iRmBekwSuOk5CHDHIX0jvI9QPjRJWcHhJaALqciMpbsAKzJdeoitRdEQwyUY1vktAar VPIXLw0dRbLHP+AQ1WhHMfRB4wUZg0DUwOIAyaT4lNRu80JU/Zg8cJ7ElWh8qU6QzG0U lmTPo0OKpqHTbbTFqeHv5gxwoVkigvqAzTDv4FJARCSJIQjwo7GRlGUMzmXyc5/41yoh ZLBsvPTo+mcBLkasVIMGfTcYaaSvjx3JNS9np3G5L2MjiL5jLggwH3vYd+I84iv4PmrZ HfEQ== 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=0HHgoLgFHf2KTo5tf1P+Sd12YWIyt1YcyhSjGYkb4R8=; b=H9bSltSMOZrLXIYtiA1fPpblKoMYXKoMsebaUcHS8rRFmos1guD9fW5VYmb+xqeDSF LfIGeRoM446emBMqy6c+lrCIxHjgzs3uoF+PqZJLQGDHQTtrySD8quABVJ8ZKnkBLxDe UekrHg5MXyL1pDFHKWwxcC8HU6Y5YG1GWgQSRno+rSSgW/aQKxTiAy9VY0PlOxC/THsJ quZelHsC80gK4VRTQ/i8pkuIJL0s6bwknISemFjaTw7RhABa6Qs8HfVA2oNtdjAKH5Ou 9Hbs44J+fWuad3kKritQa7yAR7BB2vz12BDOASCzNY3/ohh1M+YDqZ2aKAnIp+OX91Gl i60g== X-Gm-Message-State: APjAAAVB69d6gTGG+A0YC3ux2ddnkPiD0QCQW/jxA5Vzcr0EQV6h+L6q PoOIwGRzdD5gG1KZaZGhOYN8wQ== X-Google-Smtp-Source: APXvYqx45bZ9826Rs8lcmtiIU4mfdZWsykH+a2xcXPaHAxKIcIt36IpO8A21JvhcrlmQ6H6Rwr5GQw== X-Received: by 2002:adf:eb89:: with SMTP id t9mr4904521wrn.5.1575475168318; Wed, 04 Dec 2019 07:59:28 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:27 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 08/11] gpiolib: emit a debug message when adding events to a full kfifo Date: Wed, 4 Dec 2019 16:59:11 +0100 Message-Id: <20191204155912.17590-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 43f90eca6d45..c89d297da270 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -975,6 +975,9 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p) ret = kfifo_in_spinlocked(&le->events, &ge, 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 Wed Dec 4 15:59:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204242 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="X+uook1k"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Sk7h0r0sz9sPf for ; Thu, 5 Dec 2019 02:59:44 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728241AbfLDP7m (ORCPT ); Wed, 4 Dec 2019 10:59:42 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:37993 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728607AbfLDP7c (ORCPT ); Wed, 4 Dec 2019 10:59:32 -0500 Received: by mail-wr1-f66.google.com with SMTP id y17so9238590wrh.5 for ; Wed, 04 Dec 2019 07:59:30 -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=UOWwdCy6TVkju/XVgGYrNAwqmnFLPpHnEAl2yx/CBt8=; b=X+uook1kRgRvmE3McO9/BVwAAZr04AoodqHjOIAmN5n2dNHYyv9wnrCFc7p4ZfwIUq aIftfQ1yxXnKD2DorRPeTA2RzHIg7M+efba4oCK6k8QdovXqUfPIwq9AJ2Qcs8h/38P1 Gb3TB4LynazhBAZfqLQitm3PilyB3+RxPvaZNs5PmTNpwaxAWxLT9Tz0Sv7q3jdT2JrL oA8c6D9J6F9wE4DAaC4/mcXHOBgKvTBV0uPpCsUHd6W3tCXuuZR0onSsA3vSQ3+aAgDP ABki0FN/bur1VoZtBz0Pou7Aijz60w1Sw0jkPp5WxVj2yH3ry+Qsrkk6WVwfFsBcXoK3 JQIw== 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=UOWwdCy6TVkju/XVgGYrNAwqmnFLPpHnEAl2yx/CBt8=; b=SoHFbsHHmHcPSe/uyCxxF0ICtHQUcwz35IZ7s1e55R7Jvk+D4MKAAjxj0tbVECNs51 kju42uCO0dT5AZwRPEqZnZ15o3gcT3lo5bMAPsQhoVEXeQsHFMklhGrQODkEEL2c365O u5g9BScwE7+eppAGrqduV27LuuLAJv6MgxjrzgVDEDe47KoUvT3gBQTLqJBHqUXBh2Iq Pu89zMiHtXTtNw2GuZQNc5T0csg9bGn/6b7gSyqcix2e/Su06XaaYgOoza/W4MYS2CE8 j235N/Ddma5OuCfdG+JnxVV4i1UaMck38mqe2q+7aXxadyDLFaNsX2Dj+x/K6D6DQXp6 0d1A== X-Gm-Message-State: APjAAAWncNZeBk6aJoaDy3NixMcB/TMcZ7tI8E4T+8gf/Kpo44pZ57at Y25tOLBJLeWHIDUqiWk5fjjlGVNkYA4= X-Google-Smtp-Source: APXvYqyuvpUu6t8eK96hpQZQvg2O8GUu1xSz5TTabWxuZMWlPkfAxDcV1SufBPt83Rn9kt6rWiUy0A== X-Received: by 2002:a05:6000:160d:: with SMTP id u13mr5041318wrb.22.1575475169910; Wed, 04 Dec 2019 07:59:29 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id u18sm8640508wrt.26.2019.12.04.07.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:28 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 09/11] gpiolib: provide a dedicated function for setting lineinfo Date: Wed, 4 Dec 2019 16:59:12 +0100 Message-Id: <20191204155912.17590-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155912.17590-1-brgl@bgdev.pl> References: <20191204155912.17590-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 c89d297da270..711963aa9239 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1145,6 +1145,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 */ @@ -1185,49 +1239,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 Wed Dec 4 15:59:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204248 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="ewH5ytkf"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Sk9q6J6vz9sSF for ; Thu, 5 Dec 2019 03:01:35 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727878AbfLDP7s (ORCPT ); Wed, 4 Dec 2019 10:59:48 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:37182 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728658AbfLDP7s (ORCPT ); Wed, 4 Dec 2019 10:59:48 -0500 Received: by mail-wr1-f67.google.com with SMTP id w15so9268434wru.4 for ; Wed, 04 Dec 2019 07:59: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:mime-version :content-transfer-encoding; bh=GSTWzaYxR+LU0fQn09ValmDemRZs9E3PLImmxyhYvGI=; b=ewH5ytkfd3GaOGJYjRvi03EiSqU6jhugqKVSmtl63atlBdfCp+FhmlaHm5KooNxRj+ 5P6LAWm3vZp3Jyj91uZyQDEtqmYfPZ+UiyDW57FvOsPvZUmHS5C37Gr87z8CCWHxtsNW BOUHDRYOKYdkyOsLysaHvH5akREhncbAxlgCJ9YQv3A9oAB9cCwcdDGe7nBtovS6tlWF yV6G7fN5OPbPgigpSEsSQQRYn5a3+z/HkbWlMTbr7DW6WzcLXXfp3o+vka4bWrIRS1xO wxUX46tviLu9gLRo8JDPchAuwg1LsaeIYJ3EE+g4LCQYn6lTa7SX7uvNNtHtx7Sv8fVp 5DHA== 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:mime-version :content-transfer-encoding; bh=GSTWzaYxR+LU0fQn09ValmDemRZs9E3PLImmxyhYvGI=; b=KUk1i3niBM6ojiIHD6aYX53sNrLmStaalBIZnfBJZTekPh0YqoPYvObeb/UNymrT1H 7H+dD82HpYCDMS2Di6ecRYX1l1Z8afZzejCgNAm8og4Pyh+x/iU3Zb4i0o3xDlv53vAG Gj/09JOxSq4hXKIQpKLeDR0tGR8l+REoamg+GUgpTriX5CkLNN6GWJuVooWHgbHxxZMf gk/Pg/0whIND4x8ODaUmR9K8CjqHz7BC0ph6HosjKmLJzxfVXJRpFzinBdeQgX7MFcoF VW/Kmz8iEHiyN149rTpVlqyGiNsDpmz4TqAs+Z4mEiid7O+oYEN6oB2cWcOJjUcMsSV+ gRiw== X-Gm-Message-State: APjAAAWPajXspL6uGBbiI+MEkkHdSaCSK7p82CT4DcOfBiXUGU7SLqU5 nANdA47DKD+jSowp7KzuaDUsEA== X-Google-Smtp-Source: APXvYqxuyU4sbJw3DaabgGc1/OmTWGhSSlCNAels+NTfq3axxx4cdGLp1Tw2HwrWmmopbsbK+EjnBg== X-Received: by 2002:a5d:4e0a:: with SMTP id p10mr4781273wrt.229.1575475185410; Wed, 04 Dec 2019 07:59:45 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id s10sm8769070wrw.12.2019.12.04.07.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:44 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 10/11] gpiolib: add new ioctl() for monitoring changes in line info Date: Wed, 4 Dec 2019 16:59:40 +0100 Message-Id: <20191204155941.17814-1-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 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 | 177 ++++++++++++++++++++++++++++++++++++-- drivers/gpio/gpiolib.h | 2 + include/uapi/linux/gpio.h | 24 ++++++ 3 files changed, 195 insertions(+), 8 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 711963aa9239..2ff15ef0bbe0 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -547,6 +547,9 @@ static long linehandle_set_config(struct linehandle_state *lh, if (ret) return ret; } + + atomic_notifier_call_chain(&desc->gdev->notifier, + GPIOLINE_CHANGED_CONFIG, desc); } return 0; } @@ -1199,14 +1202,24 @@ 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; +}; + /* * 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) @@ -1228,9 +1241,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; @@ -1243,11 +1256,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(FLAG_WATCHED, &desc->flags); + 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(FLAG_WATCHED, &desc->flags); + return 0; } return -EINVAL; } @@ -1260,6 +1287,99 @@ 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(FLAG_WATCHED, &desc->flags)) { + 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); + + spin_lock(&priv->wait.lock); + if (!kfifo_is_empty(&priv->events)) + events = EPOLLIN | EPOLLRDNORM; + spin_unlock(&priv->wait.lock); + + 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; + int ret; + + if (count < sizeof(event)) + return -EINVAL; + + for (;;) { + spin_lock(&priv->wait.lock); + if (kfifo_is_empty(&priv->events)) { + 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) + break; + + /* We should never get here. See lineevent_read(). */ + } + + ret = copy_to_user(buf, &event, sizeof(event)); + if (ret) + return -EFAULT; + + return sizeof(event); +} + /** * gpio_chrdev_open() - open the chardev for ioctl operations * @inode: inode for this chardev @@ -1270,14 +1390,42 @@ 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; /* Fail on open if the backing gpiochip is gone */ if (!gdev->chip) return -ENODEV; + + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + 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_priv; + get_device(&gdev->dev); - filp->private_data = gdev; + filp->private_data = priv; + + ret = nonseekable_open(inode, filp); + if (ret) + goto out_unregister_notifier; - return nonseekable_open(inode, filp); + return ret; + +out_unregister_notifier: + atomic_notifier_chain_unregister(&gdev->notifier, + &priv->lineinfo_changed_nb); +out_free_priv: + kfree(priv); + return ret; } /** @@ -1288,17 +1436,22 @@ 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; + 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, @@ -1509,6 +1662,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 @@ -2848,6 +3003,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; } @@ -2945,6 +3102,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; } @@ -3107,6 +3267,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..4fca77241fb0 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 /* @@ -112,6 +113,7 @@ 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_WATCHED 16 /* GPIO line is being watched by user-space */ /* Connection label */ const char *label; diff --git a/include/uapi/linux/gpio.h b/include/uapi/linux/gpio.h index 799cf823d493..2401028ae7de 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 + * @timestamp: estimate of time of status change occurrence, in nanoseconds + * @event_type: one of GPIOLINE_CHANGED_REQUESTED, GPIOLINE_CHANGED_RELEASED + * and GPIOLINE_CHANGED_CONFIG + * @info: updated line information + */ +struct gpioline_info_changed { + __u64 timestamp; + __u32 event_type; + struct gpioline_info info; + __u32 padding[4]; /* 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) From patchwork Wed Dec 4 15:59:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1204247 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="BBMUbwY6"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47Sk9q0m4Kz9sRd for ; Thu, 5 Dec 2019 03:01:35 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728679AbfLDP7t (ORCPT ); Wed, 4 Dec 2019 10:59:49 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:39859 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728671AbfLDP7t (ORCPT ); Wed, 4 Dec 2019 10:59:49 -0500 Received: by mail-wr1-f67.google.com with SMTP id y11so9228274wrt.6 for ; Wed, 04 Dec 2019 07:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3vOhxME4oM+fCXvGw9SeZO8ZtZ/Y7PIxuNLc7TEy6Us=; b=BBMUbwY67rlDFQzYeKhhMrURjvq+SV00mi/aBQLu2XUm4f46pIt+flDS2ZdL34ILWA DzDRxfz3Yse3rm5oSdZzFW6HbJOmk/562LaDsoSkah9MDpnNm6FirJxpYFPL19eTT5XO cYgZWpYU/4cQcK2Vyv9QkBmwzwB3XE6JXTh9hU5vQ2WV1i6ExBsOUVUUh5Z9j+uOHmh4 f+Kvl8kbcALtFF3IEXsIDRhybsXftUPA+95fNvPhs/VZsBUY9y8HXjmpAUiWUbWxVJT8 7cMQILfswsNcQuk1QkzOzoHcVacLpetKWDxSOH2geHB+fbAOo159sxgUmbSf1Qjljkq6 kcGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3vOhxME4oM+fCXvGw9SeZO8ZtZ/Y7PIxuNLc7TEy6Us=; b=LR/u0RO9yyelPpiMYjkFlscdimI32BxBKAeGe/Z/nUBtvY+pjgwPeETArH1FYmPACu 8fEKreeO0gfjYHGZpB1TNyB4q07c2qEE2dFUMogXp5X/0ehWKPlbSBHZ8m6Nq7oBihEy Xu5snCMBz7P6DsYKWzyh9y+p9iPMcCb23eCE1DgqzMwVYOz7lebks1qMKMpoUE7itz7P K6PyOqeRvQGcA2NgQrXYdrzRtj388/RukcFyhnefkuzjE7CqRH7eV4iVJQe6yREiR2Sg FqRoZ0BmpFTa76KqTQU0MpajjgTW2KnXt8nEP+HalbO8/He6h2KxfFwpfog7eOYr+luB hnzA== X-Gm-Message-State: APjAAAX64He95+X4bJe5Ymlf4Kmdn71ELMAMEygk9Uv4Jd+C2ACqMyz/ ET0qD4e/vrMjvoDBq9tgDx5GsJds51U= X-Google-Smtp-Source: APXvYqxDIY/iXDiy3QJ88E3DR3VBqpPxQnwpJM1KGnR56oJlEtM54rx1fvuTnHc5kW24DTAt+3ceSQ== X-Received: by 2002:adf:dcc2:: with SMTP id x2mr4555036wrm.24.1575475186930; Wed, 04 Dec 2019 07:59:46 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id s10sm8769070wrw.12.2019.12.04.07.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 07:59:45 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 11/11] tools: gpio: implement gpio-watch Date: Wed, 4 Dec 2019 16:59:41 +0100 Message-Id: <20191204155941.17814-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191204155941.17814-1-brgl@bgdev.pl> References: <20191204155941.17814-1-brgl@bgdev.pl> MIME-Version: 1.0 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 | 112 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tools/gpio/gpio-watch.c diff --git a/tools/gpio/.gitignore b/tools/gpio/.gitignore index a94c0e83b209..fffd32969d62 100644 --- a/tools/gpio/.gitignore +++ b/tools/gpio/.gitignore @@ -1,4 +1,5 @@ gpio-event-mon gpio-hammer lsgpio +gpio-watch include/linux/gpio.h diff --git a/tools/gpio/Build b/tools/gpio/Build index 4141f35837db..67c7b7f6a717 100644 --- a/tools/gpio/Build +++ b/tools/gpio/Build @@ -2,3 +2,4 @@ gpio-utils-y += gpio-utils.o lsgpio-y += lsgpio.o gpio-utils.o gpio-hammer-y += gpio-hammer.o gpio-utils.o gpio-event-mon-y += gpio-event-mon.o gpio-utils.o +gpio-watch-y += gpio-watch.o diff --git a/tools/gpio/Makefile b/tools/gpio/Makefile index 6080de58861f..842287e42c83 100644 --- a/tools/gpio/Makefile +++ b/tools/gpio/Makefile @@ -18,7 +18,7 @@ MAKEFLAGS += -r override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon +ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon gpio-watch ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) all: $(ALL_PROGRAMS) @@ -66,6 +66,15 @@ $(GPIO_EVENT_MON_IN): prepare FORCE $(OUTPUT)gpio-utils-in.o $(OUTPUT)gpio-event-mon: $(GPIO_EVENT_MON_IN) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +# +# gpio-watch +# +GPIO_WATCH_IN := $(OUTPUT)gpio-watch-in.o +$(GPIO_WATCH_IN): prepare FORCE + $(Q)$(MAKE) $(build)=gpio-watch +$(OUTPUT)gpio-watch: $(GPIO_WATCH_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ + clean: rm -f $(ALL_PROGRAMS) rm -f $(OUTPUT)include/linux/gpio.h diff --git a/tools/gpio/gpio-watch.c b/tools/gpio/gpio-watch.c new file mode 100644 index 000000000000..69aee43655ae --- /dev/null +++ b/tools/gpio/gpio-watch.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * gpio-watch - monitor unrequested lines for property changes using the + * character device + * + * Copyright (C) 2019 BayLibre SAS + * Author: Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool isnumber(const char *str) +{ + size_t sz = strlen(str); + int i; + + for (i = 0; i < sz; i++) { + if (!isdigit(str[i])) + return false; + } + + return true; +} + +int main(int argc, char **argv) +{ + struct gpioline_info_changed chg; + struct gpioline_info req; + struct pollfd pfd; + int fd, i, j, ret; + char *event; + ssize_t rd; + + if (argc < 3) + goto err_usage; + + fd = open(argv[1], O_RDWR | O_CLOEXEC); + if (fd < 0) { + perror("unable to open gpiochip"); + return EXIT_FAILURE; + } + + for (i = 0, j = 2; i < argc - 2; i++, j++) { + if (!isnumber(argv[j])) + goto err_usage; + + memset(&req, 0, sizeof(req)); + req.line_offset = atoi(argv[j]); + + ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); + if (ret) { + perror("unable to set up line watch"); + return EXIT_FAILURE; + } + } + + pfd.fd = fd; + pfd.events = POLLIN | POLLPRI; + + for (;;) { + ret = poll(&pfd, 1, 5000); + if (ret < 0) { + perror("error polling the linechanged fd"); + return EXIT_FAILURE; + } else if (ret > 0) { + memset(&chg, 0, sizeof(chg)); + rd = read(pfd.fd, &chg, sizeof(chg)); + if (rd < 0 || rd != sizeof(chg)) { + if (rd != sizeof(chg)) + errno = EIO; + + perror("error reading line change event"); + return EXIT_FAILURE; + } + + switch (chg.event_type) { + case GPIOLINE_CHANGED_REQUESTED: + event = "requested"; + break; + case GPIOLINE_CHANGED_RELEASED: + event = "released"; + break; + case GPIOLINE_CHANGED_CONFIG: + event = "config changed"; + break; + default: + fprintf(stderr, + "invalid event type received from the kernel\n"); + return EXIT_FAILURE; + } + + printf("line %u: %s at %llu\n", + chg.info.line_offset, event, chg.timestamp); + } + } + + return 0; + +err_usage: + printf("%s: ...\n", argv[0]); + return EXIT_FAILURE; +}