From patchwork Wed Nov 24 11:42:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 518399 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD7EAC433EF for ; Wed, 24 Nov 2021 11:43:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241734AbhKXLqP (ORCPT ); Wed, 24 Nov 2021 06:46:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241726AbhKXLqO (ORCPT ); Wed, 24 Nov 2021 06:46:14 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A4A7C06173E for ; Wed, 24 Nov 2021 03:43:05 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id u1so3597649wru.13 for ; Wed, 24 Nov 2021 03:43:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sS1opnsxYro85V5S3ivgztdl1gAyjYFuzaHv2So6/ZM=; b=ukqZb6Df8wk2BR7+RB3VNpoAiHEzybZ5wK7naDQW1uDkTqo/5SeXbH0bluZIgIK/y5 yHsVSJeCbWnimbjYDhksjDQEBETXtrudYf0Rluz5BIrua02DG0wrDv9v6qVpB07EQFCC HgiJtMXCKAQ5FxV2r15Xq9wFWV38uVKAktJ1CK9RmcskvqAZ+0FuM60ay0BS/7jXJBOn rO1P/jEIRTtoJMiz2A6DC2x4WA0BO7bGQ8JcfNtz3m7tf3lFrvS0HdQsK8u0wJAMcJfG 0FB6mmWh1sJJ54bScBM79u8uhThDWiNoGbBfp8GfZA7XVvBMoQICjkcAUX1X9Igd0FJ0 81jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sS1opnsxYro85V5S3ivgztdl1gAyjYFuzaHv2So6/ZM=; b=ayFSWAnHHvYAGDoj0mThDi98rROo6lWkbRxK5go8zavy6CB2t+WsFVp6jE8mAWHEfC myoii/CrSiwpUiq1spTB1Wz03pUU6lpy1+8QubOmHS7qbu6YxPFTEtd+sgUGlO8v8jcE nb+2NG7S8Iok8HvkZqggPVv0UseWCh5fs0v5afZzefb4fF0GqbE+4SqA7qKiNa1FIUwI gyQv4N10ZLfD7SUj/JUJbuQnEWdIAynTw8uzzcnK97vpAL7TiE8b52UAGeZIee1BN7I0 yPx7OFfphNQ54v1f9XKdasV9hTzEyL8ueoEwI8Ftb1J7QZYoh+E2ZL6lUbQG1G1dv8pt 7qwQ== X-Gm-Message-State: AOAM531x8KRIOqjYUCsPFAr1tJBZAK8IldhaZw10U2Av1YqeumjKYUOt xFsxocfprr+PcYWjUExH+QdYdeWrOcs3MfjH X-Google-Smtp-Source: ABdhPJxKAZZnUDjolRDhLnzjAWh38EBmKg2rTlNyfnvjqE+5got5d7PmwHEdZRTCcGycM0SHq37agg== X-Received: by 2002:adf:f88f:: with SMTP id u15mr15957633wrp.18.1637754182346; Wed, 24 Nov 2021 03:43:02 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id s63sm5165173wme.22.2021.11.24.03.43.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:43:01 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v10 1/5] gpiolib: provide gpiod_remove_hogs() Date: Wed, 24 Nov 2021 12:42:53 +0100 Message-Id: <20211124114257.19878-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211124114257.19878-1-brgl@bgdev.pl> References: <20211124114257.19878-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Currently all users of gpiod_add_hogs() call it only once at system init so there never was any need for a mechanism allowing to remove them. Now the upcoming gpio-sim will need to tear down chips with hogged lines so provide a function that allows to remove hogs. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib.c | 11 +++++++++++ include/linux/gpio/machine.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index abfbf546d159..22b98a590a88 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -3540,6 +3540,17 @@ void gpiod_add_hogs(struct gpiod_hog *hogs) } EXPORT_SYMBOL_GPL(gpiod_add_hogs); +void gpiod_remove_hogs(struct gpiod_hog *hogs) +{ + struct gpiod_hog *hog; + + mutex_lock(&gpio_machine_hogs_mutex); + for (hog = &hogs[0]; hog->chip_label; hog++) + list_del(&hog->list); + mutex_unlock(&gpio_machine_hogs_mutex); +} +EXPORT_SYMBOL_GPL(gpiod_remove_hogs); + static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) { const char *dev_id = dev ? dev_name(dev) : NULL; diff --git a/include/linux/gpio/machine.h b/include/linux/gpio/machine.h index d755e529c1e3..2647dd10b541 100644 --- a/include/linux/gpio/machine.h +++ b/include/linux/gpio/machine.h @@ -100,6 +100,7 @@ void gpiod_add_lookup_table(struct gpiod_lookup_table *table); void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n); void gpiod_remove_lookup_table(struct gpiod_lookup_table *table); void gpiod_add_hogs(struct gpiod_hog *hogs); +void gpiod_remove_hogs(struct gpiod_hog *hogs); #else /* ! CONFIG_GPIOLIB */ static inline void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {} @@ -108,6 +109,7 @@ void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {} static inline void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {} static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {} +static inline void gpiod_remove_hogs(struct gpiod_hog *hogs) {} #endif /* CONFIG_GPIOLIB */ #endif /* __LINUX_GPIO_MACHINE_H */ From patchwork Wed Nov 24 11:42:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 517104 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4ADFEC4332F for ; Wed, 24 Nov 2021 11:43:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241763AbhKXLqU (ORCPT ); Wed, 24 Nov 2021 06:46:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241738AbhKXLqQ (ORCPT ); Wed, 24 Nov 2021 06:46:16 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36770C061714 for ; Wed, 24 Nov 2021 03:43:06 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id u1so3597763wru.13 for ; Wed, 24 Nov 2021 03:43:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u3JjDpe+RKM3dVrlZKMX/eEU3jgsxBTgoaKKDKyKZQA=; b=gEVX9aE3wCobLB45Xl7lCQctWX8QiJ50xn4Or9FKEVDuasFF2lNQfxKgqobCODTD4M jSO6ofG61gXlCI7cJ02jWRCL7SxUPQ773Nz3W/3+lOaGMR7CcjanHV8KnH7VGorkwa3q pNATIn0qtPDF1iFk5zfECmw0wKe8pYTVXMn/GLVjEXoxliTUfxOuoxdrLVo8jXdns9MM G2b/fvjqfGeK8YDbZ32FL1UV0hsEtfQSvxVO4vyhtj/+75hMzDHn7KRflUjtoO/N3kSK YgsM6GvWDK4qHD0/I+3ELbdeFMHAxyC7gf6P2xrsgwmXTmGEXhCkRzHaTBlf/bsDX3w3 igYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u3JjDpe+RKM3dVrlZKMX/eEU3jgsxBTgoaKKDKyKZQA=; b=09Frcbujwupihcad02WjJpuvhS6Q7cbVWTzirwCaAps5D/fkxWdgcW26IvG7Z8lE/N uZGv7oO6SRA3wqqsPupBVRk8J4s4ZIPr38kFrsmJOjbioiuebw+PXlaz/G0iCXkryohe Bot4DiKO9d1H45YU+NOTl2X57DGWi3dzztO90oJRtFPQqtVTsFdvtVJ7RZ1n+a4o6iNF 6Ho4pmfVMuI/1eXk15RBUGFlNwSKxxQzdB/HeyuyQpND+f24GsOF1Q9OOnwSuZjKROuy hAAOsxcjgx03RiwpIK8c35yGwDU2in7EpfpTodkmEhw74byHjsm3+JzgbdW0Ff0qK3at KyNw== X-Gm-Message-State: AOAM533y9lJZaqTthff5VqUvMLS6GVl1YoEG2dF5DRt0QMDF5xcKFWiG 39CDGCNVpoaK71aGh8f2Y+Ma1Q== X-Google-Smtp-Source: ABdhPJyCXAazGEFWmW6ZRBpVlzMNAID7T8s6tlf4cbfbJxhxA4Ps5EGNl/ygdUO+RWojN7BsmNvQvg== X-Received: by 2002:a5d:69ce:: with SMTP id s14mr17157673wrw.25.1637754184197; Wed, 24 Nov 2021 03:43:04 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id s63sm5165173wme.22.2021.11.24.03.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:43:03 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v10 2/5] gpio: sim: new testing module Date: Wed, 24 Nov 2021 12:42:54 +0100 Message-Id: <20211124114257.19878-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211124114257.19878-1-brgl@bgdev.pl> References: <20211124114257.19878-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup. Signed-off-by: Bartosz Golaszewski --- Documentation/admin-guide/gpio/gpio-sim.rst | 80 ++ drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-sim.c | 1370 +++++++++++++++++++ 4 files changed, 1459 insertions(+) create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst create mode 100644 drivers/gpio/gpio-sim.c diff --git a/Documentation/admin-guide/gpio/gpio-sim.rst b/Documentation/admin-guide/gpio/gpio-sim.rst new file mode 100644 index 000000000000..5da24bc698d7 --- /dev/null +++ b/Documentation/admin-guide/gpio/gpio-sim.rst @@ -0,0 +1,80 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Configfs GPIO Simulator +======================= + +The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO +chips for testing purposes. The lines exposed by these chips can be accessed +using the standard GPIO character device interface as well as manipulated +using sysfs attributes. + +Creating simulated chips +------------------------ + +The gpio-sim module registers a configfs subsystem called 'gpio-sim'. + +In order to instantiate a new simulated chip, the user needs to mkdir() a new +directory gpio-sim/. Inside each new directory, there's a set of attributes +that can be used to configure the new chip. Additionally the user can mkdir() +subdirectories inside the chip's directory that allow to pass additional +configuration for specific lines. The name of those subdirectories must take +the form of: 'line' (e.g. 'line0', 'line20', etc.) as the name will be +used by the module to assign the config to the specific line at given offset. + +Once the confiuration is complete, the 'live' attribute must be set to 1 in +order to instantiate the chip. It can be set back to 0 to destroy the simulated +chip. The module will synchronously wait for the new simulated device to be +successfully probed and if this doesn't happen, writing to 'live' will result +in an error. + +Currently supported chip configuration attributes are: + + num_lines - an unsigned integer value defining the number of GPIO lines to + export + + label - a string defining the label for the GPIO chip + +Additionally two read-only attributes named 'chip_name' and 'dev_name' are +exposed in order to provide users with a mapping from configfs directories to +the actual devices created in the kernel. The former returns the name of the +GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The +latter returns the parent device name as defined by the gpio-sim driver (i.e. +"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs +items both to the correct character device file as well as the associated entry +in sysfs. + +Supported line configuration attributes are: + + name - a string defining the name of this line as used by the + "gpio-line-names" device property + +Lines can be hogged by creating a new directory under the line's directory. The +directory must be called 'hog' and there can only be a single instance of it. +The hog directory also exposes additional attributes: + + name - name of the kernel consumer of this hogged line + + direction - hog direction, must be one of: 'input', 'output-high' and + 'output-low' + +Simulated GPIO chips can also be defined in device-tree. The compatible string +must be: "gpio-simulator". Supported properties are: + + "gpio-sim,label" - chip label + +Other standard GPIO properties (like "gpio-line-names", "ngpios" or gpio-hog) +are also supported. + +Manipulating simulated lines +---------------------------- + +Each simulated GPIO chip creates a separate sysfs group under its device +directory for each exposed line. The name of each group is of the form: +'sim_gpioX' where X is the offset of the line. Inside each group there are +two attibutes: + + pull - allows to read and set the current simulated pull setting for every + line, when writing the value must be one of: 'pull-up', 'pull-down' + + value - allows to read the current value of the line which may be different + from the pull if the line is being driven from user-space diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 60d9374c72c0..9acdb4d1047b 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -1694,6 +1694,14 @@ config GPIO_VIRTIO These virtual GPIOs can be routed to real GPIOs or attached to simulators on the host (like QEMU). +config GPIO_SIM + tristate "GPIO Simulator Module" + select IRQ_SIM + select CONFIGFS_FS + help + This enables the GPIO simulator - a configfs-based GPIO testing + driver. + endmenu endif diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 71ee9fc2ff83..f21577de2474 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -133,6 +133,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o obj-$(CONFIG_GPIO_SCH) += gpio-sch.o obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o +obj-$(CONFIG_GPIO_SIM) += gpio-sim.o obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o obj-$(CONFIG_GPIO_SODAVILLE) += gpio-sodaville.o diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c new file mode 100644 index 000000000000..d4ddbb4a4e3b --- /dev/null +++ b/drivers/gpio/gpio-sim.c @@ -0,0 +1,1370 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO testing driver based on configfs. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gpiolib.h" + +static DEFINE_IDA(gpio_sim_ida); + +struct gpio_sim_chip { + struct gpio_chip gc; + unsigned long *direction_map; + unsigned long *value_map; + unsigned long *pull_map; + struct irq_domain *irq_sim; + struct mutex lock; + const struct attribute_group **attr_groups; +}; + +/* value, pull and sentinel */ +#define GPIO_SIM_NUM_ATTRS 3 + +struct gpio_sim_attribute { + struct device_attribute dev_attr; + unsigned int offset; +}; + +static struct gpio_sim_attribute * +to_gpio_sim_attr(struct device_attribute *dev_attr) +{ + return container_of(dev_attr, struct gpio_sim_attribute, dev_attr); +} + +static int gpio_sim_apply_pull(struct gpio_sim_chip *chip, + unsigned int offset, int value) +{ + int curr_val, irq, irq_type, ret; + struct gpio_desc *desc; + struct gpio_chip *gc; + + gc = &chip->gc; + desc = &gc->gpiodev->descs[offset]; + + mutex_lock(&chip->lock); + + if (test_bit(FLAG_REQUESTED, &desc->flags) && + !test_bit(FLAG_IS_OUT, &desc->flags)) { + curr_val = !!test_bit(offset, chip->value_map); + if (curr_val == value) + goto set_pull; + + /* + * This is fine - it just means, nobody is listening + * for interrupts on this line, otherwise + * irq_create_mapping() would have been called from + * the to_irq() callback. + */ + irq = irq_find_mapping(chip->irq_sim, offset); + if (!irq) + goto set_value; + + irq_type = irq_get_trigger_type(irq); + + if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) || + (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) { + ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, + true); + if (ret) + goto set_pull; + } + } + +set_value: + /* Change the value unless we're actively driving the line. */ + if (!test_bit(FLAG_REQUESTED, &desc->flags) || + !test_bit(FLAG_IS_OUT, &desc->flags)) + __assign_bit(offset, chip->value_map, value); + +set_pull: + __assign_bit(offset, chip->pull_map, value); + mutex_unlock(&chip->lock); + return 0; +} + +static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + int ret; + + mutex_lock(&chip->lock); + ret = !!test_bit(offset, chip->value_map); + mutex_unlock(&chip->lock); + + return ret; +} + +static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __assign_bit(offset, chip->value_map, value); + mutex_unlock(&chip->lock); +} + +static int gpio_sim_get_multiple(struct gpio_chip *gc, + unsigned long *mask, unsigned long *bits) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + bitmap_copy(bits, chip->value_map, gc->ngpio); + mutex_unlock(&chip->lock); + + return 0; +} + +static void gpio_sim_set_multiple(struct gpio_chip *gc, + unsigned long *mask, unsigned long *bits) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + bitmap_copy(chip->value_map, bits, gc->ngpio); + mutex_unlock(&chip->lock); +} + +static int gpio_sim_direction_output(struct gpio_chip *gc, + unsigned int offset, int value) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __clear_bit(offset, chip->direction_map); + __assign_bit(offset, chip->value_map, value); + mutex_unlock(&chip->lock); + + return 0; +} + +static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __set_bit(offset, chip->direction_map); + mutex_unlock(&chip->lock); + + return 0; +} + +static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + int direction; + + mutex_lock(&chip->lock); + direction = !!test_bit(offset, chip->direction_map); + mutex_unlock(&chip->lock); + + return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT; +} + +static int gpio_sim_set_config(struct gpio_chip *gc, + unsigned int offset, unsigned long config) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_BIAS_PULL_UP: + return gpio_sim_apply_pull(chip, offset, 1); + case PIN_CONFIG_BIAS_PULL_DOWN: + return gpio_sim_apply_pull(chip, offset, 0); + default: + break; + } + + return -ENOTSUPP; +} + +static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + return irq_create_mapping(chip->irq_sim, offset); +} + +static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map)); + mutex_unlock(&chip->lock); +} + +static ssize_t gpio_sim_sysfs_val_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); + struct gpio_sim_chip *chip = dev_get_drvdata(dev); + int val; + + mutex_lock(&chip->lock); + val = !!test_bit(line_attr->offset, chip->value_map); + mutex_unlock(&chip->lock); + + return sysfs_emit(buf, "%d\n", val); +} + +static ssize_t gpio_sim_sysfs_val_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + /* + * Not assigning this function will result in write() returning -EIO + * which is confusing. Return -EPERM explicitly. + */ + return -EPERM; +} + +static ssize_t gpio_sim_sysfs_pull_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); + struct gpio_sim_chip *chip = dev_get_drvdata(dev); + char *repr; + int pull; + + mutex_lock(&chip->lock); + pull = !!test_bit(line_attr->offset, chip->pull_map); + mutex_unlock(&chip->lock); + + if (pull) + repr = "pull-up"; + else + repr = "pull-down"; + + return sysfs_emit(buf, "%s\n", repr); +} + +static ssize_t gpio_sim_sysfs_pull_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); + struct gpio_sim_chip *chip = dev_get_drvdata(dev); + int ret, pull; + + if (sysfs_streq(buf, "pull-down")) + pull = 0; + else if (sysfs_streq(buf, "pull-up")) + pull = 1; + else + return -EINVAL; + + ret = gpio_sim_apply_pull(chip, line_attr->offset, pull); + if (ret) + return ret; + + return len; +} + +static void gpio_sim_mutex_destroy(void *data) +{ + struct mutex *lock = data; + + mutex_destroy(lock); +} + +static void gpio_sim_sysfs_remove(void *data) +{ + struct gpio_sim_chip *chip = data; + + sysfs_remove_groups(&chip->gc.parent->kobj, chip->attr_groups); +} + +static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip) +{ + struct device_attribute *val_dev_attr, *pull_dev_attr; + struct gpio_sim_attribute *val_attr, *pull_attr; + unsigned int num_lines = chip->gc.ngpio; + struct device *dev = chip->gc.parent; + struct attribute_group *attr_group; + struct attribute **attrs; + int i, ret; + + chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups), + num_lines + 1, GFP_KERNEL); + if (!chip->attr_groups) + return -ENOMEM; + + for (i = 0; i < num_lines; i++) { + attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL); + attrs = devm_kcalloc(dev, sizeof(*attrs), + GPIO_SIM_NUM_ATTRS, GFP_KERNEL); + val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL); + pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL); + if (!attr_group || !attrs || !val_attr || !pull_attr) + return -ENOMEM; + + attr_group->name = devm_kasprintf(dev, GFP_KERNEL, + "sim_gpio%u", i); + if (!attr_group->name) + return -ENOMEM; + + val_attr->offset = pull_attr->offset = i; + + val_dev_attr = &val_attr->dev_attr; + pull_dev_attr = &pull_attr->dev_attr; + + sysfs_attr_init(&val_dev_attr->attr); + sysfs_attr_init(&pull_dev_attr->attr); + + val_dev_attr->attr.name = "value"; + pull_dev_attr->attr.name = "pull"; + + val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644; + + val_dev_attr->show = gpio_sim_sysfs_val_show; + val_dev_attr->store = gpio_sim_sysfs_val_store; + pull_dev_attr->show = gpio_sim_sysfs_pull_show; + pull_dev_attr->store = gpio_sim_sysfs_pull_store; + + attrs[0] = &val_dev_attr->attr; + attrs[1] = &pull_dev_attr->attr; + + attr_group->attrs = attrs; + chip->attr_groups[i] = attr_group; + } + + ret = sysfs_create_groups(&dev->kobj, chip->attr_groups); + if (ret) + return ret; + + return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip); +} + +static int gpio_sim_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gpio_sim_chip *chip; + struct gpio_chip *gc; + const char *label; + u32 num_lines; + int ret; + + ret = device_property_read_u32(dev, "ngpios", &num_lines); + if (ret) + return ret; + + ret = device_property_read_string(dev, "gpio-sim,label", &label); + if (ret) + label = dev_name(dev); + + chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL); + if (!chip->direction_map) + return -ENOMEM; + + /* Default to input mode. */ + bitmap_fill(chip->direction_map, num_lines); + + chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); + if (!chip->value_map) + return -ENOMEM; + + chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); + if (!chip->pull_map) + return -ENOMEM; + + chip->irq_sim = devm_irq_domain_create_sim(dev, NULL, num_lines); + if (IS_ERR(chip->irq_sim)) + return PTR_ERR(chip->irq_sim); + + mutex_init(&chip->lock); + ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy, + &chip->lock); + if (ret) + return ret; + + gc = &chip->gc; + gc->base = -1; + gc->ngpio = num_lines; + gc->label = label; + gc->owner = THIS_MODULE; + gc->parent = dev; + gc->get = gpio_sim_get; + gc->set = gpio_sim_set; + gc->get_multiple = gpio_sim_get_multiple; + gc->set_multiple = gpio_sim_set_multiple; + gc->direction_output = gpio_sim_direction_output; + gc->direction_input = gpio_sim_direction_input; + gc->get_direction = gpio_sim_get_direction; + gc->set_config = gpio_sim_set_config; + gc->to_irq = gpio_sim_to_irq; + gc->free = gpio_sim_free; + + ret = devm_gpiochip_add_data(dev, gc, chip); + if (ret) + return ret; + + /* Used by sysfs and configfs callbacks. */ + dev_set_drvdata(dev, chip); + + return gpio_sim_setup_sysfs(chip); +} + +static const struct of_device_id gpio_sim_of_match[] = { + { .compatible = "gpio-simulator" }, + { } +}; +MODULE_DEVICE_TABLE(of, gpio_sim_of_match); + +static struct platform_driver gpio_sim_driver = { + .driver = { + .name = "gpio-sim", + .of_match_table = gpio_sim_of_match, + }, + .probe = gpio_sim_probe, +}; + +struct gpio_sim_chip_ctx { + struct config_group group; + + /* + * If pdev is NULL, the item is 'pending' (waiting for configuration). + * Once the pointer is assigned, the device has been created and the + * item is 'live'. + */ + struct platform_device *pdev; + int id; + + /* + * Each configfs filesystem operation is protected with the subsystem + * mutex. Each separate attribute is protected with the buffer mutex. + * This structure however can be modified by callbacks of different + * attributes so we need another lock. + * + * We use this lock fo protecting all line context structures owned by + * this object too. + */ + struct mutex lock; + + char label[32]; + unsigned int num_lines; + + struct list_head line_ctx_list; + + struct notifier_block bus_notifier; + struct completion probe_completion; + bool driver_bound; + + struct gpiod_hog *hogs; +}; + +static struct gpio_sim_chip_ctx * +gpio_sim_item_to_chip_ctx(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + return container_of(group, struct gpio_sim_chip_ctx, group); +} + +static struct gpio_sim_chip_ctx * +gpio_sim_nb_to_chip_ctx(struct notifier_block *nb) +{ + return container_of(nb, struct gpio_sim_chip_ctx, bus_notifier); +} + +/* This is called with ctx->lock taken. */ +static int gpio_sim_bus_notifier_call(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_nb_to_chip_ctx(nb); + struct device *dev = data; + char devname[32]; + + snprintf(devname, sizeof(devname), "gpio-sim.%u", ctx->id); + + if (strcmp(dev_name(dev), devname) == 0) { + if (action == BUS_NOTIFY_BOUND_DRIVER) + ctx->driver_bound = true; + else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND) + ctx->driver_bound = false; + else + return NOTIFY_DONE; + + complete(&ctx->probe_completion); + return NOTIFY_OK; + } + + return NOTIFY_DONE; +} + +struct gpio_sim_line_ctx { + struct config_group group; + struct list_head list; + + /* + * We could have used the ci_parent field of the config_item but + * configfs is stupid and calls the item's release callback after + * already having cleared the parent pointer even though the parent + * is guaranteed to survive the child... + * + * So we need to store the pointer to the parent struct here. We can + * dereference it anywhere we need with no checks and no locking as + * it's guaranteed to survive the childred and protected by configfs + * locks. + */ + struct gpio_sim_chip_ctx *parent; + + unsigned int offset; + char *name; + + /* + * There can only be one hog per line so we're reusing the line context + * structure in order to simplify the code and avoid a whole new + * allocation and object management. + */ + struct config_item hog_item; + bool hogged; + char *hog_name; + int hog_dir; +}; + +static struct gpio_sim_line_ctx * +gpio_sim_item_to_line_ctx(struct config_item *item) +{ + struct config_group *group = to_config_group(item); + + return container_of(group, struct gpio_sim_line_ctx, group); +} + +static struct gpio_sim_line_ctx * +gpio_sim_hog_item_to_line_ctx(struct config_item *item) +{ + return container_of(item, struct gpio_sim_line_ctx, hog_item); +} + +static bool gpio_sim_chip_live(struct gpio_sim_chip_ctx *ctx) +{ + return !!ctx->pdev; +} + +static char *gpio_sim_strdup_trimmed(const char *str, size_t count) +{ + char *dup, *trimmed, *ret; + + dup = kstrndup(str, count, GFP_KERNEL); + if (!dup) + return NULL; + + trimmed = strstrip(dup); + ret = kstrdup(trimmed, GFP_KERNEL); + kfree(dup); + return ret; +} + +static ssize_t gpio_sim_config_chip_dev_name_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + struct platform_device *pdev; + int ret; + + mutex_lock(&ctx->lock); + pdev = ctx->pdev; + if (pdev) + ret = sprintf(page, "%s\n", dev_name(&pdev->dev)); + else + ret = sprintf(page, "gpio-sim.%d\n", ctx->id); + mutex_unlock(&ctx->lock); + + return ret; +} + +CONFIGFS_ATTR_RO(gpio_sim_config_chip_, dev_name); + +static ssize_t gpio_sim_config_chip_chip_name_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + struct gpio_sim_chip *chip = NULL; + int ret; + + mutex_lock(&ctx->lock); + if (gpio_sim_chip_live(ctx)) + chip = dev_get_drvdata(&ctx->pdev->dev); + + if (chip) + ret = sprintf(page, "%s\n", dev_name(&chip->gc.gpiodev->dev)); + else + ret = sprintf(page, "none\n"); + mutex_unlock(&ctx->lock); + + return ret; +} + +CONFIGFS_ATTR_RO(gpio_sim_config_chip_, chip_name); + +static ssize_t +gpio_sim_config_chip_label_show(struct config_item *item, char *page) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + int ret; + + mutex_lock(&ctx->lock); + ret = sprintf(page, "%s\n", ctx->label); + mutex_unlock(&ctx->lock); + + return ret; +} + +static ssize_t gpio_sim_config_chip_label_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + char *trimmed; + int ret; + + mutex_lock(&ctx->lock); + + if (gpio_sim_chip_live(ctx)) { + mutex_unlock(&ctx->lock); + return -EBUSY; + } + + trimmed = gpio_sim_strdup_trimmed(page, count); + if (!trimmed) { + mutex_unlock(&ctx->lock); + return -ENOMEM; + } + + ret = snprintf(ctx->label, sizeof(ctx->label), "%s", trimmed); + kfree(trimmed); + if (ret < 0) { + mutex_unlock(&ctx->lock); + return ret; + } + + mutex_unlock(&ctx->lock); + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_chip_, label); + +static ssize_t +gpio_sim_config_chip_num_lines_show(struct config_item *item, char *page) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + int ret; + + mutex_lock(&ctx->lock); + ret = sprintf(page, "%u\n", ctx->num_lines); + mutex_unlock(&ctx->lock); + + return ret; +} + +static ssize_t +gpio_sim_config_chip_num_lines_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + unsigned int num_lines; + int ret; + + mutex_lock(&ctx->lock); + + if (gpio_sim_chip_live(ctx)) { + mutex_unlock(&ctx->lock); + return -EBUSY; + } + + ret = kstrtouint(page, 10, &num_lines); + if (ret) { + mutex_unlock(&ctx->lock); + return ret; + } + + if (num_lines == 0) { + mutex_unlock(&ctx->lock); + return -EINVAL; + } + + ctx->num_lines = num_lines; + + mutex_unlock(&ctx->lock); + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_chip_, num_lines); + +static ssize_t +gpio_sim_config_chip_live_show(struct config_item *item, char *page) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + int ret; + + mutex_lock(&ctx->lock); + ret = sprintf(page, "%c\n", gpio_sim_chip_live(ctx) ? '1' : '0'); + mutex_unlock(&ctx->lock); + + return ret; +} + +static char **gpio_sim_make_line_names(struct gpio_sim_chip_ctx *chip_ctx, + unsigned int *line_names_size) +{ + struct gpio_sim_line_ctx *line_ctx; + unsigned int max_offset = 0; + bool has_line_names = false; + char **line_names; + + list_for_each_entry(line_ctx, &chip_ctx->line_ctx_list, list) { + if (line_ctx->name) { + if (line_ctx->offset > max_offset) + max_offset = line_ctx->offset; + + /* + * max_offset can stay at 0 so it's not an indicator + * of whether line names were configured at all. + */ + has_line_names = true; + } + } + + if (!has_line_names) + /* + * This is not an error - NULL means, there are no line + * names configured. + */ + return NULL; + + *line_names_size = max_offset + 1; + + line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL); + if (!line_names) + return ERR_PTR(-ENOMEM); + + list_for_each_entry(line_ctx, &chip_ctx->line_ctx_list, list) + line_names[line_ctx->offset] = line_ctx->name; + + return line_names; +} + +static void gpio_sim_remove_hogs(struct gpio_sim_chip_ctx *ctx) +{ + struct gpiod_hog *hog; + + if (!ctx->hogs) + return; + + gpiod_remove_hogs(ctx->hogs); + + for (hog = ctx->hogs; !hog->chip_label; hog++) { + kfree(hog->chip_label); + kfree(hog->line_name); + } + + kfree(ctx->hogs); + ctx->hogs = NULL; +} + +static int gpio_sim_add_hogs(struct gpio_sim_chip_ctx *chip_ctx) +{ + struct gpio_sim_line_ctx *line_ctx; + unsigned int num_hogs = 0, idx = 0; + struct gpiod_hog *hog; + + list_for_each_entry(line_ctx, &chip_ctx->line_ctx_list, list) { + if (line_ctx->hogged) + num_hogs++; + } + + if (!num_hogs) + return 0; + + /* Allocate one more for the sentinel. */ + chip_ctx->hogs = kcalloc(num_hogs + 1, + sizeof(*chip_ctx->hogs), GFP_KERNEL); + if (!chip_ctx->hogs) + return -ENOMEM; + + list_for_each_entry(line_ctx, &chip_ctx->line_ctx_list, list) { + hog = &chip_ctx->hogs[idx++]; + + /* + * We need to make this string manually because at this point + * the device doesn't exist yet and so dev_name() is not + * available. + */ + hog->chip_label = kasprintf(GFP_KERNEL, + "gpio-sim.%u", chip_ctx->id); + if (!hog->chip_label) { + gpio_sim_remove_hogs(chip_ctx); + return -ENOMEM; + } + + /* + * We need to duplicate this because the hog config item can + * be removed at any time (and we can't block it) and gpiolib + * doesn't make a deep copy of the hog data. + */ + if (line_ctx->hog_name) { + hog->line_name = kstrdup(line_ctx->hog_name, + GFP_KERNEL); + if (!hog->line_name) { + gpio_sim_remove_hogs(chip_ctx); + return -ENOMEM; + } + } + + hog->chip_hwnum = line_ctx->offset; + hog->dflags = line_ctx->hog_dir; + } + + gpiod_add_hogs(chip_ctx->hogs); + + return 0; +} + +static int gpio_sim_activate_chip_unlocked(struct gpio_sim_chip_ctx *ctx) +{ + unsigned int prop_idx = 0, line_names_size = 0; + struct platform_device_info pdevinfo; + struct property_entry properties[4]; /* Max 3 properties + sentinel. */ + struct fwnode_handle *fwnode; + struct platform_device *pdev; + char **line_names; + int ret; + + memset(&pdevinfo, 0, sizeof(pdevinfo)); + memset(properties, 0, sizeof(properties)); + + properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", + ctx->num_lines); + + if (ctx->label[0] != '\0') + properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", + ctx->label); + + line_names = gpio_sim_make_line_names(ctx, &line_names_size); + if (IS_ERR(line_names)) + return PTR_ERR(line_names); + + if (line_names) + properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( + "gpio-line-names", + line_names, line_names_size); + + fwnode = fwnode_create_software_node(properties, NULL); + kfree(line_names); + if (IS_ERR(fwnode)) + return PTR_ERR(fwnode); + + ret = gpio_sim_add_hogs(ctx); + if (ret) { + fwnode_remove_software_node(fwnode); + return ret; + } + + pdevinfo.name = "gpio-sim"; + pdevinfo.fwnode = fwnode; + pdevinfo.id = ctx->id; + + reinit_completion(&ctx->probe_completion); + ctx->driver_bound = false; + bus_register_notifier(&platform_bus_type, &ctx->bus_notifier); + + pdev = platform_device_register_full(&pdevinfo); + if (IS_ERR(pdev)) { + bus_unregister_notifier(&platform_bus_type, &ctx->bus_notifier); + gpio_sim_remove_hogs(ctx); + fwnode_remove_software_node(fwnode); + return PTR_ERR(pdev); + } + + wait_for_completion(&ctx->probe_completion); + bus_unregister_notifier(&platform_bus_type, &ctx->bus_notifier); + + if (!ctx->driver_bound) { + /* Probe failed, check kernel log. */ + platform_device_unregister(pdev); + gpio_sim_remove_hogs(ctx); + fwnode_remove_software_node(fwnode); + return -ENXIO; + } + + ctx->pdev = pdev; + + return 0; +} + +static void gpio_sim_deactivate_chip_unlocked(struct gpio_sim_chip_ctx *ctx) +{ + struct fwnode_handle *fwnode; + + fwnode = dev_fwnode(&ctx->pdev->dev); + platform_device_unregister(ctx->pdev); + fwnode_remove_software_node(fwnode); + ctx->pdev = NULL; + gpio_sim_remove_hogs(ctx); +} + +static ssize_t +gpio_sim_config_chip_live_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + int live, ret; + + ret = kstrtouint(page, 10, &live); + if (ret) + return ret; + + mutex_lock(&ctx->lock); + + if ((live == 0 && !gpio_sim_chip_live(ctx)) || + (live == 1 && gpio_sim_chip_live(ctx))) + ret = -EPERM; + else if (live == 1) + ret = gpio_sim_activate_chip_unlocked(ctx); + else if (live == 0) + gpio_sim_deactivate_chip_unlocked(ctx); + else + ret = -EINVAL; + + mutex_unlock(&ctx->lock); + + return ret ?: count; +} + +CONFIGFS_ATTR(gpio_sim_config_chip_, live); + +static struct configfs_attribute *gpio_sim_config_chip_attrs[] = { + &gpio_sim_config_chip_attr_dev_name, + &gpio_sim_config_chip_attr_chip_name, + &gpio_sim_config_chip_attr_label, + &gpio_sim_config_chip_attr_num_lines, + &gpio_sim_config_chip_attr_live, + NULL +}; + +static ssize_t +gpio_sim_config_line_name_show(struct config_item *item, char *page) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_item_to_line_ctx(item); + int ret; + + mutex_lock(&ctx->parent->lock); + ret = sprintf(page, "%s\n", ctx->name ?: ""); + mutex_unlock(&ctx->parent->lock); + + return ret; +} + +static ssize_t gpio_sim_config_line_name_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_item_to_line_ctx(item); + char *trimmed; + + mutex_lock(&ctx->parent->lock); + + if (gpio_sim_chip_live(ctx->parent)) { + mutex_unlock(&ctx->parent->lock); + return -EBUSY; + } + + trimmed = gpio_sim_strdup_trimmed(page, count); + if (!trimmed) { + mutex_unlock(&ctx->parent->lock); + return -ENOMEM; + } + + kfree(ctx->name); + ctx->name = trimmed; + + mutex_unlock(&ctx->parent->lock); + + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_line_, name); + +static struct configfs_attribute *gpio_sim_config_line_attrs[] = { + &gpio_sim_config_line_attr_name, + NULL, +}; + +static ssize_t gpio_sim_config_hog_name_show(struct config_item *item, + char *page) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_hog_item_to_line_ctx(item); + int ret; + + mutex_lock(&ctx->parent->lock); + ret = sprintf(page, "%s\n", ctx->hog_name ?: ""); + mutex_unlock(&ctx->parent->lock); + + return ret; +} + +static ssize_t gpio_sim_config_hog_name_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_hog_item_to_line_ctx(item); + char *trimmed; + + mutex_lock(&ctx->parent->lock); + + if (gpio_sim_chip_live(ctx->parent)) { + mutex_unlock(&ctx->parent->lock); + return -EBUSY; + } + + trimmed = gpio_sim_strdup_trimmed(page, count); + if (!trimmed) { + mutex_unlock(&ctx->parent->lock); + return -ENOMEM; + } + + kfree(ctx->hog_name); + ctx->hog_name = trimmed; + + mutex_unlock(&ctx->parent->lock); + + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_hog_, name); + +static ssize_t gpio_sim_config_hog_direction_show(struct config_item *item, + char *page) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_hog_item_to_line_ctx(item); + char *repr; + int dir; + + mutex_lock(&ctx->parent->lock); + dir = ctx->hog_dir; + mutex_unlock(&ctx->parent->lock); + + switch (dir) { + case GPIOD_IN: + repr = "input"; + break; + case GPIOD_OUT_HIGH: + repr = "output-high"; + break; + case GPIOD_OUT_LOW: + repr = "output-low"; + break; + default: + WARN(1, "Unexpected hog direction value: %d", dir); + break; + } + + return sprintf(page, "%s\n", repr); +} + +static ssize_t +gpio_sim_config_hog_direction_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_hog_item_to_line_ctx(item); + char *trimmed; + int dir; + + mutex_lock(&ctx->parent->lock); + + if (gpio_sim_chip_live(ctx->parent)) { + mutex_unlock(&ctx->parent->lock); + return -EBUSY; + } + + trimmed = gpio_sim_strdup_trimmed(page, count); + if (!trimmed) { + mutex_unlock(&ctx->parent->lock); + return -ENOMEM; + } + + if (strcmp(trimmed, "input") == 0) + dir = GPIOD_IN; + else if (strcmp(trimmed, "output-high") == 0) + dir = GPIOD_OUT_HIGH; + else if (strcmp(trimmed, "output-low") == 0) + dir = GPIOD_OUT_LOW; + else + dir = -EINVAL; + + kfree(trimmed); + + if (dir < 0) { + mutex_unlock(&ctx->parent->lock); + return dir; + } + + ctx->hog_dir = dir; + + mutex_unlock(&ctx->parent->lock); + + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_hog_, direction); + +static struct configfs_attribute *gpio_sim_config_hog_attrs[] = { + &gpio_sim_config_hog_attr_name, + &gpio_sim_config_hog_attr_direction, + NULL, +}; + +static void gpio_sim_config_hog_item_release(struct config_item *item) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_hog_item_to_line_ctx(item); + + mutex_lock(&ctx->parent->lock); + ctx->hogged = false; + kfree(ctx->hog_name); + mutex_unlock(&ctx->parent->lock); +} + +struct configfs_item_operations gpio_sim_config_hog_item_ops = { + .release = gpio_sim_config_hog_item_release, +}; + +static const struct config_item_type gpio_sim_config_hog_type = { + .ct_item_ops = &gpio_sim_config_hog_item_ops, + .ct_attrs = gpio_sim_config_hog_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_item * +gpio_sim_config_make_hog_item(struct config_group *group, const char *name) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_item_to_line_ctx(&group->cg_item); + + mutex_lock(&ctx->parent->lock); + + ctx->hogged = true; + ctx->hog_dir = GPIOD_IN; + ctx->hog_name = NULL; + + config_item_init_type_name(&ctx->hog_item, name, + &gpio_sim_config_hog_type); + + mutex_unlock(&ctx->parent->lock); + + return &ctx->hog_item; +} + +static struct config_item * +gpio_sim_config_make_line_item(struct config_group *group, const char *name) +{ + if (strcmp(name, "hog") == 0) + return gpio_sim_config_make_hog_item(group, name); + + return ERR_PTR(-EINVAL); +} + +static void gpio_sim_config_line_group_release(struct config_item *item) +{ + struct gpio_sim_line_ctx *ctx = gpio_sim_item_to_line_ctx(item); + + mutex_lock(&ctx->parent->lock); + list_del(&ctx->list); + mutex_unlock(&ctx->parent->lock); + + kfree(ctx->name); + kfree(ctx); +} + +static struct configfs_item_operations gpio_sim_config_line_item_ops = { + .release = gpio_sim_config_line_group_release, +}; + +static struct configfs_group_operations gpio_sim_config_line_group_ops = { + .make_item = gpio_sim_config_make_line_item, +}; + +static const struct config_item_type gpio_sim_config_line_type = { + .ct_item_ops = &gpio_sim_config_line_item_ops, + .ct_group_ops = &gpio_sim_config_line_group_ops, + .ct_attrs = gpio_sim_config_line_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_group * +gpio_sim_config_make_line_group(struct config_group *group, const char *name) +{ + struct gpio_sim_chip_ctx *chip_ctx; + struct gpio_sim_line_ctx *line_ctx; + unsigned int offset; + int ret, nchar; + + ret = sscanf(name, "line%u%n", &offset, &nchar); + if (ret != 1 || nchar != strlen(name)) + return ERR_PTR(-EINVAL); + + chip_ctx = gpio_sim_item_to_chip_ctx(&group->cg_item); + + mutex_lock(&chip_ctx->lock); + + if (gpio_sim_chip_live(chip_ctx)) { + mutex_unlock(&chip_ctx->lock); + return ERR_PTR(-EBUSY); + } + + line_ctx = kzalloc(sizeof(*line_ctx), GFP_KERNEL); + if (!line_ctx) { + mutex_unlock(&chip_ctx->lock); + return ERR_PTR(-ENOMEM); + } + + config_group_init_type_name(&line_ctx->group, name, + &gpio_sim_config_line_type); + + line_ctx->parent = chip_ctx; + line_ctx->offset = offset; + list_add_tail(&line_ctx->list, &chip_ctx->line_ctx_list); + + mutex_unlock(&chip_ctx->lock); + + return &line_ctx->group; +} + +static void gpio_sim_config_chip_group_release(struct config_item *item) +{ + struct gpio_sim_chip_ctx *ctx = gpio_sim_item_to_chip_ctx(item); + + mutex_lock(&ctx->lock); + if (gpio_sim_chip_live(ctx)) + gpio_sim_deactivate_chip_unlocked(ctx); + mutex_unlock(&ctx->lock); + + mutex_destroy(&ctx->lock); + ida_free(&gpio_sim_ida, ctx->id); + kfree(ctx); +} + +static struct configfs_item_operations gpio_sim_config_chip_item_ops = { + .release = gpio_sim_config_chip_group_release, +}; + +static struct configfs_group_operations gpio_sim_config_chip_group_ops = { + .make_group = gpio_sim_config_make_line_group, +}; + +static const struct config_item_type gpio_sim_config_chip_group_type = { + .ct_item_ops = &gpio_sim_config_chip_item_ops, + .ct_group_ops = &gpio_sim_config_chip_group_ops, + .ct_attrs = gpio_sim_config_chip_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_group * +gpio_sim_config_make_chip_group(struct config_group *group, const char *name) +{ + struct gpio_sim_chip_ctx *ctx; + int id; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return ERR_PTR(-ENOMEM); + + id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); + if (id < 0) { + kfree(ctx); + return ERR_PTR(id); + } + + config_group_init_type_name(&ctx->group, name, + &gpio_sim_config_chip_group_type); + ctx->num_lines = 1; + ctx->id = id; + mutex_init(&ctx->lock); + INIT_LIST_HEAD(&ctx->line_ctx_list); + + ctx->bus_notifier.notifier_call = gpio_sim_bus_notifier_call; + init_completion(&ctx->probe_completion); + + return &ctx->group; +} + +static struct configfs_group_operations gpio_sim_config_group_ops = { + .make_group = gpio_sim_config_make_chip_group, +}; + +static const struct config_item_type gpio_sim_config_type = { + .ct_group_ops = &gpio_sim_config_group_ops, + .ct_owner = THIS_MODULE, +}; + +static struct configfs_subsystem gpio_sim_config_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf = "gpio-sim", + .ci_type = &gpio_sim_config_type, + }, + }, +}; + +static int __init gpio_sim_init(void) +{ + int ret; + + ret = platform_driver_register(&gpio_sim_driver); + if (ret) { + pr_err("Error %d while registering the platform driver\n", ret); + return ret; + } + + config_group_init(&gpio_sim_config_subsys.su_group); + mutex_init(&gpio_sim_config_subsys.su_mutex); + ret = configfs_register_subsystem(&gpio_sim_config_subsys); + if (ret) { + pr_err("Error %d while registering the configfs subsystem %s\n", + ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf); + mutex_destroy(&gpio_sim_config_subsys.su_mutex); + platform_driver_unregister(&gpio_sim_driver); + return ret; + } + + return 0; +} +module_init(gpio_sim_init); + +static void __exit gpio_sim_exit(void) +{ + configfs_unregister_subsystem(&gpio_sim_config_subsys); + mutex_destroy(&gpio_sim_config_subsys.su_mutex); + platform_driver_unregister(&gpio_sim_driver); +} +module_exit(gpio_sim_exit); + +MODULE_AUTHOR("Bartosz Golaszewski X-Patchwork-Id: 518398 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A6C3C433F5 for ; Wed, 24 Nov 2021 11:43:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241740AbhKXLqa (ORCPT ); Wed, 24 Nov 2021 06:46:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241746AbhKXLqQ (ORCPT ); Wed, 24 Nov 2021 06:46:16 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9541EC06174A for ; Wed, 24 Nov 2021 03:43:06 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id 77-20020a1c0450000000b0033123de3425so5171329wme.0 for ; Wed, 24 Nov 2021 03:43:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2GWUpkiuXSzN66GVjCDh6suML+iAta4LGHtIvVqjGEk=; b=T+cTkrxv6yKl5R3DPzBZ5rm1juxysossHr8SGa9Egn+4js1aCjh/mq5jUVmTtYcgOa oBnOojqP/FqUb+TjQU0NiNcsgmHXlrwLnMI3fc/niQN58rVllNEQr2MmUbUyrflMpE2t 4p5iloXEX4Ily3XkMU0H55NejDjRn7+ce7Tx698Y7wJ5+3buzCuOnIvyM9wvvl3jPlJW o3rJ/oJ9z2uzxq2DIU6CUH8ZRfpAkKtUy7uI4ve2hRFND5rSkCWudW7KNCGTTMwzguYG d1Q0hqCHgd4Ebt2xUBCuDTlFlq81tQ8A+s/uS9LFm8WdcSbs4IozjJAxMxwfY5HoWiIz 4UKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2GWUpkiuXSzN66GVjCDh6suML+iAta4LGHtIvVqjGEk=; b=rOFEwikGZ0AQ8VEXLtnUDbXSCzl7gDUUB4sQDj7XOfCGnW2gsYmw+4UQVCRoiQ/QzV AxOXJkLk/+L9YOd5vp8xzZN/O5vvH8czRKW+OD2hcjueEA1gPaDbkMdS5/I4lQwMbFt8 nqKnILAfPP3ohU6XgcbNcXf9VBi2dmY/SZHT+TW4pxAhyP09dgb9SbuvXqQnQVgjboRi 7t6Q8kWpw16JMjQnLcLUCK22LNFMyarB+dwQSnEZiWfhVMkd/xDashqInLY1iKDL01y5 kR6nxAcvNkgtWO0vTh06FNk5ynz7QxMhkfzUnG6m45vTMKPMuev4/kcp2GlPaBm5qSUb b2Aw== X-Gm-Message-State: AOAM530lPhErCtsNi7pEBKnP0W1f86f+gvxoTcPzWx/7miT7WiWyEATY FanO1b8+ls0G7kKfq00ZYUf+AQ== X-Google-Smtp-Source: ABdhPJyzuZxOeKbqLPfgN1DtJnd990r+dnzV69Kaaz8MyitgmqoBQ+cgJzwpjO86kM+SAUTfZ+SzOw== X-Received: by 2002:a7b:c756:: with SMTP id w22mr14900744wmk.34.1637754185100; Wed, 24 Nov 2021 03:43:05 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id s63sm5165173wme.22.2021.11.24.03.43.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:43:04 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v10 3/5] selftests: gpio: provide a helper for reading chip info Date: Wed, 24 Nov 2021 12:42:55 +0100 Message-Id: <20211124114257.19878-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211124114257.19878-1-brgl@bgdev.pl> References: <20211124114257.19878-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index a4969f7ee020..4ea4f58dab1a 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev +gpio-chip-info diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index d7b312b44a62..a40b818c394e 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,7 +2,7 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info CFLAGS += -O2 -g -Wall -I../../../../usr/include/ include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-chip-info.c b/tools/testing/selftests/gpio/gpio-chip-info.c new file mode 100644 index 000000000000..8f2d2e23e9f6 --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-chip-info.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading chip information. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-chip-info [name|label|num-lines]\n"); +} + +int main(int argc, char **argv) +{ + struct gpiochip_info info; + int fd, ret; + + if (argc !=3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info); + if (ret) { + perror("chip info ioctl failed"); + return EXIT_FAILURE; + } + + if (strcmp(argv[2], "name") == 0) { + printf("%s\n", info.name); + } else if (strcmp(argv[2], "label") == 0) { + printf("%s\n", info.label); + } else if (strcmp(argv[2], "num-lines") == 0) { + printf("%u\n", info.lines); + } else { + fprintf(stderr, "unknown command: %s\n", argv[2]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} From patchwork Wed Nov 24 11:42:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 518397 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24859C433F5 for ; Wed, 24 Nov 2021 11:43:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241775AbhKXLqd (ORCPT ); Wed, 24 Nov 2021 06:46:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241755AbhKXLqT (ORCPT ); Wed, 24 Nov 2021 06:46:19 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C72AC061759 for ; Wed, 24 Nov 2021 03:43:07 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id l16so3633391wrp.11 for ; Wed, 24 Nov 2021 03:43:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sdC8L7KcajeuYgRxRzq/BDd9/Try3NuMaNKMjlQg9Bs=; b=JfQHaBeQHSrZ3rQZiqWsi9BL8YK2+nptrMjPCQoT0ouSL81Mp1dyq4OivxwIBmf/05 3m8nxt7MF1vEs9/Ew9m41Z4QJWjU75i1qWcefzNze5JdYdApVfenwAOIuVg4GLSR1W3D blgqLXRG7Wiw5/Q7VMDR7hJfyD+6Wbxr22bQYXzCPDr4nW/YiBMFtw1YeRJyzEq6iPb+ F1h2nZ7yXL64w4FPboiT3CdlUpvfw2YanraWyvO3NXkZV/dfs4xg5uwQuTHU+UdHqmyi ynLhKp6+ZMUrbfVx+qE6fy8MvCm3HC7wi9hnShE+GzM4iNeqSvm87oT/5n/I1B+1HB5P MktQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sdC8L7KcajeuYgRxRzq/BDd9/Try3NuMaNKMjlQg9Bs=; b=M4kk9Z3q0gIkqCRtKRh2tnZw3TzJz4M9k3LGR3ED/jUAGo57DErg4of8ZCCEsOKQec PcwQOBMyYfe46URl8nWP2aYEaOyRpOY9TwOKVkIdV61dTsPyTgJo8ByHjzhvQ0/HNf+2 GgbsMzuse/EKvZAZR9fNRN2RSPI58/RSynITjX/Jhyw0MhJ3+CgNlfdfsc2kN30Qb6iJ NRcG9WBdWF51zd4F4VjznYSHietX9ufn8DE9MrLhFrhYVTu1mUoGdlbNOXqpzZH0qZ14 HNsQ2L3H9+ySWiAmnXoDcYfJ9lOI+vFsyeJpwZpQ1ln6s2kAYwZhGVNQO0BGsKS+nqya K/Aw== X-Gm-Message-State: AOAM531vFNNimAitZpBk3YTbmlAFgYpUIezkV+kvDBh12aMw3CtSTN+B ISiivSgQTv/Z233CNWW7Q9H8xw== X-Google-Smtp-Source: ABdhPJxyPOtabszFxtmqMNt8tHaetSgoIQ7WYcSaBCCT9+/X2ptg2utUgV4syQmIjoYP6E73dul3Kw== X-Received: by 2002:a5d:6e82:: with SMTP id k2mr18242939wrz.147.1637754185908; Wed, 24 Nov 2021 03:43:05 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id s63sm5165173wme.22.2021.11.24.03.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:43:05 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v10 4/5] selftests: gpio: add a helper for reading GPIO line names Date: Wed, 24 Nov 2021 12:42:56 +0100 Message-Id: <20211124114257.19878-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211124114257.19878-1-brgl@bgdev.pl> References: <20211124114257.19878-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index 4ea4f58dab1a..ededb077a3a6 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev gpio-chip-info +gpio-line-name diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index a40b818c394e..293aa9749408 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,7 +2,7 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name CFLAGS += -O2 -g -Wall -I../../../../usr/include/ include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-line-name.c b/tools/testing/selftests/gpio/gpio-line-name.c new file mode 100644 index 000000000000..e635cfadbded --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-line-name.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading line names. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-line-name \n"); +} + +int main(int argc, char **argv) +{ + struct gpio_v2_line_info info; + int fd, ret; + char *endp; + + if (argc != 3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + info.offset = strtoul(argv[2], &endp, 10); + if (*endp != '\0') { + print_usage(); + return EXIT_FAILURE; + } + + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &info); + if (ret) { + perror("line info ioctl failed"); + return EXIT_FAILURE; + } + + printf("%s\n", info.name); + + return EXIT_SUCCESS; +} From patchwork Wed Nov 24 11:42:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 517103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 442D4C433EF for ; Wed, 24 Nov 2021 11:43:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241757AbhKXLqc (ORCPT ); Wed, 24 Nov 2021 06:46:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241778AbhKXLq0 (ORCPT ); Wed, 24 Nov 2021 06:46:26 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F683C06175A for ; Wed, 24 Nov 2021 03:43:08 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id n33-20020a05600c502100b0032fb900951eso5120647wmr.4 for ; Wed, 24 Nov 2021 03:43:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Hwly1RCfwZ6v/FmR8kaOQU7+G0XGMvdAoue1GOwD+54=; b=0dgl3TVCPXC7FfNjQVLR4CALFi3QR1/fcl83imQs7azrjAk+zCXCdcNMGi0MqjDSQx bS8A3KMDHL8nT3VuAQIerWERvz3eCKZUXaeD9RuHoTJ97XtiAyJNDZSLpnQtLxvcAuwQ ucF3rmtdINU1PFx80en5D1xeHEBwmK8uY/gwEYcxlP8+aWKsQ/ec3AepmfuF8r2o3MgY 07A6bbOpM2imqIojKIAmeNcWUg6eWQpMXMlfybLMIjfpTv0dIhCmX7zZucf47m9bjGhz 8zmqspPGYx+IWI4WgJ7FEfL2b9Z4FpA0zr3OetpCRdEbVniJLZ4qXFc+NrWXhC5IFs7s ylMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Hwly1RCfwZ6v/FmR8kaOQU7+G0XGMvdAoue1GOwD+54=; b=gB+uH97kqtugo15a+BCs7HTa5cUYo5qEjFkXaKCKpUFOO23FNNQ9ifSvaKas+FdoAI U/pP1j92r5U1Wn1jEKO7dHx2R9uKIEkh7CKNXkqvMOo8QQ8E9bB6jrz6SJgdFiClMz8L xfSxAMxDQU3xwm0CGwH5krMoFxJ8PSSL/BvP5e6vSq5bag30rKnmroEMZTu4E/UZrwa6 bQrzREzuav5oQkZs5LCA8jMtYnpHHhGgmoEIpvtc6wmR76IPkSiUcC6xg5MUVhXzL4tr ezGAuW1euZdxXNpa2jDpKzJlQKpaiZmrXhv43heQK/R61M/dnDq1pGQ9kIf1mZ9xE8I6 Mpng== X-Gm-Message-State: AOAM531jFjvnKKYcpviWZyLD2R2r5QST3MyifMK1P+Q2TiycdPpx2nbN KigIN/B74GrRRv7Yc0hYZSYoQg== X-Google-Smtp-Source: ABdhPJw5D53wq06caOYuh+qCXHMnmggV5nAMrCEl5Yd7YfEj/ooB0oH20ldDcH/ISvA4qDE6xhlqWw== X-Received: by 2002:a05:600c:3b83:: with SMTP id n3mr13884065wms.116.1637754186581; Wed, 24 Nov 2021 03:43:06 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id s63sm5165173wme.22.2021.11.24.03.43.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 03:43:06 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko , Shuah Khan , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v10 5/5] selftests: gpio: add test cases for gpio-sim Date: Wed, 24 Nov 2021 12:42:57 +0100 Message-Id: <20211124114257.19878-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211124114257.19878-1-brgl@bgdev.pl> References: <20211124114257.19878-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a set of tests for the new gpio-sim module. This is a pure shell test-suite and uses the helper programs available in the gpio selftests directory. These test-cases only test the functionalities exposed by the gpio-sim driver, not those handled by core gpiolib code. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/config | 1 + tools/testing/selftests/gpio/gpio-sim.sh | 306 +++++++++++++++++++++++ 3 files changed, 308 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index 293aa9749408..71b306602368 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -TEST_PROGS := gpio-mockup.sh +TEST_PROGS := gpio-mockup.sh gpio-sim.sh TEST_FILES := gpio-mockup-sysfs.sh TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name CFLAGS += -O2 -g -Wall -I../../../../usr/include/ diff --git a/tools/testing/selftests/gpio/config b/tools/testing/selftests/gpio/config index ce100342c20b..409a8532facc 100644 --- a/tools/testing/selftests/gpio/config +++ b/tools/testing/selftests/gpio/config @@ -1,3 +1,4 @@ CONFIG_GPIOLIB=y CONFIG_GPIO_CDEV=y CONFIG_GPIO_MOCKUP=m +CONFIG_GPIO_SIM=m diff --git a/tools/testing/selftests/gpio/gpio-sim.sh b/tools/testing/selftests/gpio/gpio-sim.sh new file mode 100755 index 000000000000..4d8759d29eac --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-sim.sh @@ -0,0 +1,306 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2021 Bartosz Golaszewski + +BASE_DIR=`dirname $0` +CONFIGFS_DIR="/sys/kernel/config/gpio-sim" +MODULE="gpio-sim" + +fail() { + echo "$*" >&2 + echo "GPIO $MODULE test FAIL" + exit 1 +} + +skip() { + echo "$*" >&2 + echo "GPIO $MODULE test SKIP" + exit 4 +} + +remove_chip() { + local CHIP=$1 + + LINES=`ls $CONFIGFS_DIR/$CHIP/ | egrep ^line` + if [ "$?" == 0 ]; then + for LINE in $LINES; do + if [ -e $CONFIGFS_DIR/$CHIP/$LINE/hog ]; then + rmdir $CONFIGFS_DIR/$CHIP/$LINE/hog || fail "Unable to remove the hog" + fi + + rmdir $CONFIGFS_DIR/$CHIP/$LINE || fail "Unable to remove the line" + done + fi + + rmdir $CONFIGFS_DIR/$CHIP || fail "Unable to remove the chip" +} + +configfs_cleanup() { + for CHIP in `ls $CONFIGFS_DIR/`; do + remove_chip $CHIP + done +} + +create_chip() { + local CHIP=$1 + local CHIP_DIR="$CONFIGFS_DIR/$CHIP" + + mkdir $CHIP_DIR +} + +set_label() { + local CHIP=$1 + local LABEL=$2 + + echo $LABEL > $CONFIGFS_DIR/$CHIP/label || fail "Unable to set the chip label" +} + +set_num_lines() { + local CHIP=$1 + local NUM_LINES=$2 + + echo $NUM_LINES > $CONFIGFS_DIR/$CHIP/num_lines || fail "Unable to set the number of lines" +} + +set_line_name() { + local CHIP=$1 + local OFFSET=$2 + local NAME=$3 + local LINE_DIR=$CONFIGFS_DIR/$CHIP/line$OFFSET + + test -d $LINE_DIR || mkdir $LINE_DIR + echo $NAME > $LINE_DIR/name || fail "Unable to set the line name" +} + +enable_chip() { + local CHIP=$1 + + echo 1 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to enable the chip" +} + +disable_chip() { + local CHIP=$1 + + echo 0 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to disable the chip" +} + +configfs_chip_name() { + local CHIP="$1" + + cat $CONFIGFS_DIR/$CHIP/chip_name 2> /dev/null || return 1 +} + +configfs_dev_name() { + local CHIP="$1" + + cat $CONFIGFS_DIR/$CHIP/dev_name 2> /dev/null || return 1 +} + +get_chip_num_lines() { + local CHIP="$1" + + $BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP` num-lines +} + +get_chip_label() { + local CHIP="$1" + + $BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP` label +} + +get_line_name() { + local CHIP="$1" + local OFFSET="$2" + + $BASE_DIR/gpio-line-name /dev/`configfs_chip_name $CHIP` $OFFSET +} + +sysfs_set_pull() { + local CHIP="$1" + local OFFSET="$2" + local PULL="$3" + local SYSFSPATH="/sys/devices/platform/`configfs_dev_name $CHIP`/sim_gpio$OFFSET/pull" + + echo $PULL > $SYSFSPATH || fail "Unable to set line pull in sysfs" +} + +# Load the gpio-sim module. This will pull in configfs if needed too. +modprobe gpio-sim || skip "unable to load the gpio-sim module" +# Make sure configfs is mounted at /sys/kernel/config. Wait a bit if needed. +for IDX in `seq 5`; do + if [ "$IDX" -eq "5" ]; then + skip "configfs not mounted at /sys/kernel/config" + fi + + mountpoint -q /sys/kernel/config && break + sleep 0.1 +done +# If the module was already loaded: remove all previous chips +configfs_cleanup + +trap "exit 1" SIGTERM SIGINT +trap configfs_cleanup EXIT + +echo "1. chip_name and dev_name attributes" + +echo "1.1. Chip name is communicated to user" +create_chip chip +enable_chip chip +test -n `cat $CONFIGFS_DIR/chip/chip_name` || fail "chip_name doesn't work" +remove_chip chip + +echo "1.2. chip_name returns 'none' if the chip is still pending" +create_chip chip +test "`cat $CONFIGFS_DIR/chip/chip_name`" = "none" || fail "chip_name doesn't return 'none' for a pending chip" +remove_chip chip + +echo "1.3. Device name is communicated to user" +create_chip chip +enable_chip chip +test -n `cat $CONFIGFS_DIR/chip/dev_name` || fail "dev_name doesn't work" +remove_chip chip + +echo "2. Creating and configuring simulated chips" + +echo "2.1. Default number of lines is 1" +create_chip chip +enable_chip chip +test "`get_chip_num_lines chip`" = "1" || fail "default number of lines is not 1" +remove_chip chip + +echo "2.2. Number of lines can be specified" +create_chip chip +set_num_lines chip 16 +enable_chip chip +test "`get_chip_num_lines chip`" = "16" || fail "number of lines is not 16" +remove_chip chip + +echo "2.3. Label can be set" +create_chip chip +set_label chip foobar +enable_chip chip +test "`get_chip_label chip`" = "foobar" || fail "label is incorrect" +remove_chip chip + +echo "2.4. Label can be left empty" +create_chip chip +enable_chip chip +test -z "`cat $CONFIGFS_DIR/chip/label`" || fail "label is not empty" +remove_chip chip + +echo "2.5. Line names can be configured" +create_chip chip +set_num_lines chip 16 +set_line_name chip 0 foo +set_line_name chip 2 bar +enable_chip chip +test "`get_line_name chip 0`" = "foo" || fail "line name is incorrect" +test "`get_line_name chip 2`" = "bar" || fail "line name is incorrect" +remove_chip chip + +echo "2.6. Line config can remain unused if offset is greater than number of lines" +create_chip chip +set_num_lines chip 2 +set_line_name chip 5 foobar +enable_chip chip +test "`get_line_name chip 0`" = "" || fail "line name is incorrect" +test "`get_line_name chip 1`" = "" || fail "line name is incorrect" +remove_chip chip + +echo "2.7. Line configfs directory names are sanitized" +create_chip chip +mkdir $CONFIGFS_DIR/chip/line12foobar 2> /dev/null && fail "invalid configfs line name accepted" +mkdir $CONFIGFS_DIR/chip/line_no_offset 2> /dev/null && fail "invalid configfs line name accepted" +remove_chip chip + +echo "2.8. Multiple chips can be created" +CHIPS="chip0 chip1 chip2" +for CHIP in $CHIPS; do + create_chip $CHIP + enable_chip $CHIP +done +for CHIP in $CHIPS; do + remove_chip $CHIP +done + +echo "2.9. Can't modify settings when chip is live" +create_chip chip +enable_chip chip +echo foobar > $CONFIGFS_DIR/chip/label 2> /dev/null && fail "Setting label of a live chip should fail" +echo 8 > $CONFIGFS_DIR/chip/num_lines 2> /dev/null && fail "Setting number of lines of a live chip should fail" +remove_chip chip + +echo "2.10. Can't create line items when chip is live" +create_chip chip +enable_chip chip +mkdir $CONFIGFS_DIR/chip/line0 2> /dev/null && fail "Creating line item should fail" +remove_chip chip + +echo "2.11. Probe errors are propagated to user-space" +create_chip chip +set_num_lines chip 99999 +echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Probe error was not propagated" +remove_chip chip + +echo "3. Controlling simulated chips" + +echo "3.1. Pull can be set over sysfs" +create_chip chip +set_num_lines chip 8 +enable_chip chip +sysfs_set_pull chip 0 pull-up +$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip` 0 +test "$?" = "1" || fail "pull set incorrectly" +sysfs_set_pull chip 0 pull-down +$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip` 1 +test "$?" = "0" || fail "pull set incorrectly" +remove_chip chip + +echo "3.2. Pull can be read from sysfs" +create_chip chip +set_num_lines chip 8 +enable_chip chip +SYSFS_PATH=/sys/devices/platform/`configfs_dev_name chip`/sim_gpio0/pull +test `cat $SYSFS_PATH` = "pull-down" || fail "reading the pull failed" +sysfs_set_pull chip 0 pull-up +test `cat $SYSFS_PATH` = "pull-up" || fail "reading the pull failed" +remove_chip chip + +echo "3.3. Incorrect input in sysfs is rejected" +create_chip chip +set_num_lines chip 8 +enable_chip chip +SYSFS_PATH="/sys/devices/platform/`configfs_dev_name chip`/sim_gpio0/pull" +echo foobar > $SYSFS_PATH 2> /dev/null && fail "invalid input not detected" +remove_chip chip + +echo "3.4. Can't write to value" +create_chip chip +enable_chip chip +SYSFS_PATH=/sys/devices/platform/`configfs_dev_name chip`/sim_gpio0/value +echo 1 > $SYSFS_PATH 2> /dev/null && fail "writing to 'value' succeeded unexpectedly" +remove_chip chip + +echo "4. Simulated GPIO chips are functional" + +echo "4.1. Values can be read from sysfs" +create_chip chip +set_num_lines chip 8 +enable_chip chip +SYSFS_PATH="/sys/devices/platform/`configfs_dev_name chip`/sim_gpio0/value" +test `cat $SYSFS_PATH` = "0" || fail "incorrect value read from sysfs" +$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip` 0 & +sleep 0.1 # FIXME Any better way? +test `cat $SYSFS_PATH` = "1" || fail "incorrect value read from sysfs" +kill $! +remove_chip chip + +echo "4.2. Bias settings work correctly" +create_chip chip +set_num_lines chip 8 +enable_chip chip +$BASE_DIR/gpio-mockup-cdev -b pull-up /dev/`configfs_chip_name chip` 0 +test `cat $SYSFS_PATH` = "1" || fail "bias setting does not work" +remove_chip chip + +echo "GPIO $MODULE test PASS"