diff mbox series

gpio: sim: fix hogs with custom chip labels

Message ID 20220208105218.316834-1-brgl@bgdev.pl
State Accepted
Commit c162ca0bcbfb39308c4dff4157e27c751af7032a
Headers show
Series gpio: sim: fix hogs with custom chip labels | expand

Commit Message

Bartosz Golaszewski Feb. 8, 2022, 10:52 a.m. UTC
We always assign the default device name as the chip_label in hog
structures which makes it impossible to assign hogs to chips. Let's
first check if a custom label was set and then copy it instead of the
default device name.

Fixes: cb8c474e79be ("gpio: sim: new testing module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 drivers/gpio/gpio-sim.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

Comments

Bartosz Golaszewski Feb. 9, 2022, 8:03 a.m. UTC | #1
On Tue, Feb 8, 2022 at 9:03 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Tue, Feb 8, 2022 at 8:34 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> >
> > We always assign the default device name as the chip_label in hog
> > structures which makes it impossible to assign hogs to chips. Let's
> > first check if a custom label was set and then copy it instead of the
> > default device name.
>
> ...
>
> > +       return (bank->label && (strlen(bank->label) > 0));
>
> It can be simply
>
>   return bank->label && strlen(bank->label);
>
> Or even w/o strlen() call:
>
>   return bank->label && *bank->label; // or bank->label && bank->label[0]
>

Yeah makes sense, I will change it when applying.

Bart

> Either way the code LGTM,
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
>
> --
> With Best Regards,
> Andy Shevchenko
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 04b137eca8da..33df418ea8d1 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -570,6 +570,11 @@  static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
 	return container_of(group, struct gpio_sim_bank, group);
 }
 
+static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
+{
+	return (bank->label && (strlen(bank->label) > 0));
+}
+
 static struct gpio_sim_device *
 gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
 {
@@ -770,9 +775,15 @@  static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
 			 * point the device doesn't exist yet and so dev_name()
 			 * is not available.
 			 */
-			hog->chip_label = kasprintf(GFP_KERNEL,
-						    "gpio-sim.%u-%s", dev->id,
-						    fwnode_get_name(bank->swnode));
+			if (gpio_sim_bank_has_label(bank))
+				hog->chip_label = kstrdup(bank->label,
+							  GFP_KERNEL);
+			else
+				hog->chip_label = kasprintf(GFP_KERNEL,
+							"gpio-sim.%u-%s",
+							dev->id,
+							fwnode_get_name(
+								bank->swnode));
 			if (!hog->chip_label) {
 				gpio_sim_remove_hogs(dev);
 				return -ENOMEM;
@@ -816,7 +827,7 @@  gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
 
 	properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
 
-	if (bank->label && (strlen(bank->label) > 0))
+	if (gpio_sim_bank_has_label(bank))
 		properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
 							       bank->label);