xref: /openbmc/linux/drivers/acpi/proc.c (revision 0cd08b10)
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 
10 #include "sleep.h"
11 #include "internal.h"
12 
13 #define _COMPONENT		ACPI_SYSTEM_COMPONENT
14 
15 /*
16  * this file provides support for:
17  * /proc/acpi/wakeup
18  */
19 
20 ACPI_MODULE_NAME("sleep")
21 
22 static int
23 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
24 {
25 	struct acpi_device *dev, *tmp;
26 
27 	seq_printf(seq, "Device\tS-state\t  Status   Sysfs node\n");
28 
29 	mutex_lock(&acpi_device_lock);
30 	list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
31 				 wakeup_list) {
32 		struct acpi_device_physical_node *entry;
33 
34 		if (!dev->wakeup.flags.valid)
35 			continue;
36 
37 		seq_printf(seq, "%s\t  S%d\t",
38 			   dev->pnp.bus_id,
39 			   (u32) dev->wakeup.sleep_state);
40 
41 		mutex_lock(&dev->physical_node_lock);
42 
43 		if (!dev->physical_node_count) {
44 			seq_printf(seq, "%c%-8s\n",
45 				dev->wakeup.flags.valid ? '*' : ' ',
46 				device_may_wakeup(&dev->dev) ?
47 					"enabled" : "disabled");
48 		} else {
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",
61 					dev->wakeup.flags.valid ? '*' : ' ',
62 					(device_may_wakeup(&dev->dev) ||
63 					device_may_wakeup(ldev)) ?
64 					"enabled" : "disabled",
65 					ldev->bus ? ldev->bus->name :
66 					"no-bus", dev_name(ldev));
67 				put_device(ldev);
68 			}
69 		}
70 
71 		mutex_unlock(&dev->physical_node_lock);
72 	}
73 	mutex_unlock(&acpi_device_lock);
74 	return 0;
75 }
76 
77 static void physical_device_enable_wakeup(struct acpi_device *adev)
78 {
79 	struct acpi_device_physical_node *entry;
80 
81 	mutex_lock(&adev->physical_node_lock);
82 
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 		}
89 
90 	mutex_unlock(&adev->physical_node_lock);
91 }
92 
93 static ssize_t
94 acpi_system_write_wakeup_device(struct file *file,
95 				const char __user * buffer,
96 				size_t count, loff_t * ppos)
97 {
98 	struct acpi_device *dev, *tmp;
99 	char strbuf[5];
100 	char str[5] = "";
101 
102 	if (count > 4)
103 		count = 4;
104 
105 	if (copy_from_user(strbuf, buffer, count))
106 		return -EFAULT;
107 	strbuf[count] = '\0';
108 	sscanf(strbuf, "%s", str);
109 
110 	mutex_lock(&acpi_device_lock);
111 	list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
112 				 wakeup_list) {
113 		if (!dev->wakeup.flags.valid)
114 			continue;
115 
116 		if (!strncmp(dev->pnp.bus_id, str, 4)) {
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 			}
123 			break;
124 		}
125 	}
126 	mutex_unlock(&acpi_device_lock);
127 	return count;
128 }
129 
130 static int
131 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
132 {
133 	return single_open(file, acpi_system_wakeup_device_seq_show,
134 			   PDE_DATA(inode));
135 }
136 
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,
143 };
144 
145 void __init acpi_sleep_proc_init(void)
146 {
147 	/* 'wakeup device' [R/W] */
148 	proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
149 		    acpi_root_dir, &acpi_system_wakeup_device_proc_ops);
150 }
151