1
2
3
4
5
6
7
8#include <linux/console.h>
9#include <linux/vt_kern.h>
10#include <linux/kbd_kern.h>
11#include <linux/vt.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include "power.h"
15
16#define SUSPEND_CONSOLE (MAX_NR_CONSOLES-1)
17
18static int orig_fgconsole, orig_kmsg;
19
20static DEFINE_MUTEX(vt_switch_mutex);
21
22struct pm_vt_switch {
23 struct list_head head;
24 struct device *dev;
25 bool required;
26};
27
28static LIST_HEAD(pm_vt_switch_list);
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46void pm_vt_switch_required(struct device *dev, bool required)
47{
48 struct pm_vt_switch *entry, *tmp;
49
50 mutex_lock(&vt_switch_mutex);
51 list_for_each_entry(tmp, &pm_vt_switch_list, head) {
52 if (tmp->dev == dev) {
53
54 tmp->required = required;
55 goto out;
56 }
57 }
58
59 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
60 if (!entry)
61 goto out;
62
63 entry->required = required;
64 entry->dev = dev;
65
66 list_add(&entry->head, &pm_vt_switch_list);
67out:
68 mutex_unlock(&vt_switch_mutex);
69}
70EXPORT_SYMBOL(pm_vt_switch_required);
71
72
73
74
75
76
77
78void pm_vt_switch_unregister(struct device *dev)
79{
80 struct pm_vt_switch *tmp;
81
82 mutex_lock(&vt_switch_mutex);
83 list_for_each_entry(tmp, &pm_vt_switch_list, head) {
84 if (tmp->dev == dev) {
85 list_del(&tmp->head);
86 kfree(tmp);
87 break;
88 }
89 }
90 mutex_unlock(&vt_switch_mutex);
91}
92EXPORT_SYMBOL(pm_vt_switch_unregister);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107static bool pm_vt_switch(void)
108{
109 struct pm_vt_switch *entry;
110 bool ret = true;
111
112 mutex_lock(&vt_switch_mutex);
113 if (list_empty(&pm_vt_switch_list))
114 goto out;
115
116 if (!console_suspend_enabled)
117 goto out;
118
119 list_for_each_entry(entry, &pm_vt_switch_list, head) {
120 if (entry->required)
121 goto out;
122 }
123
124 ret = false;
125out:
126 mutex_unlock(&vt_switch_mutex);
127 return ret;
128}
129
130void pm_prepare_console(void)
131{
132 if (!pm_vt_switch())
133 return;
134
135 orig_fgconsole = vt_move_to_console(SUSPEND_CONSOLE, 1);
136 if (orig_fgconsole < 0)
137 return;
138
139 orig_kmsg = vt_kmsg_redirect(SUSPEND_CONSOLE);
140 return;
141}
142
143void pm_restore_console(void)
144{
145 if (!pm_vt_switch())
146 return;
147
148 if (orig_fgconsole >= 0) {
149 vt_move_to_console(orig_fgconsole, 0);
150 vt_kmsg_redirect(orig_kmsg);
151 }
152}
153