sysfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 <linux/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. device_lock(dev);
  112. if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
  113. pm_runtime_allow(dev);
  114. else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
  115. pm_runtime_forbid(dev);
  116. else
  117. n = -EINVAL;
  118. device_unlock(dev);
  119. return n;
  120. }
  121. static DEVICE_ATTR(control, 0644, control_show, control_store);
  122. static ssize_t rtpm_active_time_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. int ret;
  126. spin_lock_irq(&dev->power.lock);
  127. update_pm_runtime_accounting(dev);
  128. ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
  129. spin_unlock_irq(&dev->power.lock);
  130. return ret;
  131. }
  132. static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
  133. static ssize_t rtpm_suspended_time_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. int ret;
  137. spin_lock_irq(&dev->power.lock);
  138. update_pm_runtime_accounting(dev);
  139. ret = sprintf(buf, "%i\n",
  140. jiffies_to_msecs(dev->power.suspended_jiffies));
  141. spin_unlock_irq(&dev->power.lock);
  142. return ret;
  143. }
  144. static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
  145. static ssize_t rtpm_status_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. const char *p;
  149. if (dev->power.runtime_error) {
  150. p = "error\n";
  151. } else if (dev->power.disable_depth) {
  152. p = "unsupported\n";
  153. } else {
  154. switch (dev->power.runtime_status) {
  155. case RPM_SUSPENDED:
  156. p = "suspended\n";
  157. break;
  158. case RPM_SUSPENDING:
  159. p = "suspending\n";
  160. break;
  161. case RPM_RESUMING:
  162. p = "resuming\n";
  163. break;
  164. case RPM_ACTIVE:
  165. p = "active\n";
  166. break;
  167. default:
  168. return -EIO;
  169. }
  170. }
  171. return sprintf(buf, p);
  172. }
  173. static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
  174. static ssize_t autosuspend_delay_ms_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. if (!dev->power.use_autosuspend)
  178. return -EIO;
  179. return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
  180. }
  181. static ssize_t autosuspend_delay_ms_store(struct device *dev,
  182. struct device_attribute *attr, const char *buf, size_t n)
  183. {
  184. long delay;
  185. if (!dev->power.use_autosuspend)
  186. return -EIO;
  187. if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
  188. return -EINVAL;
  189. device_lock(dev);
  190. pm_runtime_set_autosuspend_delay(dev, delay);
  191. device_unlock(dev);
  192. return n;
  193. }
  194. static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
  195. autosuspend_delay_ms_store);
  196. #endif /* CONFIG_PM_RUNTIME */
  197. #ifdef CONFIG_PM_SLEEP
  198. static ssize_t
  199. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  200. {
  201. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  202. ? (device_may_wakeup(dev) ? enabled : disabled)
  203. : "");
  204. }
  205. static ssize_t
  206. wake_store(struct device * dev, struct device_attribute *attr,
  207. const char * buf, size_t n)
  208. {
  209. char *cp;
  210. int len = n;
  211. if (!device_can_wakeup(dev))
  212. return -EINVAL;
  213. cp = memchr(buf, '\n', n);
  214. if (cp)
  215. len = cp - buf;
  216. if (len == sizeof enabled - 1
  217. && strncmp(buf, enabled, sizeof enabled - 1) == 0)
  218. device_set_wakeup_enable(dev, 1);
  219. else if (len == sizeof disabled - 1
  220. && strncmp(buf, disabled, sizeof disabled - 1) == 0)
  221. device_set_wakeup_enable(dev, 0);
  222. else
  223. return -EINVAL;
  224. return n;
  225. }
  226. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  227. static ssize_t wakeup_count_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. unsigned long count = 0;
  231. bool enabled = false;
  232. spin_lock_irq(&dev->power.lock);
  233. if (dev->power.wakeup) {
  234. count = dev->power.wakeup->event_count;
  235. enabled = true;
  236. }
  237. spin_unlock_irq(&dev->power.lock);
  238. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  239. }
  240. static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
  241. static ssize_t wakeup_active_count_show(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. unsigned long count = 0;
  245. bool enabled = false;
  246. spin_lock_irq(&dev->power.lock);
  247. if (dev->power.wakeup) {
  248. count = dev->power.wakeup->active_count;
  249. enabled = true;
  250. }
  251. spin_unlock_irq(&dev->power.lock);
  252. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  253. }
  254. static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
  255. static ssize_t wakeup_hit_count_show(struct device *dev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. unsigned long count = 0;
  259. bool enabled = false;
  260. spin_lock_irq(&dev->power.lock);
  261. if (dev->power.wakeup) {
  262. count = dev->power.wakeup->hit_count;
  263. enabled = true;
  264. }
  265. spin_unlock_irq(&dev->power.lock);
  266. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  267. }
  268. static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
  269. static ssize_t wakeup_active_show(struct device *dev,
  270. struct device_attribute *attr, char *buf)
  271. {
  272. unsigned int active = 0;
  273. bool enabled = false;
  274. spin_lock_irq(&dev->power.lock);
  275. if (dev->power.wakeup) {
  276. active = dev->power.wakeup->active;
  277. enabled = true;
  278. }
  279. spin_unlock_irq(&dev->power.lock);
  280. return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
  281. }
  282. static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
  283. static ssize_t wakeup_total_time_show(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. s64 msec = 0;
  287. bool enabled = false;
  288. spin_lock_irq(&dev->power.lock);
  289. if (dev->power.wakeup) {
  290. msec = ktime_to_ms(dev->power.wakeup->total_time);
  291. enabled = true;
  292. }
  293. spin_unlock_irq(&dev->power.lock);
  294. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  295. }
  296. static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
  297. static ssize_t wakeup_max_time_show(struct device *dev,
  298. struct device_attribute *attr, char *buf)
  299. {
  300. s64 msec = 0;
  301. bool enabled = false;
  302. spin_lock_irq(&dev->power.lock);
  303. if (dev->power.wakeup) {
  304. msec = ktime_to_ms(dev->power.wakeup->max_time);
  305. enabled = true;
  306. }
  307. spin_unlock_irq(&dev->power.lock);
  308. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  309. }
  310. static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
  311. static ssize_t wakeup_last_time_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. s64 msec = 0;
  315. bool enabled = false;
  316. spin_lock_irq(&dev->power.lock);
  317. if (dev->power.wakeup) {
  318. msec = ktime_to_ms(dev->power.wakeup->last_time);
  319. enabled = true;
  320. }
  321. spin_unlock_irq(&dev->power.lock);
  322. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  323. }
  324. static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
  325. #endif /* CONFIG_PM_SLEEP */
  326. #ifdef CONFIG_PM_ADVANCED_DEBUG
  327. #ifdef CONFIG_PM_RUNTIME
  328. static ssize_t rtpm_usagecount_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
  332. }
  333. static ssize_t rtpm_children_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. return sprintf(buf, "%d\n", dev->power.ignore_children ?
  337. 0 : atomic_read(&dev->power.child_count));
  338. }
  339. static ssize_t rtpm_enabled_show(struct device *dev,
  340. struct device_attribute *attr, char *buf)
  341. {
  342. if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
  343. return sprintf(buf, "disabled & forbidden\n");
  344. else if (dev->power.disable_depth)
  345. return sprintf(buf, "disabled\n");
  346. else if (dev->power.runtime_auto == false)
  347. return sprintf(buf, "forbidden\n");
  348. return sprintf(buf, "enabled\n");
  349. }
  350. static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
  351. static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
  352. static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
  353. #endif
  354. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  355. char *buf)
  356. {
  357. return sprintf(buf, "%s\n",
  358. device_async_suspend_enabled(dev) ? enabled : disabled);
  359. }
  360. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  361. const char *buf, size_t n)
  362. {
  363. char *cp;
  364. int len = n;
  365. cp = memchr(buf, '\n', n);
  366. if (cp)
  367. len = cp - buf;
  368. if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
  369. device_enable_async_suspend(dev);
  370. else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
  371. device_disable_async_suspend(dev);
  372. else
  373. return -EINVAL;
  374. return n;
  375. }
  376. static DEVICE_ATTR(async, 0644, async_show, async_store);
  377. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  378. static struct attribute *power_attrs[] = {
  379. #ifdef CONFIG_PM_ADVANCED_DEBUG
  380. #ifdef CONFIG_PM_SLEEP
  381. &dev_attr_async.attr,
  382. #endif
  383. #ifdef CONFIG_PM_RUNTIME
  384. &dev_attr_runtime_status.attr,
  385. &dev_attr_runtime_usage.attr,
  386. &dev_attr_runtime_active_kids.attr,
  387. &dev_attr_runtime_enabled.attr,
  388. #endif
  389. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  390. NULL,
  391. };
  392. static struct attribute_group pm_attr_group = {
  393. .name = power_group_name,
  394. .attrs = power_attrs,
  395. };
  396. static struct attribute *wakeup_attrs[] = {
  397. #ifdef CONFIG_PM_SLEEP
  398. &dev_attr_wakeup.attr,
  399. &dev_attr_wakeup_count.attr,
  400. &dev_attr_wakeup_active_count.attr,
  401. &dev_attr_wakeup_hit_count.attr,
  402. &dev_attr_wakeup_active.attr,
  403. &dev_attr_wakeup_total_time_ms.attr,
  404. &dev_attr_wakeup_max_time_ms.attr,
  405. &dev_attr_wakeup_last_time_ms.attr,
  406. #endif
  407. NULL,
  408. };
  409. static struct attribute_group pm_wakeup_attr_group = {
  410. .name = power_group_name,
  411. .attrs = wakeup_attrs,
  412. };
  413. static struct attribute *runtime_attrs[] = {
  414. #ifdef CONFIG_PM_RUNTIME
  415. #ifndef CONFIG_PM_ADVANCED_DEBUG
  416. &dev_attr_runtime_status.attr,
  417. #endif
  418. &dev_attr_control.attr,
  419. &dev_attr_runtime_suspended_time.attr,
  420. &dev_attr_runtime_active_time.attr,
  421. &dev_attr_autosuspend_delay_ms.attr,
  422. #endif /* CONFIG_PM_RUNTIME */
  423. NULL,
  424. };
  425. static struct attribute_group pm_runtime_attr_group = {
  426. .name = power_group_name,
  427. .attrs = runtime_attrs,
  428. };
  429. int dpm_sysfs_add(struct device *dev)
  430. {
  431. int rc;
  432. rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
  433. if (rc)
  434. return rc;
  435. if (pm_runtime_callbacks_present(dev)) {
  436. rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
  437. if (rc)
  438. goto err_out;
  439. }
  440. if (device_can_wakeup(dev)) {
  441. rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  442. if (rc) {
  443. if (pm_runtime_callbacks_present(dev))
  444. sysfs_unmerge_group(&dev->kobj,
  445. &pm_runtime_attr_group);
  446. goto err_out;
  447. }
  448. }
  449. return 0;
  450. err_out:
  451. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  452. return rc;
  453. }
  454. int wakeup_sysfs_add(struct device *dev)
  455. {
  456. return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  457. }
  458. void wakeup_sysfs_remove(struct device *dev)
  459. {
  460. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  461. }
  462. void rpm_sysfs_remove(struct device *dev)
  463. {
  464. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  465. }
  466. void dpm_sysfs_remove(struct device *dev)
  467. {
  468. rpm_sysfs_remove(dev);
  469. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  470. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  471. }