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