sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * drivers/base/power/sysfs.c - sysfs entries for device PM
  3. */
  4. #include <linux/device.h>
  5. #include <linux/string.h>
  6. #include <linux/pm_runtime.h>
  7. #include <asm/atomic.h>
  8. #include <linux/jiffies.h>
  9. #include "power.h"
  10. /*
  11. * control - Report/change current runtime PM setting of the device
  12. *
  13. * Runtime power management of a device can be blocked with the help of
  14. * this attribute. All devices have one of the following two values for
  15. * the power/control file:
  16. *
  17. * + "auto\n" to allow the device to be power managed at run time;
  18. * + "on\n" to prevent the device from being power managed at run time;
  19. *
  20. * The default for all devices is "auto", which means that devices may be
  21. * subject to automatic power management, depending on their drivers.
  22. * Changing this attribute to "on" prevents the driver from power managing
  23. * the device at run time. Doing that while the device is suspended causes
  24. * it to be woken up.
  25. *
  26. * wakeup - Report/change current wakeup option for device
  27. *
  28. * Some devices support "wakeup" events, which are hardware signals
  29. * used to activate devices from suspended or low power states. Such
  30. * devices have one of three values for the sysfs power/wakeup file:
  31. *
  32. * + "enabled\n" to issue the events;
  33. * + "disabled\n" not to do so; or
  34. * + "\n" for temporary or permanent inability to issue wakeup.
  35. *
  36. * (For example, unconfigured USB devices can't issue wakeups.)
  37. *
  38. * Familiar examples of devices that can issue wakeup events include
  39. * keyboards and mice (both PS2 and USB styles), power buttons, modems,
  40. * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
  41. * will wake the entire system from a suspend state; others may just
  42. * wake up the device (if the system as a whole is already active).
  43. * Some wakeup events use normal IRQ lines; other use special out
  44. * of band signaling.
  45. *
  46. * It is the responsibility of device drivers to enable (or disable)
  47. * wakeup signaling as part of changing device power states, respecting
  48. * the policy choices provided through the driver model.
  49. *
  50. * Devices may not be able to generate wakeup events from all power
  51. * states. Also, the events may be ignored in some configurations;
  52. * for example, they might need help from other devices that aren't
  53. * active, or which may have wakeup disabled. Some drivers rely on
  54. * wakeup events internally (unless they are disabled), keeping
  55. * their hardware in low power modes whenever they're unused. This
  56. * saves runtime power, without requiring system-wide sleep states.
  57. *
  58. * async - Report/change current async suspend setting for the device
  59. *
  60. * Asynchronous suspend and resume of the device during system-wide power
  61. * state transitions can be enabled by writing "enabled" to this file.
  62. * Analogously, if "disabled" is written to this file, the device will be
  63. * suspended and resumed synchronously.
  64. *
  65. * All devices have one of the following two values for power/async:
  66. *
  67. * + "enabled\n" to permit the asynchronous suspend/resume of the device;
  68. * + "disabled\n" to forbid it;
  69. *
  70. * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
  71. * of a device unless it is certain that all of the PM dependencies of the
  72. * device are known to the PM core. However, for some devices this
  73. * attribute is set to "enabled" by bus type code or device drivers and in
  74. * that cases it should be safe to leave the default value.
  75. *
  76. * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
  77. *
  78. * Some drivers don't want to carry out a runtime suspend as soon as a
  79. * device becomes idle; they want it always to remain idle for some period
  80. * of time before suspending it. This period is the autosuspend_delay
  81. * value (expressed in milliseconds) and it can be controlled by the user.
  82. * If the value is negative then the device will never be runtime
  83. * suspended.
  84. *
  85. * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
  86. * value are used only if the driver calls pm_runtime_use_autosuspend().
  87. *
  88. * wakeup_count - Report the number of wakeup events related to the device
  89. */
  90. static const char enabled[] = "enabled";
  91. static const char disabled[] = "disabled";
  92. const char power_group_name[] = "power";
  93. EXPORT_SYMBOL_GPL(power_group_name);
  94. #ifdef CONFIG_PM_RUNTIME
  95. static const char ctrl_auto[] = "auto";
  96. static const char ctrl_on[] = "on";
  97. static ssize_t control_show(struct device *dev, struct device_attribute *attr,
  98. char *buf)
  99. {
  100. return sprintf(buf, "%s\n",
  101. dev->power.runtime_auto ? ctrl_auto : ctrl_on);
  102. }
  103. static ssize_t control_store(struct device * dev, struct device_attribute *attr,
  104. const char * buf, size_t n)
  105. {
  106. char *cp;
  107. int len = n;
  108. cp = memchr(buf, '\n', n);
  109. if (cp)
  110. len = cp - buf;
  111. if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
  112. pm_runtime_allow(dev);
  113. else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
  114. pm_runtime_forbid(dev);
  115. else
  116. return -EINVAL;
  117. return n;
  118. }
  119. static DEVICE_ATTR(control, 0644, control_show, control_store);
  120. static ssize_t rtpm_active_time_show(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. int ret;
  124. spin_lock_irq(&dev->power.lock);
  125. update_pm_runtime_accounting(dev);
  126. ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
  127. spin_unlock_irq(&dev->power.lock);
  128. return ret;
  129. }
  130. static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
  131. static ssize_t rtpm_suspended_time_show(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. int ret;
  135. spin_lock_irq(&dev->power.lock);
  136. update_pm_runtime_accounting(dev);
  137. ret = sprintf(buf, "%i\n",
  138. jiffies_to_msecs(dev->power.suspended_jiffies));
  139. spin_unlock_irq(&dev->power.lock);
  140. return ret;
  141. }
  142. static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
  143. static ssize_t rtpm_status_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. const char *p;
  147. if (dev->power.runtime_error) {
  148. p = "error\n";
  149. } else if (dev->power.disable_depth) {
  150. p = "unsupported\n";
  151. } else {
  152. switch (dev->power.runtime_status) {
  153. case RPM_SUSPENDED:
  154. p = "suspended\n";
  155. break;
  156. case RPM_SUSPENDING:
  157. p = "suspending\n";
  158. break;
  159. case RPM_RESUMING:
  160. p = "resuming\n";
  161. break;
  162. case RPM_ACTIVE:
  163. p = "active\n";
  164. break;
  165. default:
  166. return -EIO;
  167. }
  168. }
  169. return sprintf(buf, p);
  170. }
  171. static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
  172. static ssize_t autosuspend_delay_ms_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. if (!dev->power.use_autosuspend)
  176. return -EIO;
  177. return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
  178. }
  179. static ssize_t autosuspend_delay_ms_store(struct device *dev,
  180. struct device_attribute *attr, const char *buf, size_t n)
  181. {
  182. long delay;
  183. if (!dev->power.use_autosuspend)
  184. return -EIO;
  185. if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
  186. return -EINVAL;
  187. pm_runtime_set_autosuspend_delay(dev, delay);
  188. return n;
  189. }
  190. static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
  191. autosuspend_delay_ms_store);
  192. #endif
  193. static ssize_t
  194. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  195. {
  196. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  197. ? (device_may_wakeup(dev) ? enabled : disabled)
  198. : "");
  199. }
  200. static ssize_t
  201. wake_store(struct device * dev, struct device_attribute *attr,
  202. const char * buf, size_t n)
  203. {
  204. char *cp;
  205. int len = n;
  206. if (!device_can_wakeup(dev))
  207. return -EINVAL;
  208. cp = memchr(buf, '\n', n);
  209. if (cp)
  210. len = cp - buf;
  211. if (len == sizeof enabled - 1
  212. && strncmp(buf, enabled, sizeof enabled - 1) == 0)
  213. device_set_wakeup_enable(dev, 1);
  214. else if (len == sizeof disabled - 1
  215. && strncmp(buf, disabled, sizeof disabled - 1) == 0)
  216. device_set_wakeup_enable(dev, 0);
  217. else
  218. return -EINVAL;
  219. return n;
  220. }
  221. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  222. #ifdef CONFIG_PM_SLEEP
  223. static ssize_t wakeup_count_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. unsigned long count = 0;
  227. bool enabled = false;
  228. spin_lock_irq(&dev->power.lock);
  229. if (dev->power.wakeup) {
  230. count = dev->power.wakeup->event_count;
  231. enabled = true;
  232. }
  233. spin_unlock_irq(&dev->power.lock);
  234. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  235. }
  236. static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
  237. static ssize_t wakeup_active_count_show(struct device *dev,
  238. struct device_attribute *attr, char *buf)
  239. {
  240. unsigned long count = 0;
  241. bool enabled = false;
  242. spin_lock_irq(&dev->power.lock);
  243. if (dev->power.wakeup) {
  244. count = dev->power.wakeup->active_count;
  245. enabled = true;
  246. }
  247. spin_unlock_irq(&dev->power.lock);
  248. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  249. }
  250. static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
  251. static ssize_t wakeup_hit_count_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. unsigned long count = 0;
  255. bool enabled = false;
  256. spin_lock_irq(&dev->power.lock);
  257. if (dev->power.wakeup) {
  258. count = dev->power.wakeup->hit_count;
  259. enabled = true;
  260. }
  261. spin_unlock_irq(&dev->power.lock);
  262. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  263. }
  264. static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
  265. static ssize_t wakeup_active_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. unsigned int active = 0;
  269. bool enabled = false;
  270. spin_lock_irq(&dev->power.lock);
  271. if (dev->power.wakeup) {
  272. active = dev->power.wakeup->active;
  273. enabled = true;
  274. }
  275. spin_unlock_irq(&dev->power.lock);
  276. return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
  277. }
  278. static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
  279. static ssize_t wakeup_total_time_show(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. s64 msec = 0;
  283. bool enabled = false;
  284. spin_lock_irq(&dev->power.lock);
  285. if (dev->power.wakeup) {
  286. msec = ktime_to_ms(dev->power.wakeup->total_time);
  287. enabled = true;
  288. }
  289. spin_unlock_irq(&dev->power.lock);
  290. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  291. }
  292. static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
  293. static ssize_t wakeup_max_time_show(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. s64 msec = 0;
  297. bool enabled = false;
  298. spin_lock_irq(&dev->power.lock);
  299. if (dev->power.wakeup) {
  300. msec = ktime_to_ms(dev->power.wakeup->max_time);
  301. enabled = true;
  302. }
  303. spin_unlock_irq(&dev->power.lock);
  304. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  305. }
  306. static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
  307. static ssize_t wakeup_last_time_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. s64 msec = 0;
  311. bool enabled = false;
  312. spin_lock_irq(&dev->power.lock);
  313. if (dev->power.wakeup) {
  314. msec = ktime_to_ms(dev->power.wakeup->last_time);
  315. enabled = true;
  316. }
  317. spin_unlock_irq(&dev->power.lock);
  318. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  319. }
  320. static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
  321. #endif /* CONFIG_PM_SLEEP */
  322. #ifdef CONFIG_PM_ADVANCED_DEBUG
  323. #ifdef CONFIG_PM_RUNTIME
  324. static ssize_t rtpm_usagecount_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
  328. }
  329. static ssize_t rtpm_children_show(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. return sprintf(buf, "%d\n", dev->power.ignore_children ?
  333. 0 : atomic_read(&dev->power.child_count));
  334. }
  335. static ssize_t rtpm_enabled_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
  339. return sprintf(buf, "disabled & forbidden\n");
  340. else if (dev->power.disable_depth)
  341. return sprintf(buf, "disabled\n");
  342. else if (dev->power.runtime_auto == false)
  343. return sprintf(buf, "forbidden\n");
  344. return sprintf(buf, "enabled\n");
  345. }
  346. static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
  347. static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
  348. static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
  349. #endif
  350. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  351. char *buf)
  352. {
  353. return sprintf(buf, "%s\n",
  354. device_async_suspend_enabled(dev) ? enabled : disabled);
  355. }
  356. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  357. const char *buf, size_t n)
  358. {
  359. char *cp;
  360. int len = n;
  361. cp = memchr(buf, '\n', n);
  362. if (cp)
  363. len = cp - buf;
  364. if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
  365. device_enable_async_suspend(dev);
  366. else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
  367. device_disable_async_suspend(dev);
  368. else
  369. return -EINVAL;
  370. return n;
  371. }
  372. static DEVICE_ATTR(async, 0644, async_show, async_store);
  373. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  374. static struct attribute * power_attrs[] = {
  375. &dev_attr_wakeup.attr,
  376. #ifdef CONFIG_PM_SLEEP
  377. &dev_attr_wakeup_count.attr,
  378. &dev_attr_wakeup_active_count.attr,
  379. &dev_attr_wakeup_hit_count.attr,
  380. &dev_attr_wakeup_active.attr,
  381. &dev_attr_wakeup_total_time_ms.attr,
  382. &dev_attr_wakeup_max_time_ms.attr,
  383. &dev_attr_wakeup_last_time_ms.attr,
  384. #endif
  385. #ifdef CONFIG_PM_ADVANCED_DEBUG
  386. &dev_attr_async.attr,
  387. #ifdef CONFIG_PM_RUNTIME
  388. &dev_attr_runtime_status.attr,
  389. &dev_attr_runtime_usage.attr,
  390. &dev_attr_runtime_active_kids.attr,
  391. &dev_attr_runtime_enabled.attr,
  392. #endif
  393. #endif
  394. NULL,
  395. };
  396. static struct attribute_group pm_attr_group = {
  397. .name = power_group_name,
  398. .attrs = power_attrs,
  399. };
  400. #ifdef CONFIG_PM_RUNTIME
  401. static struct attribute *runtime_attrs[] = {
  402. #ifndef CONFIG_PM_ADVANCED_DEBUG
  403. &dev_attr_runtime_status.attr,
  404. #endif
  405. &dev_attr_control.attr,
  406. &dev_attr_runtime_suspended_time.attr,
  407. &dev_attr_runtime_active_time.attr,
  408. &dev_attr_autosuspend_delay_ms.attr,
  409. NULL,
  410. };
  411. static struct attribute_group pm_runtime_attr_group = {
  412. .name = power_group_name,
  413. .attrs = runtime_attrs,
  414. };
  415. int dpm_sysfs_add(struct device *dev)
  416. {
  417. int rc;
  418. rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
  419. if (rc == 0 && !dev->power.no_callbacks) {
  420. rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
  421. if (rc)
  422. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  423. }
  424. return rc;
  425. }
  426. void rpm_sysfs_remove(struct device *dev)
  427. {
  428. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  429. }
  430. void dpm_sysfs_remove(struct device *dev)
  431. {
  432. rpm_sysfs_remove(dev);
  433. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  434. }
  435. #else /* CONFIG_PM_RUNTIME */
  436. int dpm_sysfs_add(struct device * dev)
  437. {
  438. return sysfs_create_group(&dev->kobj, &pm_attr_group);
  439. }
  440. void dpm_sysfs_remove(struct device * dev)
  441. {
  442. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  443. }
  444. #endif