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