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