proc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/proc_fs.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/export.h>
  5. #include <linux/suspend.h>
  6. #include <linux/bcd.h>
  7. #include <linux/acpi.h>
  8. #include <linux/uaccess.h>
  9. #include "sleep.h"
  10. #include "internal.h"
  11. /*
  12. * this file provides support for:
  13. * /proc/acpi/wakeup
  14. */
  15. static int
  16. acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
  17. {
  18. struct acpi_device *dev, *tmp;
  19. seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
  20. mutex_lock(&acpi_device_lock);
  21. list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
  22. wakeup_list) {
  23. struct acpi_device_physical_node *entry;
  24. if (!dev->wakeup.flags.valid)
  25. continue;
  26. seq_printf(seq, "%s\t S%d\t",
  27. dev->pnp.bus_id,
  28. (u32) dev->wakeup.sleep_state);
  29. mutex_lock(&dev->physical_node_lock);
  30. if (!dev->physical_node_count) {
  31. seq_printf(seq, "%c%-8s\n",
  32. dev->wakeup.flags.valid ? '*' : ' ',
  33. device_may_wakeup(&dev->dev) ?
  34. "enabled" : "disabled");
  35. } else {
  36. struct device *ldev;
  37. list_for_each_entry(entry, &dev->physical_node_list,
  38. node) {
  39. ldev = get_device(entry->dev);
  40. if (!ldev)
  41. continue;
  42. if (&entry->node !=
  43. dev->physical_node_list.next)
  44. seq_printf(seq, "\t\t");
  45. seq_printf(seq, "%c%-8s %s:%s\n",
  46. dev->wakeup.flags.valid ? '*' : ' ',
  47. (device_may_wakeup(&dev->dev) ||
  48. device_may_wakeup(ldev)) ?
  49. "enabled" : "disabled",
  50. ldev->bus ? ldev->bus->name :
  51. "no-bus", dev_name(ldev));
  52. put_device(ldev);
  53. }
  54. }
  55. mutex_unlock(&dev->physical_node_lock);
  56. }
  57. mutex_unlock(&acpi_device_lock);
  58. return 0;
  59. }
  60. static void physical_device_enable_wakeup(struct acpi_device *adev)
  61. {
  62. struct acpi_device_physical_node *entry;
  63. mutex_lock(&adev->physical_node_lock);
  64. list_for_each_entry(entry,
  65. &adev->physical_node_list, node)
  66. if (entry->dev && device_can_wakeup(entry->dev)) {
  67. bool enable = !device_may_wakeup(entry->dev);
  68. device_set_wakeup_enable(entry->dev, enable);
  69. }
  70. mutex_unlock(&adev->physical_node_lock);
  71. }
  72. static ssize_t
  73. acpi_system_write_wakeup_device(struct file *file,
  74. const char __user * buffer,
  75. size_t count, loff_t * ppos)
  76. {
  77. struct acpi_device *dev, *tmp;
  78. char strbuf[5];
  79. char str[5] = "";
  80. if (count > 4)
  81. count = 4;
  82. if (copy_from_user(strbuf, buffer, count))
  83. return -EFAULT;
  84. strbuf[count] = '\0';
  85. sscanf(strbuf, "%s", str);
  86. mutex_lock(&acpi_device_lock);
  87. list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
  88. wakeup_list) {
  89. if (!dev->wakeup.flags.valid)
  90. continue;
  91. if (!strncmp(dev->pnp.bus_id, str, 4)) {
  92. if (device_can_wakeup(&dev->dev)) {
  93. bool enable = !device_may_wakeup(&dev->dev);
  94. device_set_wakeup_enable(&dev->dev, enable);
  95. } else {
  96. physical_device_enable_wakeup(dev);
  97. }
  98. break;
  99. }
  100. }
  101. mutex_unlock(&acpi_device_lock);
  102. return count;
  103. }
  104. static int
  105. acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
  106. {
  107. return single_open(file, acpi_system_wakeup_device_seq_show,
  108. PDE_DATA(inode));
  109. }
  110. static const struct proc_ops acpi_system_wakeup_device_proc_ops = {
  111. .proc_open = acpi_system_wakeup_device_open_fs,
  112. .proc_read = seq_read,
  113. .proc_write = acpi_system_write_wakeup_device,
  114. .proc_lseek = seq_lseek,
  115. .proc_release = single_release,
  116. };
  117. void __init acpi_sleep_proc_init(void)
  118. {
  119. /* 'wakeup device' [R/W] */
  120. proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
  121. acpi_root_dir, &acpi_system_wakeup_device_proc_ops);
  122. }