1 // SPDX-License-Identifier: GPL-2.0
3 * Functions for saving/restoring console.
5 * Originally from swsusp.
8 #include <linux/console.h>
9 #include <linux/vt_kern.h>
10 #include <linux/kbd_kern.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
16 #define SUSPEND_CONSOLE (MAX_NR_CONSOLES-1)
18 static int orig_fgconsole
, orig_kmsg
;
20 static DEFINE_MUTEX(vt_switch_mutex
);
23 struct list_head head
;
28 static LIST_HEAD(pm_vt_switch_list
);
32 * pm_vt_switch_required - indicate VT switch at suspend requirements
34 * @required: if true, caller needs VT switch at suspend/resume time
36 * The different console drivers may or may not require VT switches across
37 * suspend/resume, depending on how they handle restoring video state and
38 * what may be running.
40 * Drivers can indicate support for switchless suspend/resume, which can
41 * save time and flicker, by using this routine and passing 'false' as
42 * the argument. If any loaded driver needs VT switching, or the
43 * no_console_suspend argument has been passed on the command line, VT
44 * switches will occur.
46 void pm_vt_switch_required(struct device
*dev
, bool required
)
48 struct pm_vt_switch
*entry
, *tmp
;
50 mutex_lock(&vt_switch_mutex
);
51 list_for_each_entry(tmp
, &pm_vt_switch_list
, head
) {
52 if (tmp
->dev
== dev
) {
53 /* already registered, update requirement */
54 tmp
->required
= required
;
59 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
63 entry
->required
= required
;
66 list_add(&entry
->head
, &pm_vt_switch_list
);
68 mutex_unlock(&vt_switch_mutex
);
70 EXPORT_SYMBOL(pm_vt_switch_required
);
73 * pm_vt_switch_unregister - stop tracking a device's VT switching needs
76 * Remove @dev from the vt switch list.
78 void pm_vt_switch_unregister(struct device
*dev
)
80 struct pm_vt_switch
*tmp
;
82 mutex_lock(&vt_switch_mutex
);
83 list_for_each_entry(tmp
, &pm_vt_switch_list
, head
) {
84 if (tmp
->dev
== dev
) {
90 mutex_unlock(&vt_switch_mutex
);
92 EXPORT_SYMBOL(pm_vt_switch_unregister
);
95 * There are three cases when a VT switch on suspend/resume are required:
96 * 1) no driver has indicated a requirement one way or another, so preserve
98 * 2) console suspend is disabled, we want to see debug messages across
100 * 3) any registered driver indicates it needs a VT switch
102 * If none of these conditions is present, meaning we have at least one driver
103 * that doesn't need the switch, and none that do, we can avoid it to make
104 * resume look a little prettier (and suspend too, but that's usually hidden,
105 * e.g. when closing the lid on a laptop).
107 static bool pm_vt_switch(void)
109 struct pm_vt_switch
*entry
;
112 mutex_lock(&vt_switch_mutex
);
113 if (list_empty(&pm_vt_switch_list
))
116 if (!console_suspend_enabled
)
119 list_for_each_entry(entry
, &pm_vt_switch_list
, head
) {
126 mutex_unlock(&vt_switch_mutex
);
130 void pm_prepare_console(void)
135 orig_fgconsole
= vt_move_to_console(SUSPEND_CONSOLE
, 1);
136 if (orig_fgconsole
< 0)
139 orig_kmsg
= vt_kmsg_redirect(SUSPEND_CONSOLE
);
143 void pm_restore_console(void)
148 if (orig_fgconsole
>= 0) {
149 vt_move_to_console(orig_fgconsole
, 0);
150 vt_kmsg_redirect(orig_kmsg
);