sysfs.c 15 KB

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