Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* sysfs entries for device PM */
0003 #include <linux/device.h>
0004 #include <linux/kobject.h>
0005 #include <linux/string.h>
0006 #include <linux/export.h>
0007 #include <linux/pm_qos.h>
0008 #include <linux/pm_runtime.h>
0009 #include <linux/pm_wakeup.h>
0010 #include <linux/atomic.h>
0011 #include <linux/jiffies.h>
0012 #include "power.h"
0013 
0014 /*
0015  *  control - Report/change current runtime PM setting of the device
0016  *
0017  *  Runtime power management of a device can be blocked with the help of
0018  *  this attribute.  All devices have one of the following two values for
0019  *  the power/control file:
0020  *
0021  *   + "auto\n" to allow the device to be power managed at run time;
0022  *   + "on\n" to prevent the device from being power managed at run time;
0023  *
0024  *  The default for all devices is "auto", which means that devices may be
0025  *  subject to automatic power management, depending on their drivers.
0026  *  Changing this attribute to "on" prevents the driver from power managing
0027  *  the device at run time.  Doing that while the device is suspended causes
0028  *  it to be woken up.
0029  *
0030  *  wakeup - Report/change current wakeup option for device
0031  *
0032  *  Some devices support "wakeup" events, which are hardware signals
0033  *  used to activate devices from suspended or low power states.  Such
0034  *  devices have one of three values for the sysfs power/wakeup file:
0035  *
0036  *   + "enabled\n" to issue the events;
0037  *   + "disabled\n" not to do so; or
0038  *   + "\n" for temporary or permanent inability to issue wakeup.
0039  *
0040  *  (For example, unconfigured USB devices can't issue wakeups.)
0041  *
0042  *  Familiar examples of devices that can issue wakeup events include
0043  *  keyboards and mice (both PS2 and USB styles), power buttons, modems,
0044  *  "Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
0045  *  will wake the entire system from a suspend state; others may just
0046  *  wake up the device (if the system as a whole is already active).
0047  *  Some wakeup events use normal IRQ lines; other use special out
0048  *  of band signaling.
0049  *
0050  *  It is the responsibility of device drivers to enable (or disable)
0051  *  wakeup signaling as part of changing device power states, respecting
0052  *  the policy choices provided through the driver model.
0053  *
0054  *  Devices may not be able to generate wakeup events from all power
0055  *  states.  Also, the events may be ignored in some configurations;
0056  *  for example, they might need help from other devices that aren't
0057  *  active, or which may have wakeup disabled.  Some drivers rely on
0058  *  wakeup events internally (unless they are disabled), keeping
0059  *  their hardware in low power modes whenever they're unused.  This
0060  *  saves runtime power, without requiring system-wide sleep states.
0061  *
0062  *  async - Report/change current async suspend setting for the device
0063  *
0064  *  Asynchronous suspend and resume of the device during system-wide power
0065  *  state transitions can be enabled by writing "enabled" to this file.
0066  *  Analogously, if "disabled" is written to this file, the device will be
0067  *  suspended and resumed synchronously.
0068  *
0069  *  All devices have one of the following two values for power/async:
0070  *
0071  *   + "enabled\n" to permit the asynchronous suspend/resume of the device;
0072  *   + "disabled\n" to forbid it;
0073  *
0074  *  NOTE: It generally is unsafe to permit the asynchronous suspend/resume
0075  *  of a device unless it is certain that all of the PM dependencies of the
0076  *  device are known to the PM core.  However, for some devices this
0077  *  attribute is set to "enabled" by bus type code or device drivers and in
0078  *  that cases it should be safe to leave the default value.
0079  *
0080  *  autosuspend_delay_ms - Report/change a device's autosuspend_delay value
0081  *
0082  *  Some drivers don't want to carry out a runtime suspend as soon as a
0083  *  device becomes idle; they want it always to remain idle for some period
0084  *  of time before suspending it.  This period is the autosuspend_delay
0085  *  value (expressed in milliseconds) and it can be controlled by the user.
0086  *  If the value is negative then the device will never be runtime
0087  *  suspended.
0088  *
0089  *  NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
0090  *  value are used only if the driver calls pm_runtime_use_autosuspend().
0091  *
0092  *  wakeup_count - Report the number of wakeup events related to the device
0093  */
0094 
0095 const char power_group_name[] = "power";
0096 EXPORT_SYMBOL_GPL(power_group_name);
0097 
0098 static const char ctrl_auto[] = "auto";
0099 static const char ctrl_on[] = "on";
0100 
0101 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
0102                 char *buf)
0103 {
0104     return sysfs_emit(buf, "%s\n",
0105               dev->power.runtime_auto ? ctrl_auto : ctrl_on);
0106 }
0107 
0108 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
0109                  const char * buf, size_t n)
0110 {
0111     device_lock(dev);
0112     if (sysfs_streq(buf, ctrl_auto))
0113         pm_runtime_allow(dev);
0114     else if (sysfs_streq(buf, ctrl_on))
0115         pm_runtime_forbid(dev);
0116     else
0117         n = -EINVAL;
0118     device_unlock(dev);
0119     return n;
0120 }
0121 
0122 static DEVICE_ATTR_RW(control);
0123 
0124 static ssize_t runtime_active_time_show(struct device *dev,
0125                     struct device_attribute *attr,
0126                     char *buf)
0127 {
0128     u64 tmp = pm_runtime_active_time(dev);
0129 
0130     do_div(tmp, NSEC_PER_MSEC);
0131 
0132     return sysfs_emit(buf, "%llu\n", tmp);
0133 }
0134 
0135 static DEVICE_ATTR_RO(runtime_active_time);
0136 
0137 static ssize_t runtime_suspended_time_show(struct device *dev,
0138                        struct device_attribute *attr,
0139                        char *buf)
0140 {
0141     u64 tmp = pm_runtime_suspended_time(dev);
0142 
0143     do_div(tmp, NSEC_PER_MSEC);
0144 
0145     return sysfs_emit(buf, "%llu\n", tmp);
0146 }
0147 
0148 static DEVICE_ATTR_RO(runtime_suspended_time);
0149 
0150 static ssize_t runtime_status_show(struct device *dev,
0151                    struct device_attribute *attr, char *buf)
0152 {
0153     const char *output;
0154 
0155     if (dev->power.runtime_error) {
0156         output = "error";
0157     } else if (dev->power.disable_depth) {
0158         output = "unsupported";
0159     } else {
0160         switch (dev->power.runtime_status) {
0161         case RPM_SUSPENDED:
0162             output = "suspended";
0163             break;
0164         case RPM_SUSPENDING:
0165             output = "suspending";
0166             break;
0167         case RPM_RESUMING:
0168             output = "resuming";
0169             break;
0170         case RPM_ACTIVE:
0171             output = "active";
0172             break;
0173         default:
0174             return -EIO;
0175         }
0176     }
0177     return sysfs_emit(buf, "%s\n", output);
0178 }
0179 
0180 static DEVICE_ATTR_RO(runtime_status);
0181 
0182 static ssize_t autosuspend_delay_ms_show(struct device *dev,
0183                      struct device_attribute *attr,
0184                      char *buf)
0185 {
0186     if (!dev->power.use_autosuspend)
0187         return -EIO;
0188 
0189     return sysfs_emit(buf, "%d\n", dev->power.autosuspend_delay);
0190 }
0191 
0192 static ssize_t autosuspend_delay_ms_store(struct device *dev,
0193         struct device_attribute *attr, const char *buf, size_t n)
0194 {
0195     long delay;
0196 
0197     if (!dev->power.use_autosuspend)
0198         return -EIO;
0199 
0200     if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
0201         return -EINVAL;
0202 
0203     device_lock(dev);
0204     pm_runtime_set_autosuspend_delay(dev, delay);
0205     device_unlock(dev);
0206     return n;
0207 }
0208 
0209 static DEVICE_ATTR_RW(autosuspend_delay_ms);
0210 
0211 static ssize_t pm_qos_resume_latency_us_show(struct device *dev,
0212                          struct device_attribute *attr,
0213                          char *buf)
0214 {
0215     s32 value = dev_pm_qos_requested_resume_latency(dev);
0216 
0217     if (value == 0)
0218         return sysfs_emit(buf, "n/a\n");
0219     if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
0220         value = 0;
0221 
0222     return sysfs_emit(buf, "%d\n", value);
0223 }
0224 
0225 static ssize_t pm_qos_resume_latency_us_store(struct device *dev,
0226                           struct device_attribute *attr,
0227                           const char *buf, size_t n)
0228 {
0229     s32 value;
0230     int ret;
0231 
0232     if (!kstrtos32(buf, 0, &value)) {
0233         /*
0234          * Prevent users from writing negative or "no constraint" values
0235          * directly.
0236          */
0237         if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
0238             return -EINVAL;
0239 
0240         if (value == 0)
0241             value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
0242     } else if (sysfs_streq(buf, "n/a")) {
0243         value = 0;
0244     } else {
0245         return -EINVAL;
0246     }
0247 
0248     ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
0249                     value);
0250     return ret < 0 ? ret : n;
0251 }
0252 
0253 static DEVICE_ATTR_RW(pm_qos_resume_latency_us);
0254 
0255 static ssize_t pm_qos_latency_tolerance_us_show(struct device *dev,
0256                         struct device_attribute *attr,
0257                         char *buf)
0258 {
0259     s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
0260 
0261     if (value < 0)
0262         return sysfs_emit(buf, "%s\n", "auto");
0263     if (value == PM_QOS_LATENCY_ANY)
0264         return sysfs_emit(buf, "%s\n", "any");
0265 
0266     return sysfs_emit(buf, "%d\n", value);
0267 }
0268 
0269 static ssize_t pm_qos_latency_tolerance_us_store(struct device *dev,
0270                          struct device_attribute *attr,
0271                          const char *buf, size_t n)
0272 {
0273     s32 value;
0274     int ret;
0275 
0276     if (kstrtos32(buf, 0, &value) == 0) {
0277         /* Users can't write negative values directly */
0278         if (value < 0)
0279             return -EINVAL;
0280     } else {
0281         if (sysfs_streq(buf, "auto"))
0282             value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
0283         else if (sysfs_streq(buf, "any"))
0284             value = PM_QOS_LATENCY_ANY;
0285         else
0286             return -EINVAL;
0287     }
0288     ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
0289     return ret < 0 ? ret : n;
0290 }
0291 
0292 static DEVICE_ATTR_RW(pm_qos_latency_tolerance_us);
0293 
0294 static ssize_t pm_qos_no_power_off_show(struct device *dev,
0295                     struct device_attribute *attr,
0296                     char *buf)
0297 {
0298     return sysfs_emit(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
0299                       & PM_QOS_FLAG_NO_POWER_OFF));
0300 }
0301 
0302 static ssize_t pm_qos_no_power_off_store(struct device *dev,
0303                      struct device_attribute *attr,
0304                      const char *buf, size_t n)
0305 {
0306     int ret;
0307 
0308     if (kstrtoint(buf, 0, &ret))
0309         return -EINVAL;
0310 
0311     if (ret != 0 && ret != 1)
0312         return -EINVAL;
0313 
0314     ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
0315     return ret < 0 ? ret : n;
0316 }
0317 
0318 static DEVICE_ATTR_RW(pm_qos_no_power_off);
0319 
0320 #ifdef CONFIG_PM_SLEEP
0321 static const char _enabled[] = "enabled";
0322 static const char _disabled[] = "disabled";
0323 
0324 static ssize_t wakeup_show(struct device *dev, struct device_attribute *attr,
0325                char *buf)
0326 {
0327     return sysfs_emit(buf, "%s\n", device_can_wakeup(dev)
0328               ? (device_may_wakeup(dev) ? _enabled : _disabled)
0329               : "");
0330 }
0331 
0332 static ssize_t wakeup_store(struct device *dev, struct device_attribute *attr,
0333                 const char *buf, size_t n)
0334 {
0335     if (!device_can_wakeup(dev))
0336         return -EINVAL;
0337 
0338     if (sysfs_streq(buf, _enabled))
0339         device_set_wakeup_enable(dev, 1);
0340     else if (sysfs_streq(buf, _disabled))
0341         device_set_wakeup_enable(dev, 0);
0342     else
0343         return -EINVAL;
0344     return n;
0345 }
0346 
0347 static DEVICE_ATTR_RW(wakeup);
0348 
0349 static ssize_t wakeup_count_show(struct device *dev,
0350                  struct device_attribute *attr, char *buf)
0351 {
0352     unsigned long count;
0353     bool enabled = false;
0354 
0355     spin_lock_irq(&dev->power.lock);
0356     if (dev->power.wakeup) {
0357         count = dev->power.wakeup->wakeup_count;
0358         enabled = true;
0359     }
0360     spin_unlock_irq(&dev->power.lock);
0361 
0362     if (!enabled)
0363         return sysfs_emit(buf, "\n");
0364     return sysfs_emit(buf, "%lu\n", count);
0365 }
0366 
0367 static DEVICE_ATTR_RO(wakeup_count);
0368 
0369 static ssize_t wakeup_active_count_show(struct device *dev,
0370                     struct device_attribute *attr,
0371                     char *buf)
0372 {
0373     unsigned long count;
0374     bool enabled = false;
0375 
0376     spin_lock_irq(&dev->power.lock);
0377     if (dev->power.wakeup) {
0378         count = dev->power.wakeup->active_count;
0379         enabled = true;
0380     }
0381     spin_unlock_irq(&dev->power.lock);
0382 
0383     if (!enabled)
0384         return sysfs_emit(buf, "\n");
0385     return sysfs_emit(buf, "%lu\n", count);
0386 }
0387 
0388 static DEVICE_ATTR_RO(wakeup_active_count);
0389 
0390 static ssize_t wakeup_abort_count_show(struct device *dev,
0391                        struct device_attribute *attr,
0392                        char *buf)
0393 {
0394     unsigned long count;
0395     bool enabled = false;
0396 
0397     spin_lock_irq(&dev->power.lock);
0398     if (dev->power.wakeup) {
0399         count = dev->power.wakeup->wakeup_count;
0400         enabled = true;
0401     }
0402     spin_unlock_irq(&dev->power.lock);
0403 
0404     if (!enabled)
0405         return sysfs_emit(buf, "\n");
0406     return sysfs_emit(buf, "%lu\n", count);
0407 }
0408 
0409 static DEVICE_ATTR_RO(wakeup_abort_count);
0410 
0411 static ssize_t wakeup_expire_count_show(struct device *dev,
0412                     struct device_attribute *attr,
0413                     char *buf)
0414 {
0415     unsigned long count;
0416     bool enabled = false;
0417 
0418     spin_lock_irq(&dev->power.lock);
0419     if (dev->power.wakeup) {
0420         count = dev->power.wakeup->expire_count;
0421         enabled = true;
0422     }
0423     spin_unlock_irq(&dev->power.lock);
0424 
0425     if (!enabled)
0426         return sysfs_emit(buf, "\n");
0427     return sysfs_emit(buf, "%lu\n", count);
0428 }
0429 
0430 static DEVICE_ATTR_RO(wakeup_expire_count);
0431 
0432 static ssize_t wakeup_active_show(struct device *dev,
0433                   struct device_attribute *attr, char *buf)
0434 {
0435     unsigned int active;
0436     bool enabled = false;
0437 
0438     spin_lock_irq(&dev->power.lock);
0439     if (dev->power.wakeup) {
0440         active = dev->power.wakeup->active;
0441         enabled = true;
0442     }
0443     spin_unlock_irq(&dev->power.lock);
0444 
0445     if (!enabled)
0446         return sysfs_emit(buf, "\n");
0447     return sysfs_emit(buf, "%u\n", active);
0448 }
0449 
0450 static DEVICE_ATTR_RO(wakeup_active);
0451 
0452 static ssize_t wakeup_total_time_ms_show(struct device *dev,
0453                      struct device_attribute *attr,
0454                      char *buf)
0455 {
0456     s64 msec;
0457     bool enabled = false;
0458 
0459     spin_lock_irq(&dev->power.lock);
0460     if (dev->power.wakeup) {
0461         msec = ktime_to_ms(dev->power.wakeup->total_time);
0462         enabled = true;
0463     }
0464     spin_unlock_irq(&dev->power.lock);
0465 
0466     if (!enabled)
0467         return sysfs_emit(buf, "\n");
0468     return sysfs_emit(buf, "%lld\n", msec);
0469 }
0470 
0471 static DEVICE_ATTR_RO(wakeup_total_time_ms);
0472 
0473 static ssize_t wakeup_max_time_ms_show(struct device *dev,
0474                        struct device_attribute *attr, char *buf)
0475 {
0476     s64 msec;
0477     bool enabled = false;
0478 
0479     spin_lock_irq(&dev->power.lock);
0480     if (dev->power.wakeup) {
0481         msec = ktime_to_ms(dev->power.wakeup->max_time);
0482         enabled = true;
0483     }
0484     spin_unlock_irq(&dev->power.lock);
0485 
0486     if (!enabled)
0487         return sysfs_emit(buf, "\n");
0488     return sysfs_emit(buf, "%lld\n", msec);
0489 }
0490 
0491 static DEVICE_ATTR_RO(wakeup_max_time_ms);
0492 
0493 static ssize_t wakeup_last_time_ms_show(struct device *dev,
0494                     struct device_attribute *attr,
0495                     char *buf)
0496 {
0497     s64 msec;
0498     bool enabled = false;
0499 
0500     spin_lock_irq(&dev->power.lock);
0501     if (dev->power.wakeup) {
0502         msec = ktime_to_ms(dev->power.wakeup->last_time);
0503         enabled = true;
0504     }
0505     spin_unlock_irq(&dev->power.lock);
0506 
0507     if (!enabled)
0508         return sysfs_emit(buf, "\n");
0509     return sysfs_emit(buf, "%lld\n", msec);
0510 }
0511 
0512 static inline int dpm_sysfs_wakeup_change_owner(struct device *dev, kuid_t kuid,
0513                         kgid_t kgid)
0514 {
0515     if (dev->power.wakeup && dev->power.wakeup->dev)
0516         return device_change_owner(dev->power.wakeup->dev, kuid, kgid);
0517     return 0;
0518 }
0519 
0520 static DEVICE_ATTR_RO(wakeup_last_time_ms);
0521 
0522 #ifdef CONFIG_PM_AUTOSLEEP
0523 static ssize_t wakeup_prevent_sleep_time_ms_show(struct device *dev,
0524                          struct device_attribute *attr,
0525                          char *buf)
0526 {
0527     s64 msec;
0528     bool enabled = false;
0529 
0530     spin_lock_irq(&dev->power.lock);
0531     if (dev->power.wakeup) {
0532         msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
0533         enabled = true;
0534     }
0535     spin_unlock_irq(&dev->power.lock);
0536 
0537     if (!enabled)
0538         return sysfs_emit(buf, "\n");
0539     return sysfs_emit(buf, "%lld\n", msec);
0540 }
0541 
0542 static DEVICE_ATTR_RO(wakeup_prevent_sleep_time_ms);
0543 #endif /* CONFIG_PM_AUTOSLEEP */
0544 #else /* CONFIG_PM_SLEEP */
0545 static inline int dpm_sysfs_wakeup_change_owner(struct device *dev, kuid_t kuid,
0546                         kgid_t kgid)
0547 {
0548     return 0;
0549 }
0550 #endif
0551 
0552 #ifdef CONFIG_PM_ADVANCED_DEBUG
0553 static ssize_t runtime_usage_show(struct device *dev,
0554                   struct device_attribute *attr, char *buf)
0555 {
0556     return sysfs_emit(buf, "%d\n", atomic_read(&dev->power.usage_count));
0557 }
0558 static DEVICE_ATTR_RO(runtime_usage);
0559 
0560 static ssize_t runtime_active_kids_show(struct device *dev,
0561                     struct device_attribute *attr,
0562                     char *buf)
0563 {
0564     return sysfs_emit(buf, "%d\n", dev->power.ignore_children ?
0565               0 : atomic_read(&dev->power.child_count));
0566 }
0567 static DEVICE_ATTR_RO(runtime_active_kids);
0568 
0569 static ssize_t runtime_enabled_show(struct device *dev,
0570                     struct device_attribute *attr, char *buf)
0571 {
0572     const char *output;
0573 
0574     if (dev->power.disable_depth && !dev->power.runtime_auto)
0575         output = "disabled & forbidden";
0576     else if (dev->power.disable_depth)
0577         output = "disabled";
0578     else if (!dev->power.runtime_auto)
0579         output = "forbidden";
0580     else
0581         output = "enabled";
0582 
0583     return sysfs_emit(buf, "%s\n", output);
0584 }
0585 static DEVICE_ATTR_RO(runtime_enabled);
0586 
0587 #ifdef CONFIG_PM_SLEEP
0588 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
0589               char *buf)
0590 {
0591     return sysfs_emit(buf, "%s\n",
0592               device_async_suspend_enabled(dev) ?
0593               _enabled : _disabled);
0594 }
0595 
0596 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
0597                const char *buf, size_t n)
0598 {
0599     if (sysfs_streq(buf, _enabled))
0600         device_enable_async_suspend(dev);
0601     else if (sysfs_streq(buf, _disabled))
0602         device_disable_async_suspend(dev);
0603     else
0604         return -EINVAL;
0605     return n;
0606 }
0607 
0608 static DEVICE_ATTR_RW(async);
0609 
0610 #endif /* CONFIG_PM_SLEEP */
0611 #endif /* CONFIG_PM_ADVANCED_DEBUG */
0612 
0613 static struct attribute *power_attrs[] = {
0614 #ifdef CONFIG_PM_ADVANCED_DEBUG
0615 #ifdef CONFIG_PM_SLEEP
0616     &dev_attr_async.attr,
0617 #endif
0618     &dev_attr_runtime_status.attr,
0619     &dev_attr_runtime_usage.attr,
0620     &dev_attr_runtime_active_kids.attr,
0621     &dev_attr_runtime_enabled.attr,
0622 #endif /* CONFIG_PM_ADVANCED_DEBUG */
0623     NULL,
0624 };
0625 static const struct attribute_group pm_attr_group = {
0626     .name   = power_group_name,
0627     .attrs  = power_attrs,
0628 };
0629 
0630 static struct attribute *wakeup_attrs[] = {
0631 #ifdef CONFIG_PM_SLEEP
0632     &dev_attr_wakeup.attr,
0633     &dev_attr_wakeup_count.attr,
0634     &dev_attr_wakeup_active_count.attr,
0635     &dev_attr_wakeup_abort_count.attr,
0636     &dev_attr_wakeup_expire_count.attr,
0637     &dev_attr_wakeup_active.attr,
0638     &dev_attr_wakeup_total_time_ms.attr,
0639     &dev_attr_wakeup_max_time_ms.attr,
0640     &dev_attr_wakeup_last_time_ms.attr,
0641 #ifdef CONFIG_PM_AUTOSLEEP
0642     &dev_attr_wakeup_prevent_sleep_time_ms.attr,
0643 #endif
0644 #endif
0645     NULL,
0646 };
0647 static const struct attribute_group pm_wakeup_attr_group = {
0648     .name   = power_group_name,
0649     .attrs  = wakeup_attrs,
0650 };
0651 
0652 static struct attribute *runtime_attrs[] = {
0653 #ifndef CONFIG_PM_ADVANCED_DEBUG
0654     &dev_attr_runtime_status.attr,
0655 #endif
0656     &dev_attr_control.attr,
0657     &dev_attr_runtime_suspended_time.attr,
0658     &dev_attr_runtime_active_time.attr,
0659     &dev_attr_autosuspend_delay_ms.attr,
0660     NULL,
0661 };
0662 static const struct attribute_group pm_runtime_attr_group = {
0663     .name   = power_group_name,
0664     .attrs  = runtime_attrs,
0665 };
0666 
0667 static struct attribute *pm_qos_resume_latency_attrs[] = {
0668     &dev_attr_pm_qos_resume_latency_us.attr,
0669     NULL,
0670 };
0671 static const struct attribute_group pm_qos_resume_latency_attr_group = {
0672     .name   = power_group_name,
0673     .attrs  = pm_qos_resume_latency_attrs,
0674 };
0675 
0676 static struct attribute *pm_qos_latency_tolerance_attrs[] = {
0677     &dev_attr_pm_qos_latency_tolerance_us.attr,
0678     NULL,
0679 };
0680 static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
0681     .name   = power_group_name,
0682     .attrs  = pm_qos_latency_tolerance_attrs,
0683 };
0684 
0685 static struct attribute *pm_qos_flags_attrs[] = {
0686     &dev_attr_pm_qos_no_power_off.attr,
0687     NULL,
0688 };
0689 static const struct attribute_group pm_qos_flags_attr_group = {
0690     .name   = power_group_name,
0691     .attrs  = pm_qos_flags_attrs,
0692 };
0693 
0694 int dpm_sysfs_add(struct device *dev)
0695 {
0696     int rc;
0697 
0698     /* No need to create PM sysfs if explicitly disabled. */
0699     if (device_pm_not_required(dev))
0700         return 0;
0701 
0702     rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
0703     if (rc)
0704         return rc;
0705 
0706     if (!pm_runtime_has_no_callbacks(dev)) {
0707         rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
0708         if (rc)
0709             goto err_out;
0710     }
0711     if (device_can_wakeup(dev)) {
0712         rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
0713         if (rc)
0714             goto err_runtime;
0715     }
0716     if (dev->power.set_latency_tolerance) {
0717         rc = sysfs_merge_group(&dev->kobj,
0718                        &pm_qos_latency_tolerance_attr_group);
0719         if (rc)
0720             goto err_wakeup;
0721     }
0722     rc = pm_wakeup_source_sysfs_add(dev);
0723     if (rc)
0724         goto err_latency;
0725     return 0;
0726 
0727  err_latency:
0728     sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
0729  err_wakeup:
0730     sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
0731  err_runtime:
0732     sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
0733  err_out:
0734     sysfs_remove_group(&dev->kobj, &pm_attr_group);
0735     return rc;
0736 }
0737 
0738 int dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
0739 {
0740     int rc;
0741 
0742     if (device_pm_not_required(dev))
0743         return 0;
0744 
0745     rc = sysfs_group_change_owner(&dev->kobj, &pm_attr_group, kuid, kgid);
0746     if (rc)
0747         return rc;
0748 
0749     if (!pm_runtime_has_no_callbacks(dev)) {
0750         rc = sysfs_group_change_owner(
0751             &dev->kobj, &pm_runtime_attr_group, kuid, kgid);
0752         if (rc)
0753             return rc;
0754     }
0755 
0756     if (device_can_wakeup(dev)) {
0757         rc = sysfs_group_change_owner(&dev->kobj, &pm_wakeup_attr_group,
0758                           kuid, kgid);
0759         if (rc)
0760             return rc;
0761 
0762         rc = dpm_sysfs_wakeup_change_owner(dev, kuid, kgid);
0763         if (rc)
0764             return rc;
0765     }
0766 
0767     if (dev->power.set_latency_tolerance) {
0768         rc = sysfs_group_change_owner(
0769             &dev->kobj, &pm_qos_latency_tolerance_attr_group, kuid,
0770             kgid);
0771         if (rc)
0772             return rc;
0773     }
0774     return 0;
0775 }
0776 
0777 int wakeup_sysfs_add(struct device *dev)
0778 {
0779     int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
0780 
0781     if (!ret)
0782         kobject_uevent(&dev->kobj, KOBJ_CHANGE);
0783 
0784     return ret;
0785 }
0786 
0787 void wakeup_sysfs_remove(struct device *dev)
0788 {
0789     sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
0790     kobject_uevent(&dev->kobj, KOBJ_CHANGE);
0791 }
0792 
0793 int pm_qos_sysfs_add_resume_latency(struct device *dev)
0794 {
0795     return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
0796 }
0797 
0798 void pm_qos_sysfs_remove_resume_latency(struct device *dev)
0799 {
0800     sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
0801 }
0802 
0803 int pm_qos_sysfs_add_flags(struct device *dev)
0804 {
0805     return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
0806 }
0807 
0808 void pm_qos_sysfs_remove_flags(struct device *dev)
0809 {
0810     sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
0811 }
0812 
0813 int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
0814 {
0815     return sysfs_merge_group(&dev->kobj,
0816                  &pm_qos_latency_tolerance_attr_group);
0817 }
0818 
0819 void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
0820 {
0821     sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
0822 }
0823 
0824 void rpm_sysfs_remove(struct device *dev)
0825 {
0826     sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
0827 }
0828 
0829 void dpm_sysfs_remove(struct device *dev)
0830 {
0831     if (device_pm_not_required(dev))
0832         return;
0833     sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
0834     dev_pm_qos_constraints_destroy(dev);
0835     rpm_sysfs_remove(dev);
0836     sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
0837     sysfs_remove_group(&dev->kobj, &pm_attr_group);
0838 }