Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/idr.h>
0003 #include <linux/mutex.h>
0004 #include <linux/device.h>
0005 #include <linux/sysfs.h>
0006 #include <linux/gpio/consumer.h>
0007 #include <linux/gpio/driver.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/kdev_t.h>
0010 #include <linux/slab.h>
0011 #include <linux/ctype.h>
0012 
0013 #include "gpiolib.h"
0014 #include "gpiolib-sysfs.h"
0015 
0016 #define GPIO_IRQF_TRIGGER_NONE      0
0017 #define GPIO_IRQF_TRIGGER_FALLING   BIT(0)
0018 #define GPIO_IRQF_TRIGGER_RISING    BIT(1)
0019 #define GPIO_IRQF_TRIGGER_BOTH      (GPIO_IRQF_TRIGGER_FALLING | \
0020                      GPIO_IRQF_TRIGGER_RISING)
0021 
0022 struct gpiod_data {
0023     struct gpio_desc *desc;
0024 
0025     struct mutex mutex;
0026     struct kernfs_node *value_kn;
0027     int irq;
0028     unsigned char irq_flags;
0029 
0030     bool direction_can_change;
0031 };
0032 
0033 /*
0034  * Lock to serialise gpiod export and unexport, and prevent re-export of
0035  * gpiod whose chip is being unregistered.
0036  */
0037 static DEFINE_MUTEX(sysfs_lock);
0038 
0039 /*
0040  * /sys/class/gpio/gpioN... only for GPIOs that are exported
0041  *   /direction
0042  *      * MAY BE OMITTED if kernel won't allow direction changes
0043  *      * is read/write as "in" or "out"
0044  *      * may also be written as "high" or "low", initializing
0045  *        output value as specified ("out" implies "low")
0046  *   /value
0047  *      * always readable, subject to hardware behavior
0048  *      * may be writable, as zero/nonzero
0049  *   /edge
0050  *      * configures behavior of poll(2) on /value
0051  *      * available only if pin can generate IRQs on input
0052  *      * is read/write as "none", "falling", "rising", or "both"
0053  *   /active_low
0054  *      * configures polarity of /value
0055  *      * is read/write as zero/nonzero
0056  *      * also affects existing and subsequent "falling" and "rising"
0057  *        /edge configuration
0058  */
0059 
0060 static ssize_t direction_show(struct device *dev,
0061         struct device_attribute *attr, char *buf)
0062 {
0063     struct gpiod_data *data = dev_get_drvdata(dev);
0064     struct gpio_desc *desc = data->desc;
0065     int value;
0066 
0067     mutex_lock(&data->mutex);
0068 
0069     gpiod_get_direction(desc);
0070     value = !!test_bit(FLAG_IS_OUT, &desc->flags);
0071 
0072     mutex_unlock(&data->mutex);
0073 
0074     return sysfs_emit(buf, "%s\n", value ? "out" : "in");
0075 }
0076 
0077 static ssize_t direction_store(struct device *dev,
0078         struct device_attribute *attr, const char *buf, size_t size)
0079 {
0080     struct gpiod_data *data = dev_get_drvdata(dev);
0081     struct gpio_desc *desc = data->desc;
0082     ssize_t         status;
0083 
0084     mutex_lock(&data->mutex);
0085 
0086     if (sysfs_streq(buf, "high"))
0087         status = gpiod_direction_output_raw(desc, 1);
0088     else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
0089         status = gpiod_direction_output_raw(desc, 0);
0090     else if (sysfs_streq(buf, "in"))
0091         status = gpiod_direction_input(desc);
0092     else
0093         status = -EINVAL;
0094 
0095     mutex_unlock(&data->mutex);
0096 
0097     return status ? : size;
0098 }
0099 static DEVICE_ATTR_RW(direction);
0100 
0101 static ssize_t value_show(struct device *dev,
0102         struct device_attribute *attr, char *buf)
0103 {
0104     struct gpiod_data *data = dev_get_drvdata(dev);
0105     struct gpio_desc *desc = data->desc;
0106     ssize_t         status;
0107 
0108     mutex_lock(&data->mutex);
0109 
0110     status = gpiod_get_value_cansleep(desc);
0111 
0112     mutex_unlock(&data->mutex);
0113 
0114     if (status < 0)
0115         return status;
0116 
0117     return sysfs_emit(buf, "%zd\n", status);
0118 }
0119 
0120 static ssize_t value_store(struct device *dev,
0121         struct device_attribute *attr, const char *buf, size_t size)
0122 {
0123     struct gpiod_data *data = dev_get_drvdata(dev);
0124     struct gpio_desc *desc = data->desc;
0125     ssize_t status;
0126     long value;
0127 
0128     status = kstrtol(buf, 0, &value);
0129 
0130     mutex_lock(&data->mutex);
0131 
0132     if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
0133         status = -EPERM;
0134     } else if (status == 0) {
0135         gpiod_set_value_cansleep(desc, value);
0136         status = size;
0137     }
0138 
0139     mutex_unlock(&data->mutex);
0140 
0141     return status;
0142 }
0143 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
0144 
0145 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
0146 {
0147     struct gpiod_data *data = priv;
0148 
0149     sysfs_notify_dirent(data->value_kn);
0150 
0151     return IRQ_HANDLED;
0152 }
0153 
0154 /* Caller holds gpiod-data mutex. */
0155 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
0156 {
0157     struct gpiod_data   *data = dev_get_drvdata(dev);
0158     struct gpio_desc    *desc = data->desc;
0159     unsigned long       irq_flags;
0160     int         ret;
0161 
0162     data->irq = gpiod_to_irq(desc);
0163     if (data->irq < 0)
0164         return -EIO;
0165 
0166     data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
0167     if (!data->value_kn)
0168         return -ENODEV;
0169 
0170     irq_flags = IRQF_SHARED;
0171     if (flags & GPIO_IRQF_TRIGGER_FALLING)
0172         irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
0173             IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
0174     if (flags & GPIO_IRQF_TRIGGER_RISING)
0175         irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
0176             IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
0177 
0178     /*
0179      * FIXME: This should be done in the irq_request_resources callback
0180      *        when the irq is requested, but a few drivers currently fail
0181      *        to do so.
0182      *
0183      *        Remove this redundant call (along with the corresponding
0184      *        unlock) when those drivers have been fixed.
0185      */
0186     ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0187     if (ret < 0)
0188         goto err_put_kn;
0189 
0190     ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
0191                 "gpiolib", data);
0192     if (ret < 0)
0193         goto err_unlock;
0194 
0195     data->irq_flags = flags;
0196 
0197     return 0;
0198 
0199 err_unlock:
0200     gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0201 err_put_kn:
0202     sysfs_put(data->value_kn);
0203 
0204     return ret;
0205 }
0206 
0207 /*
0208  * Caller holds gpiod-data mutex (unless called after class-device
0209  * deregistration).
0210  */
0211 static void gpio_sysfs_free_irq(struct device *dev)
0212 {
0213     struct gpiod_data *data = dev_get_drvdata(dev);
0214     struct gpio_desc *desc = data->desc;
0215 
0216     data->irq_flags = 0;
0217     free_irq(data->irq, data);
0218     gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0219     sysfs_put(data->value_kn);
0220 }
0221 
0222 static const char * const trigger_names[] = {
0223     [GPIO_IRQF_TRIGGER_NONE]    = "none",
0224     [GPIO_IRQF_TRIGGER_FALLING] = "falling",
0225     [GPIO_IRQF_TRIGGER_RISING]  = "rising",
0226     [GPIO_IRQF_TRIGGER_BOTH]    = "both",
0227 };
0228 
0229 static ssize_t edge_show(struct device *dev,
0230         struct device_attribute *attr, char *buf)
0231 {
0232     struct gpiod_data *data = dev_get_drvdata(dev);
0233     int flags;
0234 
0235     mutex_lock(&data->mutex);
0236 
0237     flags = data->irq_flags;
0238 
0239     mutex_unlock(&data->mutex);
0240 
0241     if (flags >= ARRAY_SIZE(trigger_names))
0242         return 0;
0243 
0244     return sysfs_emit(buf, "%s\n", trigger_names[flags]);
0245 }
0246 
0247 static ssize_t edge_store(struct device *dev,
0248         struct device_attribute *attr, const char *buf, size_t size)
0249 {
0250     struct gpiod_data *data = dev_get_drvdata(dev);
0251     ssize_t status = size;
0252     int flags;
0253 
0254     flags = sysfs_match_string(trigger_names, buf);
0255     if (flags < 0)
0256         return flags;
0257 
0258     mutex_lock(&data->mutex);
0259 
0260     if (flags == data->irq_flags) {
0261         status = size;
0262         goto out_unlock;
0263     }
0264 
0265     if (data->irq_flags)
0266         gpio_sysfs_free_irq(dev);
0267 
0268     if (flags) {
0269         status = gpio_sysfs_request_irq(dev, flags);
0270         if (!status)
0271             status = size;
0272     }
0273 
0274 out_unlock:
0275     mutex_unlock(&data->mutex);
0276 
0277     return status;
0278 }
0279 static DEVICE_ATTR_RW(edge);
0280 
0281 /* Caller holds gpiod-data mutex. */
0282 static int gpio_sysfs_set_active_low(struct device *dev, int value)
0283 {
0284     struct gpiod_data   *data = dev_get_drvdata(dev);
0285     struct gpio_desc    *desc = data->desc;
0286     int         status = 0;
0287     unsigned int        flags = data->irq_flags;
0288 
0289     if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
0290         return 0;
0291 
0292     assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
0293 
0294     /* reconfigure poll(2) support if enabled on one edge only */
0295     if (flags == GPIO_IRQF_TRIGGER_FALLING ||
0296                     flags == GPIO_IRQF_TRIGGER_RISING) {
0297         gpio_sysfs_free_irq(dev);
0298         status = gpio_sysfs_request_irq(dev, flags);
0299     }
0300 
0301     return status;
0302 }
0303 
0304 static ssize_t active_low_show(struct device *dev,
0305         struct device_attribute *attr, char *buf)
0306 {
0307     struct gpiod_data *data = dev_get_drvdata(dev);
0308     struct gpio_desc *desc = data->desc;
0309     int value;
0310 
0311     mutex_lock(&data->mutex);
0312 
0313     value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
0314 
0315     mutex_unlock(&data->mutex);
0316 
0317     return sysfs_emit(buf, "%d\n", value);
0318 }
0319 
0320 static ssize_t active_low_store(struct device *dev,
0321         struct device_attribute *attr, const char *buf, size_t size)
0322 {
0323     struct gpiod_data   *data = dev_get_drvdata(dev);
0324     ssize_t         status;
0325     long            value;
0326 
0327     status = kstrtol(buf, 0, &value);
0328     if (status)
0329         return status;
0330 
0331     mutex_lock(&data->mutex);
0332 
0333     status = gpio_sysfs_set_active_low(dev, value);
0334 
0335     mutex_unlock(&data->mutex);
0336 
0337     return status ? : size;
0338 }
0339 static DEVICE_ATTR_RW(active_low);
0340 
0341 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
0342                    int n)
0343 {
0344     struct device *dev = kobj_to_dev(kobj);
0345     struct gpiod_data *data = dev_get_drvdata(dev);
0346     struct gpio_desc *desc = data->desc;
0347     umode_t mode = attr->mode;
0348     bool show_direction = data->direction_can_change;
0349 
0350     if (attr == &dev_attr_direction.attr) {
0351         if (!show_direction)
0352             mode = 0;
0353     } else if (attr == &dev_attr_edge.attr) {
0354         if (gpiod_to_irq(desc) < 0)
0355             mode = 0;
0356         if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
0357             mode = 0;
0358     }
0359 
0360     return mode;
0361 }
0362 
0363 static struct attribute *gpio_attrs[] = {
0364     &dev_attr_direction.attr,
0365     &dev_attr_edge.attr,
0366     &dev_attr_value.attr,
0367     &dev_attr_active_low.attr,
0368     NULL,
0369 };
0370 
0371 static const struct attribute_group gpio_group = {
0372     .attrs = gpio_attrs,
0373     .is_visible = gpio_is_visible,
0374 };
0375 
0376 static const struct attribute_group *gpio_groups[] = {
0377     &gpio_group,
0378     NULL
0379 };
0380 
0381 /*
0382  * /sys/class/gpio/gpiochipN/
0383  *   /base ... matching gpio_chip.base (N)
0384  *   /label ... matching gpio_chip.label
0385  *   /ngpio ... matching gpio_chip.ngpio
0386  */
0387 
0388 static ssize_t base_show(struct device *dev,
0389                    struct device_attribute *attr, char *buf)
0390 {
0391     const struct gpio_chip  *chip = dev_get_drvdata(dev);
0392 
0393     return sysfs_emit(buf, "%d\n", chip->base);
0394 }
0395 static DEVICE_ATTR_RO(base);
0396 
0397 static ssize_t label_show(struct device *dev,
0398                    struct device_attribute *attr, char *buf)
0399 {
0400     const struct gpio_chip  *chip = dev_get_drvdata(dev);
0401 
0402     return sysfs_emit(buf, "%s\n", chip->label ?: "");
0403 }
0404 static DEVICE_ATTR_RO(label);
0405 
0406 static ssize_t ngpio_show(struct device *dev,
0407                    struct device_attribute *attr, char *buf)
0408 {
0409     const struct gpio_chip  *chip = dev_get_drvdata(dev);
0410 
0411     return sysfs_emit(buf, "%u\n", chip->ngpio);
0412 }
0413 static DEVICE_ATTR_RO(ngpio);
0414 
0415 static struct attribute *gpiochip_attrs[] = {
0416     &dev_attr_base.attr,
0417     &dev_attr_label.attr,
0418     &dev_attr_ngpio.attr,
0419     NULL,
0420 };
0421 ATTRIBUTE_GROUPS(gpiochip);
0422 
0423 /*
0424  * /sys/class/gpio/export ... write-only
0425  *  integer N ... number of GPIO to export (full access)
0426  * /sys/class/gpio/unexport ... write-only
0427  *  integer N ... number of GPIO to unexport
0428  */
0429 static ssize_t export_store(struct class *class,
0430                 struct class_attribute *attr,
0431                 const char *buf, size_t len)
0432 {
0433     long            gpio;
0434     struct gpio_desc    *desc;
0435     int         status;
0436     struct gpio_chip    *gc;
0437     int         offset;
0438 
0439     status = kstrtol(buf, 0, &gpio);
0440     if (status < 0)
0441         goto done;
0442 
0443     desc = gpio_to_desc(gpio);
0444     /* reject invalid GPIOs */
0445     if (!desc) {
0446         pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
0447         return -EINVAL;
0448     }
0449     gc = desc->gdev->chip;
0450     offset = gpio_chip_hwgpio(desc);
0451     if (!gpiochip_line_is_valid(gc, offset)) {
0452         pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
0453         return -EINVAL;
0454     }
0455 
0456     /* No extra locking here; FLAG_SYSFS just signifies that the
0457      * request and export were done by on behalf of userspace, so
0458      * they may be undone on its behalf too.
0459      */
0460 
0461     status = gpiod_request_user(desc, "sysfs");
0462     if (status)
0463         goto done;
0464 
0465     status = gpiod_set_transitory(desc, false);
0466     if (!status) {
0467         status = gpiod_export(desc, true);
0468         if (status < 0)
0469             gpiod_free(desc);
0470         else
0471             set_bit(FLAG_SYSFS, &desc->flags);
0472     }
0473 
0474 done:
0475     if (status)
0476         pr_debug("%s: status %d\n", __func__, status);
0477     return status ? : len;
0478 }
0479 static CLASS_ATTR_WO(export);
0480 
0481 static ssize_t unexport_store(struct class *class,
0482                 struct class_attribute *attr,
0483                 const char *buf, size_t len)
0484 {
0485     long            gpio;
0486     struct gpio_desc    *desc;
0487     int         status;
0488 
0489     status = kstrtol(buf, 0, &gpio);
0490     if (status < 0)
0491         goto done;
0492 
0493     desc = gpio_to_desc(gpio);
0494     /* reject bogus commands (gpio_unexport ignores them) */
0495     if (!desc) {
0496         pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
0497         return -EINVAL;
0498     }
0499 
0500     status = -EINVAL;
0501 
0502     /* No extra locking here; FLAG_SYSFS just signifies that the
0503      * request and export were done by on behalf of userspace, so
0504      * they may be undone on its behalf too.
0505      */
0506     if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
0507         status = 0;
0508         gpiod_free(desc);
0509     }
0510 done:
0511     if (status)
0512         pr_debug("%s: status %d\n", __func__, status);
0513     return status ? : len;
0514 }
0515 static CLASS_ATTR_WO(unexport);
0516 
0517 static struct attribute *gpio_class_attrs[] = {
0518     &class_attr_export.attr,
0519     &class_attr_unexport.attr,
0520     NULL,
0521 };
0522 ATTRIBUTE_GROUPS(gpio_class);
0523 
0524 static struct class gpio_class = {
0525     .name =     "gpio",
0526     .owner =    THIS_MODULE,
0527 
0528     .class_groups = gpio_class_groups,
0529 };
0530 
0531 
0532 /**
0533  * gpiod_export - export a GPIO through sysfs
0534  * @desc: GPIO to make available, already requested
0535  * @direction_may_change: true if userspace may change GPIO direction
0536  * Context: arch_initcall or later
0537  *
0538  * When drivers want to make a GPIO accessible to userspace after they
0539  * have requested it -- perhaps while debugging, or as part of their
0540  * public interface -- they may use this routine.  If the GPIO can
0541  * change direction (some can't) and the caller allows it, userspace
0542  * will see "direction" sysfs attribute which may be used to change
0543  * the gpio's direction.  A "value" attribute will always be provided.
0544  *
0545  * Returns zero on success, else an error.
0546  */
0547 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
0548 {
0549     struct gpio_chip    *chip;
0550     struct gpio_device  *gdev;
0551     struct gpiod_data   *data;
0552     unsigned long       flags;
0553     int         status;
0554     const char      *ioname = NULL;
0555     struct device       *dev;
0556     int         offset;
0557 
0558     /* can't export until sysfs is available ... */
0559     if (!gpio_class.p) {
0560         pr_debug("%s: called too early!\n", __func__);
0561         return -ENOENT;
0562     }
0563 
0564     if (!desc) {
0565         pr_debug("%s: invalid gpio descriptor\n", __func__);
0566         return -EINVAL;
0567     }
0568 
0569     gdev = desc->gdev;
0570     chip = gdev->chip;
0571 
0572     mutex_lock(&sysfs_lock);
0573 
0574     /* check if chip is being removed */
0575     if (!chip || !gdev->mockdev) {
0576         status = -ENODEV;
0577         goto err_unlock;
0578     }
0579 
0580     spin_lock_irqsave(&gpio_lock, flags);
0581     if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
0582          test_bit(FLAG_EXPORT, &desc->flags)) {
0583         spin_unlock_irqrestore(&gpio_lock, flags);
0584         gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
0585                 __func__,
0586                 test_bit(FLAG_REQUESTED, &desc->flags),
0587                 test_bit(FLAG_EXPORT, &desc->flags));
0588         status = -EPERM;
0589         goto err_unlock;
0590     }
0591     spin_unlock_irqrestore(&gpio_lock, flags);
0592 
0593     data = kzalloc(sizeof(*data), GFP_KERNEL);
0594     if (!data) {
0595         status = -ENOMEM;
0596         goto err_unlock;
0597     }
0598 
0599     data->desc = desc;
0600     mutex_init(&data->mutex);
0601     if (chip->direction_input && chip->direction_output)
0602         data->direction_can_change = direction_may_change;
0603     else
0604         data->direction_can_change = false;
0605 
0606     offset = gpio_chip_hwgpio(desc);
0607     if (chip->names && chip->names[offset])
0608         ioname = chip->names[offset];
0609 
0610     dev = device_create_with_groups(&gpio_class, &gdev->dev,
0611                     MKDEV(0, 0), data, gpio_groups,
0612                     ioname ? ioname : "gpio%u",
0613                     desc_to_gpio(desc));
0614     if (IS_ERR(dev)) {
0615         status = PTR_ERR(dev);
0616         goto err_free_data;
0617     }
0618 
0619     set_bit(FLAG_EXPORT, &desc->flags);
0620     mutex_unlock(&sysfs_lock);
0621     return 0;
0622 
0623 err_free_data:
0624     kfree(data);
0625 err_unlock:
0626     mutex_unlock(&sysfs_lock);
0627     gpiod_dbg(desc, "%s: status %d\n", __func__, status);
0628     return status;
0629 }
0630 EXPORT_SYMBOL_GPL(gpiod_export);
0631 
0632 static int match_export(struct device *dev, const void *desc)
0633 {
0634     struct gpiod_data *data = dev_get_drvdata(dev);
0635 
0636     return data->desc == desc;
0637 }
0638 
0639 /**
0640  * gpiod_export_link - create a sysfs link to an exported GPIO node
0641  * @dev: device under which to create symlink
0642  * @name: name of the symlink
0643  * @desc: GPIO to create symlink to, already exported
0644  *
0645  * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
0646  * node. Caller is responsible for unlinking.
0647  *
0648  * Returns zero on success, else an error.
0649  */
0650 int gpiod_export_link(struct device *dev, const char *name,
0651               struct gpio_desc *desc)
0652 {
0653     struct device *cdev;
0654     int ret;
0655 
0656     if (!desc) {
0657         pr_warn("%s: invalid GPIO\n", __func__);
0658         return -EINVAL;
0659     }
0660 
0661     cdev = class_find_device(&gpio_class, NULL, desc, match_export);
0662     if (!cdev)
0663         return -ENODEV;
0664 
0665     ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
0666     put_device(cdev);
0667 
0668     return ret;
0669 }
0670 EXPORT_SYMBOL_GPL(gpiod_export_link);
0671 
0672 /**
0673  * gpiod_unexport - reverse effect of gpiod_export()
0674  * @desc: GPIO to make unavailable
0675  *
0676  * This is implicit on gpiod_free().
0677  */
0678 void gpiod_unexport(struct gpio_desc *desc)
0679 {
0680     struct gpiod_data *data;
0681     struct device *dev;
0682 
0683     if (!desc) {
0684         pr_warn("%s: invalid GPIO\n", __func__);
0685         return;
0686     }
0687 
0688     mutex_lock(&sysfs_lock);
0689 
0690     if (!test_bit(FLAG_EXPORT, &desc->flags))
0691         goto err_unlock;
0692 
0693     dev = class_find_device(&gpio_class, NULL, desc, match_export);
0694     if (!dev)
0695         goto err_unlock;
0696 
0697     data = dev_get_drvdata(dev);
0698 
0699     clear_bit(FLAG_EXPORT, &desc->flags);
0700 
0701     device_unregister(dev);
0702 
0703     /*
0704      * Release irq after deregistration to prevent race with edge_store.
0705      */
0706     if (data->irq_flags)
0707         gpio_sysfs_free_irq(dev);
0708 
0709     mutex_unlock(&sysfs_lock);
0710 
0711     put_device(dev);
0712     kfree(data);
0713 
0714     return;
0715 
0716 err_unlock:
0717     mutex_unlock(&sysfs_lock);
0718 }
0719 EXPORT_SYMBOL_GPL(gpiod_unexport);
0720 
0721 int gpiochip_sysfs_register(struct gpio_device *gdev)
0722 {
0723     struct device   *dev;
0724     struct device   *parent;
0725     struct gpio_chip *chip = gdev->chip;
0726 
0727     /*
0728      * Many systems add gpio chips for SOC support very early,
0729      * before driver model support is available.  In those cases we
0730      * register later, in gpiolib_sysfs_init() ... here we just
0731      * verify that _some_ field of gpio_class got initialized.
0732      */
0733     if (!gpio_class.p)
0734         return 0;
0735 
0736     /*
0737      * For sysfs backward compatibility we need to preserve this
0738      * preferred parenting to the gpio_chip parent field, if set.
0739      */
0740     if (chip->parent)
0741         parent = chip->parent;
0742     else
0743         parent = &gdev->dev;
0744 
0745     /* use chip->base for the ID; it's already known to be unique */
0746     dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip,
0747                     gpiochip_groups, GPIOCHIP_NAME "%d",
0748                     chip->base);
0749     if (IS_ERR(dev))
0750         return PTR_ERR(dev);
0751 
0752     mutex_lock(&sysfs_lock);
0753     gdev->mockdev = dev;
0754     mutex_unlock(&sysfs_lock);
0755 
0756     return 0;
0757 }
0758 
0759 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
0760 {
0761     struct gpio_desc *desc;
0762     struct gpio_chip *chip = gdev->chip;
0763 
0764     if (!gdev->mockdev)
0765         return;
0766 
0767     device_unregister(gdev->mockdev);
0768 
0769     /* prevent further gpiod exports */
0770     mutex_lock(&sysfs_lock);
0771     gdev->mockdev = NULL;
0772     mutex_unlock(&sysfs_lock);
0773 
0774     /* unregister gpiod class devices owned by sysfs */
0775     for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS)
0776         gpiod_free(desc);
0777 }
0778 
0779 static int __init gpiolib_sysfs_init(void)
0780 {
0781     int     status;
0782     unsigned long   flags;
0783     struct gpio_device *gdev;
0784 
0785     status = class_register(&gpio_class);
0786     if (status < 0)
0787         return status;
0788 
0789     /* Scan and register the gpio_chips which registered very
0790      * early (e.g. before the class_register above was called).
0791      *
0792      * We run before arch_initcall() so chip->dev nodes can have
0793      * registered, and so arch_initcall() can always gpio_export().
0794      */
0795     spin_lock_irqsave(&gpio_lock, flags);
0796     list_for_each_entry(gdev, &gpio_devices, list) {
0797         if (gdev->mockdev)
0798             continue;
0799 
0800         /*
0801          * TODO we yield gpio_lock here because
0802          * gpiochip_sysfs_register() acquires a mutex. This is unsafe
0803          * and needs to be fixed.
0804          *
0805          * Also it would be nice to use gpiochip_find() here so we
0806          * can keep gpio_chips local to gpiolib.c, but the yield of
0807          * gpio_lock prevents us from doing this.
0808          */
0809         spin_unlock_irqrestore(&gpio_lock, flags);
0810         status = gpiochip_sysfs_register(gdev);
0811         spin_lock_irqsave(&gpio_lock, flags);
0812     }
0813     spin_unlock_irqrestore(&gpio_lock, flags);
0814 
0815     return status;
0816 }
0817 postcore_initcall(gpiolib_sysfs_init);