proc.c 3.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
4
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
5 6
#include <linux/suspend.h>
#include <linux/bcd.h>
7
#include <linux/acpi.h>
8
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
9 10

#include "sleep.h"
11
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed
12 13

#define _COMPONENT		ACPI_SYSTEM_COMPONENT
14 15 16 17 18 19

/*
 * this file provides support for:
 * /proc/acpi/wakeup
 */

Len Brown's avatar
Len Brown committed
20
ACPI_MODULE_NAME("sleep")
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24

static int
acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
{
Len Brown's avatar
Len Brown committed
25
	struct list_head *node, *next;
Linus Torvalds's avatar
Linus Torvalds committed
26

27
	seq_printf(seq, "Device\tS-state\t  Status   Sysfs node\n");
Linus Torvalds's avatar
Linus Torvalds committed
28

29
	mutex_lock(&acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
30
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown's avatar
Len Brown committed
31 32
		struct acpi_device *dev =
		    container_of(node, struct acpi_device, wakeup_list);
33
		struct acpi_device_physical_node *entry;
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36

		if (!dev->wakeup.flags.valid)
			continue;
37

38
		seq_printf(seq, "%s\t  S%d\t",
Len Brown's avatar
Len Brown committed
39
			   dev->pnp.bus_id,
40 41
			   (u32) dev->wakeup.sleep_state);

42 43
		mutex_lock(&dev->physical_node_lock);

44
		if (!dev->physical_node_count) {
45
			seq_printf(seq, "%c%-8s\n",
46
				dev->wakeup.flags.valid ? '*' : ' ',
47 48 49
				device_may_wakeup(&dev->dev) ?
					"enabled" : "disabled");
		} else {
50 51 52 53 54 55 56 57 58 59 60 61
			struct device *ldev;
			list_for_each_entry(entry, &dev->physical_node_list,
					node) {
				ldev = get_device(entry->dev);
				if (!ldev)
					continue;

				if (&entry->node !=
						dev->physical_node_list.next)
					seq_printf(seq, "\t\t");

				seq_printf(seq, "%c%-8s  %s:%s\n",
62
					dev->wakeup.flags.valid ? '*' : ' ',
63
					(device_may_wakeup(&dev->dev) ||
64
					device_may_wakeup(ldev)) ?
65 66 67 68 69 70
					"enabled" : "disabled",
					ldev->bus ? ldev->bus->name :
					"no-bus", dev_name(ldev));
				put_device(ldev);
			}
		}
71 72

		mutex_unlock(&dev->physical_node_lock);
Linus Torvalds's avatar
Linus Torvalds committed
73
	}
74
	mutex_unlock(&acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77
	return 0;
}

78 79
static void physical_device_enable_wakeup(struct acpi_device *adev)
{
80
	struct acpi_device_physical_node *entry;
81

82 83
	mutex_lock(&adev->physical_node_lock);

84 85 86 87 88 89
	list_for_each_entry(entry,
		&adev->physical_node_list, node)
		if (entry->dev && device_can_wakeup(entry->dev)) {
			bool enable = !device_may_wakeup(entry->dev);
			device_set_wakeup_enable(entry->dev, enable);
		}
90 91

	mutex_unlock(&adev->physical_node_lock);
92 93
}

Linus Torvalds's avatar
Linus Torvalds committed
94
static ssize_t
Len Brown's avatar
Len Brown committed
95 96 97
acpi_system_write_wakeup_device(struct file *file,
				const char __user * buffer,
				size_t count, loff_t * ppos)
Linus Torvalds's avatar
Linus Torvalds committed
98
{
Len Brown's avatar
Len Brown committed
99 100 101
	struct list_head *node, *next;
	char strbuf[5];
	char str[5] = "";
Linus Torvalds's avatar
Linus Torvalds committed
102

103 104
	if (count > 4)
		count = 4;
Linus Torvalds's avatar
Linus Torvalds committed
105

106
	if (copy_from_user(strbuf, buffer, count))
Linus Torvalds's avatar
Linus Torvalds committed
107
		return -EFAULT;
108
	strbuf[count] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
109 110
	sscanf(strbuf, "%s", str);

111
	mutex_lock(&acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
112
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown's avatar
Len Brown committed
113 114
		struct acpi_device *dev =
		    container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118
		if (!dev->wakeup.flags.valid)
			continue;

		if (!strncmp(dev->pnp.bus_id, str, 4)) {
119 120 121 122 123 124
			if (device_can_wakeup(&dev->dev)) {
				bool enable = !device_may_wakeup(&dev->dev);
				device_set_wakeup_enable(&dev->dev, enable);
			} else {
				physical_device_enable_wakeup(dev);
			}
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127
			break;
		}
	}
128
	mutex_unlock(&acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
129 130 131 132 133 134
	return count;
}

static int
acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
{
Len Brown's avatar
Len Brown committed
135
	return single_open(file, acpi_system_wakeup_device_seq_show,
136
			   PDE_DATA(inode));
Linus Torvalds's avatar
Linus Torvalds committed
137 138
}

139
static const struct file_operations acpi_system_wakeup_device_fops = {
140
	.owner = THIS_MODULE,
Len Brown's avatar
Len Brown committed
141 142 143 144 145
	.open = acpi_system_wakeup_device_open_fs,
	.read = seq_read,
	.write = acpi_system_write_wakeup_device,
	.llseek = seq_lseek,
	.release = single_release,
Linus Torvalds's avatar
Linus Torvalds committed
146 147
};

148
void __init acpi_sleep_proc_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
149
{
Pavel Machek's avatar
Pavel Machek committed
150
	/* 'wakeup device' [R/W] */
151 152
	proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
		    acpi_root_dir, &acpi_system_wakeup_device_fops);
Linus Torvalds's avatar
Linus Torvalds committed
153
}