Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/proc_fs.h> |
| 3 | #include <linux/seq_file.h> |
Paul Gortmaker | 214f2c9 | 2011-10-26 16:22:14 -0400 | [diff] [blame] | 4 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/suspend.h> |
| 6 | #include <linux/bcd.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 7 | #include <linux/acpi.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 8 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "sleep.h" |
Rashika | 6a36875 | 2013-12-17 15:04:17 +0530 | [diff] [blame] | 11 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
Len Brown | 43532c8 | 2007-07-24 02:16:50 -0400 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * this file provides support for: |
Len Brown | 43532c8 | 2007-07-24 02:16:50 -0400 | [diff] [blame] | 17 | * /proc/acpi/wakeup |
| 18 | */ |
| 19 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 20 | ACPI_MODULE_NAME("sleep") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static int |
| 23 | acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset) |
| 24 | { |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 25 | struct acpi_device *dev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 27 | seq_printf(seq, "Device\tS-state\t Status Sysfs node\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 29 | mutex_lock(&acpi_device_lock); |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 30 | list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list, |
| 31 | wakeup_list) { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 32 | struct acpi_device_physical_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | if (!dev->wakeup.flags.valid) |
| 35 | continue; |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 36 | |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 37 | seq_printf(seq, "%s\t S%d\t", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 38 | dev->pnp.bus_id, |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 39 | (u32) dev->wakeup.sleep_state); |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 40 | |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 41 | mutex_lock(&dev->physical_node_lock); |
| 42 | |
Andreas Fleig | 65ab96f | 2013-01-27 14:17:55 +0000 | [diff] [blame] | 43 | if (!dev->physical_node_count) { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 44 | seq_printf(seq, "%c%-8s\n", |
Rafael J. Wysocki | a1a6639 | 2017-06-24 01:53:14 +0200 | [diff] [blame] | 45 | dev->wakeup.flags.valid ? '*' : ' ', |
Andreas Fleig | 65ab96f | 2013-01-27 14:17:55 +0000 | [diff] [blame] | 46 | device_may_wakeup(&dev->dev) ? |
| 47 | "enabled" : "disabled"); |
| 48 | } else { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 49 | struct device *ldev; |
| 50 | list_for_each_entry(entry, &dev->physical_node_list, |
| 51 | node) { |
| 52 | ldev = get_device(entry->dev); |
| 53 | if (!ldev) |
| 54 | continue; |
| 55 | |
| 56 | if (&entry->node != |
| 57 | dev->physical_node_list.next) |
| 58 | seq_printf(seq, "\t\t"); |
| 59 | |
| 60 | seq_printf(seq, "%c%-8s %s:%s\n", |
Rafael J. Wysocki | a1a6639 | 2017-06-24 01:53:14 +0200 | [diff] [blame] | 61 | dev->wakeup.flags.valid ? '*' : ' ', |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 62 | (device_may_wakeup(&dev->dev) || |
Dan Carpenter | 085ca11 | 2014-01-30 14:55:15 +0300 | [diff] [blame] | 63 | device_may_wakeup(ldev)) ? |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 64 | "enabled" : "disabled", |
| 65 | ldev->bus ? ldev->bus->name : |
| 66 | "no-bus", dev_name(ldev)); |
| 67 | put_device(ldev); |
| 68 | } |
| 69 | } |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 70 | |
| 71 | mutex_unlock(&dev->physical_node_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 73 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 77 | static void physical_device_enable_wakeup(struct acpi_device *adev) |
| 78 | { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 79 | struct acpi_device_physical_node *entry; |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 80 | |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 81 | mutex_lock(&adev->physical_node_lock); |
| 82 | |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 83 | list_for_each_entry(entry, |
| 84 | &adev->physical_node_list, node) |
| 85 | if (entry->dev && device_can_wakeup(entry->dev)) { |
| 86 | bool enable = !device_may_wakeup(entry->dev); |
| 87 | device_set_wakeup_enable(entry->dev, enable); |
| 88 | } |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 89 | |
| 90 | mutex_unlock(&adev->physical_node_lock); |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 94 | acpi_system_write_wakeup_device(struct file *file, |
| 95 | const char __user * buffer, |
| 96 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 98 | struct acpi_device *dev, *tmp; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 99 | char strbuf[5]; |
| 100 | char str[5] = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 102 | if (count > 4) |
| 103 | count = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 105 | if (copy_from_user(strbuf, buffer, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EFAULT; |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 107 | strbuf[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | sscanf(strbuf, "%s", str); |
| 109 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 110 | mutex_lock(&acpi_device_lock); |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 111 | list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list, |
| 112 | wakeup_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (!dev->wakeup.flags.valid) |
| 114 | continue; |
| 115 | |
| 116 | if (!strncmp(dev->pnp.bus_id, str, 4)) { |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 117 | if (device_can_wakeup(&dev->dev)) { |
| 118 | bool enable = !device_may_wakeup(&dev->dev); |
| 119 | device_set_wakeup_enable(&dev->dev, enable); |
| 120 | } else { |
| 121 | physical_device_enable_wakeup(dev); |
| 122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | } |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 126 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return count; |
| 128 | } |
| 129 | |
| 130 | static int |
| 131 | acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file) |
| 132 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 133 | return single_open(file, acpi_system_wakeup_device_seq_show, |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 134 | PDE_DATA(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 137 | static const struct proc_ops acpi_system_wakeup_device_proc_ops = { |
| 138 | .proc_open = acpi_system_wakeup_device_open_fs, |
| 139 | .proc_read = seq_read, |
| 140 | .proc_write = acpi_system_write_wakeup_device, |
| 141 | .proc_lseek = seq_lseek, |
| 142 | .proc_release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Rami Rosen | f934c74 | 2015-09-11 17:49:22 +0300 | [diff] [blame] | 145 | void __init acpi_sleep_proc_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Pavel Machek | c65ade4 | 2005-08-05 00:37:45 -0400 | [diff] [blame] | 147 | /* 'wakeup device' [R/W] */ |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 148 | proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR, |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 149 | acpi_root_dir, &acpi_system_wakeup_device_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |