Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GPIO testing driver based on configfs.
0004  *
0005  * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/bitmap.h>
0011 #include <linux/completion.h>
0012 #include <linux/configfs.h>
0013 #include <linux/device.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/gpio/machine.h>
0016 #include <linux/idr.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/irq.h>
0019 #include <linux/irq_sim.h>
0020 #include <linux/list.h>
0021 #include <linux/mod_devicetable.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 #include <linux/notifier.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/property.h>
0027 #include <linux/slab.h>
0028 #include <linux/string.h>
0029 #include <linux/string_helpers.h>
0030 #include <linux/sysfs.h>
0031 
0032 #include "gpiolib.h"
0033 
0034 #define GPIO_SIM_PROP_MAX   4 /* Max 3 properties + sentinel. */
0035 #define GPIO_SIM_NUM_ATTRS  3 /* value, pull and sentinel */
0036 
0037 static DEFINE_IDA(gpio_sim_ida);
0038 
0039 struct gpio_sim_chip {
0040     struct gpio_chip gc;
0041     unsigned long *direction_map;
0042     unsigned long *value_map;
0043     unsigned long *pull_map;
0044     struct irq_domain *irq_sim;
0045     struct mutex lock;
0046     const struct attribute_group **attr_groups;
0047 };
0048 
0049 struct gpio_sim_attribute {
0050     struct device_attribute dev_attr;
0051     unsigned int offset;
0052 };
0053 
0054 static struct gpio_sim_attribute *
0055 to_gpio_sim_attr(struct device_attribute *dev_attr)
0056 {
0057     return container_of(dev_attr, struct gpio_sim_attribute, dev_attr);
0058 }
0059 
0060 static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
0061                    unsigned int offset, int value)
0062 {
0063     int irq, irq_type, ret;
0064     struct gpio_desc *desc;
0065     struct gpio_chip *gc;
0066 
0067     gc = &chip->gc;
0068     desc = &gc->gpiodev->descs[offset];
0069 
0070     mutex_lock(&chip->lock);
0071 
0072     if (test_bit(FLAG_REQUESTED, &desc->flags) &&
0073         !test_bit(FLAG_IS_OUT, &desc->flags)) {
0074         if (value == !!test_bit(offset, chip->value_map))
0075             goto set_pull;
0076 
0077         /*
0078          * This is fine - it just means, nobody is listening
0079          * for interrupts on this line, otherwise
0080          * irq_create_mapping() would have been called from
0081          * the to_irq() callback.
0082          */
0083         irq = irq_find_mapping(chip->irq_sim, offset);
0084         if (!irq)
0085             goto set_value;
0086 
0087         irq_type = irq_get_trigger_type(irq);
0088 
0089         if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
0090             (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
0091             ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
0092                             true);
0093             if (ret)
0094                 goto set_pull;
0095         }
0096     }
0097 
0098 set_value:
0099     /* Change the value unless we're actively driving the line. */
0100     if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
0101         !test_bit(FLAG_IS_OUT, &desc->flags))
0102         __assign_bit(offset, chip->value_map, value);
0103 
0104 set_pull:
0105     __assign_bit(offset, chip->pull_map, value);
0106     mutex_unlock(&chip->lock);
0107     return 0;
0108 }
0109 
0110 static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset)
0111 {
0112     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0113     int ret;
0114 
0115     mutex_lock(&chip->lock);
0116     ret = !!test_bit(offset, chip->value_map);
0117     mutex_unlock(&chip->lock);
0118 
0119     return ret;
0120 }
0121 
0122 static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
0123 {
0124     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0125 
0126     mutex_lock(&chip->lock);
0127     __assign_bit(offset, chip->value_map, value);
0128     mutex_unlock(&chip->lock);
0129 }
0130 
0131 static int gpio_sim_get_multiple(struct gpio_chip *gc,
0132                  unsigned long *mask, unsigned long *bits)
0133 {
0134     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0135 
0136     mutex_lock(&chip->lock);
0137     bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio);
0138     mutex_unlock(&chip->lock);
0139 
0140     return 0;
0141 }
0142 
0143 static void gpio_sim_set_multiple(struct gpio_chip *gc,
0144                   unsigned long *mask, unsigned long *bits)
0145 {
0146     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0147 
0148     mutex_lock(&chip->lock);
0149     bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);
0150     mutex_unlock(&chip->lock);
0151 }
0152 
0153 static int gpio_sim_direction_output(struct gpio_chip *gc,
0154                      unsigned int offset, int value)
0155 {
0156     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0157 
0158     mutex_lock(&chip->lock);
0159     __clear_bit(offset, chip->direction_map);
0160     __assign_bit(offset, chip->value_map, value);
0161     mutex_unlock(&chip->lock);
0162 
0163     return 0;
0164 }
0165 
0166 static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset)
0167 {
0168     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0169 
0170     mutex_lock(&chip->lock);
0171     __set_bit(offset, chip->direction_map);
0172     mutex_unlock(&chip->lock);
0173 
0174     return 0;
0175 }
0176 
0177 static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset)
0178 {
0179     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0180     int direction;
0181 
0182     mutex_lock(&chip->lock);
0183     direction = !!test_bit(offset, chip->direction_map);
0184     mutex_unlock(&chip->lock);
0185 
0186     return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
0187 }
0188 
0189 static int gpio_sim_set_config(struct gpio_chip *gc,
0190                   unsigned int offset, unsigned long config)
0191 {
0192     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0193 
0194     switch (pinconf_to_config_param(config)) {
0195     case PIN_CONFIG_BIAS_PULL_UP:
0196         return gpio_sim_apply_pull(chip, offset, 1);
0197     case PIN_CONFIG_BIAS_PULL_DOWN:
0198         return gpio_sim_apply_pull(chip, offset, 0);
0199     default:
0200         break;
0201     }
0202 
0203     return -ENOTSUPP;
0204 }
0205 
0206 static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset)
0207 {
0208     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0209 
0210     return irq_create_mapping(chip->irq_sim, offset);
0211 }
0212 
0213 static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
0214 {
0215     struct gpio_sim_chip *chip = gpiochip_get_data(gc);
0216 
0217     mutex_lock(&chip->lock);
0218     __assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map));
0219     mutex_unlock(&chip->lock);
0220 }
0221 
0222 static ssize_t gpio_sim_sysfs_val_show(struct device *dev,
0223                        struct device_attribute *attr, char *buf)
0224 {
0225     struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
0226     struct gpio_sim_chip *chip = dev_get_drvdata(dev);
0227     int val;
0228 
0229     mutex_lock(&chip->lock);
0230     val = !!test_bit(line_attr->offset, chip->value_map);
0231     mutex_unlock(&chip->lock);
0232 
0233     return sysfs_emit(buf, "%d\n", val);
0234 }
0235 
0236 static ssize_t gpio_sim_sysfs_val_store(struct device *dev,
0237                     struct device_attribute *attr,
0238                     const char *buf, size_t count)
0239 {
0240     /*
0241      * Not assigning this function will result in write() returning -EIO
0242      * which is confusing. Return -EPERM explicitly.
0243      */
0244     return -EPERM;
0245 }
0246 
0247 static const char *const gpio_sim_sysfs_pull_strings[] = {
0248     [0] = "pull-down",
0249     [1] = "pull-up",
0250 };
0251 
0252 static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
0253                     struct device_attribute *attr,
0254                     char *buf)
0255 {
0256     struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
0257     struct gpio_sim_chip *chip = dev_get_drvdata(dev);
0258     int pull;
0259 
0260     mutex_lock(&chip->lock);
0261     pull = !!test_bit(line_attr->offset, chip->pull_map);
0262     mutex_unlock(&chip->lock);
0263 
0264     return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]);
0265 }
0266 
0267 static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
0268                      struct device_attribute *attr,
0269                      const char *buf, size_t len)
0270 {
0271     struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
0272     struct gpio_sim_chip *chip = dev_get_drvdata(dev);
0273     int ret, pull;
0274 
0275     pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf);
0276     if (pull < 0)
0277         return pull;
0278 
0279     ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
0280     if (ret)
0281         return ret;
0282 
0283     return len;
0284 }
0285 
0286 static void gpio_sim_mutex_destroy(void *data)
0287 {
0288     struct mutex *lock = data;
0289 
0290     mutex_destroy(lock);
0291 }
0292 
0293 static void gpio_sim_sysfs_remove(void *data)
0294 {
0295     struct gpio_sim_chip *chip = data;
0296 
0297     sysfs_remove_groups(&chip->gc.gpiodev->dev.kobj, chip->attr_groups);
0298 }
0299 
0300 static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
0301 {
0302     struct device_attribute *val_dev_attr, *pull_dev_attr;
0303     struct gpio_sim_attribute *val_attr, *pull_attr;
0304     unsigned int num_lines = chip->gc.ngpio;
0305     struct device *dev = chip->gc.parent;
0306     struct attribute_group *attr_group;
0307     struct attribute **attrs;
0308     int i, ret;
0309 
0310     chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups),
0311                      num_lines + 1, GFP_KERNEL);
0312     if (!chip->attr_groups)
0313         return -ENOMEM;
0314 
0315     for (i = 0; i < num_lines; i++) {
0316         attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL);
0317         attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs),
0318                      GFP_KERNEL);
0319         val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL);
0320         pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL);
0321         if (!attr_group || !attrs || !val_attr || !pull_attr)
0322             return -ENOMEM;
0323 
0324         attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
0325                           "sim_gpio%u", i);
0326         if (!attr_group->name)
0327             return -ENOMEM;
0328 
0329         val_attr->offset = pull_attr->offset = i;
0330 
0331         val_dev_attr = &val_attr->dev_attr;
0332         pull_dev_attr = &pull_attr->dev_attr;
0333 
0334         sysfs_attr_init(&val_dev_attr->attr);
0335         sysfs_attr_init(&pull_dev_attr->attr);
0336 
0337         val_dev_attr->attr.name = "value";
0338         pull_dev_attr->attr.name = "pull";
0339 
0340         val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644;
0341 
0342         val_dev_attr->show = gpio_sim_sysfs_val_show;
0343         val_dev_attr->store = gpio_sim_sysfs_val_store;
0344         pull_dev_attr->show = gpio_sim_sysfs_pull_show;
0345         pull_dev_attr->store = gpio_sim_sysfs_pull_store;
0346 
0347         attrs[0] = &val_dev_attr->attr;
0348         attrs[1] = &pull_dev_attr->attr;
0349 
0350         attr_group->attrs = attrs;
0351         chip->attr_groups[i] = attr_group;
0352     }
0353 
0354     ret = sysfs_create_groups(&chip->gc.gpiodev->dev.kobj,
0355                   chip->attr_groups);
0356     if (ret)
0357         return ret;
0358 
0359     return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
0360 }
0361 
0362 static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
0363 {
0364     struct gpio_sim_chip *chip;
0365     struct gpio_chip *gc;
0366     const char *label;
0367     u32 num_lines;
0368     int ret;
0369 
0370     ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines);
0371     if (ret)
0372         return ret;
0373 
0374     ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label);
0375     if (ret) {
0376         label = devm_kasprintf(dev, GFP_KERNEL, "%s-%s",
0377                        dev_name(dev), fwnode_get_name(swnode));
0378         if (!label)
0379             return -ENOMEM;
0380     }
0381 
0382     chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
0383     if (!chip)
0384         return -ENOMEM;
0385 
0386     chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL);
0387     if (!chip->direction_map)
0388         return -ENOMEM;
0389 
0390     /* Default to input mode. */
0391     bitmap_fill(chip->direction_map, num_lines);
0392 
0393     chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
0394     if (!chip->value_map)
0395         return -ENOMEM;
0396 
0397     chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
0398     if (!chip->pull_map)
0399         return -ENOMEM;
0400 
0401     chip->irq_sim = devm_irq_domain_create_sim(dev, NULL, num_lines);
0402     if (IS_ERR(chip->irq_sim))
0403         return PTR_ERR(chip->irq_sim);
0404 
0405     mutex_init(&chip->lock);
0406     ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
0407                        &chip->lock);
0408     if (ret)
0409         return ret;
0410 
0411     gc = &chip->gc;
0412     gc->base = -1;
0413     gc->ngpio = num_lines;
0414     gc->label = label;
0415     gc->owner = THIS_MODULE;
0416     gc->parent = dev;
0417     gc->fwnode = swnode;
0418     gc->get = gpio_sim_get;
0419     gc->set = gpio_sim_set;
0420     gc->get_multiple = gpio_sim_get_multiple;
0421     gc->set_multiple = gpio_sim_set_multiple;
0422     gc->direction_output = gpio_sim_direction_output;
0423     gc->direction_input = gpio_sim_direction_input;
0424     gc->get_direction = gpio_sim_get_direction;
0425     gc->set_config = gpio_sim_set_config;
0426     gc->to_irq = gpio_sim_to_irq;
0427     gc->free = gpio_sim_free;
0428 
0429     ret = devm_gpiochip_add_data(dev, gc, chip);
0430     if (ret)
0431         return ret;
0432 
0433     /* Used by sysfs and configfs callbacks. */
0434     dev_set_drvdata(&gc->gpiodev->dev, chip);
0435 
0436     return gpio_sim_setup_sysfs(chip);
0437 }
0438 
0439 static int gpio_sim_probe(struct platform_device *pdev)
0440 {
0441     struct device *dev = &pdev->dev;
0442     struct fwnode_handle *swnode;
0443     int ret;
0444 
0445     device_for_each_child_node(dev, swnode) {
0446         ret = gpio_sim_add_bank(swnode, dev);
0447         if (ret) {
0448             fwnode_handle_put(swnode);
0449             return ret;
0450         }
0451     }
0452 
0453     return 0;
0454 }
0455 
0456 static const struct of_device_id gpio_sim_of_match[] = {
0457     { .compatible = "gpio-simulator" },
0458     { }
0459 };
0460 MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
0461 
0462 static struct platform_driver gpio_sim_driver = {
0463     .driver = {
0464         .name = "gpio-sim",
0465         .of_match_table = gpio_sim_of_match,
0466     },
0467     .probe = gpio_sim_probe,
0468 };
0469 
0470 struct gpio_sim_device {
0471     struct config_group group;
0472 
0473     /*
0474      * If pdev is NULL, the device is 'pending' (waiting for configuration).
0475      * Once the pointer is assigned, the device has been created and the
0476      * item is 'live'.
0477      */
0478     struct platform_device *pdev;
0479     int id;
0480 
0481     /*
0482      * Each configfs filesystem operation is protected with the subsystem
0483      * mutex. Each separate attribute is protected with the buffer mutex.
0484      * This structure however can be modified by callbacks of different
0485      * attributes so we need another lock.
0486      *
0487      * We use this lock fo protecting all data structures owned by this
0488      * object too.
0489      */
0490     struct mutex lock;
0491 
0492     /*
0493      * This is used to synchronously wait for the driver's probe to complete
0494      * and notify the user-space about any errors.
0495      */
0496     struct notifier_block bus_notifier;
0497     struct completion probe_completion;
0498     bool driver_bound;
0499 
0500     struct gpiod_hog *hogs;
0501 
0502     struct list_head bank_list;
0503 };
0504 
0505 /* This is called with dev->lock already taken. */
0506 static int gpio_sim_bus_notifier_call(struct notifier_block *nb,
0507                       unsigned long action, void *data)
0508 {
0509     struct gpio_sim_device *simdev = container_of(nb,
0510                               struct gpio_sim_device,
0511                               bus_notifier);
0512     struct device *dev = data;
0513     char devname[32];
0514 
0515     snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id);
0516 
0517     if (strcmp(dev_name(dev), devname) == 0) {
0518         if (action == BUS_NOTIFY_BOUND_DRIVER)
0519             simdev->driver_bound = true;
0520         else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND)
0521             simdev->driver_bound = false;
0522         else
0523             return NOTIFY_DONE;
0524 
0525         complete(&simdev->probe_completion);
0526         return NOTIFY_OK;
0527     }
0528 
0529     return NOTIFY_DONE;
0530 }
0531 
0532 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
0533 {
0534     struct config_group *group = to_config_group(item);
0535 
0536     return container_of(group, struct gpio_sim_device, group);
0537 }
0538 
0539 struct gpio_sim_bank {
0540     struct config_group group;
0541 
0542     /*
0543      * We could have used the ci_parent field of the config_item but
0544      * configfs is stupid and calls the item's release callback after
0545      * already having cleared the parent pointer even though the parent
0546      * is guaranteed to survive the child...
0547      *
0548      * So we need to store the pointer to the parent struct here. We can
0549      * dereference it anywhere we need with no checks and no locking as
0550      * it's guaranteed to survive the children and protected by configfs
0551      * locks.
0552      *
0553      * Same for other structures.
0554      */
0555     struct gpio_sim_device *parent;
0556     struct list_head siblings;
0557 
0558     char *label;
0559     unsigned int num_lines;
0560 
0561     struct list_head line_list;
0562 
0563     struct fwnode_handle *swnode;
0564 };
0565 
0566 static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
0567 {
0568     struct config_group *group = to_config_group(item);
0569 
0570     return container_of(group, struct gpio_sim_bank, group);
0571 }
0572 
0573 static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
0574 {
0575     return bank->label && *bank->label;
0576 }
0577 
0578 static struct gpio_sim_device *
0579 gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
0580 {
0581     return bank->parent;
0582 }
0583 
0584 struct gpio_sim_hog;
0585 
0586 struct gpio_sim_line {
0587     struct config_group group;
0588 
0589     struct gpio_sim_bank *parent;
0590     struct list_head siblings;
0591 
0592     unsigned int offset;
0593     char *name;
0594 
0595     /* There can only be one hog per line. */
0596     struct gpio_sim_hog *hog;
0597 };
0598 
0599 static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item)
0600 {
0601     struct config_group *group = to_config_group(item);
0602 
0603     return container_of(group, struct gpio_sim_line, group);
0604 }
0605 
0606 static struct gpio_sim_device *
0607 gpio_sim_line_get_device(struct gpio_sim_line *line)
0608 {
0609     struct gpio_sim_bank *bank = line->parent;
0610 
0611     return gpio_sim_bank_get_device(bank);
0612 }
0613 
0614 struct gpio_sim_hog {
0615     struct config_item item;
0616     struct gpio_sim_line *parent;
0617 
0618     char *name;
0619     int dir;
0620 };
0621 
0622 static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item)
0623 {
0624     return container_of(item, struct gpio_sim_hog, item);
0625 }
0626 
0627 static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog)
0628 {
0629     struct gpio_sim_line *line = hog->parent;
0630 
0631     return gpio_sim_line_get_device(line);
0632 }
0633 
0634 static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev)
0635 {
0636     return !!dev->pdev;
0637 }
0638 
0639 static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
0640 {
0641     char *dup, *trimmed;
0642 
0643     dup = kstrndup(str, count, GFP_KERNEL);
0644     if (!dup)
0645         return NULL;
0646 
0647     trimmed = strstrip(dup);
0648     memmove(dup, trimmed, strlen(trimmed) + 1);
0649 
0650     return dup;
0651 }
0652 
0653 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
0654                             char *page)
0655 {
0656     struct gpio_sim_device *dev = to_gpio_sim_device(item);
0657     struct platform_device *pdev;
0658     int ret;
0659 
0660     mutex_lock(&dev->lock);
0661     pdev = dev->pdev;
0662     if (pdev)
0663         ret = sprintf(page, "%s\n", dev_name(&pdev->dev));
0664     else
0665         ret = sprintf(page, "gpio-sim.%d\n", dev->id);
0666     mutex_unlock(&dev->lock);
0667 
0668     return ret;
0669 }
0670 
0671 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
0672 
0673 static ssize_t
0674 gpio_sim_device_config_live_show(struct config_item *item, char *page)
0675 {
0676     struct gpio_sim_device *dev = to_gpio_sim_device(item);
0677     bool live;
0678 
0679     mutex_lock(&dev->lock);
0680     live = gpio_sim_device_is_live_unlocked(dev);
0681     mutex_unlock(&dev->lock);
0682 
0683     return sprintf(page, "%c\n", live ? '1' : '0');
0684 }
0685 
0686 static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
0687                        unsigned int *line_names_size)
0688 {
0689     unsigned int max_offset = 0;
0690     bool has_line_names = false;
0691     struct gpio_sim_line *line;
0692     char **line_names;
0693 
0694     list_for_each_entry(line, &bank->line_list, siblings) {
0695         if (line->name) {
0696             if (line->offset > max_offset)
0697                 max_offset = line->offset;
0698 
0699             /*
0700              * max_offset can stay at 0 so it's not an indicator
0701              * of whether line names were configured at all.
0702              */
0703             has_line_names = true;
0704         }
0705     }
0706 
0707     if (!has_line_names)
0708         /*
0709          * This is not an error - NULL means, there are no line
0710          * names configured.
0711          */
0712         return NULL;
0713 
0714     *line_names_size = max_offset + 1;
0715 
0716     line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL);
0717     if (!line_names)
0718         return ERR_PTR(-ENOMEM);
0719 
0720     list_for_each_entry(line, &bank->line_list, siblings)
0721         line_names[line->offset] = line->name;
0722 
0723     return line_names;
0724 }
0725 
0726 static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
0727 {
0728     struct gpiod_hog *hog;
0729 
0730     if (!dev->hogs)
0731         return;
0732 
0733     gpiod_remove_hogs(dev->hogs);
0734 
0735     for (hog = dev->hogs; !hog->chip_label; hog++) {
0736         kfree(hog->chip_label);
0737         kfree(hog->line_name);
0738     }
0739 
0740     kfree(dev->hogs);
0741     dev->hogs = NULL;
0742 }
0743 
0744 static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
0745 {
0746     unsigned int num_hogs = 0, idx = 0;
0747     struct gpio_sim_bank *bank;
0748     struct gpio_sim_line *line;
0749     struct gpiod_hog *hog;
0750 
0751     list_for_each_entry(bank, &dev->bank_list, siblings) {
0752         list_for_each_entry(line, &bank->line_list, siblings) {
0753             if (line->hog)
0754                 num_hogs++;
0755         }
0756     }
0757 
0758     if (!num_hogs)
0759         return 0;
0760 
0761     /* Allocate one more for the sentinel. */
0762     dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
0763     if (!dev->hogs)
0764         return -ENOMEM;
0765 
0766     list_for_each_entry(bank, &dev->bank_list, siblings) {
0767         list_for_each_entry(line, &bank->line_list, siblings) {
0768             if (!line->hog)
0769                 continue;
0770 
0771             hog = &dev->hogs[idx++];
0772 
0773             /*
0774              * We need to make this string manually because at this
0775              * point the device doesn't exist yet and so dev_name()
0776              * is not available.
0777              */
0778             if (gpio_sim_bank_has_label(bank))
0779                 hog->chip_label = kstrdup(bank->label,
0780                               GFP_KERNEL);
0781             else
0782                 hog->chip_label = kasprintf(GFP_KERNEL,
0783                             "gpio-sim.%u-%s",
0784                             dev->id,
0785                             fwnode_get_name(
0786                                 bank->swnode));
0787             if (!hog->chip_label) {
0788                 gpio_sim_remove_hogs(dev);
0789                 return -ENOMEM;
0790             }
0791 
0792             /*
0793              * We need to duplicate this because the hog config
0794              * item can be removed at any time (and we can't block
0795              * it) and gpiolib doesn't make a deep copy of the hog
0796              * data.
0797              */
0798             if (line->hog->name) {
0799                 hog->line_name = kstrdup(line->hog->name,
0800                              GFP_KERNEL);
0801                 if (!hog->line_name) {
0802                     gpio_sim_remove_hogs(dev);
0803                     return -ENOMEM;
0804                 }
0805             }
0806 
0807             hog->chip_hwnum = line->offset;
0808             hog->dflags = line->hog->dir;
0809         }
0810     }
0811 
0812     gpiod_add_hogs(dev->hogs);
0813 
0814     return 0;
0815 }
0816 
0817 static struct fwnode_handle *
0818 gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
0819               struct fwnode_handle *parent)
0820 {
0821     struct property_entry properties[GPIO_SIM_PROP_MAX];
0822     unsigned int prop_idx = 0, line_names_size = 0;
0823     struct fwnode_handle *swnode;
0824     char **line_names;
0825 
0826     memset(properties, 0, sizeof(properties));
0827 
0828     properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
0829 
0830     if (gpio_sim_bank_has_label(bank))
0831         properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
0832                                    bank->label);
0833 
0834     line_names = gpio_sim_make_line_names(bank, &line_names_size);
0835     if (IS_ERR(line_names))
0836         return ERR_CAST(line_names);
0837 
0838     if (line_names)
0839         properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
0840                         "gpio-line-names",
0841                         line_names, line_names_size);
0842 
0843     swnode = fwnode_create_software_node(properties, parent);
0844     kfree(line_names);
0845     return swnode;
0846 }
0847 
0848 static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
0849 {
0850     struct fwnode_handle *child;
0851 
0852     fwnode_for_each_child_node(swnode, child)
0853         fwnode_remove_software_node(child);
0854 
0855     fwnode_remove_software_node(swnode);
0856 }
0857 
0858 static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
0859 {
0860     struct gpio_sim_bank *this, *pos;
0861 
0862     list_for_each_entry(this, &dev->bank_list, siblings) {
0863         list_for_each_entry(pos, &dev->bank_list, siblings) {
0864             if (this == pos || (!this->label || !pos->label))
0865                 continue;
0866 
0867             if (strcmp(this->label, pos->label) == 0)
0868                 return true;
0869         }
0870     }
0871 
0872     return false;
0873 }
0874 
0875 static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev)
0876 {
0877     struct platform_device_info pdevinfo;
0878     struct fwnode_handle *swnode;
0879     struct platform_device *pdev;
0880     struct gpio_sim_bank *bank;
0881     int ret;
0882 
0883     if (list_empty(&dev->bank_list))
0884         return -ENODATA;
0885 
0886     /*
0887      * Non-unique GPIO device labels are a corner-case we don't support
0888      * as it would interfere with machine hogging mechanism and has little
0889      * use in real life.
0890      */
0891     if (gpio_sim_bank_labels_non_unique(dev))
0892         return -EINVAL;
0893 
0894     memset(&pdevinfo, 0, sizeof(pdevinfo));
0895 
0896     swnode = fwnode_create_software_node(NULL, NULL);
0897     if (IS_ERR(swnode))
0898         return PTR_ERR(swnode);
0899 
0900     list_for_each_entry(bank, &dev->bank_list, siblings) {
0901         bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
0902         if (IS_ERR(bank->swnode)) {
0903             ret = PTR_ERR(bank->swnode);
0904             gpio_sim_remove_swnode_recursive(swnode);
0905             return ret;
0906         }
0907     }
0908 
0909     ret = gpio_sim_add_hogs(dev);
0910     if (ret) {
0911         gpio_sim_remove_swnode_recursive(swnode);
0912         return ret;
0913     }
0914 
0915     pdevinfo.name = "gpio-sim";
0916     pdevinfo.fwnode = swnode;
0917     pdevinfo.id = dev->id;
0918 
0919     reinit_completion(&dev->probe_completion);
0920     dev->driver_bound = false;
0921     bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
0922 
0923     pdev = platform_device_register_full(&pdevinfo);
0924     if (IS_ERR(pdev)) {
0925         bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
0926         gpio_sim_remove_hogs(dev);
0927         gpio_sim_remove_swnode_recursive(swnode);
0928         return PTR_ERR(pdev);
0929     }
0930 
0931     wait_for_completion(&dev->probe_completion);
0932     bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
0933 
0934     if (!dev->driver_bound) {
0935         /* Probe failed, check kernel log. */
0936         platform_device_unregister(pdev);
0937         gpio_sim_remove_hogs(dev);
0938         gpio_sim_remove_swnode_recursive(swnode);
0939         return -ENXIO;
0940     }
0941 
0942     dev->pdev = pdev;
0943 
0944     return 0;
0945 }
0946 
0947 static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev)
0948 {
0949     struct fwnode_handle *swnode;
0950 
0951     swnode = dev_fwnode(&dev->pdev->dev);
0952     platform_device_unregister(dev->pdev);
0953     gpio_sim_remove_swnode_recursive(swnode);
0954     dev->pdev = NULL;
0955     gpio_sim_remove_hogs(dev);
0956 }
0957 
0958 static ssize_t
0959 gpio_sim_device_config_live_store(struct config_item *item,
0960                   const char *page, size_t count)
0961 {
0962     struct gpio_sim_device *dev = to_gpio_sim_device(item);
0963     bool live;
0964     int ret;
0965 
0966     ret = kstrtobool(page, &live);
0967     if (ret)
0968         return ret;
0969 
0970     mutex_lock(&dev->lock);
0971 
0972     if ((!live && !gpio_sim_device_is_live_unlocked(dev)) ||
0973         (live && gpio_sim_device_is_live_unlocked(dev)))
0974         ret = -EPERM;
0975     else if (live)
0976         ret = gpio_sim_device_activate_unlocked(dev);
0977     else
0978         gpio_sim_device_deactivate_unlocked(dev);
0979 
0980     mutex_unlock(&dev->lock);
0981 
0982     return ret ?: count;
0983 }
0984 
0985 CONFIGFS_ATTR(gpio_sim_device_config_, live);
0986 
0987 static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
0988     &gpio_sim_device_config_attr_dev_name,
0989     &gpio_sim_device_config_attr_live,
0990     NULL
0991 };
0992 
0993 struct gpio_sim_chip_name_ctx {
0994     struct fwnode_handle *swnode;
0995     char *page;
0996 };
0997 
0998 static int gpio_sim_emit_chip_name(struct device *dev, void *data)
0999 {
1000     struct gpio_sim_chip_name_ctx *ctx = data;
1001 
1002     /* This would be the sysfs device exported in /sys/class/gpio. */
1003     if (dev->class)
1004         return 0;
1005 
1006     if (device_match_fwnode(dev, ctx->swnode))
1007         return sprintf(ctx->page, "%s\n", dev_name(dev));
1008 
1009     return 0;
1010 }
1011 
1012 static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
1013                            char *page)
1014 {
1015     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1016     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1017     struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
1018     int ret;
1019 
1020     mutex_lock(&dev->lock);
1021     if (gpio_sim_device_is_live_unlocked(dev))
1022         ret = device_for_each_child(&dev->pdev->dev, &ctx,
1023                         gpio_sim_emit_chip_name);
1024     else
1025         ret = sprintf(page, "none\n");
1026     mutex_unlock(&dev->lock);
1027 
1028     return ret;
1029 }
1030 
1031 CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
1032 
1033 static ssize_t
1034 gpio_sim_bank_config_label_show(struct config_item *item, char *page)
1035 {
1036     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1037     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1038     int ret;
1039 
1040     mutex_lock(&dev->lock);
1041     ret = sprintf(page, "%s\n", bank->label ?: "");
1042     mutex_unlock(&dev->lock);
1043 
1044     return ret;
1045 }
1046 
1047 static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
1048                         const char *page, size_t count)
1049 {
1050     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1051     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1052     char *trimmed;
1053 
1054     mutex_lock(&dev->lock);
1055 
1056     if (gpio_sim_device_is_live_unlocked(dev)) {
1057         mutex_unlock(&dev->lock);
1058         return -EBUSY;
1059     }
1060 
1061     trimmed = gpio_sim_strdup_trimmed(page, count);
1062     if (!trimmed) {
1063         mutex_unlock(&dev->lock);
1064         return -ENOMEM;
1065     }
1066 
1067     kfree(bank->label);
1068     bank->label = trimmed;
1069 
1070     mutex_unlock(&dev->lock);
1071     return count;
1072 }
1073 
1074 CONFIGFS_ATTR(gpio_sim_bank_config_, label);
1075 
1076 static ssize_t
1077 gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
1078 {
1079     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1080     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1081     int ret;
1082 
1083     mutex_lock(&dev->lock);
1084     ret = sprintf(page, "%u\n", bank->num_lines);
1085     mutex_unlock(&dev->lock);
1086 
1087     return ret;
1088 }
1089 
1090 static ssize_t
1091 gpio_sim_bank_config_num_lines_store(struct config_item *item,
1092                      const char *page, size_t count)
1093 {
1094     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1095     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1096     unsigned int num_lines;
1097     int ret;
1098 
1099     ret = kstrtouint(page, 0, &num_lines);
1100     if (ret)
1101         return ret;
1102 
1103     if (num_lines == 0)
1104         return -EINVAL;
1105 
1106     mutex_lock(&dev->lock);
1107 
1108     if (gpio_sim_device_is_live_unlocked(dev)) {
1109         mutex_unlock(&dev->lock);
1110         return -EBUSY;
1111     }
1112 
1113     bank->num_lines = num_lines;
1114 
1115     mutex_unlock(&dev->lock);
1116     return count;
1117 }
1118 
1119 CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
1120 
1121 static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
1122     &gpio_sim_bank_config_attr_chip_name,
1123     &gpio_sim_bank_config_attr_label,
1124     &gpio_sim_bank_config_attr_num_lines,
1125     NULL
1126 };
1127 
1128 static ssize_t
1129 gpio_sim_line_config_name_show(struct config_item *item, char *page)
1130 {
1131     struct gpio_sim_line *line = to_gpio_sim_line(item);
1132     struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1133     int ret;
1134 
1135     mutex_lock(&dev->lock);
1136     ret = sprintf(page, "%s\n", line->name ?: "");
1137     mutex_unlock(&dev->lock);
1138 
1139     return ret;
1140 }
1141 
1142 static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
1143                            const char *page, size_t count)
1144 {
1145     struct gpio_sim_line *line = to_gpio_sim_line(item);
1146     struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1147     char *trimmed;
1148 
1149     mutex_lock(&dev->lock);
1150 
1151     if (gpio_sim_device_is_live_unlocked(dev)) {
1152         mutex_unlock(&dev->lock);
1153         return -EBUSY;
1154     }
1155 
1156     trimmed = gpio_sim_strdup_trimmed(page, count);
1157     if (!trimmed) {
1158         mutex_unlock(&dev->lock);
1159         return -ENOMEM;
1160     }
1161 
1162     kfree(line->name);
1163     line->name = trimmed;
1164 
1165     mutex_unlock(&dev->lock);
1166 
1167     return count;
1168 }
1169 
1170 CONFIGFS_ATTR(gpio_sim_line_config_, name);
1171 
1172 static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
1173     &gpio_sim_line_config_attr_name,
1174     NULL
1175 };
1176 
1177 static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
1178                          char *page)
1179 {
1180     struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1181     struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1182     int ret;
1183 
1184     mutex_lock(&dev->lock);
1185     ret = sprintf(page, "%s\n", hog->name ?: "");
1186     mutex_unlock(&dev->lock);
1187 
1188     return ret;
1189 }
1190 
1191 static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
1192                           const char *page, size_t count)
1193 {
1194     struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1195     struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1196     char *trimmed;
1197 
1198     mutex_lock(&dev->lock);
1199 
1200     if (gpio_sim_device_is_live_unlocked(dev)) {
1201         mutex_unlock(&dev->lock);
1202         return -EBUSY;
1203     }
1204 
1205     trimmed = gpio_sim_strdup_trimmed(page, count);
1206     if (!trimmed) {
1207         mutex_unlock(&dev->lock);
1208         return -ENOMEM;
1209     }
1210 
1211     kfree(hog->name);
1212     hog->name = trimmed;
1213 
1214     mutex_unlock(&dev->lock);
1215 
1216     return count;
1217 }
1218 
1219 CONFIGFS_ATTR(gpio_sim_hog_config_, name);
1220 
1221 static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
1222                           char *page)
1223 {
1224     struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1225     struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1226     char *repr;
1227     int dir;
1228 
1229     mutex_lock(&dev->lock);
1230     dir = hog->dir;
1231     mutex_unlock(&dev->lock);
1232 
1233     switch (dir) {
1234     case GPIOD_IN:
1235         repr = "input";
1236         break;
1237     case GPIOD_OUT_HIGH:
1238         repr = "output-high";
1239         break;
1240     case GPIOD_OUT_LOW:
1241         repr = "output-low";
1242         break;
1243     default:
1244         /* This would be a programmer bug. */
1245         WARN(1, "Unexpected hog direction value: %d", dir);
1246         return -EINVAL;
1247     }
1248 
1249     return sprintf(page, "%s\n", repr);
1250 }
1251 
1252 static ssize_t
1253 gpio_sim_hog_config_direction_store(struct config_item *item,
1254                     const char *page, size_t count)
1255 {
1256     struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1257     struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1258     char *trimmed;
1259     int dir;
1260 
1261     mutex_lock(&dev->lock);
1262 
1263     if (gpio_sim_device_is_live_unlocked(dev)) {
1264         mutex_unlock(&dev->lock);
1265         return -EBUSY;
1266     }
1267 
1268     trimmed = gpio_sim_strdup_trimmed(page, count);
1269     if (!trimmed) {
1270         mutex_unlock(&dev->lock);
1271         return -ENOMEM;
1272     }
1273 
1274     if (strcmp(trimmed, "input") == 0)
1275         dir = GPIOD_IN;
1276     else if (strcmp(trimmed, "output-high") == 0)
1277         dir = GPIOD_OUT_HIGH;
1278     else if (strcmp(trimmed, "output-low") == 0)
1279         dir = GPIOD_OUT_LOW;
1280     else
1281         dir = -EINVAL;
1282 
1283     kfree(trimmed);
1284 
1285     if (dir < 0) {
1286         mutex_unlock(&dev->lock);
1287         return dir;
1288     }
1289 
1290     hog->dir = dir;
1291 
1292     mutex_unlock(&dev->lock);
1293 
1294     return count;
1295 }
1296 
1297 CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
1298 
1299 static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
1300     &gpio_sim_hog_config_attr_name,
1301     &gpio_sim_hog_config_attr_direction,
1302     NULL
1303 };
1304 
1305 static void gpio_sim_hog_config_item_release(struct config_item *item)
1306 {
1307     struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
1308     struct gpio_sim_line *line = hog->parent;
1309     struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
1310 
1311     mutex_lock(&dev->lock);
1312     line->hog = NULL;
1313     mutex_unlock(&dev->lock);
1314 
1315     kfree(hog->name);
1316     kfree(hog);
1317 }
1318 
1319 static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
1320     .release    = gpio_sim_hog_config_item_release,
1321 };
1322 
1323 static const struct config_item_type gpio_sim_hog_config_type = {
1324     .ct_item_ops    = &gpio_sim_hog_config_item_ops,
1325     .ct_attrs   = gpio_sim_hog_config_attrs,
1326     .ct_owner   = THIS_MODULE,
1327 };
1328 
1329 static struct config_item *
1330 gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
1331 {
1332     struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
1333     struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1334     struct gpio_sim_hog *hog;
1335 
1336     if (strcmp(name, "hog") != 0)
1337         return ERR_PTR(-EINVAL);
1338 
1339     mutex_lock(&dev->lock);
1340 
1341     hog = kzalloc(sizeof(*hog), GFP_KERNEL);
1342     if (!hog) {
1343         mutex_unlock(&dev->lock);
1344         return ERR_PTR(-ENOMEM);
1345     }
1346 
1347     config_item_init_type_name(&hog->item, name,
1348                    &gpio_sim_hog_config_type);
1349 
1350     hog->dir = GPIOD_IN;
1351     hog->name = NULL;
1352     hog->parent = line;
1353     line->hog = hog;
1354 
1355     mutex_unlock(&dev->lock);
1356 
1357     return &hog->item;
1358 }
1359 
1360 static void gpio_sim_line_config_group_release(struct config_item *item)
1361 {
1362     struct gpio_sim_line *line = to_gpio_sim_line(item);
1363     struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
1364 
1365     mutex_lock(&dev->lock);
1366     list_del(&line->siblings);
1367     mutex_unlock(&dev->lock);
1368 
1369     kfree(line->name);
1370     kfree(line);
1371 }
1372 
1373 static struct configfs_item_operations gpio_sim_line_config_item_ops = {
1374     .release    = gpio_sim_line_config_group_release,
1375 };
1376 
1377 static struct configfs_group_operations gpio_sim_line_config_group_ops = {
1378     .make_item  = gpio_sim_line_config_make_hog_item,
1379 };
1380 
1381 static const struct config_item_type gpio_sim_line_config_type = {
1382     .ct_item_ops    = &gpio_sim_line_config_item_ops,
1383     .ct_group_ops   = &gpio_sim_line_config_group_ops,
1384     .ct_attrs   = gpio_sim_line_config_attrs,
1385     .ct_owner       = THIS_MODULE,
1386 };
1387 
1388 static struct config_group *
1389 gpio_sim_bank_config_make_line_group(struct config_group *group,
1390                      const char *name)
1391 {
1392     struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
1393     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1394     struct gpio_sim_line *line;
1395     unsigned int offset;
1396     int ret, nchar;
1397 
1398     ret = sscanf(name, "line%u%n", &offset, &nchar);
1399     if (ret != 1 || nchar != strlen(name))
1400         return ERR_PTR(-EINVAL);
1401 
1402     mutex_lock(&dev->lock);
1403 
1404     if (gpio_sim_device_is_live_unlocked(dev)) {
1405         mutex_unlock(&dev->lock);
1406         return ERR_PTR(-EBUSY);
1407     }
1408 
1409     line = kzalloc(sizeof(*line), GFP_KERNEL);
1410     if (!line) {
1411         mutex_unlock(&dev->lock);
1412         return ERR_PTR(-ENOMEM);
1413     }
1414 
1415     config_group_init_type_name(&line->group, name,
1416                     &gpio_sim_line_config_type);
1417 
1418     line->parent = bank;
1419     line->offset = offset;
1420     list_add_tail(&line->siblings, &bank->line_list);
1421 
1422     mutex_unlock(&dev->lock);
1423 
1424     return &line->group;
1425 }
1426 
1427 static void gpio_sim_bank_config_group_release(struct config_item *item)
1428 {
1429     struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
1430     struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
1431 
1432     mutex_lock(&dev->lock);
1433     list_del(&bank->siblings);
1434     mutex_unlock(&dev->lock);
1435 
1436     kfree(bank->label);
1437     kfree(bank);
1438 }
1439 
1440 static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
1441     .release    = gpio_sim_bank_config_group_release,
1442 };
1443 
1444 static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
1445     .make_group = gpio_sim_bank_config_make_line_group,
1446 };
1447 
1448 static const struct config_item_type gpio_sim_bank_config_group_type = {
1449     .ct_item_ops    = &gpio_sim_bank_config_item_ops,
1450     .ct_group_ops   = &gpio_sim_bank_config_group_ops,
1451     .ct_attrs   = gpio_sim_bank_config_attrs,
1452     .ct_owner   = THIS_MODULE,
1453 };
1454 
1455 static struct config_group *
1456 gpio_sim_device_config_make_bank_group(struct config_group *group,
1457                        const char *name)
1458 {
1459     struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
1460     struct gpio_sim_bank *bank;
1461 
1462     mutex_lock(&dev->lock);
1463 
1464     if (gpio_sim_device_is_live_unlocked(dev)) {
1465         mutex_unlock(&dev->lock);
1466         return ERR_PTR(-EBUSY);
1467     }
1468 
1469     bank = kzalloc(sizeof(*bank), GFP_KERNEL);
1470     if (!bank) {
1471         mutex_unlock(&dev->lock);
1472         return ERR_PTR(-ENOMEM);
1473     }
1474 
1475     config_group_init_type_name(&bank->group, name,
1476                     &gpio_sim_bank_config_group_type);
1477     bank->num_lines = 1;
1478     bank->parent = dev;
1479     INIT_LIST_HEAD(&bank->line_list);
1480     list_add_tail(&bank->siblings, &dev->bank_list);
1481 
1482     mutex_unlock(&dev->lock);
1483 
1484     return &bank->group;
1485 }
1486 
1487 static void gpio_sim_device_config_group_release(struct config_item *item)
1488 {
1489     struct gpio_sim_device *dev = to_gpio_sim_device(item);
1490 
1491     mutex_lock(&dev->lock);
1492     if (gpio_sim_device_is_live_unlocked(dev))
1493         gpio_sim_device_deactivate_unlocked(dev);
1494     mutex_unlock(&dev->lock);
1495 
1496     mutex_destroy(&dev->lock);
1497     ida_free(&gpio_sim_ida, dev->id);
1498     kfree(dev);
1499 }
1500 
1501 static struct configfs_item_operations gpio_sim_device_config_item_ops = {
1502     .release    = gpio_sim_device_config_group_release,
1503 };
1504 
1505 static struct configfs_group_operations gpio_sim_device_config_group_ops = {
1506     .make_group = gpio_sim_device_config_make_bank_group,
1507 };
1508 
1509 static const struct config_item_type gpio_sim_device_config_group_type = {
1510     .ct_item_ops    = &gpio_sim_device_config_item_ops,
1511     .ct_group_ops   = &gpio_sim_device_config_group_ops,
1512     .ct_attrs   = gpio_sim_device_config_attrs,
1513     .ct_owner   = THIS_MODULE,
1514 };
1515 
1516 static struct config_group *
1517 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
1518 {
1519     struct gpio_sim_device *dev;
1520     int id;
1521 
1522     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1523     if (!dev)
1524         return ERR_PTR(-ENOMEM);
1525 
1526     id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
1527     if (id < 0) {
1528         kfree(dev);
1529         return ERR_PTR(id);
1530     }
1531 
1532     config_group_init_type_name(&dev->group, name,
1533                     &gpio_sim_device_config_group_type);
1534     dev->id = id;
1535     mutex_init(&dev->lock);
1536     INIT_LIST_HEAD(&dev->bank_list);
1537 
1538     dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
1539     init_completion(&dev->probe_completion);
1540 
1541     return &dev->group;
1542 }
1543 
1544 static struct configfs_group_operations gpio_sim_config_group_ops = {
1545     .make_group = gpio_sim_config_make_device_group,
1546 };
1547 
1548 static const struct config_item_type gpio_sim_config_type = {
1549     .ct_group_ops   = &gpio_sim_config_group_ops,
1550     .ct_owner   = THIS_MODULE,
1551 };
1552 
1553 static struct configfs_subsystem gpio_sim_config_subsys = {
1554     .su_group = {
1555         .cg_item = {
1556             .ci_namebuf = "gpio-sim",
1557             .ci_type    = &gpio_sim_config_type,
1558         },
1559     },
1560 };
1561 
1562 static int __init gpio_sim_init(void)
1563 {
1564     int ret;
1565 
1566     ret = platform_driver_register(&gpio_sim_driver);
1567     if (ret) {
1568         pr_err("Error %d while registering the platform driver\n", ret);
1569         return ret;
1570     }
1571 
1572     config_group_init(&gpio_sim_config_subsys.su_group);
1573     mutex_init(&gpio_sim_config_subsys.su_mutex);
1574     ret = configfs_register_subsystem(&gpio_sim_config_subsys);
1575     if (ret) {
1576         pr_err("Error %d while registering the configfs subsystem %s\n",
1577                ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
1578         mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1579         platform_driver_unregister(&gpio_sim_driver);
1580         return ret;
1581     }
1582 
1583     return 0;
1584 }
1585 module_init(gpio_sim_init);
1586 
1587 static void __exit gpio_sim_exit(void)
1588 {
1589     configfs_unregister_subsystem(&gpio_sim_config_subsys);
1590     mutex_destroy(&gpio_sim_config_subsys.su_mutex);
1591     platform_driver_unregister(&gpio_sim_driver);
1592 }
1593 module_exit(gpio_sim_exit);
1594 
1595 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl");
1596 MODULE_DESCRIPTION("GPIO Simulator Module");
1597 MODULE_LICENSE("GPL");