public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Linus Walleij <linus.walleij@linaro.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Kent Gibson <warthog618@gmail.com>
Cc: linux-gpio@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [PATCH 22/23] gpio: mockup: provide a way to create new dummy chips
Date: Fri,  4 Sep 2020 17:45:46 +0200	[thread overview]
Message-ID: <20200904154547.3836-23-brgl@bgdev.pl> (raw)
In-Reply-To: <20200904154547.3836-1-brgl@bgdev.pl>

From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Add a new debugfs attribute 'new_device' that allows to dynamically
create new dummy chips according to specification.

New chips are created by writing a number of supported parameters to the
new attribute of which 'label' and 'num_lines' are mandatory. The new
attribute is designed to be easily exstensible with new parameters. For
now we simply provide the same functionality that the module params
expose but with the intention of introducing new options (such as custom
line name formats).

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/gpio/gpio-mockup.c | 231 +++++++++++++++++++++++++++++++++++--
 1 file changed, 220 insertions(+), 11 deletions(-)

diff --git a/drivers/gpio/gpio-mockup.c b/drivers/gpio/gpio-mockup.c
index 9d2de78a45c2..6577d18671df 100644
--- a/drivers/gpio/gpio-mockup.c
+++ b/drivers/gpio/gpio-mockup.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2014  Kamlakant Patel <kamlakant.patel@broadcom.com>
  * Copyright (C) 2015-2016  Bamvor Jian Zhang <bamv2005@gmail.com>
  * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
+ * Copyright (C) 2020 Bartosz Golaszewski <bgolaszewski@baylibre.com>
  */
 
 #include <linux/debugfs.h>
@@ -14,6 +15,7 @@
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
 #include <linux/irqdomain.h>
+#include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -546,8 +548,8 @@ static void gpio_mockup_unregister_devices(void)
 	mutex_unlock(&gpio_mockup_devices_lock);
 }
 
-static __init char **gpio_mockup_make_line_names(const char *label,
-						 unsigned int num_lines)
+static char **gpio_mockup_make_line_names(const char *label,
+					  unsigned int num_lines)
 {
 	unsigned int i;
 	char **names;
@@ -567,7 +569,7 @@ static __init char **gpio_mockup_make_line_names(const char *label,
 	return names;
 }
 
-static int __init gpio_mockup_register_device(struct property_entry *properties)
+static int gpio_mockup_register_device(struct property_entry *properties)
 {
 	struct gpio_mockup_device *mockup_dev;
 	struct platform_device_info pdevinfo;
@@ -641,8 +643,7 @@ static int __init gpio_mockup_register_chips_from_params(void)
 {
 	int num_chips, i, ret;
 
-	if ((gpio_mockup_num_ranges < 2) ||
-	    (gpio_mockup_num_ranges % 2) ||
+	if ((gpio_mockup_num_ranges % 2) ||
 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
 		return -EINVAL;
 
@@ -669,6 +670,205 @@ static int __init gpio_mockup_register_chips_from_params(void)
 	return 0;
 }
 
+/*
+ * We store all data associated with device properties in this structure. It's
+ * only needed until we register the platform device at which point the driver
+ * core makes a deep copy of all property data (even the string arrays).
+ *
+ * The reason to keep them bunched up is simple: we can have a single function
+ * to free all resources which simplifies error handling.
+ */
+struct gpio_mockup_prop_data {
+	char chip_label[GPIO_MOCKUP_LABEL_SIZE];
+	u16 ngpio;
+	bool named_lines;
+	char **line_names;
+};
+
+/* We don't free the structure itself - it's expected to live on the stack. */
+static void
+gpio_mockup_free_property_data(struct gpio_mockup_prop_data *prop_data)
+{
+	kfree_strarray(prop_data->line_names, prop_data->ngpio);
+}
+
+/*
+ * Each supported option is parsed by a separate callback - this way the
+ * 'new_device' attribute is easily exstensible.
+ */
+struct gpio_mockup_new_device_opt {
+	char *name;
+	int (*func)(const char *val, struct gpio_mockup_prop_data *prop_data,
+		    struct property_entry *properties, int *prop_idx);
+	bool has_val;
+};
+
+static int
+gpio_mockup_parse_label(const char *val,
+		struct gpio_mockup_prop_data *prop_data,
+		struct property_entry *properties, int *prop_idx)
+{
+	snprintf(prop_data->chip_label, sizeof(prop_data->chip_label), val);
+	properties[(*prop_idx)++] =
+		PROPERTY_ENTRY_STRING("chip-label", prop_data->chip_label);
+
+	return 0;
+}
+
+static int gpio_mockup_parse_num_lines(const char *val,
+			struct gpio_mockup_prop_data *prop_data,
+			struct property_entry *properties, int *prop_idx)
+{
+	int ret;
+
+	ret = kstrtou16(val, 10, &prop_data->ngpio);
+	if (ret) {
+		pr_err("invalid new_lines format: %s\n", val);
+		return ret;
+	}
+
+	properties[(*prop_idx)++] = PROPERTY_ENTRY_U16("nr-gpios",
+						     prop_data->ngpio);
+
+	return 0;
+}
+
+static int gpio_mockup_parse_named_lines(const char *val,
+			struct gpio_mockup_prop_data *prop_data,
+			struct property_entry *properties, int *prop_idx)
+{
+	prop_data->named_lines = true;
+
+	return 0;
+}
+
+static struct gpio_mockup_new_device_opt gpio_mockup_new_device_opts[] = {
+	{
+		.name = "label",
+		.func = gpio_mockup_parse_label,
+		.has_val = true,
+	},
+	{
+		.name = "num_lines",
+		.func = gpio_mockup_parse_num_lines,
+		.has_val = true,
+	},
+	{
+		.name = "named_lines",
+		.func = gpio_mockup_parse_named_lines,
+		.has_val = false,
+	},
+};
+
+static int
+gpio_mockup_parse_one_opt(const char *key, const char *val,
+			  struct gpio_mockup_prop_data *prop_data,
+			  struct property_entry *properties, int *prop_idx)
+{
+	struct gpio_mockup_new_device_opt *opt;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(gpio_mockup_new_device_opts); i++) {
+		opt = &gpio_mockup_new_device_opts[i];
+
+		if (strcmp(key, opt->name) == 0) {
+			if (opt->has_val && !val) {
+				pr_err("%s option requires an argument\n",
+				       opt->name);
+				return -EINVAL;
+			}
+
+			if (!opt->has_val && val) {
+				pr_err("%s option doesn't take any arguments\n",
+				       opt->name);
+				return -EINVAL;
+			}
+
+			return opt->func(val, prop_data, properties, prop_idx);
+		}
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int
+gpio_mockup_new_device_from_opts(char *opts,
+				 struct gpio_mockup_prop_data *prop_data)
+{
+	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
+	int prop_idx = 0, ret;
+	char *key, *val;
+
+	memset(properties, 0, sizeof(properties));
+
+	while (*opts) {
+		if (prop_idx >= GPIO_MOCKUP_MAX_PROP)
+			return -EINVAL;
+
+		opts = next_arg(opts, &key, &val);
+
+		ret = gpio_mockup_parse_one_opt(key, val, prop_data,
+						properties, &prop_idx);
+		if (ret)
+			return ret;
+	}
+
+	/* This is the only mandatory property. */
+	if (!prop_data->ngpio) {
+		pr_err("number of lines must be specified\n");
+		return -EINVAL;
+	}
+
+	/*
+	 * Line names must be created at the end - once we know how
+	 * many GPIOs there are.
+	 */
+	if (prop_data->named_lines) {
+		prop_data->line_names = gpio_mockup_make_line_names(
+							prop_data->chip_label,
+							prop_data->ngpio);
+		if (!prop_data->line_names)
+			return -ENOMEM;
+
+		properties[prop_idx++] =
+			PROPERTY_ENTRY_STRING_ARRAY_LEN("gpio-line-names",
+							prop_data->line_names,
+							prop_data->ngpio);
+	}
+
+	return gpio_mockup_register_device(properties);
+}
+
+static ssize_t gpio_mockup_debugfs_new_device_write(struct file *file,
+						    const char __user *usr_buf,
+						    size_t size, loff_t *ppos)
+{
+	struct gpio_mockup_prop_data prop_data;
+	char opts[128];
+	int ret;
+
+	if (*ppos != 0 || size > sizeof(opts))
+		return -EINVAL;
+
+	ret = getline_from_user(opts, sizeof(opts), usr_buf, size);
+	if (ret < 0)
+		return ret;
+
+	memset(&prop_data, 0, sizeof(prop_data));
+
+	ret = gpio_mockup_new_device_from_opts(opts, &prop_data);
+	gpio_mockup_free_property_data(&prop_data);
+	return ret < 0 ? ret : size;
+}
+
+static const struct file_operations gpio_mockup_debugfs_new_device_ops = {
+	.owner = THIS_MODULE,
+	.open = gpio_mockup_debugfs_open,
+	.write = gpio_mockup_debugfs_new_device_write,
+	.llseek = no_llseek,
+	.release = single_release,
+};
+
 static ssize_t gpio_mockup_debugfs_delete_device_write(struct file *file,
 						const char __user *usr_buf,
 						size_t size, loff_t *ppos)
@@ -726,6 +926,13 @@ static int __init gpio_mockup_debugfs_init(void)
 	if (IS_ERR(gpio_mockup_dbg_dir))
 		return PTR_ERR(gpio_mockup_dbg_dir);
 
+	entry = debugfs_create_file("new_device", 0200, gpio_mockup_dbg_dir,
+				NULL, &gpio_mockup_debugfs_new_device_ops);
+	if (IS_ERR(entry)) {
+		debugfs_remove_recursive(gpio_mockup_dbg_dir);
+		return PTR_ERR(entry);
+	}
+
 	entry = debugfs_create_file("delete_device", 0200, gpio_mockup_dbg_dir,
 				NULL, &gpio_mockup_debugfs_delete_device_ops);
 	if (IS_ERR(entry)) {
@@ -751,12 +958,14 @@ static int __init gpio_mockup_init(void)
 		return ret;
 	}
 
-	ret = gpio_mockup_register_chips_from_params();
-	if (ret) {
-		pr_err("error registering device");
-		debugfs_remove_recursive(gpio_mockup_dbg_dir);
-		platform_driver_unregister(&gpio_mockup_driver);
-		return ret;
+	if (gpio_mockup_ranges > 0) {
+		ret = gpio_mockup_register_chips_from_params();
+		if (ret) {
+			pr_err("error registering device");
+			debugfs_remove_recursive(gpio_mockup_dbg_dir);
+			platform_driver_unregister(&gpio_mockup_driver);
+			return ret;
+		}
 	}
 
 	return 0;
-- 
2.26.1


  parent reply	other threads:[~2020-09-04 15:48 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-04 15:45 [PATCH 00/23] gpio: mockup: support dynamically created and removed chips Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 01/23] lib: cmdline: export next_arg() Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 02/23] lib: string_helpers: provide kfree_strarray() Bartosz Golaszewski
2020-09-04 16:33   ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 03/23] lib: uaccess: provide getline_from_user() Bartosz Golaszewski
2020-09-04 16:35   ` Andy Shevchenko
2020-09-07 10:02     ` Bartosz Golaszewski
2020-09-07 10:18       ` Andy Shevchenko
2020-09-07 10:28         ` Bartosz Golaszewski
2020-09-07 11:45           ` Andy Shevchenko
2020-09-07 11:57             ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 04/23] gpiolib: generalize devprop_gpiochip_set_names() for device properties Bartosz Golaszewski
2020-09-04 16:38   ` Andy Shevchenko
2020-09-07 10:56     ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 05/23] gpiolib: unexport devprop_gpiochip_set_names() Bartosz Golaszewski
2020-09-04 16:40   ` Andy Shevchenko
2020-09-07 10:53     ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 06/23] gpiolib: switch to simpler IDA interface Bartosz Golaszewski
2020-09-04 16:41   ` Andy Shevchenko
2020-09-07 10:50     ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 07/23] gpio: mockup: drop unneeded includes Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 08/23] gpio: mockup: use pr_fmt() Bartosz Golaszewski
2020-09-04 16:29   ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 09/23] gpio: mockup: use KBUILD_MODNAME Bartosz Golaszewski
2020-09-04 16:30   ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 10/23] gpio: mockup: fix resource leak in error path Bartosz Golaszewski
2020-09-04 17:00   ` Andy Shevchenko
2020-09-07 11:04     ` Bartosz Golaszewski
2020-09-07 11:47       ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 11/23] gpio: mockup: remove the limit on number of dummy chips Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 12/23] gpio: mockup: define a constant for chip label size Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 13/23] gpio: mockup: pass the chip label as device property Bartosz Golaszewski
2020-09-04 16:48   ` Andy Shevchenko
2020-09-07 11:01     ` Bartosz Golaszewski
2020-09-07 11:48       ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 14/23] gpio: mockup: use the generic 'gpio-line-names' property Bartosz Golaszewski
2020-09-04 16:46   ` Andy Shevchenko
2020-09-07 10:58     ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 15/23] gpio: mockup: use dynamic device IDs Bartosz Golaszewski
2020-09-04 16:49   ` Andy Shevchenko
2020-09-07 11:04     ` Bartosz Golaszewski
2020-09-07 11:50       ` Andy Shevchenko
2020-09-07 11:59         ` Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 16/23] gpio: mockup: refactor the module init function Bartosz Golaszewski
2020-09-04 16:50   ` Andy Shevchenko
2020-09-07 11:05     ` Bartosz Golaszewski
2020-09-07 11:51       ` Andy Shevchenko
2020-09-04 15:45 ` [PATCH 17/23] gpio: mockup: rename and move around debugfs callbacks Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 18/23] gpio: mockup: require debugfs to build Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 19/23] gpio: mockup: add a symlink for the per-chip debugfs directory Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 20/23] gpio: mockup: add a lock for dummy device list Bartosz Golaszewski
2020-09-04 15:45 ` [PATCH 21/23] gpio: mockup: provide a way to delete dummy chips Bartosz Golaszewski
2020-09-04 16:56   ` Andy Shevchenko
2020-09-04 15:45 ` Bartosz Golaszewski [this message]
2020-09-04 15:45 ` [PATCH 23/23] Documentation: gpio: add documentation for gpio-mockup Bartosz Golaszewski
2020-09-04 16:58   ` Andy Shevchenko
2020-09-05  3:15   ` Randy Dunlap
2020-09-07  9:59     ` Andy Shevchenko
2020-09-07 10:26       ` Bartosz Golaszewski
2020-09-07 11:53         ` Andy Shevchenko
2020-09-07 12:06           ` Bartosz Golaszewski
2020-09-07 12:22             ` Greg Kroah-Hartman
2020-09-07 13:49               ` Bartosz Golaszewski
2020-09-07 14:08                 ` Andy Shevchenko
2020-09-07 15:14                   ` Bartosz Golaszewski
2020-09-07 15:23                   ` Geert Uytterhoeven
2020-09-07 16:08                     ` Bartosz Golaszewski
2020-09-08 17:03               ` Bartosz Golaszewski
2020-09-11 12:56                 ` Greg Kroah-Hartman
2020-09-11 13:07                   ` Bartosz Golaszewski
2020-09-07 12:38             ` Andy Shevchenko
2020-09-07 12:57               ` Bartosz Golaszewski
2020-09-07 13:52                 ` Andy Shevchenko
2020-09-07 10:45     ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200904154547.3836-23-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=corbet@lwn.net \
    --cc=linus.walleij@linaro.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox