Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1992 obz under the linux copyright |
| 3 | * |
| 4 | * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993 |
| 5 | * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994 |
| 6 | * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995 |
| 7 | * Some code moved for less code duplication - Andi Kleen - Mar 1997 |
| 8 | * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001 |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/tty.h> |
| 15 | #include <linux/timer.h> |
| 16 | #include <linux/kernel.h> |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 17 | #include <linux/compat.h> |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kd.h> |
| 20 | #include <linux/vt.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/major.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/console.h> |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 26 | #include <linux/consolemap.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 27 | #include <linux/signal.h> |
Josh Triplett | a5e04b7 | 2012-11-18 21:27:41 -0800 | [diff] [blame] | 28 | #include <linux/suspend.h> |
Emmanuel Colbus | bcc8ca0 | 2005-06-28 20:44:49 -0700 | [diff] [blame] | 29 | #include <linux/timex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | |
Gustavo A. R. Silva | 0ec459e | 2018-08-16 15:30:38 -0500 | [diff] [blame] | 34 | #include <linux/nospec.h> |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/kbd_kern.h> |
| 37 | #include <linux/vt_kern.h> |
| 38 | #include <linux/kbd_diacr.h> |
| 39 | #include <linux/selection.h> |
| 40 | |
Jiri Slaby | 2e1c84e | 2020-02-19 08:39:48 +0100 | [diff] [blame] | 41 | bool vt_dont_switch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jiri Slaby | 8c3bc92d | 2020-02-19 08:39:44 +0100 | [diff] [blame] | 43 | static inline bool vt_in_use(unsigned int i) |
| 44 | { |
| 45 | extern struct tty_driver *console_driver; |
| 46 | |
| 47 | return console_driver->ttys[i] && console_driver->ttys[i]->count; |
| 48 | } |
| 49 | |
| 50 | static inline bool vt_busy(int i) |
| 51 | { |
| 52 | if (vt_in_use(i)) |
| 53 | return true; |
| 54 | if (i == fg_console) |
| 55 | return true; |
| 56 | if (vc_is_sel(vc_cons[i].d)) |
| 57 | return true; |
| 58 | |
| 59 | return false; |
| 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Console (vt and kd) routines, as defined by USL SVR4 manual, and by |
| 64 | * experimentation and study of X386 SYSV handling. |
| 65 | * |
| 66 | * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and |
| 67 | * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console, |
| 68 | * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will |
| 69 | * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to |
| 70 | * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using |
| 71 | * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing |
| 72 | * to the current console is done by the main ioctl code. |
| 73 | */ |
| 74 | |
| 75 | #ifdef CONFIG_X86 |
| 76 | #include <linux/syscalls.h> |
| 77 | #endif |
| 78 | |
| 79 | static void complete_change_console(struct vc_data *vc); |
| 80 | |
| 81 | /* |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 82 | * User space VT_EVENT handlers |
| 83 | */ |
| 84 | |
| 85 | struct vt_event_wait { |
| 86 | struct list_head list; |
| 87 | struct vt_event event; |
| 88 | int done; |
| 89 | }; |
| 90 | |
| 91 | static LIST_HEAD(vt_events); |
| 92 | static DEFINE_SPINLOCK(vt_event_lock); |
| 93 | static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue); |
| 94 | |
| 95 | /** |
| 96 | * vt_event_post |
| 97 | * @event: the event that occurred |
| 98 | * @old: old console |
| 99 | * @new: new console |
| 100 | * |
| 101 | * Post an VT event to interested VT handlers |
| 102 | */ |
| 103 | |
| 104 | void vt_event_post(unsigned int event, unsigned int old, unsigned int new) |
| 105 | { |
| 106 | struct list_head *pos, *head; |
| 107 | unsigned long flags; |
| 108 | int wake = 0; |
| 109 | |
| 110 | spin_lock_irqsave(&vt_event_lock, flags); |
| 111 | head = &vt_events; |
| 112 | |
| 113 | list_for_each(pos, head) { |
| 114 | struct vt_event_wait *ve = list_entry(pos, |
| 115 | struct vt_event_wait, list); |
| 116 | if (!(ve->event.event & event)) |
| 117 | continue; |
| 118 | ve->event.event = event; |
| 119 | /* kernel view is consoles 0..n-1, user space view is |
| 120 | console 1..n with 0 meaning current, so we must bias */ |
Alan Cox | 308efab | 2009-11-19 13:30:36 +0000 | [diff] [blame] | 121 | ve->event.oldev = old + 1; |
| 122 | ve->event.newev = new + 1; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 123 | wake = 1; |
| 124 | ve->done = 1; |
| 125 | } |
| 126 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 127 | if (wake) |
| 128 | wake_up_interruptible(&vt_event_waitqueue); |
| 129 | } |
| 130 | |
Rabin Vincent | a7b1292 | 2012-05-21 13:38:42 +0530 | [diff] [blame] | 131 | static void __vt_event_queue(struct vt_event_wait *vw) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | /* Prepare the event */ |
| 135 | INIT_LIST_HEAD(&vw->list); |
| 136 | vw->done = 0; |
| 137 | /* Queue our event */ |
| 138 | spin_lock_irqsave(&vt_event_lock, flags); |
| 139 | list_add(&vw->list, &vt_events); |
| 140 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 141 | } |
| 142 | |
| 143 | static void __vt_event_wait(struct vt_event_wait *vw) |
| 144 | { |
| 145 | /* Wait for it to pass */ |
| 146 | wait_event_interruptible(vt_event_waitqueue, vw->done); |
| 147 | } |
| 148 | |
| 149 | static void __vt_event_dequeue(struct vt_event_wait *vw) |
| 150 | { |
| 151 | unsigned long flags; |
| 152 | |
| 153 | /* Dequeue it */ |
| 154 | spin_lock_irqsave(&vt_event_lock, flags); |
| 155 | list_del(&vw->list); |
| 156 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 157 | } |
| 158 | |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 159 | /** |
| 160 | * vt_event_wait - wait for an event |
| 161 | * @vw: our event |
| 162 | * |
| 163 | * Waits for an event to occur which completes our vt_event_wait |
| 164 | * structure. On return the structure has wv->done set to 1 for success |
| 165 | * or 0 if some event such as a signal ended the wait. |
| 166 | */ |
| 167 | |
| 168 | static void vt_event_wait(struct vt_event_wait *vw) |
| 169 | { |
Rabin Vincent | a7b1292 | 2012-05-21 13:38:42 +0530 | [diff] [blame] | 170 | __vt_event_queue(vw); |
| 171 | __vt_event_wait(vw); |
| 172 | __vt_event_dequeue(vw); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * vt_event_wait_ioctl - event ioctl handler |
| 177 | * @arg: argument to ioctl |
| 178 | * |
| 179 | * Implement the VT_WAITEVENT ioctl using the VT event interface |
| 180 | */ |
| 181 | |
| 182 | static int vt_event_wait_ioctl(struct vt_event __user *event) |
| 183 | { |
| 184 | struct vt_event_wait vw; |
| 185 | |
| 186 | if (copy_from_user(&vw.event, event, sizeof(struct vt_event))) |
| 187 | return -EFAULT; |
| 188 | /* Highest supported event for now */ |
| 189 | if (vw.event.event & ~VT_MAX_EVENT) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | vt_event_wait(&vw); |
| 193 | /* If it occurred report it */ |
| 194 | if (vw.done) { |
| 195 | if (copy_to_user(event, &vw.event, sizeof(struct vt_event))) |
| 196 | return -EFAULT; |
| 197 | return 0; |
| 198 | } |
| 199 | return -EINTR; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * vt_waitactive - active console wait |
| 204 | * @event: event code |
| 205 | * @n: new console |
| 206 | * |
| 207 | * Helper for event waits. Used to implement the legacy |
| 208 | * event waiting ioctls in terms of events |
| 209 | */ |
| 210 | |
| 211 | int vt_waitactive(int n) |
| 212 | { |
| 213 | struct vt_event_wait vw; |
| 214 | do { |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 215 | vw.event.event = VT_EVENT_SWITCH; |
Rabin Vincent | a7b1292 | 2012-05-21 13:38:42 +0530 | [diff] [blame] | 216 | __vt_event_queue(&vw); |
| 217 | if (n == fg_console + 1) { |
| 218 | __vt_event_dequeue(&vw); |
| 219 | break; |
| 220 | } |
| 221 | __vt_event_wait(&vw); |
| 222 | __vt_event_dequeue(&vw); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 223 | if (vw.done == 0) |
| 224 | return -EINTR; |
Alan Cox | 308efab | 2009-11-19 13:30:36 +0000 | [diff] [blame] | 225 | } while (vw.event.newev != n); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * these are the valid i/o ports we're allowed to change. they map all the |
| 231 | * video ports |
| 232 | */ |
| 233 | #define GPFIRST 0x3b4 |
| 234 | #define GPLAST 0x3df |
| 235 | #define GPNUM (GPLAST - GPFIRST + 1) |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | static inline int |
| 240 | do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op) |
| 241 | { |
| 242 | struct consolefontdesc cfdarg; |
| 243 | int i; |
| 244 | |
| 245 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) |
| 246 | return -EFAULT; |
| 247 | |
| 248 | switch (cmd) { |
| 249 | case PIO_FONTX: |
| 250 | if (!perm) |
| 251 | return -EPERM; |
| 252 | op->op = KD_FONT_OP_SET; |
| 253 | op->flags = KD_FONT_FLAG_OLD; |
| 254 | op->width = 8; |
| 255 | op->height = cfdarg.charheight; |
| 256 | op->charcount = cfdarg.charcount; |
| 257 | op->data = cfdarg.chardata; |
| 258 | return con_font_op(vc_cons[fg_console].d, op); |
| 259 | case GIO_FONTX: { |
| 260 | op->op = KD_FONT_OP_GET; |
| 261 | op->flags = KD_FONT_FLAG_OLD; |
| 262 | op->width = 8; |
| 263 | op->height = cfdarg.charheight; |
| 264 | op->charcount = cfdarg.charcount; |
| 265 | op->data = cfdarg.chardata; |
| 266 | i = con_font_op(vc_cons[fg_console].d, op); |
| 267 | if (i) |
| 268 | return i; |
| 269 | cfdarg.charheight = op->height; |
| 270 | cfdarg.charcount = op->charcount; |
| 271 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc))) |
| 272 | return -EFAULT; |
| 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
| 279 | static inline int |
| 280 | do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc) |
| 281 | { |
| 282 | struct unimapdesc tmp; |
| 283 | |
| 284 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) |
| 285 | return -EFAULT; |
| 286 | if (tmp.entries) |
| 287 | if (!access_ok(VERIFY_WRITE, tmp.entries, |
| 288 | tmp.entry_ct*sizeof(struct unipair))) |
| 289 | return -EFAULT; |
| 290 | switch (cmd) { |
| 291 | case PIO_UNIMAP: |
| 292 | if (!perm) |
| 293 | return -EPERM; |
| 294 | return con_set_unimap(vc, tmp.entry_ct, tmp.entries); |
| 295 | case GIO_UNIMAP: |
| 296 | if (!perm && fg_console != vc->vc_num) |
| 297 | return -EPERM; |
| 298 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries); |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 303 | /* deallocate a single console, if possible (leave 0) */ |
| 304 | static int vt_disallocate(unsigned int vc_num) |
| 305 | { |
| 306 | struct vc_data *vc = NULL; |
| 307 | int ret = 0; |
| 308 | |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 309 | console_lock(); |
Jiri Slaby | 8c3bc92d | 2020-02-19 08:39:44 +0100 | [diff] [blame] | 310 | if (vt_busy(vc_num)) |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 311 | ret = -EBUSY; |
Ross Lagerwall | ef223fb | 2013-06-14 23:24:25 +0100 | [diff] [blame] | 312 | else if (vc_num) |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 313 | vc = vc_deallocate(vc_num); |
| 314 | console_unlock(); |
| 315 | |
Eric Biggers | 6bc9bf7 | 2020-03-21 20:43:04 -0700 | [diff] [blame^] | 316 | if (vc && vc_num >= MIN_NR_CONSOLES) |
| 317 | tty_port_put(&vc->port); |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* deallocate all unused consoles, but leave 0 */ |
| 323 | static void vt_disallocate_all(void) |
| 324 | { |
| 325 | struct vc_data *vc[MAX_NR_CONSOLES]; |
| 326 | int i; |
| 327 | |
| 328 | console_lock(); |
| 329 | for (i = 1; i < MAX_NR_CONSOLES; i++) |
Jiri Slaby | 8c3bc92d | 2020-02-19 08:39:44 +0100 | [diff] [blame] | 330 | if (!vt_busy(i)) |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 331 | vc[i] = vc_deallocate(i); |
| 332 | else |
| 333 | vc[i] = NULL; |
| 334 | console_unlock(); |
| 335 | |
| 336 | for (i = 1; i < MAX_NR_CONSOLES; i++) { |
Eric Biggers | 6bc9bf7 | 2020-03-21 20:43:04 -0700 | [diff] [blame^] | 337 | if (vc[i] && i >= MIN_NR_CONSOLES) |
| 338 | tty_port_put(&vc[i]->port); |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 341 | |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | /* |
| 344 | * We handle the console-specific ioctl's here. We allow the |
| 345 | * capability to modify any console, not just the fg_console. |
| 346 | */ |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 347 | int vt_ioctl(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | unsigned int cmd, unsigned long arg) |
| 349 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 350 | struct vc_data *vc = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct console_font_op op; /* used in multiple places here */ |
Eric Biggers | 35d6480 | 2020-02-24 00:03:26 -0800 | [diff] [blame] | 352 | unsigned int console = vc->vc_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | unsigned char ucval; |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 354 | unsigned int uival; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | void __user *up = (void __user *)arg; |
| 356 | int i, perm; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 357 | int ret = 0; |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * To have permissions to do most of the vt ioctls, we either have |
| 361 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. |
| 362 | */ |
| 363 | perm = 0; |
| 364 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) |
| 365 | perm = 1; |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | switch (cmd) { |
Alan Cox | e688510 | 2008-10-13 10:36:40 +0100 | [diff] [blame] | 368 | case TIOCLINUX: |
Jiri Slaby | a115902 | 2009-06-22 18:42:18 +0100 | [diff] [blame] | 369 | ret = tioclinux(tty, arg); |
| 370 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | case KIOCSOUND: |
| 372 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 373 | return -EPERM; |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 374 | /* |
| 375 | * The use of PIT_TICK_RATE is historic, it used to be |
| 376 | * the platform-dependent CLOCK_TICK_RATE between 2.6.12 |
| 377 | * and 2.6.36, which was a minor but unfortunate ABI |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 378 | * change. kd_mksound is locked by the input layer. |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 379 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (arg) |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 381 | arg = PIT_TICK_RATE / arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | kd_mksound(arg, 0); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 383 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | case KDMKTONE: |
| 386 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 387 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
| 389 | unsigned int ticks, count; |
| 390 | |
| 391 | /* |
| 392 | * Generate the tone for the appropriate number of ticks. |
| 393 | * If the time is zero, turn off sound ourselves. |
| 394 | */ |
Nicholas Mc Guire | 4c0d9b1 | 2015-02-09 10:53:25 -0500 | [diff] [blame] | 395 | ticks = msecs_to_jiffies((arg >> 16) & 0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | count = ticks ? (arg & 0xffff) : 0; |
| 397 | if (count) |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 398 | count = PIT_TICK_RATE / count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | kd_mksound(count, ticks); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 400 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | case KDGKBTYPE: |
| 404 | /* |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 405 | * this is naïve. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | */ |
| 407 | ucval = KB_101; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 408 | ret = put_user(ucval, (char __user *)arg); |
| 409 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * These cannot be implemented on any machine that implements |
| 413 | * ioperm() in user level (such as Alpha PCs) or not at all. |
| 414 | * |
| 415 | * XXX: you should never use these, just call ioperm directly.. |
| 416 | */ |
| 417 | #ifdef CONFIG_X86 |
| 418 | case KDADDIO: |
| 419 | case KDDELIO: |
| 420 | /* |
| 421 | * KDADDIO and KDDELIO may be able to add ports beyond what |
| 422 | * we reject here, but to be safe... |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 423 | * |
| 424 | * These are locked internally via sys_ioperm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 426 | if (arg < GPFIRST || arg > GPLAST) { |
| 427 | ret = -EINVAL; |
| 428 | break; |
| 429 | } |
| 430 | ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0; |
| 431 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | case KDENABIO: |
| 434 | case KDDISABIO: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 435 | ret = sys_ioperm(GPFIRST, GPNUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | (cmd == KDENABIO)) ? -ENXIO : 0; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 437 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | #endif |
| 439 | |
| 440 | /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */ |
| 441 | |
| 442 | case KDKBDREP: |
| 443 | { |
| 444 | struct kbd_repeat kbrep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 447 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 449 | if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) { |
| 450 | ret = -EFAULT; |
| 451 | break; |
| 452 | } |
| 453 | ret = kbd_rate(&kbrep); |
| 454 | if (ret) |
| 455 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat))) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 457 | ret = -EFAULT; |
| 458 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | case KDSETMODE: |
| 462 | /* |
| 463 | * currently, setting the mode from KD_TEXT to KD_GRAPHICS |
| 464 | * doesn't do a whole lot. i'm not sure if it should do any |
| 465 | * restoration of modes or what... |
| 466 | * |
| 467 | * XXX It should at least call into the driver, fbdev's definitely |
| 468 | * need to restore their engine state. --BenH |
| 469 | */ |
| 470 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 471 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | switch (arg) { |
| 473 | case KD_GRAPHICS: |
| 474 | break; |
| 475 | case KD_TEXT0: |
| 476 | case KD_TEXT1: |
| 477 | arg = KD_TEXT; |
| 478 | case KD_TEXT: |
| 479 | break; |
| 480 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 481 | ret = -EINVAL; |
| 482 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 484 | /* FIXME: this needs the console lock extending */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | if (vc->vc_mode == (unsigned char) arg) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 486 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | vc->vc_mode = (unsigned char) arg; |
| 488 | if (console != fg_console) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 489 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* |
| 491 | * explicitly blank/unblank the screen if switching modes |
| 492 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 493 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (arg == KD_TEXT) |
| 495 | do_unblank_screen(1); |
| 496 | else |
| 497 | do_blank_screen(1); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 498 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 499 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | case KDGETMODE: |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 502 | uival = vc->vc_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | goto setint; |
| 504 | |
| 505 | case KDMAPDISP: |
| 506 | case KDUNMAPDISP: |
| 507 | /* |
| 508 | * these work like a combination of mmap and KDENABIO. |
| 509 | * this could be easily finished. |
| 510 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 511 | ret = -EINVAL; |
| 512 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | case KDSKBMODE: |
| 515 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 516 | return -EPERM; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 517 | ret = vt_do_kdskbmode(console, arg); |
| 518 | if (ret == 0) |
| 519 | tty_ldisc_flush(tty); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 520 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | case KDGKBMODE: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 523 | uival = vt_do_kdgkbmode(console); |
| 524 | ret = put_user(uival, (int __user *)arg); |
| 525 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | /* this could be folded into KDSKBMODE, but for compatibility |
| 528 | reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */ |
| 529 | case KDSKBMETA: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 530 | ret = vt_do_kdskbmeta(console, arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 531 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
| 533 | case KDGKBMETA: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 534 | /* FIXME: should review whether this is worth locking */ |
| 535 | uival = vt_do_kdgkbmeta(console); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | setint: |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 537 | ret = put_user(uival, (int __user *)arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 538 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | case KDGETKEYCODE: |
| 541 | case KDSETKEYCODE: |
| 542 | if(!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 543 | perm = 0; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 544 | ret = vt_do_kbkeycode_ioctl(cmd, up, perm); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 545 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | case KDGKBENT: |
| 548 | case KDSKBENT: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 549 | ret = vt_do_kdsk_ioctl(cmd, up, perm, console); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 550 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | case KDGKBSENT: |
| 553 | case KDSKBSENT: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 554 | ret = vt_do_kdgkb_ioctl(cmd, up, perm); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 555 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Alan Cox | 247ff8e | 2012-02-24 12:47:11 +0000 | [diff] [blame] | 557 | /* Diacritical processing. Handled in keyboard.c as it has |
| 558 | to operate on the keyboard locks and structures */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | case KDGKBDIACR: |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 560 | case KDGKBDIACRUC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | case KDSKBDIACR: |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 562 | case KDSKBDIACRUC: |
Alan Cox | 247ff8e | 2012-02-24 12:47:11 +0000 | [diff] [blame] | 563 | ret = vt_do_diacrit(cmd, up, perm); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 564 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | /* the ioctls below read/set the flags usually shown in the leds */ |
| 567 | /* don't use them - they will go away without warning */ |
| 568 | case KDGKBLED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | case KDSKBLED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | case KDGETLED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | case KDSETLED: |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 572 | ret = vt_do_kdskled(console, cmd, arg, perm); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 573 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * A process can indicate its willingness to accept signals |
| 577 | * generated by pressing an appropriate key combination. |
| 578 | * Thus, one can have a daemon that e.g. spawns a new console |
| 579 | * upon a keypress and then changes to it. |
| 580 | * See also the kbrequest field of inittab(5). |
| 581 | */ |
| 582 | case KDSIGACCEPT: |
| 583 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | if (!perm || !capable(CAP_KILL)) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 585 | return -EPERM; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 586 | if (!valid_signal(arg) || arg < 1 || arg == SIGKILL) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 587 | ret = -EINVAL; |
| 588 | else { |
| 589 | spin_lock_irq(&vt_spawn_con.lock); |
| 590 | put_pid(vt_spawn_con.pid); |
| 591 | vt_spawn_con.pid = get_pid(task_pid(current)); |
| 592 | vt_spawn_con.sig = arg; |
| 593 | spin_unlock_irq(&vt_spawn_con.lock); |
| 594 | } |
| 595 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | case VT_SETMODE: |
| 599 | { |
| 600 | struct vt_mode tmp; |
| 601 | |
| 602 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 603 | return -EPERM; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 604 | if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) { |
| 605 | ret = -EFAULT; |
| 606 | goto out; |
| 607 | } |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 608 | if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 609 | ret = -EINVAL; |
| 610 | goto out; |
| 611 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 612 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | vc->vt_mode = tmp; |
| 614 | /* the frsig is ignored, so we set it to 0 */ |
| 615 | vc->vt_mode.frsig = 0; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 616 | put_pid(vc->vt_pid); |
| 617 | vc->vt_pid = get_pid(task_pid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | /* no switch is required -- saw@shade.msu.ru */ |
| 619 | vc->vt_newvt = -1; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 620 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 621 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | case VT_GETMODE: |
| 625 | { |
| 626 | struct vt_mode tmp; |
| 627 | int rc; |
| 628 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 629 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 631 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 634 | if (rc) |
| 635 | ret = -EFAULT; |
| 636 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Returns global vt state. Note that VT 0 is always open, since |
| 641 | * it's an alias for the current VT, and people can't use it here. |
| 642 | * We cannot return state for more than 16 VTs, since v_state is short. |
| 643 | */ |
| 644 | case VT_GETSTATE: |
| 645 | { |
| 646 | struct vt_stat __user *vtstat = up; |
| 647 | unsigned short state, mask; |
| 648 | |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 649 | /* Review: FIXME: Console lock ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (put_user(fg_console + 1, &vtstat->v_active)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 651 | ret = -EFAULT; |
| 652 | else { |
| 653 | state = 1; /* /dev/tty0 is always open */ |
| 654 | for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; |
| 655 | ++i, mask <<= 1) |
Jiri Slaby | 8c3bc92d | 2020-02-19 08:39:44 +0100 | [diff] [blame] | 656 | if (vt_in_use(i)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 657 | state |= mask; |
| 658 | ret = put_user(state, &vtstat->v_state); |
| 659 | } |
| 660 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Returns the first available (non-opened) console. |
| 665 | */ |
| 666 | case VT_OPENQRY: |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 667 | /* FIXME: locking ? - but then this is a stupid API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | for (i = 0; i < MAX_NR_CONSOLES; ++i) |
Jiri Slaby | 8c3bc92d | 2020-02-19 08:39:44 +0100 | [diff] [blame] | 669 | if (!vt_in_use(i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | break; |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 671 | uival = i < MAX_NR_CONSOLES ? (i+1) : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | goto setint; |
| 673 | |
| 674 | /* |
| 675 | * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num, |
| 676 | * with num >= 1 (switches to vt 0, our console, are not allowed, just |
| 677 | * to preserve sanity). |
| 678 | */ |
| 679 | case VT_ACTIVATE: |
| 680 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 681 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (arg == 0 || arg > MAX_NR_CONSOLES) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 683 | ret = -ENXIO; |
| 684 | else { |
| 685 | arg--; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 686 | console_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 687 | ret = vc_allocate(arg); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 688 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 689 | if (ret) |
| 690 | break; |
| 691 | set_console(arg); |
| 692 | } |
| 693 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 695 | case VT_SETACTIVATE: |
| 696 | { |
| 697 | struct vt_setactivate vsa; |
| 698 | |
| 699 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 700 | return -EPERM; |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 701 | |
| 702 | if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg, |
Jiri Slaby | a09efb0 | 2009-10-01 15:43:59 -0700 | [diff] [blame] | 703 | sizeof(struct vt_setactivate))) { |
| 704 | ret = -EFAULT; |
| 705 | goto out; |
| 706 | } |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 707 | if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES) |
| 708 | ret = -ENXIO; |
| 709 | else { |
Gustavo A. R. Silva | 0ec459e | 2018-08-16 15:30:38 -0500 | [diff] [blame] | 710 | vsa.console = array_index_nospec(vsa.console, |
| 711 | MAX_NR_CONSOLES + 1); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 712 | vsa.console--; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 713 | console_lock(); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 714 | ret = vc_allocate(vsa.console); |
| 715 | if (ret == 0) { |
| 716 | struct vc_data *nvc; |
| 717 | /* This is safe providing we don't drop the |
| 718 | console sem between vc_allocate and |
| 719 | finishing referencing nvc */ |
| 720 | nvc = vc_cons[vsa.console].d; |
| 721 | nvc->vt_mode = vsa.mode; |
| 722 | nvc->vt_mode.frsig = 0; |
| 723 | put_pid(nvc->vt_pid); |
| 724 | nvc->vt_pid = get_pid(task_pid(current)); |
| 725 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 726 | console_unlock(); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 727 | if (ret) |
| 728 | break; |
| 729 | /* Commence switch and lock */ |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 730 | /* Review set_console locks */ |
Jiri Olsa | d637837 | 2011-02-11 15:39:28 +0100 | [diff] [blame] | 731 | set_console(vsa.console); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 732 | } |
Jiri Olsa | d637837 | 2011-02-11 15:39:28 +0100 | [diff] [blame] | 733 | break; |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | /* |
| 737 | * wait until the specified VT has been activated |
| 738 | */ |
| 739 | case VT_WAITACTIVE: |
| 740 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 741 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (arg == 0 || arg > MAX_NR_CONSOLES) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 743 | ret = -ENXIO; |
Alan Cox | 99cceb4e | 2012-03-02 14:59:49 +0000 | [diff] [blame] | 744 | else |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 745 | ret = vt_waitactive(arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 746 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | /* |
| 749 | * If a vt is under process control, the kernel will not switch to it |
| 750 | * immediately, but postpone the operation until the process calls this |
| 751 | * ioctl, allowing the switch to complete. |
| 752 | * |
| 753 | * According to the X sources this is the behavior: |
| 754 | * 0: pending switch-from not OK |
| 755 | * 1: pending switch-from OK |
| 756 | * 2: completed switch-to OK |
| 757 | */ |
| 758 | case VT_RELDISP: |
| 759 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 760 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 762 | console_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 763 | if (vc->vt_mode.mode != VT_PROCESS) { |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 764 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 765 | ret = -EINVAL; |
| 766 | break; |
| 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Switching-from response |
| 770 | */ |
| 771 | if (vc->vt_newvt >= 0) { |
| 772 | if (arg == 0) |
| 773 | /* |
| 774 | * Switch disallowed, so forget we were trying |
| 775 | * to do it. |
| 776 | */ |
| 777 | vc->vt_newvt = -1; |
| 778 | |
| 779 | else { |
| 780 | /* |
| 781 | * The current vt has been released, so |
| 782 | * complete the switch. |
| 783 | */ |
| 784 | int newvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | newvt = vc->vt_newvt; |
| 786 | vc->vt_newvt = -1; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 787 | ret = vc_allocate(newvt); |
| 788 | if (ret) { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 789 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 790 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | /* |
| 793 | * When we actually do the console switch, |
| 794 | * make sure we are atomic with respect to |
| 795 | * other console switches.. |
| 796 | */ |
| 797 | complete_change_console(vc_cons[newvt].d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 799 | } else { |
| 800 | /* |
| 801 | * Switched-to response |
| 802 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | /* |
| 804 | * If it's just an ACK, ignore it |
| 805 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 806 | if (arg != VT_ACKACQ) |
| 807 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 809 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 810 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | * Disallocate memory associated to VT (but leave VT1) |
| 814 | */ |
| 815 | case VT_DISALLOCATE: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 816 | if (arg > MAX_NR_CONSOLES) { |
| 817 | ret = -ENXIO; |
| 818 | break; |
| 819 | } |
Peter Hurley | 421b40a | 2013-05-17 12:41:03 -0400 | [diff] [blame] | 820 | if (arg == 0) |
| 821 | vt_disallocate_all(); |
| 822 | else |
| 823 | ret = vt_disallocate(--arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 824 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
| 826 | case VT_RESIZE: |
| 827 | { |
| 828 | struct vt_sizes __user *vtsizes = up; |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 829 | struct vc_data *vc; |
| 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | ushort ll,cc; |
| 832 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 833 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | if (get_user(ll, &vtsizes->v_rows) || |
| 835 | get_user(cc, &vtsizes->v_cols)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 836 | ret = -EFAULT; |
| 837 | else { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 838 | console_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 839 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
| 840 | vc = vc_cons[i].d; |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 841 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 842 | if (vc) { |
| 843 | vc->vc_resize_user = 1; |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 844 | /* FIXME: review v tty lock */ |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 845 | vc_resize(vc_cons[i].d, cc, ll); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 846 | } |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 847 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 848 | console_unlock(); |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 849 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 850 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | case VT_RESIZEX: |
| 854 | { |
Al Viro | 97b58fa | 2017-09-29 12:34:13 -0400 | [diff] [blame] | 855 | struct vt_consize v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 857 | return -EPERM; |
Al Viro | 97b58fa | 2017-09-29 12:34:13 -0400 | [diff] [blame] | 858 | if (copy_from_user(&v, up, sizeof(struct vt_consize))) |
| 859 | return -EFAULT; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 860 | /* FIXME: Should check the copies properly */ |
Al Viro | 97b58fa | 2017-09-29 12:34:13 -0400 | [diff] [blame] | 861 | if (!v.v_vlin) |
| 862 | v.v_vlin = vc->vc_scan_lines; |
| 863 | if (v.v_clin) { |
| 864 | int rows = v.v_vlin/v.v_clin; |
| 865 | if (v.v_rows != rows) { |
| 866 | if (v.v_rows) /* Parameters don't add up */ |
| 867 | return -EINVAL; |
| 868 | v.v_rows = rows; |
| 869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
Al Viro | 97b58fa | 2017-09-29 12:34:13 -0400 | [diff] [blame] | 871 | if (v.v_vcol && v.v_ccol) { |
| 872 | int cols = v.v_vcol/v.v_ccol; |
| 873 | if (v.v_cols != cols) { |
| 874 | if (v.v_cols) |
| 875 | return -EINVAL; |
| 876 | v.v_cols = cols; |
| 877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Al Viro | 97b58fa | 2017-09-29 12:34:13 -0400 | [diff] [blame] | 880 | if (v.v_clin > 32) |
| 881 | return -EINVAL; |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
Eric Dumazet | 160fbca | 2020-02-10 11:07:21 -0800 | [diff] [blame] | 884 | struct vc_data *vcp; |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (!vc_cons[i].d) |
| 887 | continue; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 888 | console_lock(); |
Eric Dumazet | 160fbca | 2020-02-10 11:07:21 -0800 | [diff] [blame] | 889 | vcp = vc_cons[i].d; |
| 890 | if (vcp) { |
| 891 | if (v.v_vlin) |
| 892 | vcp->vc_scan_lines = v.v_vlin; |
| 893 | if (v.v_clin) |
| 894 | vcp->vc_font.height = v.v_clin; |
| 895 | vcp->vc_resize_user = 1; |
| 896 | vc_resize(vcp, v.v_cols, v.v_rows); |
| 897 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 898 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 900 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | case PIO_FONT: { |
| 904 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 905 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | op.op = KD_FONT_OP_SET; |
| 907 | op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */ |
| 908 | op.width = 8; |
| 909 | op.height = 0; |
| 910 | op.charcount = 256; |
| 911 | op.data = up; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 912 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 913 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | case GIO_FONT: { |
| 917 | op.op = KD_FONT_OP_GET; |
| 918 | op.flags = KD_FONT_FLAG_OLD; |
| 919 | op.width = 8; |
| 920 | op.height = 32; |
| 921 | op.charcount = 256; |
| 922 | op.data = up; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 923 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 924 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | case PIO_CMAP: |
| 928 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 929 | ret = -EPERM; |
| 930 | else |
| 931 | ret = con_set_cmap(up); |
| 932 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | |
| 934 | case GIO_CMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 935 | ret = con_get_cmap(up); |
| 936 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | case PIO_FONTX: |
| 939 | case GIO_FONTX: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 940 | ret = do_fontx_ioctl(cmd, up, perm, &op); |
| 941 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | case PIO_FONTRESET: |
| 944 | { |
| 945 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 946 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | #ifdef BROKEN_GRAPHICS_PROGRAMS |
| 949 | /* With BROKEN_GRAPHICS_PROGRAMS defined, the default |
| 950 | font is not saved. */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 951 | ret = -ENOSYS; |
| 952 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | #else |
| 954 | { |
| 955 | op.op = KD_FONT_OP_SET_DEFAULT; |
| 956 | op.data = NULL; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 957 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 958 | if (ret) |
| 959 | break; |
Alan Cox | 5d1a33f | 2012-04-24 11:06:06 +0100 | [diff] [blame] | 960 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | con_set_default_unimap(vc_cons[fg_console].d); |
Alan Cox | 5d1a33f | 2012-04-24 11:06:06 +0100 | [diff] [blame] | 962 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 963 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | #endif |
| 966 | } |
| 967 | |
| 968 | case KDFONTOP: { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 969 | if (copy_from_user(&op, up, sizeof(op))) { |
| 970 | ret = -EFAULT; |
| 971 | break; |
| 972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | if (!perm && op.op != KD_FONT_OP_GET) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 974 | return -EPERM; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 975 | ret = con_font_op(vc, &op); |
| 976 | if (ret) |
| 977 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | if (copy_to_user(up, &op, sizeof(op))) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 979 | ret = -EFAULT; |
| 980 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | case PIO_SCRNMAP: |
| 984 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 985 | ret = -EPERM; |
Alan Cox | 5d1a33f | 2012-04-24 11:06:06 +0100 | [diff] [blame] | 986 | else |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 987 | ret = con_set_trans_old(up); |
| 988 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
| 990 | case GIO_SCRNMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 991 | ret = con_get_trans_old(up); |
| 992 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | case PIO_UNISCRNMAP: |
| 995 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 996 | ret = -EPERM; |
Alan Cox | 5d1a33f | 2012-04-24 11:06:06 +0100 | [diff] [blame] | 997 | else |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 998 | ret = con_set_trans_new(up); |
| 999 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | case GIO_UNISCRNMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1002 | ret = con_get_trans_new(up); |
| 1003 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | case PIO_UNIMAPCLR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | if (!perm) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 1007 | return -EPERM; |
Jiri Slaby | f8b0c2e | 2016-06-23 13:34:36 +0200 | [diff] [blame] | 1008 | con_clear_unimap(vc); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1009 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | case PIO_UNIMAP: |
| 1012 | case GIO_UNIMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1013 | ret = do_unimap_ioctl(cmd, up, perm, vc); |
| 1014 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
| 1016 | case VT_LOCKSWITCH: |
| 1017 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 1018 | return -EPERM; |
Jiri Slaby | 2e1c84e | 2020-02-19 08:39:48 +0100 | [diff] [blame] | 1019 | vt_dont_switch = true; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1020 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | case VT_UNLOCKSWITCH: |
| 1022 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 4001d7b | 2012-03-02 14:59:20 +0000 | [diff] [blame] | 1023 | return -EPERM; |
Jiri Slaby | 2e1c84e | 2020-02-19 08:39:48 +0100 | [diff] [blame] | 1024 | vt_dont_switch = false; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1025 | break; |
Samuel Thibault | 533475d | 2006-08-27 01:23:39 -0700 | [diff] [blame] | 1026 | case VT_GETHIFONTMASK: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1027 | ret = put_user(vc->vc_hi_font_mask, |
| 1028 | (unsigned short __user *)arg); |
| 1029 | break; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1030 | case VT_WAITEVENT: |
| 1031 | ret = vt_event_wait_ioctl((struct vt_event __user *)arg); |
| 1032 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1034 | ret = -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1036 | out: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1037 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | void reset_vc(struct vc_data *vc) |
| 1041 | { |
| 1042 | vc->vc_mode = KD_TEXT; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 1043 | vt_reset_unicode(vc->vc_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | vc->vt_mode.mode = VT_AUTO; |
| 1045 | vc->vt_mode.waitv = 0; |
| 1046 | vc->vt_mode.relsig = 0; |
| 1047 | vc->vt_mode.acqsig = 0; |
| 1048 | vc->vt_mode.frsig = 0; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1049 | put_pid(vc->vt_pid); |
| 1050 | vc->vt_pid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | vc->vt_newvt = -1; |
| 1052 | if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */ |
| 1053 | reset_palette(vc); |
| 1054 | } |
| 1055 | |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1056 | void vc_SAK(struct work_struct *work) |
| 1057 | { |
| 1058 | struct vc *vc_con = |
| 1059 | container_of(work, struct vc, SAK_work); |
| 1060 | struct vc_data *vc; |
| 1061 | struct tty_struct *tty; |
| 1062 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1063 | console_lock(); |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1064 | vc = vc_con->d; |
| 1065 | if (vc) { |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 1066 | /* FIXME: review tty ref counting */ |
Alan Cox | 8ce7326 | 2010-06-01 22:52:56 +0200 | [diff] [blame] | 1067 | tty = vc->port.tty; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1068 | /* |
| 1069 | * SAK should also work in all raw modes and reset |
| 1070 | * them properly. |
| 1071 | */ |
| 1072 | if (tty) |
| 1073 | __do_SAK(tty); |
| 1074 | reset_vc(vc); |
| 1075 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1076 | console_unlock(); |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1079 | #ifdef CONFIG_COMPAT |
| 1080 | |
| 1081 | struct compat_consolefontdesc { |
| 1082 | unsigned short charcount; /* characters in font (256 or 512) */ |
| 1083 | unsigned short charheight; /* scan lines per character (1-32) */ |
| 1084 | compat_caddr_t chardata; /* font data in expanded form */ |
| 1085 | }; |
| 1086 | |
| 1087 | static inline int |
| 1088 | compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd, |
| 1089 | int perm, struct console_font_op *op) |
| 1090 | { |
| 1091 | struct compat_consolefontdesc cfdarg; |
| 1092 | int i; |
| 1093 | |
| 1094 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc))) |
| 1095 | return -EFAULT; |
| 1096 | |
| 1097 | switch (cmd) { |
| 1098 | case PIO_FONTX: |
| 1099 | if (!perm) |
| 1100 | return -EPERM; |
| 1101 | op->op = KD_FONT_OP_SET; |
| 1102 | op->flags = KD_FONT_FLAG_OLD; |
| 1103 | op->width = 8; |
| 1104 | op->height = cfdarg.charheight; |
| 1105 | op->charcount = cfdarg.charcount; |
| 1106 | op->data = compat_ptr(cfdarg.chardata); |
| 1107 | return con_font_op(vc_cons[fg_console].d, op); |
| 1108 | case GIO_FONTX: |
| 1109 | op->op = KD_FONT_OP_GET; |
| 1110 | op->flags = KD_FONT_FLAG_OLD; |
| 1111 | op->width = 8; |
| 1112 | op->height = cfdarg.charheight; |
| 1113 | op->charcount = cfdarg.charcount; |
| 1114 | op->data = compat_ptr(cfdarg.chardata); |
| 1115 | i = con_font_op(vc_cons[fg_console].d, op); |
| 1116 | if (i) |
| 1117 | return i; |
| 1118 | cfdarg.charheight = op->height; |
| 1119 | cfdarg.charcount = op->charcount; |
| 1120 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc))) |
| 1121 | return -EFAULT; |
| 1122 | return 0; |
| 1123 | } |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | |
| 1127 | struct compat_console_font_op { |
| 1128 | compat_uint_t op; /* operation code KD_FONT_OP_* */ |
| 1129 | compat_uint_t flags; /* KD_FONT_FLAG_* */ |
| 1130 | compat_uint_t width, height; /* font size */ |
| 1131 | compat_uint_t charcount; |
| 1132 | compat_caddr_t data; /* font data with height fixed to 32 */ |
| 1133 | }; |
| 1134 | |
| 1135 | static inline int |
| 1136 | compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop, |
| 1137 | int perm, struct console_font_op *op, struct vc_data *vc) |
| 1138 | { |
| 1139 | int i; |
| 1140 | |
| 1141 | if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op))) |
| 1142 | return -EFAULT; |
| 1143 | if (!perm && op->op != KD_FONT_OP_GET) |
| 1144 | return -EPERM; |
| 1145 | op->data = compat_ptr(((struct compat_console_font_op *)op)->data); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1146 | i = con_font_op(vc, op); |
| 1147 | if (i) |
| 1148 | return i; |
| 1149 | ((struct compat_console_font_op *)op)->data = (unsigned long)op->data; |
| 1150 | if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op))) |
| 1151 | return -EFAULT; |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | struct compat_unimapdesc { |
| 1156 | unsigned short entry_ct; |
| 1157 | compat_caddr_t entries; |
| 1158 | }; |
| 1159 | |
| 1160 | static inline int |
| 1161 | compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud, |
| 1162 | int perm, struct vc_data *vc) |
| 1163 | { |
| 1164 | struct compat_unimapdesc tmp; |
| 1165 | struct unipair __user *tmp_entries; |
| 1166 | |
| 1167 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) |
| 1168 | return -EFAULT; |
| 1169 | tmp_entries = compat_ptr(tmp.entries); |
| 1170 | if (tmp_entries) |
| 1171 | if (!access_ok(VERIFY_WRITE, tmp_entries, |
| 1172 | tmp.entry_ct*sizeof(struct unipair))) |
| 1173 | return -EFAULT; |
| 1174 | switch (cmd) { |
| 1175 | case PIO_UNIMAP: |
| 1176 | if (!perm) |
| 1177 | return -EPERM; |
| 1178 | return con_set_unimap(vc, tmp.entry_ct, tmp_entries); |
| 1179 | case GIO_UNIMAP: |
| 1180 | if (!perm && fg_console != vc->vc_num) |
| 1181 | return -EPERM; |
| 1182 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries); |
| 1183 | } |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1187 | long vt_compat_ioctl(struct tty_struct *tty, |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1188 | unsigned int cmd, unsigned long arg) |
| 1189 | { |
| 1190 | struct vc_data *vc = tty->driver_data; |
| 1191 | struct console_font_op op; /* used in multiple places here */ |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1192 | void __user *up = (void __user *)arg; |
| 1193 | int perm; |
| 1194 | int ret = 0; |
| 1195 | |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1196 | /* |
| 1197 | * To have permissions to do most of the vt ioctls, we either have |
| 1198 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. |
| 1199 | */ |
| 1200 | perm = 0; |
| 1201 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) |
| 1202 | perm = 1; |
| 1203 | |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1204 | switch (cmd) { |
| 1205 | /* |
| 1206 | * these need special handlers for incompatible data structures |
| 1207 | */ |
| 1208 | case PIO_FONTX: |
| 1209 | case GIO_FONTX: |
| 1210 | ret = compat_fontx_ioctl(cmd, up, perm, &op); |
| 1211 | break; |
| 1212 | |
| 1213 | case KDFONTOP: |
| 1214 | ret = compat_kdfontop_ioctl(up, perm, &op, vc); |
| 1215 | break; |
| 1216 | |
| 1217 | case PIO_UNIMAP: |
| 1218 | case GIO_UNIMAP: |
Andreas Schwab | 4b1fe77 | 2009-09-28 20:10:02 +0200 | [diff] [blame] | 1219 | ret = compat_unimap_ioctl(cmd, up, perm, vc); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1220 | break; |
| 1221 | |
| 1222 | /* |
| 1223 | * all these treat 'arg' as an integer |
| 1224 | */ |
| 1225 | case KIOCSOUND: |
| 1226 | case KDMKTONE: |
| 1227 | #ifdef CONFIG_X86 |
| 1228 | case KDADDIO: |
| 1229 | case KDDELIO: |
| 1230 | #endif |
| 1231 | case KDSETMODE: |
| 1232 | case KDMAPDISP: |
| 1233 | case KDUNMAPDISP: |
| 1234 | case KDSKBMODE: |
| 1235 | case KDSKBMETA: |
| 1236 | case KDSKBLED: |
| 1237 | case KDSETLED: |
| 1238 | case KDSIGACCEPT: |
| 1239 | case VT_ACTIVATE: |
| 1240 | case VT_WAITACTIVE: |
| 1241 | case VT_RELDISP: |
| 1242 | case VT_DISALLOCATE: |
| 1243 | case VT_RESIZE: |
| 1244 | case VT_RESIZEX: |
| 1245 | goto fallback; |
| 1246 | |
| 1247 | /* |
| 1248 | * the rest has a compatible data structure behind arg, |
| 1249 | * but we have to convert it to a proper 64 bit pointer. |
| 1250 | */ |
| 1251 | default: |
| 1252 | arg = (unsigned long)compat_ptr(arg); |
| 1253 | goto fallback; |
| 1254 | } |
Eric Biggers | 35d6480 | 2020-02-24 00:03:26 -0800 | [diff] [blame] | 1255 | |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1256 | return ret; |
| 1257 | |
| 1258 | fallback: |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1259 | return vt_ioctl(tty, cmd, arg); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | #endif /* CONFIG_COMPAT */ |
| 1264 | |
| 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | /* |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1267 | * Performs the back end of a vt switch. Called under the console |
| 1268 | * semaphore. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | */ |
| 1270 | static void complete_change_console(struct vc_data *vc) |
| 1271 | { |
| 1272 | unsigned char old_vc_mode; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1273 | int old = fg_console; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
| 1275 | last_console = fg_console; |
| 1276 | |
| 1277 | /* |
| 1278 | * If we're switching, we could be going from KD_GRAPHICS to |
| 1279 | * KD_TEXT mode or vice versa, which means we need to blank or |
| 1280 | * unblank the screen later. |
| 1281 | */ |
| 1282 | old_vc_mode = vc_cons[fg_console].d->vc_mode; |
| 1283 | switch_screen(vc); |
| 1284 | |
| 1285 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1286 | * This can't appear below a successful kill_pid(). If it did, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | * then the *blank_screen operation could occur while X, having |
| 1288 | * received acqsig, is waking up on another processor. This |
| 1289 | * condition can lead to overlapping accesses to the VGA range |
| 1290 | * and the framebuffer (causing system lockups). |
| 1291 | * |
| 1292 | * To account for this we duplicate this code below only if the |
| 1293 | * controlling process is gone and we've called reset_vc. |
| 1294 | */ |
| 1295 | if (old_vc_mode != vc->vc_mode) { |
| 1296 | if (vc->vc_mode == KD_TEXT) |
| 1297 | do_unblank_screen(1); |
| 1298 | else |
| 1299 | do_blank_screen(1); |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * If this new console is under process control, send it a signal |
| 1304 | * telling it that it has acquired. Also check if it has died and |
| 1305 | * clean up (similar to logic employed in change_console()) |
| 1306 | */ |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1307 | if (vc->vt_mode.mode == VT_PROCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1309 | * Send the signal as privileged - kill_pid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | * tell us if the process has gone or something else |
| 1311 | * is awry |
| 1312 | */ |
Eric W. Biederman | bde0d2c9 | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1313 | if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | /* |
| 1315 | * The controlling process has died, so we revert back to |
| 1316 | * normal operation. In this case, we'll also change back |
| 1317 | * to KD_TEXT mode. I'm not sure if this is strictly correct |
| 1318 | * but it saves the agony when the X server dies and the screen |
| 1319 | * remains blanked due to KD_GRAPHICS! It would be nice to do |
| 1320 | * this outside of VT_PROCESS but there is no single process |
| 1321 | * to account for and tracking tty count may be undesirable. |
| 1322 | */ |
| 1323 | reset_vc(vc); |
| 1324 | |
| 1325 | if (old_vc_mode != vc->vc_mode) { |
| 1326 | if (vc->vc_mode == KD_TEXT) |
| 1327 | do_unblank_screen(1); |
| 1328 | else |
| 1329 | do_blank_screen(1); |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * Wake anyone waiting for their VT to activate |
| 1336 | */ |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1337 | vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Performs the front-end of a vt switch |
| 1343 | */ |
| 1344 | void change_console(struct vc_data *new_vc) |
| 1345 | { |
| 1346 | struct vc_data *vc; |
| 1347 | |
| 1348 | if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch) |
| 1349 | return; |
| 1350 | |
| 1351 | /* |
| 1352 | * If this vt is in process mode, then we need to handshake with |
| 1353 | * that process before switching. Essentially, we store where that |
| 1354 | * vt wants to switch to and wait for it to tell us when it's done |
| 1355 | * (via VT_RELDISP ioctl). |
| 1356 | * |
| 1357 | * We also check to see if the controlling process still exists. |
| 1358 | * If it doesn't, we reset this vt to auto mode and continue. |
| 1359 | * This is a cheap way to track process control. The worst thing |
| 1360 | * that can happen is: we send a signal to a process, it dies, and |
| 1361 | * the switch gets "lost" waiting for a response; hopefully, the |
| 1362 | * user will try again, we'll detect the process is gone (unless |
| 1363 | * the user waits just the right amount of time :-) and revert the |
| 1364 | * vt to auto control. |
| 1365 | */ |
| 1366 | vc = vc_cons[fg_console].d; |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1367 | if (vc->vt_mode.mode == VT_PROCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1369 | * Send the signal as privileged - kill_pid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | * tell us if the process has gone or something else |
Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1371 | * is awry. |
| 1372 | * |
| 1373 | * We need to set vt_newvt *before* sending the signal or we |
| 1374 | * have a race. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | */ |
Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1376 | vc->vt_newvt = new_vc->vc_num; |
Eric W. Biederman | bde0d2c9 | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1377 | if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | /* |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1379 | * It worked. Mark the vt to switch to and |
| 1380 | * return. The process needs to send us a |
| 1381 | * VT_RELDISP ioctl to complete the switch. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | */ |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1383 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1387 | * The controlling process has died, so we revert back to |
| 1388 | * normal operation. In this case, we'll also change back |
| 1389 | * to KD_TEXT mode. I'm not sure if this is strictly correct |
| 1390 | * but it saves the agony when the X server dies and the screen |
| 1391 | * remains blanked due to KD_GRAPHICS! It would be nice to do |
| 1392 | * this outside of VT_PROCESS but there is no single process |
| 1393 | * to account for and tracking tty count may be undesirable. |
| 1394 | */ |
| 1395 | reset_vc(vc); |
| 1396 | |
| 1397 | /* |
| 1398 | * Fall through to normal (VT_AUTO) handling of the switch... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | */ |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * Ignore all switches in KD_GRAPHICS+VT_AUTO mode |
| 1404 | */ |
| 1405 | if (vc->vc_mode == KD_GRAPHICS) |
| 1406 | return; |
| 1407 | |
| 1408 | complete_change_console(new_vc); |
| 1409 | } |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1410 | |
| 1411 | /* Perform a kernel triggered VT switch for suspend/resume */ |
| 1412 | |
| 1413 | static int disable_vt_switch; |
| 1414 | |
| 1415 | int vt_move_to_console(unsigned int vt, int alloc) |
| 1416 | { |
| 1417 | int prev; |
| 1418 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1419 | console_lock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1420 | /* Graphics mode - up to X */ |
| 1421 | if (disable_vt_switch) { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1422 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | prev = fg_console; |
| 1426 | |
| 1427 | if (alloc && vc_allocate(vt)) { |
| 1428 | /* we can't have a free VC for now. Too bad, |
| 1429 | * we don't want to mess the screen for now. */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1430 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1431 | return -ENOSPC; |
| 1432 | } |
| 1433 | |
| 1434 | if (set_console(vt)) { |
| 1435 | /* |
| 1436 | * We're unable to switch to the SUSPEND_CONSOLE. |
| 1437 | * Let the calling function know so it can decide |
| 1438 | * what to do. |
| 1439 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1440 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1441 | return -EIO; |
| 1442 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1443 | console_unlock(); |
Jiri Slaby | 797938b | 2009-08-11 23:20:41 +0200 | [diff] [blame] | 1444 | if (vt_waitactive(vt + 1)) { |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1445 | pr_debug("Suspend: Can't switch VCs."); |
| 1446 | return -EINTR; |
| 1447 | } |
| 1448 | return prev; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Normally during a suspend, we allocate a new console and switch to it. |
| 1453 | * When we resume, we switch back to the original console. This switch |
| 1454 | * can be slow, so on systems where the framebuffer can handle restoration |
| 1455 | * of video registers anyways, there's little point in doing the console |
| 1456 | * switch. This function allows you to disable it by passing it '0'. |
| 1457 | */ |
| 1458 | void pm_set_vt_switch(int do_switch) |
| 1459 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1460 | console_lock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1461 | disable_vt_switch = !do_switch; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1462 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1463 | } |
| 1464 | EXPORT_SYMBOL(pm_set_vt_switch); |