Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
H Hartley Sweeten | 37cce26 | 2011-09-21 22:47:55 +0200 | [diff] [blame] | 3 | * Functions for saving/restoring console. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Originally from swsusp. |
| 6 | */ |
| 7 | |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 8 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/vt_kern.h> |
| 10 | #include <linux/kbd_kern.h> |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 11 | #include <linux/vt.h> |
Andres Salomon | b6f448e | 2008-04-28 02:15:03 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Tejun Heo | 1ff6bbf | 2014-01-28 18:10:37 -0500 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "power.h" |
| 15 | |
Rafael J. Wysocki | 46cd2f3 | 2006-02-07 12:58:50 -0800 | [diff] [blame] | 16 | #define SUSPEND_CONSOLE (MAX_NR_CONSOLES-1) |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | static int orig_fgconsole, orig_kmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 20 | static DEFINE_MUTEX(vt_switch_mutex); |
| 21 | |
| 22 | struct pm_vt_switch { |
| 23 | struct list_head head; |
| 24 | struct device *dev; |
| 25 | bool required; |
| 26 | }; |
| 27 | |
| 28 | static LIST_HEAD(pm_vt_switch_list); |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * pm_vt_switch_required - indicate VT switch at suspend requirements |
| 33 | * @dev: device |
| 34 | * @required: if true, caller needs VT switch at suspend/resume time |
| 35 | * |
| 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. |
| 39 | * |
| 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. |
| 45 | */ |
| 46 | void 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 | /* already registered, update requirement */ |
| 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); |
| 67 | out: |
| 68 | mutex_unlock(&vt_switch_mutex); |
| 69 | } |
| 70 | EXPORT_SYMBOL(pm_vt_switch_required); |
| 71 | |
| 72 | /** |
| 73 | * pm_vt_switch_unregister - stop tracking a device's VT switching needs |
| 74 | * @dev: device |
| 75 | * |
| 76 | * Remove @dev from the vt switch list. |
| 77 | */ |
| 78 | void 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); |
Masami Ichikawa | c606850 | 2013-12-19 20:00:47 +0900 | [diff] [blame] | 86 | kfree(tmp); |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
| 90 | mutex_unlock(&vt_switch_mutex); |
| 91 | } |
| 92 | EXPORT_SYMBOL(pm_vt_switch_unregister); |
| 93 | |
| 94 | /* |
| 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 |
| 97 | * the old behavior |
| 98 | * 2) console suspend is disabled, we want to see debug messages across |
| 99 | * suspend/resume |
| 100 | * 3) any registered driver indicates it needs a VT switch |
| 101 | * |
| 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). |
| 106 | */ |
| 107 | static 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; |
| 125 | out: |
| 126 | mutex_unlock(&vt_switch_mutex); |
| 127 | return ret; |
| 128 | } |
| 129 | |
Borislav Petkov | ca5f2b4 | 2016-06-14 16:23:22 +0200 | [diff] [blame] | 130 | void pm_prepare_console(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 132 | if (!pm_vt_switch()) |
Borislav Petkov | ca5f2b4 | 2016-06-14 16:23:22 +0200 | [diff] [blame] | 133 | return; |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 134 | |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 135 | orig_fgconsole = vt_move_to_console(SUSPEND_CONSOLE, 1); |
| 136 | if (orig_fgconsole < 0) |
Borislav Petkov | ca5f2b4 | 2016-06-14 16:23:22 +0200 | [diff] [blame] | 137 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 139 | orig_kmsg = vt_kmsg_redirect(SUSPEND_CONSOLE); |
Borislav Petkov | ca5f2b4 | 2016-06-14 16:23:22 +0200 | [diff] [blame] | 140 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void pm_restore_console(void) |
| 144 | { |
Jesse Barnes | f43f627 | 2013-02-04 13:37:20 +0000 | [diff] [blame] | 145 | if (!pm_vt_switch()) |
| 146 | return; |
| 147 | |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 148 | if (orig_fgconsole >= 0) { |
| 149 | vt_move_to_console(orig_fgconsole, 0); |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 150 | vt_kmsg_redirect(orig_kmsg); |
Andres Salomon | b6f448e | 2008-04-28 02:15:03 -0700 | [diff] [blame] | 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |