Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/vt_ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 1992 obz under the linux copyright |
| 5 | * |
| 6 | * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993 |
| 7 | * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994 |
| 8 | * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995 |
| 9 | * Some code moved for less code duplication - Andi Kleen - Mar 1997 |
| 10 | * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001 |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/tty.h> |
| 17 | #include <linux/timer.h> |
| 18 | #include <linux/kernel.h> |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 19 | #include <linux/compat.h> |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kd.h> |
| 22 | #include <linux/vt.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/major.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/console.h> |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 28 | #include <linux/consolemap.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 29 | #include <linux/signal.h> |
Emmanuel Colbus | bcc8ca0 | 2005-06-28 20:44:49 -0700 | [diff] [blame] | 30 | #include <linux/timex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/io.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | |
| 35 | #include <linux/kbd_kern.h> |
| 36 | #include <linux/vt_kern.h> |
| 37 | #include <linux/kbd_diacr.h> |
| 38 | #include <linux/selection.h> |
| 39 | |
Andrew Johnson | b257bc0 | 2007-03-16 13:38:24 -0800 | [diff] [blame] | 40 | char vt_dont_switch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | extern struct tty_driver *console_driver; |
| 42 | |
| 43 | #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count) |
| 44 | #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons) |
| 45 | |
| 46 | /* |
| 47 | * Console (vt and kd) routines, as defined by USL SVR4 manual, and by |
| 48 | * experimentation and study of X386 SYSV handling. |
| 49 | * |
| 50 | * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and |
| 51 | * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console, |
| 52 | * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will |
| 53 | * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to |
| 54 | * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using |
| 55 | * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing |
| 56 | * to the current console is done by the main ioctl code. |
| 57 | */ |
| 58 | |
| 59 | #ifdef CONFIG_X86 |
| 60 | #include <linux/syscalls.h> |
| 61 | #endif |
| 62 | |
| 63 | static void complete_change_console(struct vc_data *vc); |
| 64 | |
| 65 | /* |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 66 | * User space VT_EVENT handlers |
| 67 | */ |
| 68 | |
| 69 | struct vt_event_wait { |
| 70 | struct list_head list; |
| 71 | struct vt_event event; |
| 72 | int done; |
| 73 | }; |
| 74 | |
| 75 | static LIST_HEAD(vt_events); |
| 76 | static DEFINE_SPINLOCK(vt_event_lock); |
| 77 | static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue); |
| 78 | |
| 79 | /** |
| 80 | * vt_event_post |
| 81 | * @event: the event that occurred |
| 82 | * @old: old console |
| 83 | * @new: new console |
| 84 | * |
| 85 | * Post an VT event to interested VT handlers |
| 86 | */ |
| 87 | |
| 88 | void vt_event_post(unsigned int event, unsigned int old, unsigned int new) |
| 89 | { |
| 90 | struct list_head *pos, *head; |
| 91 | unsigned long flags; |
| 92 | int wake = 0; |
| 93 | |
| 94 | spin_lock_irqsave(&vt_event_lock, flags); |
| 95 | head = &vt_events; |
| 96 | |
| 97 | list_for_each(pos, head) { |
| 98 | struct vt_event_wait *ve = list_entry(pos, |
| 99 | struct vt_event_wait, list); |
| 100 | if (!(ve->event.event & event)) |
| 101 | continue; |
| 102 | ve->event.event = event; |
| 103 | /* kernel view is consoles 0..n-1, user space view is |
| 104 | console 1..n with 0 meaning current, so we must bias */ |
Alan Cox | 308efab | 2009-11-19 13:30:36 +0000 | [diff] [blame] | 105 | ve->event.oldev = old + 1; |
| 106 | ve->event.newev = new + 1; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 107 | wake = 1; |
| 108 | ve->done = 1; |
| 109 | } |
| 110 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 111 | if (wake) |
| 112 | wake_up_interruptible(&vt_event_waitqueue); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * vt_event_wait - wait for an event |
| 117 | * @vw: our event |
| 118 | * |
| 119 | * Waits for an event to occur which completes our vt_event_wait |
| 120 | * structure. On return the structure has wv->done set to 1 for success |
| 121 | * or 0 if some event such as a signal ended the wait. |
| 122 | */ |
| 123 | |
| 124 | static void vt_event_wait(struct vt_event_wait *vw) |
| 125 | { |
| 126 | unsigned long flags; |
| 127 | /* Prepare the event */ |
| 128 | INIT_LIST_HEAD(&vw->list); |
| 129 | vw->done = 0; |
| 130 | /* Queue our event */ |
| 131 | spin_lock_irqsave(&vt_event_lock, flags); |
| 132 | list_add(&vw->list, &vt_events); |
| 133 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 134 | /* Wait for it to pass */ |
Arnd Bergmann | be1bc28 | 2010-06-01 22:53:05 +0200 | [diff] [blame] | 135 | wait_event_interruptible_tty(vt_event_waitqueue, vw->done); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 136 | /* Dequeue it */ |
| 137 | spin_lock_irqsave(&vt_event_lock, flags); |
| 138 | list_del(&vw->list); |
| 139 | spin_unlock_irqrestore(&vt_event_lock, flags); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * vt_event_wait_ioctl - event ioctl handler |
| 144 | * @arg: argument to ioctl |
| 145 | * |
| 146 | * Implement the VT_WAITEVENT ioctl using the VT event interface |
| 147 | */ |
| 148 | |
| 149 | static int vt_event_wait_ioctl(struct vt_event __user *event) |
| 150 | { |
| 151 | struct vt_event_wait vw; |
| 152 | |
| 153 | if (copy_from_user(&vw.event, event, sizeof(struct vt_event))) |
| 154 | return -EFAULT; |
| 155 | /* Highest supported event for now */ |
| 156 | if (vw.event.event & ~VT_MAX_EVENT) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | vt_event_wait(&vw); |
| 160 | /* If it occurred report it */ |
| 161 | if (vw.done) { |
| 162 | if (copy_to_user(event, &vw.event, sizeof(struct vt_event))) |
| 163 | return -EFAULT; |
| 164 | return 0; |
| 165 | } |
| 166 | return -EINTR; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * vt_waitactive - active console wait |
| 171 | * @event: event code |
| 172 | * @n: new console |
| 173 | * |
| 174 | * Helper for event waits. Used to implement the legacy |
| 175 | * event waiting ioctls in terms of events |
| 176 | */ |
| 177 | |
| 178 | int vt_waitactive(int n) |
| 179 | { |
| 180 | struct vt_event_wait vw; |
| 181 | do { |
| 182 | if (n == fg_console + 1) |
| 183 | break; |
| 184 | vw.event.event = VT_EVENT_SWITCH; |
| 185 | vt_event_wait(&vw); |
| 186 | if (vw.done == 0) |
| 187 | return -EINTR; |
Alan Cox | 308efab | 2009-11-19 13:30:36 +0000 | [diff] [blame] | 188 | } while (vw.event.newev != n); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * these are the valid i/o ports we're allowed to change. they map all the |
| 194 | * video ports |
| 195 | */ |
| 196 | #define GPFIRST 0x3b4 |
| 197 | #define GPLAST 0x3df |
| 198 | #define GPNUM (GPLAST - GPFIRST + 1) |
| 199 | |
| 200 | #define i (tmp.kb_index) |
| 201 | #define s (tmp.kb_table) |
| 202 | #define v (tmp.kb_value) |
| 203 | static inline int |
| 204 | do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd) |
| 205 | { |
| 206 | struct kbentry tmp; |
| 207 | ushort *key_map, val, ov; |
| 208 | |
| 209 | if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) |
| 210 | return -EFAULT; |
| 211 | |
Marcelo Tosatti | e3f17f0 | 2005-11-07 00:59:34 -0800 | [diff] [blame] | 212 | if (!capable(CAP_SYS_TTY_CONFIG)) |
| 213 | perm = 0; |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | switch (cmd) { |
| 216 | case KDGKBENT: |
| 217 | key_map = key_maps[s]; |
| 218 | if (key_map) { |
| 219 | val = U(key_map[i]); |
| 220 | if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) |
| 221 | val = K_HOLE; |
| 222 | } else |
| 223 | val = (i ? K_HOLE : K_NOSUCHMAP); |
| 224 | return put_user(val, &user_kbe->kb_value); |
| 225 | case KDSKBENT: |
| 226 | if (!perm) |
| 227 | return -EPERM; |
| 228 | if (!i && v == K_NOSUCHMAP) { |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 229 | /* deallocate map */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | key_map = key_maps[s]; |
| 231 | if (s && key_map) { |
| 232 | key_maps[s] = NULL; |
| 233 | if (key_map[0] == U(K_ALLOCATED)) { |
| 234 | kfree(key_map); |
| 235 | keymap_count--; |
| 236 | } |
| 237 | } |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | if (KTYP(v) < NR_TYPES) { |
| 242 | if (KVAL(v) > max_vals[KTYP(v)]) |
| 243 | return -EINVAL; |
| 244 | } else |
| 245 | if (kbd->kbdmode != VC_UNICODE) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | /* ++Geert: non-PC keyboards may generate keycode zero */ |
| 249 | #if !defined(__mc68000__) && !defined(__powerpc__) |
| 250 | /* assignment to entry 0 only tests validity of args */ |
| 251 | if (!i) |
| 252 | break; |
| 253 | #endif |
| 254 | |
| 255 | if (!(key_map = key_maps[s])) { |
| 256 | int j; |
| 257 | |
| 258 | if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && |
| 259 | !capable(CAP_SYS_RESOURCE)) |
| 260 | return -EPERM; |
| 261 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 262 | key_map = kmalloc(sizeof(plain_map), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | GFP_KERNEL); |
| 264 | if (!key_map) |
| 265 | return -ENOMEM; |
| 266 | key_maps[s] = key_map; |
| 267 | key_map[0] = U(K_ALLOCATED); |
| 268 | for (j = 1; j < NR_KEYS; j++) |
| 269 | key_map[j] = U(K_HOLE); |
| 270 | keymap_count++; |
| 271 | } |
| 272 | ov = U(key_map[i]); |
| 273 | if (v == ov) |
| 274 | break; /* nothing to do */ |
| 275 | /* |
| 276 | * Attention Key. |
| 277 | */ |
| 278 | if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) |
| 279 | return -EPERM; |
| 280 | key_map[i] = U(v); |
| 281 | if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) |
| 282 | compute_shiftstate(); |
| 283 | break; |
| 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | #undef i |
| 288 | #undef s |
| 289 | #undef v |
| 290 | |
| 291 | static inline int |
| 292 | do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm) |
| 293 | { |
| 294 | struct kbkeycode tmp; |
| 295 | int kc = 0; |
| 296 | |
| 297 | if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) |
| 298 | return -EFAULT; |
| 299 | switch (cmd) { |
| 300 | case KDGETKEYCODE: |
| 301 | kc = getkeycode(tmp.scancode); |
| 302 | if (kc >= 0) |
| 303 | kc = put_user(kc, &user_kbkc->keycode); |
| 304 | break; |
| 305 | case KDSETKEYCODE: |
| 306 | if (!perm) |
| 307 | return -EPERM; |
| 308 | kc = setkeycode(tmp.scancode, tmp.keycode); |
| 309 | break; |
| 310 | } |
| 311 | return kc; |
| 312 | } |
| 313 | |
| 314 | static inline int |
| 315 | do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) |
| 316 | { |
| 317 | struct kbsentry *kbs; |
| 318 | char *p; |
| 319 | u_char *q; |
| 320 | u_char __user *up; |
| 321 | int sz; |
| 322 | int delta; |
| 323 | char *first_free, *fj, *fnw; |
| 324 | int i, j, k; |
| 325 | int ret; |
| 326 | |
Andrew Morton | 0b360ad | 2005-10-30 15:03:02 -0800 | [diff] [blame] | 327 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Marcelo Tosatti | e3f17f0 | 2005-11-07 00:59:34 -0800 | [diff] [blame] | 328 | perm = 0; |
Andrew Morton | 0b360ad | 2005-10-30 15:03:02 -0800 | [diff] [blame] | 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | kbs = kmalloc(sizeof(*kbs), GFP_KERNEL); |
| 331 | if (!kbs) { |
| 332 | ret = -ENOMEM; |
| 333 | goto reterr; |
| 334 | } |
| 335 | |
| 336 | /* we mostly copy too much here (512bytes), but who cares ;) */ |
| 337 | if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) { |
| 338 | ret = -EFAULT; |
| 339 | goto reterr; |
| 340 | } |
| 341 | kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0'; |
| 342 | i = kbs->kb_func; |
| 343 | |
| 344 | switch (cmd) { |
| 345 | case KDGKBSENT: |
| 346 | sz = sizeof(kbs->kb_string) - 1; /* sz should have been |
| 347 | a struct member */ |
| 348 | up = user_kdgkb->kb_string; |
| 349 | p = func_table[i]; |
| 350 | if(p) |
| 351 | for ( ; *p && sz; p++, sz--) |
| 352 | if (put_user(*p, up++)) { |
| 353 | ret = -EFAULT; |
| 354 | goto reterr; |
| 355 | } |
| 356 | if (put_user('\0', up)) { |
| 357 | ret = -EFAULT; |
| 358 | goto reterr; |
| 359 | } |
| 360 | kfree(kbs); |
| 361 | return ((p && *p) ? -EOVERFLOW : 0); |
| 362 | case KDSKBSENT: |
| 363 | if (!perm) { |
| 364 | ret = -EPERM; |
| 365 | goto reterr; |
| 366 | } |
| 367 | |
| 368 | q = func_table[i]; |
| 369 | first_free = funcbufptr + (funcbufsize - funcbufleft); |
| 370 | for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) |
| 371 | ; |
| 372 | if (j < MAX_NR_FUNC) |
| 373 | fj = func_table[j]; |
| 374 | else |
| 375 | fj = first_free; |
| 376 | |
| 377 | delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string); |
| 378 | if (delta <= funcbufleft) { /* it fits in current buf */ |
| 379 | if (j < MAX_NR_FUNC) { |
| 380 | memmove(fj + delta, fj, first_free - fj); |
| 381 | for (k = j; k < MAX_NR_FUNC; k++) |
| 382 | if (func_table[k]) |
| 383 | func_table[k] += delta; |
| 384 | } |
| 385 | if (!q) |
| 386 | func_table[i] = fj; |
| 387 | funcbufleft -= delta; |
| 388 | } else { /* allocate a larger buffer */ |
| 389 | sz = 256; |
| 390 | while (sz < funcbufsize - funcbufleft + delta) |
| 391 | sz <<= 1; |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 392 | fnw = kmalloc(sz, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if(!fnw) { |
| 394 | ret = -ENOMEM; |
| 395 | goto reterr; |
| 396 | } |
| 397 | |
| 398 | if (!q) |
| 399 | func_table[i] = fj; |
| 400 | if (fj > funcbufptr) |
| 401 | memmove(fnw, funcbufptr, fj - funcbufptr); |
| 402 | for (k = 0; k < j; k++) |
| 403 | if (func_table[k]) |
| 404 | func_table[k] = fnw + (func_table[k] - funcbufptr); |
| 405 | |
| 406 | if (first_free > fj) { |
| 407 | memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj); |
| 408 | for (k = j; k < MAX_NR_FUNC; k++) |
| 409 | if (func_table[k]) |
| 410 | func_table[k] = fnw + (func_table[k] - funcbufptr) + delta; |
| 411 | } |
| 412 | if (funcbufptr != func_buf) |
| 413 | kfree(funcbufptr); |
| 414 | funcbufptr = fnw; |
| 415 | funcbufleft = funcbufleft - delta + sz - funcbufsize; |
| 416 | funcbufsize = sz; |
| 417 | } |
| 418 | strcpy(func_table[i], kbs->kb_string); |
| 419 | break; |
| 420 | } |
| 421 | ret = 0; |
| 422 | reterr: |
| 423 | kfree(kbs); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static inline int |
| 428 | do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op) |
| 429 | { |
| 430 | struct consolefontdesc cfdarg; |
| 431 | int i; |
| 432 | |
| 433 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | switch (cmd) { |
| 437 | case PIO_FONTX: |
| 438 | if (!perm) |
| 439 | return -EPERM; |
| 440 | op->op = KD_FONT_OP_SET; |
| 441 | op->flags = KD_FONT_FLAG_OLD; |
| 442 | op->width = 8; |
| 443 | op->height = cfdarg.charheight; |
| 444 | op->charcount = cfdarg.charcount; |
| 445 | op->data = cfdarg.chardata; |
| 446 | return con_font_op(vc_cons[fg_console].d, op); |
| 447 | case GIO_FONTX: { |
| 448 | op->op = KD_FONT_OP_GET; |
| 449 | op->flags = KD_FONT_FLAG_OLD; |
| 450 | op->width = 8; |
| 451 | op->height = cfdarg.charheight; |
| 452 | op->charcount = cfdarg.charcount; |
| 453 | op->data = cfdarg.chardata; |
| 454 | i = con_font_op(vc_cons[fg_console].d, op); |
| 455 | if (i) |
| 456 | return i; |
| 457 | cfdarg.charheight = op->height; |
| 458 | cfdarg.charcount = op->charcount; |
| 459 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc))) |
| 460 | return -EFAULT; |
| 461 | return 0; |
| 462 | } |
| 463 | } |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | static inline int |
| 468 | do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc) |
| 469 | { |
| 470 | struct unimapdesc tmp; |
| 471 | |
| 472 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) |
| 473 | return -EFAULT; |
| 474 | if (tmp.entries) |
| 475 | if (!access_ok(VERIFY_WRITE, tmp.entries, |
| 476 | tmp.entry_ct*sizeof(struct unipair))) |
| 477 | return -EFAULT; |
| 478 | switch (cmd) { |
| 479 | case PIO_UNIMAP: |
| 480 | if (!perm) |
| 481 | return -EPERM; |
| 482 | return con_set_unimap(vc, tmp.entry_ct, tmp.entries); |
| 483 | case GIO_UNIMAP: |
| 484 | if (!perm && fg_console != vc->vc_num) |
| 485 | return -EPERM; |
| 486 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries); |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 491 | |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /* |
| 494 | * We handle the console-specific ioctl's here. We allow the |
| 495 | * capability to modify any console, not just the fg_console. |
| 496 | */ |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 497 | int vt_ioctl(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | unsigned int cmd, unsigned long arg) |
| 499 | { |
Alan Cox | c9f19e9 | 2009-01-02 13:47:26 +0000 | [diff] [blame] | 500 | struct vc_data *vc = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | struct console_font_op op; /* used in multiple places here */ |
| 502 | struct kbd_struct * kbd; |
| 503 | unsigned int console; |
| 504 | unsigned char ucval; |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 505 | unsigned int uival; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | void __user *up = (void __user *)arg; |
| 507 | int i, perm; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 508 | int ret = 0; |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | console = vc->vc_num; |
| 511 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 512 | tty_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 513 | |
| 514 | if (!vc_cons_allocated(console)) { /* impossible? */ |
| 515 | ret = -ENOIOCTLCMD; |
| 516 | goto out; |
| 517 | } |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * To have permissions to do most of the vt ioctls, we either have |
| 522 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. |
| 523 | */ |
| 524 | perm = 0; |
| 525 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) |
| 526 | perm = 1; |
| 527 | |
| 528 | kbd = kbd_table + console; |
| 529 | switch (cmd) { |
Alan Cox | e688510 | 2008-10-13 10:36:40 +0100 | [diff] [blame] | 530 | case TIOCLINUX: |
Jiri Slaby | a115902 | 2009-06-22 18:42:18 +0100 | [diff] [blame] | 531 | ret = tioclinux(tty, arg); |
| 532 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | case KIOCSOUND: |
| 534 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 535 | goto eperm; |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 536 | /* |
| 537 | * The use of PIT_TICK_RATE is historic, it used to be |
| 538 | * the platform-dependent CLOCK_TICK_RATE between 2.6.12 |
| 539 | * and 2.6.36, which was a minor but unfortunate ABI |
| 540 | * change. |
| 541 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (arg) |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 543 | arg = PIT_TICK_RATE / arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | kd_mksound(arg, 0); |
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 KDMKTONE: |
| 548 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 549 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | unsigned int ticks, count; |
| 552 | |
| 553 | /* |
| 554 | * Generate the tone for the appropriate number of ticks. |
| 555 | * If the time is zero, turn off sound ourselves. |
| 556 | */ |
| 557 | ticks = HZ * ((arg >> 16) & 0xffff) / 1000; |
| 558 | count = ticks ? (arg & 0xffff) : 0; |
| 559 | if (count) |
Arnd Bergmann | 2c4e967 | 2010-08-28 20:35:17 -0700 | [diff] [blame] | 560 | count = PIT_TICK_RATE / count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | kd_mksound(count, ticks); |
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 | |
| 565 | case KDGKBTYPE: |
| 566 | /* |
| 567 | * this is naive. |
| 568 | */ |
| 569 | ucval = KB_101; |
| 570 | goto setchar; |
| 571 | |
| 572 | /* |
| 573 | * These cannot be implemented on any machine that implements |
| 574 | * ioperm() in user level (such as Alpha PCs) or not at all. |
| 575 | * |
| 576 | * XXX: you should never use these, just call ioperm directly.. |
| 577 | */ |
| 578 | #ifdef CONFIG_X86 |
| 579 | case KDADDIO: |
| 580 | case KDDELIO: |
| 581 | /* |
| 582 | * KDADDIO and KDDELIO may be able to add ports beyond what |
| 583 | * we reject here, but to be safe... |
| 584 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 585 | if (arg < GPFIRST || arg > GPLAST) { |
| 586 | ret = -EINVAL; |
| 587 | break; |
| 588 | } |
| 589 | ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0; |
| 590 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | case KDENABIO: |
| 593 | case KDDISABIO: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 594 | ret = sys_ioperm(GPFIRST, GPNUM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | (cmd == KDENABIO)) ? -ENXIO : 0; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 596 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | #endif |
| 598 | |
| 599 | /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */ |
| 600 | |
| 601 | case KDKBDREP: |
| 602 | { |
| 603 | struct kbd_repeat kbrep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 606 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 608 | if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) { |
| 609 | ret = -EFAULT; |
| 610 | break; |
| 611 | } |
| 612 | ret = kbd_rate(&kbrep); |
| 613 | if (ret) |
| 614 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat))) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 616 | ret = -EFAULT; |
| 617 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | case KDSETMODE: |
| 621 | /* |
| 622 | * currently, setting the mode from KD_TEXT to KD_GRAPHICS |
| 623 | * doesn't do a whole lot. i'm not sure if it should do any |
| 624 | * restoration of modes or what... |
| 625 | * |
| 626 | * XXX It should at least call into the driver, fbdev's definitely |
| 627 | * need to restore their engine state. --BenH |
| 628 | */ |
| 629 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 630 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | switch (arg) { |
| 632 | case KD_GRAPHICS: |
| 633 | break; |
| 634 | case KD_TEXT0: |
| 635 | case KD_TEXT1: |
| 636 | arg = KD_TEXT; |
| 637 | case KD_TEXT: |
| 638 | break; |
| 639 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 640 | ret = -EINVAL; |
| 641 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | if (vc->vc_mode == (unsigned char) arg) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 644 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | vc->vc_mode = (unsigned char) arg; |
| 646 | if (console != fg_console) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 647 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* |
| 649 | * explicitly blank/unblank the screen if switching modes |
| 650 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 651 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (arg == KD_TEXT) |
| 653 | do_unblank_screen(1); |
| 654 | else |
| 655 | do_blank_screen(1); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 656 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 657 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | case KDGETMODE: |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 660 | uival = vc->vc_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | goto setint; |
| 662 | |
| 663 | case KDMAPDISP: |
| 664 | case KDUNMAPDISP: |
| 665 | /* |
| 666 | * these work like a combination of mmap and KDENABIO. |
| 667 | * this could be easily finished. |
| 668 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 669 | ret = -EINVAL; |
| 670 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | case KDSKBMODE: |
| 673 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 674 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | switch(arg) { |
| 676 | case K_RAW: |
| 677 | kbd->kbdmode = VC_RAW; |
| 678 | break; |
| 679 | case K_MEDIUMRAW: |
| 680 | kbd->kbdmode = VC_MEDIUMRAW; |
| 681 | break; |
| 682 | case K_XLATE: |
| 683 | kbd->kbdmode = VC_XLATE; |
| 684 | compute_shiftstate(); |
| 685 | break; |
| 686 | case K_UNICODE: |
| 687 | kbd->kbdmode = VC_UNICODE; |
| 688 | compute_shiftstate(); |
| 689 | break; |
Arthur Taylor | 9fc3de9 | 2011-02-04 13:55:50 -0800 | [diff] [blame] | 690 | case K_OFF: |
| 691 | kbd->kbdmode = VC_OFF; |
| 692 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 694 | ret = -EINVAL; |
| 695 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | tty_ldisc_flush(tty); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 698 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
| 700 | case KDGKBMODE: |
Andrew Morton | 6da9e95f | 2011-04-07 16:13:01 -0700 | [diff] [blame^] | 701 | switch (kbd->kbdmode) { |
| 702 | case VC_RAW: |
| 703 | uival = K_RAW; |
| 704 | break; |
| 705 | case VC_MEDIUMRAW: |
| 706 | uival = K_MEDIUMRAW; |
| 707 | break; |
| 708 | case VC_UNICODE: |
| 709 | uival = K_UNICODE; |
| 710 | break; |
| 711 | case VC_OFF: |
| 712 | uival = K_OFF; |
| 713 | break; |
| 714 | default: |
| 715 | uival = K_XLATE; |
| 716 | break; |
| 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | goto setint; |
| 719 | |
| 720 | /* this could be folded into KDSKBMODE, but for compatibility |
| 721 | reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */ |
| 722 | case KDSKBMETA: |
| 723 | switch(arg) { |
| 724 | case K_METABIT: |
| 725 | clr_vc_kbd_mode(kbd, VC_META); |
| 726 | break; |
| 727 | case K_ESCPREFIX: |
| 728 | set_vc_kbd_mode(kbd, VC_META); |
| 729 | break; |
| 730 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 731 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 733 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | case KDGKBMETA: |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 736 | uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | setint: |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 738 | ret = put_user(uival, (int __user *)arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 739 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | case KDGETKEYCODE: |
| 742 | case KDSETKEYCODE: |
| 743 | if(!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 744 | perm = 0; |
| 745 | ret = do_kbkeycode_ioctl(cmd, up, perm); |
| 746 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | case KDGKBENT: |
| 749 | case KDSKBENT: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 750 | ret = do_kdsk_ioctl(cmd, up, perm, kbd); |
| 751 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | case KDGKBSENT: |
| 754 | case KDSKBSENT: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 755 | ret = do_kdgkb_ioctl(cmd, up, perm); |
| 756 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | case KDGKBDIACR: |
| 759 | { |
| 760 | struct kbdiacrs __user *a = up; |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 761 | struct kbdiacr diacr; |
| 762 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 764 | if (put_user(accent_table_size, &a->kb_cnt)) { |
| 765 | ret = -EFAULT; |
| 766 | break; |
| 767 | } |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 768 | for (i = 0; i < accent_table_size; i++) { |
| 769 | diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr); |
| 770 | diacr.base = conv_uni_to_8bit(accent_table[i].base); |
| 771 | diacr.result = conv_uni_to_8bit(accent_table[i].result); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 772 | if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) { |
| 773 | ret = -EFAULT; |
| 774 | break; |
| 775 | } |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 776 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 777 | break; |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 778 | } |
| 779 | case KDGKBDIACRUC: |
| 780 | { |
| 781 | struct kbdiacrsuc __user *a = up; |
| 782 | |
| 783 | if (put_user(accent_table_size, &a->kb_cnt)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 784 | ret = -EFAULT; |
| 785 | else if (copy_to_user(a->kbdiacruc, accent_table, |
| 786 | accent_table_size*sizeof(struct kbdiacruc))) |
| 787 | ret = -EFAULT; |
| 788 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | case KDSKBDIACR: |
| 792 | { |
| 793 | struct kbdiacrs __user *a = up; |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 794 | struct kbdiacr diacr; |
| 795 | unsigned int ct; |
| 796 | int i; |
| 797 | |
| 798 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 799 | goto eperm; |
| 800 | if (get_user(ct,&a->kb_cnt)) { |
| 801 | ret = -EFAULT; |
| 802 | break; |
| 803 | } |
| 804 | if (ct >= MAX_DIACR) { |
| 805 | ret = -EINVAL; |
| 806 | break; |
| 807 | } |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 808 | accent_table_size = ct; |
| 809 | for (i = 0; i < ct; i++) { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 810 | if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) { |
| 811 | ret = -EFAULT; |
| 812 | break; |
| 813 | } |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 814 | accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr); |
| 815 | accent_table[i].base = conv_8bit_to_uni(diacr.base); |
| 816 | accent_table[i].result = conv_8bit_to_uni(diacr.result); |
| 817 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 818 | break; |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | case KDSKBDIACRUC: |
| 822 | { |
| 823 | struct kbdiacrsuc __user *a = up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | unsigned int ct; |
| 825 | |
| 826 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 827 | goto eperm; |
| 828 | if (get_user(ct,&a->kb_cnt)) { |
| 829 | ret = -EFAULT; |
| 830 | break; |
| 831 | } |
| 832 | if (ct >= MAX_DIACR) { |
| 833 | ret = -EINVAL; |
| 834 | break; |
| 835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | accent_table_size = ct; |
Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 837 | if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc))) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 838 | ret = -EFAULT; |
| 839 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* the ioctls below read/set the flags usually shown in the leds */ |
| 843 | /* don't use them - they will go away without warning */ |
| 844 | case KDGKBLED: |
| 845 | ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4); |
| 846 | goto setchar; |
| 847 | |
| 848 | case KDSKBLED: |
| 849 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 850 | goto eperm; |
| 851 | if (arg & ~0x77) { |
| 852 | ret = -EINVAL; |
| 853 | break; |
| 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | kbd->ledflagstate = (arg & 7); |
| 856 | kbd->default_ledflagstate = ((arg >> 4) & 7); |
| 857 | set_leds(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 858 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | /* the ioctls below only set the lights, not the functions */ |
| 861 | /* for those, see KDGKBLED and KDSKBLED above */ |
| 862 | case KDGETLED: |
| 863 | ucval = getledstate(); |
| 864 | setchar: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 865 | ret = put_user(ucval, (char __user *)arg); |
| 866 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | case KDSETLED: |
| 869 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 870 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | setledstate(kbd, arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 872 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * A process can indicate its willingness to accept signals |
| 876 | * generated by pressing an appropriate key combination. |
| 877 | * Thus, one can have a daemon that e.g. spawns a new console |
| 878 | * upon a keypress and then changes to it. |
| 879 | * See also the kbrequest field of inittab(5). |
| 880 | */ |
| 881 | case KDSIGACCEPT: |
| 882 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if (!perm || !capable(CAP_KILL)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 884 | goto eperm; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 885 | if (!valid_signal(arg) || arg < 1 || arg == SIGKILL) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 886 | ret = -EINVAL; |
| 887 | else { |
| 888 | spin_lock_irq(&vt_spawn_con.lock); |
| 889 | put_pid(vt_spawn_con.pid); |
| 890 | vt_spawn_con.pid = get_pid(task_pid(current)); |
| 891 | vt_spawn_con.sig = arg; |
| 892 | spin_unlock_irq(&vt_spawn_con.lock); |
| 893 | } |
| 894 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | case VT_SETMODE: |
| 898 | { |
| 899 | struct vt_mode tmp; |
| 900 | |
| 901 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 902 | goto eperm; |
| 903 | if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) { |
| 904 | ret = -EFAULT; |
| 905 | goto out; |
| 906 | } |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 907 | if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 908 | ret = -EINVAL; |
| 909 | goto out; |
| 910 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 911 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | vc->vt_mode = tmp; |
| 913 | /* the frsig is ignored, so we set it to 0 */ |
| 914 | vc->vt_mode.frsig = 0; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 915 | put_pid(vc->vt_pid); |
| 916 | vc->vt_pid = get_pid(task_pid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | /* no switch is required -- saw@shade.msu.ru */ |
| 918 | vc->vt_newvt = -1; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 919 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 920 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | case VT_GETMODE: |
| 924 | { |
| 925 | struct vt_mode tmp; |
| 926 | int rc; |
| 927 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 928 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 930 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
| 932 | rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 933 | if (rc) |
| 934 | ret = -EFAULT; |
| 935 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Returns global vt state. Note that VT 0 is always open, since |
| 940 | * it's an alias for the current VT, and people can't use it here. |
| 941 | * We cannot return state for more than 16 VTs, since v_state is short. |
| 942 | */ |
| 943 | case VT_GETSTATE: |
| 944 | { |
| 945 | struct vt_stat __user *vtstat = up; |
| 946 | unsigned short state, mask; |
| 947 | |
| 948 | if (put_user(fg_console + 1, &vtstat->v_active)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 949 | ret = -EFAULT; |
| 950 | else { |
| 951 | state = 1; /* /dev/tty0 is always open */ |
| 952 | for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; |
| 953 | ++i, mask <<= 1) |
| 954 | if (VT_IS_IN_USE(i)) |
| 955 | state |= mask; |
| 956 | ret = put_user(state, &vtstat->v_state); |
| 957 | } |
| 958 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Returns the first available (non-opened) console. |
| 963 | */ |
| 964 | case VT_OPENQRY: |
| 965 | for (i = 0; i < MAX_NR_CONSOLES; ++i) |
| 966 | if (! VT_IS_IN_USE(i)) |
| 967 | break; |
Graham Gower | 1e0ad28 | 2010-10-27 15:33:00 -0700 | [diff] [blame] | 968 | uival = i < MAX_NR_CONSOLES ? (i+1) : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | goto setint; |
| 970 | |
| 971 | /* |
| 972 | * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num, |
| 973 | * with num >= 1 (switches to vt 0, our console, are not allowed, just |
| 974 | * to preserve sanity). |
| 975 | */ |
| 976 | case VT_ACTIVATE: |
| 977 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 978 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (arg == 0 || arg > MAX_NR_CONSOLES) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 980 | ret = -ENXIO; |
| 981 | else { |
| 982 | arg--; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 983 | console_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 984 | ret = vc_allocate(arg); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 985 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 986 | if (ret) |
| 987 | break; |
| 988 | set_console(arg); |
| 989 | } |
| 990 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 992 | case VT_SETACTIVATE: |
| 993 | { |
| 994 | struct vt_setactivate vsa; |
| 995 | |
| 996 | if (!perm) |
| 997 | goto eperm; |
| 998 | |
| 999 | if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg, |
Jiri Slaby | a09efb0 | 2009-10-01 15:43:59 -0700 | [diff] [blame] | 1000 | sizeof(struct vt_setactivate))) { |
| 1001 | ret = -EFAULT; |
| 1002 | goto out; |
| 1003 | } |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1004 | if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES) |
| 1005 | ret = -ENXIO; |
| 1006 | else { |
| 1007 | vsa.console--; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1008 | console_lock(); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1009 | ret = vc_allocate(vsa.console); |
| 1010 | if (ret == 0) { |
| 1011 | struct vc_data *nvc; |
| 1012 | /* This is safe providing we don't drop the |
| 1013 | console sem between vc_allocate and |
| 1014 | finishing referencing nvc */ |
| 1015 | nvc = vc_cons[vsa.console].d; |
| 1016 | nvc->vt_mode = vsa.mode; |
| 1017 | nvc->vt_mode.frsig = 0; |
| 1018 | put_pid(nvc->vt_pid); |
| 1019 | nvc->vt_pid = get_pid(task_pid(current)); |
| 1020 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1021 | console_unlock(); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1022 | if (ret) |
| 1023 | break; |
| 1024 | /* Commence switch and lock */ |
Jiri Olsa | d637837 | 2011-02-11 15:39:28 +0100 | [diff] [blame] | 1025 | set_console(vsa.console); |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1026 | } |
Jiri Olsa | d637837 | 2011-02-11 15:39:28 +0100 | [diff] [blame] | 1027 | break; |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | /* |
| 1031 | * wait until the specified VT has been activated |
| 1032 | */ |
| 1033 | case VT_WAITACTIVE: |
| 1034 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1035 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (arg == 0 || arg > MAX_NR_CONSOLES) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1037 | ret = -ENXIO; |
| 1038 | else |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1039 | ret = vt_waitactive(arg); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1040 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | /* |
| 1043 | * If a vt is under process control, the kernel will not switch to it |
| 1044 | * immediately, but postpone the operation until the process calls this |
| 1045 | * ioctl, allowing the switch to complete. |
| 1046 | * |
| 1047 | * According to the X sources this is the behavior: |
| 1048 | * 0: pending switch-from not OK |
| 1049 | * 1: pending switch-from OK |
| 1050 | * 2: completed switch-to OK |
| 1051 | */ |
| 1052 | case VT_RELDISP: |
| 1053 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1054 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1056 | if (vc->vt_mode.mode != VT_PROCESS) { |
| 1057 | ret = -EINVAL; |
| 1058 | break; |
| 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * Switching-from response |
| 1062 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1063 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | if (vc->vt_newvt >= 0) { |
| 1065 | if (arg == 0) |
| 1066 | /* |
| 1067 | * Switch disallowed, so forget we were trying |
| 1068 | * to do it. |
| 1069 | */ |
| 1070 | vc->vt_newvt = -1; |
| 1071 | |
| 1072 | else { |
| 1073 | /* |
| 1074 | * The current vt has been released, so |
| 1075 | * complete the switch. |
| 1076 | */ |
| 1077 | int newvt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | newvt = vc->vt_newvt; |
| 1079 | vc->vt_newvt = -1; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1080 | ret = vc_allocate(newvt); |
| 1081 | if (ret) { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1082 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1083 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | } |
| 1085 | /* |
| 1086 | * When we actually do the console switch, |
| 1087 | * make sure we are atomic with respect to |
| 1088 | * other console switches.. |
| 1089 | */ |
| 1090 | complete_change_console(vc_cons[newvt].d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1092 | } else { |
| 1093 | /* |
| 1094 | * Switched-to response |
| 1095 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* |
| 1097 | * If it's just an ACK, ignore it |
| 1098 | */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1099 | if (arg != VT_ACKACQ) |
| 1100 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1102 | console_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1103 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
| 1105 | /* |
| 1106 | * Disallocate memory associated to VT (but leave VT1) |
| 1107 | */ |
| 1108 | case VT_DISALLOCATE: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1109 | if (arg > MAX_NR_CONSOLES) { |
| 1110 | ret = -ENXIO; |
| 1111 | break; |
| 1112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (arg == 0) { |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 1114 | /* deallocate all unused consoles, but leave 0 */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1115 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | for (i=1; i<MAX_NR_CONSOLES; i++) |
| 1117 | if (! VT_BUSY(i)) |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 1118 | vc_deallocate(i); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1119 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } else { |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 1121 | /* deallocate a single console, if possible */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | arg--; |
| 1123 | if (VT_BUSY(arg)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1124 | ret = -EBUSY; |
| 1125 | else if (arg) { /* leave 0 */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1126 | console_lock(); |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 1127 | vc_deallocate(arg); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1128 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
| 1130 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1131 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
| 1133 | case VT_RESIZE: |
| 1134 | { |
| 1135 | struct vt_sizes __user *vtsizes = up; |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1136 | struct vc_data *vc; |
| 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | ushort ll,cc; |
| 1139 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1140 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | if (get_user(ll, &vtsizes->v_rows) || |
| 1142 | get_user(cc, &vtsizes->v_cols)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1143 | ret = -EFAULT; |
| 1144 | else { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1145 | console_lock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1146 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
| 1147 | vc = vc_cons[i].d; |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1148 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1149 | if (vc) { |
| 1150 | vc->vc_resize_user = 1; |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 1151 | vc_resize(vc_cons[i].d, cc, ll); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1152 | } |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1153 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1154 | console_unlock(); |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1155 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | case VT_RESIZEX: |
| 1160 | { |
| 1161 | struct vt_consize __user *vtconsize = up; |
| 1162 | ushort ll,cc,vlin,clin,vcol,ccol; |
| 1163 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1164 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | if (!access_ok(VERIFY_READ, vtconsize, |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1166 | sizeof(struct vt_consize))) { |
| 1167 | ret = -EFAULT; |
| 1168 | break; |
| 1169 | } |
| 1170 | /* FIXME: Should check the copies properly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | __get_user(ll, &vtconsize->v_rows); |
| 1172 | __get_user(cc, &vtconsize->v_cols); |
| 1173 | __get_user(vlin, &vtconsize->v_vlin); |
| 1174 | __get_user(clin, &vtconsize->v_clin); |
| 1175 | __get_user(vcol, &vtconsize->v_vcol); |
| 1176 | __get_user(ccol, &vtconsize->v_ccol); |
| 1177 | vlin = vlin ? vlin : vc->vc_scan_lines; |
| 1178 | if (clin) { |
| 1179 | if (ll) { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1180 | if (ll != vlin/clin) { |
| 1181 | /* Parameters don't add up */ |
| 1182 | ret = -EINVAL; |
| 1183 | break; |
| 1184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } else |
| 1186 | ll = vlin/clin; |
| 1187 | } |
| 1188 | if (vcol && ccol) { |
| 1189 | if (cc) { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1190 | if (cc != vcol/ccol) { |
| 1191 | ret = -EINVAL; |
| 1192 | break; |
| 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } else |
| 1195 | cc = vcol/ccol; |
| 1196 | } |
| 1197 | |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1198 | if (clin > 32) { |
| 1199 | ret = -EINVAL; |
| 1200 | break; |
| 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
| 1204 | if (!vc_cons[i].d) |
| 1205 | continue; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1206 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | if (vlin) |
| 1208 | vc_cons[i].d->vc_scan_lines = vlin; |
| 1209 | if (clin) |
| 1210 | vc_cons[i].d->vc_font.height = clin; |
Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1211 | vc_cons[i].d->vc_resize_user = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | vc_resize(vc_cons[i].d, cc, ll); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1213 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1215 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | case PIO_FONT: { |
| 1219 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1220 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | op.op = KD_FONT_OP_SET; |
| 1222 | op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */ |
| 1223 | op.width = 8; |
| 1224 | op.height = 0; |
| 1225 | op.charcount = 256; |
| 1226 | op.data = up; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1227 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 1228 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | case GIO_FONT: { |
| 1232 | op.op = KD_FONT_OP_GET; |
| 1233 | op.flags = KD_FONT_FLAG_OLD; |
| 1234 | op.width = 8; |
| 1235 | op.height = 32; |
| 1236 | op.charcount = 256; |
| 1237 | op.data = up; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1238 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 1239 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | case PIO_CMAP: |
| 1243 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1244 | ret = -EPERM; |
| 1245 | else |
| 1246 | ret = con_set_cmap(up); |
| 1247 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | case GIO_CMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1250 | ret = con_get_cmap(up); |
| 1251 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
| 1253 | case PIO_FONTX: |
| 1254 | case GIO_FONTX: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1255 | ret = do_fontx_ioctl(cmd, up, perm, &op); |
| 1256 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | case PIO_FONTRESET: |
| 1259 | { |
| 1260 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1261 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | #ifdef BROKEN_GRAPHICS_PROGRAMS |
| 1264 | /* With BROKEN_GRAPHICS_PROGRAMS defined, the default |
| 1265 | font is not saved. */ |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1266 | ret = -ENOSYS; |
| 1267 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | #else |
| 1269 | { |
| 1270 | op.op = KD_FONT_OP_SET_DEFAULT; |
| 1271 | op.data = NULL; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1272 | ret = con_font_op(vc_cons[fg_console].d, &op); |
| 1273 | if (ret) |
| 1274 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | con_set_default_unimap(vc_cons[fg_console].d); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1276 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | } |
| 1278 | #endif |
| 1279 | } |
| 1280 | |
| 1281 | case KDFONTOP: { |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1282 | if (copy_from_user(&op, up, sizeof(op))) { |
| 1283 | ret = -EFAULT; |
| 1284 | break; |
| 1285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | if (!perm && op.op != KD_FONT_OP_GET) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1287 | goto eperm; |
| 1288 | ret = con_font_op(vc, &op); |
| 1289 | if (ret) |
| 1290 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | if (copy_to_user(up, &op, sizeof(op))) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1292 | ret = -EFAULT; |
| 1293 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | case PIO_SCRNMAP: |
| 1297 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1298 | ret = -EPERM; |
| 1299 | else |
| 1300 | ret = con_set_trans_old(up); |
| 1301 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | case GIO_SCRNMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1304 | ret = con_get_trans_old(up); |
| 1305 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
| 1307 | case PIO_UNISCRNMAP: |
| 1308 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1309 | ret = -EPERM; |
| 1310 | else |
| 1311 | ret = con_set_trans_new(up); |
| 1312 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
| 1314 | case GIO_UNISCRNMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1315 | ret = con_get_trans_new(up); |
| 1316 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
| 1318 | case PIO_UNIMAPCLR: |
| 1319 | { struct unimapinit ui; |
| 1320 | if (!perm) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1321 | goto eperm; |
| 1322 | ret = copy_from_user(&ui, up, sizeof(struct unimapinit)); |
Dan Carpenter | 3fde85d | 2010-06-04 12:20:46 +0200 | [diff] [blame] | 1323 | if (ret) |
| 1324 | ret = -EFAULT; |
| 1325 | else |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1326 | con_clear_unimap(vc, &ui); |
| 1327 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | case PIO_UNIMAP: |
| 1331 | case GIO_UNIMAP: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1332 | ret = do_unimap_ioctl(cmd, up, perm, vc); |
| 1333 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
| 1335 | case VT_LOCKSWITCH: |
| 1336 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1337 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | vt_dont_switch = 1; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1339 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | case VT_UNLOCKSWITCH: |
| 1341 | if (!capable(CAP_SYS_TTY_CONFIG)) |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1342 | goto eperm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | vt_dont_switch = 0; |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1344 | break; |
Samuel Thibault | 533475d | 2006-08-27 01:23:39 -0700 | [diff] [blame] | 1345 | case VT_GETHIFONTMASK: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1346 | ret = put_user(vc->vc_hi_font_mask, |
| 1347 | (unsigned short __user *)arg); |
| 1348 | break; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1349 | case VT_WAITEVENT: |
| 1350 | ret = vt_event_wait_ioctl((struct vt_event __user *)arg); |
| 1351 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | default: |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1353 | ret = -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1355 | out: |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1356 | tty_unlock(); |
Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1357 | return ret; |
| 1358 | eperm: |
| 1359 | ret = -EPERM; |
| 1360 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | void reset_vc(struct vc_data *vc) |
| 1364 | { |
| 1365 | vc->vc_mode = KD_TEXT; |
Bill Nottingham | 2e8ecb9 | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1366 | kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | vc->vt_mode.mode = VT_AUTO; |
| 1368 | vc->vt_mode.waitv = 0; |
| 1369 | vc->vt_mode.relsig = 0; |
| 1370 | vc->vt_mode.acqsig = 0; |
| 1371 | vc->vt_mode.frsig = 0; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1372 | put_pid(vc->vt_pid); |
| 1373 | vc->vt_pid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | vc->vt_newvt = -1; |
| 1375 | if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */ |
| 1376 | reset_palette(vc); |
| 1377 | } |
| 1378 | |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1379 | void vc_SAK(struct work_struct *work) |
| 1380 | { |
| 1381 | struct vc *vc_con = |
| 1382 | container_of(work, struct vc, SAK_work); |
| 1383 | struct vc_data *vc; |
| 1384 | struct tty_struct *tty; |
| 1385 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1386 | console_lock(); |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1387 | vc = vc_con->d; |
| 1388 | if (vc) { |
Alan Cox | 8ce7326 | 2010-06-01 22:52:56 +0200 | [diff] [blame] | 1389 | tty = vc->port.tty; |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1390 | /* |
| 1391 | * SAK should also work in all raw modes and reset |
| 1392 | * them properly. |
| 1393 | */ |
| 1394 | if (tty) |
| 1395 | __do_SAK(tty); |
| 1396 | reset_vc(vc); |
| 1397 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1398 | console_unlock(); |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1401 | #ifdef CONFIG_COMPAT |
| 1402 | |
| 1403 | struct compat_consolefontdesc { |
| 1404 | unsigned short charcount; /* characters in font (256 or 512) */ |
| 1405 | unsigned short charheight; /* scan lines per character (1-32) */ |
| 1406 | compat_caddr_t chardata; /* font data in expanded form */ |
| 1407 | }; |
| 1408 | |
| 1409 | static inline int |
| 1410 | compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd, |
| 1411 | int perm, struct console_font_op *op) |
| 1412 | { |
| 1413 | struct compat_consolefontdesc cfdarg; |
| 1414 | int i; |
| 1415 | |
| 1416 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc))) |
| 1417 | return -EFAULT; |
| 1418 | |
| 1419 | switch (cmd) { |
| 1420 | case PIO_FONTX: |
| 1421 | if (!perm) |
| 1422 | return -EPERM; |
| 1423 | op->op = KD_FONT_OP_SET; |
| 1424 | op->flags = KD_FONT_FLAG_OLD; |
| 1425 | op->width = 8; |
| 1426 | op->height = cfdarg.charheight; |
| 1427 | op->charcount = cfdarg.charcount; |
| 1428 | op->data = compat_ptr(cfdarg.chardata); |
| 1429 | return con_font_op(vc_cons[fg_console].d, op); |
| 1430 | case GIO_FONTX: |
| 1431 | op->op = KD_FONT_OP_GET; |
| 1432 | op->flags = KD_FONT_FLAG_OLD; |
| 1433 | op->width = 8; |
| 1434 | op->height = cfdarg.charheight; |
| 1435 | op->charcount = cfdarg.charcount; |
| 1436 | op->data = compat_ptr(cfdarg.chardata); |
| 1437 | i = con_font_op(vc_cons[fg_console].d, op); |
| 1438 | if (i) |
| 1439 | return i; |
| 1440 | cfdarg.charheight = op->height; |
| 1441 | cfdarg.charcount = op->charcount; |
| 1442 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc))) |
| 1443 | return -EFAULT; |
| 1444 | return 0; |
| 1445 | } |
| 1446 | return -EINVAL; |
| 1447 | } |
| 1448 | |
| 1449 | struct compat_console_font_op { |
| 1450 | compat_uint_t op; /* operation code KD_FONT_OP_* */ |
| 1451 | compat_uint_t flags; /* KD_FONT_FLAG_* */ |
| 1452 | compat_uint_t width, height; /* font size */ |
| 1453 | compat_uint_t charcount; |
| 1454 | compat_caddr_t data; /* font data with height fixed to 32 */ |
| 1455 | }; |
| 1456 | |
| 1457 | static inline int |
| 1458 | compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop, |
| 1459 | int perm, struct console_font_op *op, struct vc_data *vc) |
| 1460 | { |
| 1461 | int i; |
| 1462 | |
| 1463 | if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op))) |
| 1464 | return -EFAULT; |
| 1465 | if (!perm && op->op != KD_FONT_OP_GET) |
| 1466 | return -EPERM; |
| 1467 | op->data = compat_ptr(((struct compat_console_font_op *)op)->data); |
| 1468 | op->flags |= KD_FONT_FLAG_OLD; |
| 1469 | i = con_font_op(vc, op); |
| 1470 | if (i) |
| 1471 | return i; |
| 1472 | ((struct compat_console_font_op *)op)->data = (unsigned long)op->data; |
| 1473 | if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op))) |
| 1474 | return -EFAULT; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | struct compat_unimapdesc { |
| 1479 | unsigned short entry_ct; |
| 1480 | compat_caddr_t entries; |
| 1481 | }; |
| 1482 | |
| 1483 | static inline int |
| 1484 | compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud, |
| 1485 | int perm, struct vc_data *vc) |
| 1486 | { |
| 1487 | struct compat_unimapdesc tmp; |
| 1488 | struct unipair __user *tmp_entries; |
| 1489 | |
| 1490 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) |
| 1491 | return -EFAULT; |
| 1492 | tmp_entries = compat_ptr(tmp.entries); |
| 1493 | if (tmp_entries) |
| 1494 | if (!access_ok(VERIFY_WRITE, tmp_entries, |
| 1495 | tmp.entry_ct*sizeof(struct unipair))) |
| 1496 | return -EFAULT; |
| 1497 | switch (cmd) { |
| 1498 | case PIO_UNIMAP: |
| 1499 | if (!perm) |
| 1500 | return -EPERM; |
| 1501 | return con_set_unimap(vc, tmp.entry_ct, tmp_entries); |
| 1502 | case GIO_UNIMAP: |
| 1503 | if (!perm && fg_console != vc->vc_num) |
| 1504 | return -EPERM; |
| 1505 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries); |
| 1506 | } |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1510 | long vt_compat_ioctl(struct tty_struct *tty, |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1511 | unsigned int cmd, unsigned long arg) |
| 1512 | { |
| 1513 | struct vc_data *vc = tty->driver_data; |
| 1514 | struct console_font_op op; /* used in multiple places here */ |
| 1515 | struct kbd_struct *kbd; |
| 1516 | unsigned int console; |
| 1517 | void __user *up = (void __user *)arg; |
| 1518 | int perm; |
| 1519 | int ret = 0; |
| 1520 | |
| 1521 | console = vc->vc_num; |
| 1522 | |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1523 | tty_lock(); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1524 | |
| 1525 | if (!vc_cons_allocated(console)) { /* impossible? */ |
| 1526 | ret = -ENOIOCTLCMD; |
| 1527 | goto out; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * To have permissions to do most of the vt ioctls, we either have |
| 1532 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. |
| 1533 | */ |
| 1534 | perm = 0; |
| 1535 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) |
| 1536 | perm = 1; |
| 1537 | |
| 1538 | kbd = kbd_table + console; |
| 1539 | switch (cmd) { |
| 1540 | /* |
| 1541 | * these need special handlers for incompatible data structures |
| 1542 | */ |
| 1543 | case PIO_FONTX: |
| 1544 | case GIO_FONTX: |
| 1545 | ret = compat_fontx_ioctl(cmd, up, perm, &op); |
| 1546 | break; |
| 1547 | |
| 1548 | case KDFONTOP: |
| 1549 | ret = compat_kdfontop_ioctl(up, perm, &op, vc); |
| 1550 | break; |
| 1551 | |
| 1552 | case PIO_UNIMAP: |
| 1553 | case GIO_UNIMAP: |
Andreas Schwab | 4b1fe77 | 2009-09-28 20:10:02 +0200 | [diff] [blame] | 1554 | ret = compat_unimap_ioctl(cmd, up, perm, vc); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1555 | break; |
| 1556 | |
| 1557 | /* |
| 1558 | * all these treat 'arg' as an integer |
| 1559 | */ |
| 1560 | case KIOCSOUND: |
| 1561 | case KDMKTONE: |
| 1562 | #ifdef CONFIG_X86 |
| 1563 | case KDADDIO: |
| 1564 | case KDDELIO: |
| 1565 | #endif |
| 1566 | case KDSETMODE: |
| 1567 | case KDMAPDISP: |
| 1568 | case KDUNMAPDISP: |
| 1569 | case KDSKBMODE: |
| 1570 | case KDSKBMETA: |
| 1571 | case KDSKBLED: |
| 1572 | case KDSETLED: |
| 1573 | case KDSIGACCEPT: |
| 1574 | case VT_ACTIVATE: |
| 1575 | case VT_WAITACTIVE: |
| 1576 | case VT_RELDISP: |
| 1577 | case VT_DISALLOCATE: |
| 1578 | case VT_RESIZE: |
| 1579 | case VT_RESIZEX: |
| 1580 | goto fallback; |
| 1581 | |
| 1582 | /* |
| 1583 | * the rest has a compatible data structure behind arg, |
| 1584 | * but we have to convert it to a proper 64 bit pointer. |
| 1585 | */ |
| 1586 | default: |
| 1587 | arg = (unsigned long)compat_ptr(arg); |
| 1588 | goto fallback; |
| 1589 | } |
| 1590 | out: |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1591 | tty_unlock(); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1592 | return ret; |
| 1593 | |
| 1594 | fallback: |
Arnd Bergmann | ec79d60 | 2010-06-01 22:53:01 +0200 | [diff] [blame] | 1595 | tty_unlock(); |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1596 | return vt_ioctl(tty, cmd, arg); |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | |
| 1600 | #endif /* CONFIG_COMPAT */ |
| 1601 | |
| 1602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | /* |
Alan Cox | d3b5cff | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 1604 | * Performs the back end of a vt switch. Called under the console |
| 1605 | * semaphore. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | */ |
| 1607 | static void complete_change_console(struct vc_data *vc) |
| 1608 | { |
| 1609 | unsigned char old_vc_mode; |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1610 | int old = fg_console; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | last_console = fg_console; |
| 1613 | |
| 1614 | /* |
| 1615 | * If we're switching, we could be going from KD_GRAPHICS to |
| 1616 | * KD_TEXT mode or vice versa, which means we need to blank or |
| 1617 | * unblank the screen later. |
| 1618 | */ |
| 1619 | old_vc_mode = vc_cons[fg_console].d->vc_mode; |
| 1620 | switch_screen(vc); |
| 1621 | |
| 1622 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1623 | * This can't appear below a successful kill_pid(). If it did, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | * then the *blank_screen operation could occur while X, having |
| 1625 | * received acqsig, is waking up on another processor. This |
| 1626 | * condition can lead to overlapping accesses to the VGA range |
| 1627 | * and the framebuffer (causing system lockups). |
| 1628 | * |
| 1629 | * To account for this we duplicate this code below only if the |
| 1630 | * controlling process is gone and we've called reset_vc. |
| 1631 | */ |
| 1632 | if (old_vc_mode != vc->vc_mode) { |
| 1633 | if (vc->vc_mode == KD_TEXT) |
| 1634 | do_unblank_screen(1); |
| 1635 | else |
| 1636 | do_blank_screen(1); |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * If this new console is under process control, send it a signal |
| 1641 | * telling it that it has acquired. Also check if it has died and |
| 1642 | * clean up (similar to logic employed in change_console()) |
| 1643 | */ |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1644 | if (vc->vt_mode.mode == VT_PROCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1646 | * Send the signal as privileged - kill_pid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | * tell us if the process has gone or something else |
| 1648 | * is awry |
| 1649 | */ |
Eric W. Biederman | bde0d2c | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1650 | if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | /* |
| 1652 | * The controlling process has died, so we revert back to |
| 1653 | * normal operation. In this case, we'll also change back |
| 1654 | * to KD_TEXT mode. I'm not sure if this is strictly correct |
| 1655 | * but it saves the agony when the X server dies and the screen |
| 1656 | * remains blanked due to KD_GRAPHICS! It would be nice to do |
| 1657 | * this outside of VT_PROCESS but there is no single process |
| 1658 | * to account for and tracking tty count may be undesirable. |
| 1659 | */ |
| 1660 | reset_vc(vc); |
| 1661 | |
| 1662 | if (old_vc_mode != vc->vc_mode) { |
| 1663 | if (vc->vc_mode == KD_TEXT) |
| 1664 | do_unblank_screen(1); |
| 1665 | else |
| 1666 | do_blank_screen(1); |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * Wake anyone waiting for their VT to activate |
| 1673 | */ |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 1674 | vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Performs the front-end of a vt switch |
| 1680 | */ |
| 1681 | void change_console(struct vc_data *new_vc) |
| 1682 | { |
| 1683 | struct vc_data *vc; |
| 1684 | |
| 1685 | if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch) |
| 1686 | return; |
| 1687 | |
| 1688 | /* |
| 1689 | * If this vt is in process mode, then we need to handshake with |
| 1690 | * that process before switching. Essentially, we store where that |
| 1691 | * vt wants to switch to and wait for it to tell us when it's done |
| 1692 | * (via VT_RELDISP ioctl). |
| 1693 | * |
| 1694 | * We also check to see if the controlling process still exists. |
| 1695 | * If it doesn't, we reset this vt to auto mode and continue. |
| 1696 | * This is a cheap way to track process control. The worst thing |
| 1697 | * that can happen is: we send a signal to a process, it dies, and |
| 1698 | * the switch gets "lost" waiting for a response; hopefully, the |
| 1699 | * user will try again, we'll detect the process is gone (unless |
| 1700 | * the user waits just the right amount of time :-) and revert the |
| 1701 | * vt to auto control. |
| 1702 | */ |
| 1703 | vc = vc_cons[fg_console].d; |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1704 | if (vc->vt_mode.mode == VT_PROCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | /* |
Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1706 | * Send the signal as privileged - kill_pid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | * tell us if the process has gone or something else |
Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1708 | * is awry. |
| 1709 | * |
| 1710 | * We need to set vt_newvt *before* sending the signal or we |
| 1711 | * have a race. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | */ |
Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1713 | vc->vt_newvt = new_vc->vc_num; |
Eric W. Biederman | bde0d2c | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1714 | if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | /* |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1716 | * It worked. Mark the vt to switch to and |
| 1717 | * return. The process needs to send us a |
| 1718 | * VT_RELDISP ioctl to complete the switch. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | */ |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1720 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /* |
Greg Kroah-Hartman | 87a6aca | 2010-03-15 17:14:15 -0700 | [diff] [blame] | 1724 | * The controlling process has died, so we revert back to |
| 1725 | * normal operation. In this case, we'll also change back |
| 1726 | * to KD_TEXT mode. I'm not sure if this is strictly correct |
| 1727 | * but it saves the agony when the X server dies and the screen |
| 1728 | * remains blanked due to KD_GRAPHICS! It would be nice to do |
| 1729 | * this outside of VT_PROCESS but there is no single process |
| 1730 | * to account for and tracking tty count may be undesirable. |
| 1731 | */ |
| 1732 | reset_vc(vc); |
| 1733 | |
| 1734 | /* |
| 1735 | * Fall through to normal (VT_AUTO) handling of the switch... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | */ |
| 1737 | } |
| 1738 | |
| 1739 | /* |
| 1740 | * Ignore all switches in KD_GRAPHICS+VT_AUTO mode |
| 1741 | */ |
| 1742 | if (vc->vc_mode == KD_GRAPHICS) |
| 1743 | return; |
| 1744 | |
| 1745 | complete_change_console(new_vc); |
| 1746 | } |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1747 | |
| 1748 | /* Perform a kernel triggered VT switch for suspend/resume */ |
| 1749 | |
| 1750 | static int disable_vt_switch; |
| 1751 | |
| 1752 | int vt_move_to_console(unsigned int vt, int alloc) |
| 1753 | { |
| 1754 | int prev; |
| 1755 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1756 | console_lock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1757 | /* Graphics mode - up to X */ |
| 1758 | if (disable_vt_switch) { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1759 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | prev = fg_console; |
| 1763 | |
| 1764 | if (alloc && vc_allocate(vt)) { |
| 1765 | /* we can't have a free VC for now. Too bad, |
| 1766 | * we don't want to mess the screen for now. */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1767 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1768 | return -ENOSPC; |
| 1769 | } |
| 1770 | |
| 1771 | if (set_console(vt)) { |
| 1772 | /* |
| 1773 | * We're unable to switch to the SUSPEND_CONSOLE. |
| 1774 | * Let the calling function know so it can decide |
| 1775 | * what to do. |
| 1776 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1777 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1778 | return -EIO; |
| 1779 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1780 | console_unlock(); |
Arnd Bergmann | be1bc28 | 2010-06-01 22:53:05 +0200 | [diff] [blame] | 1781 | tty_lock(); |
Jiri Slaby | 797938b | 2009-08-11 23:20:41 +0200 | [diff] [blame] | 1782 | if (vt_waitactive(vt + 1)) { |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1783 | pr_debug("Suspend: Can't switch VCs."); |
Arnd Bergmann | be1bc28 | 2010-06-01 22:53:05 +0200 | [diff] [blame] | 1784 | tty_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1785 | return -EINTR; |
| 1786 | } |
Arnd Bergmann | be1bc28 | 2010-06-01 22:53:05 +0200 | [diff] [blame] | 1787 | tty_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1788 | return prev; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * Normally during a suspend, we allocate a new console and switch to it. |
| 1793 | * When we resume, we switch back to the original console. This switch |
| 1794 | * can be slow, so on systems where the framebuffer can handle restoration |
| 1795 | * of video registers anyways, there's little point in doing the console |
| 1796 | * switch. This function allows you to disable it by passing it '0'. |
| 1797 | */ |
| 1798 | void pm_set_vt_switch(int do_switch) |
| 1799 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1800 | console_lock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1801 | disable_vt_switch = !do_switch; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1802 | console_unlock(); |
Alan Cox | 8d23355 | 2009-09-19 13:13:25 -0700 | [diff] [blame] | 1803 | } |
| 1804 | EXPORT_SYMBOL(pm_set_vt_switch); |