Linux 2.6.30.8
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / vt_ioctl.c
blob7539bed0f7e07ec315e9dff21a3a0914161afee6
1 /*
2 * linux/drivers/char/vt_ioctl.c
4 * Copyright (C) 1992 obz under the linux copyright
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
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>
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>
26 #include <linux/consolemap.h>
27 #include <linux/signal.h>
28 #include <linux/timex.h>
30 #include <asm/io.h>
31 #include <asm/uaccess.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/vt_kern.h>
35 #include <linux/kbd_diacr.h>
36 #include <linux/selection.h>
38 char vt_dont_switch;
39 extern struct tty_driver *console_driver;
41 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
42 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
45 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
46 * experimentation and study of X386 SYSV handling.
48 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
49 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
50 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
51 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
52 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
53 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
54 * to the current console is done by the main ioctl code.
57 #ifdef CONFIG_X86
58 #include <linux/syscalls.h>
59 #endif
61 static void complete_change_console(struct vc_data *vc);
64 * these are the valid i/o ports we're allowed to change. they map all the
65 * video ports
67 #define GPFIRST 0x3b4
68 #define GPLAST 0x3df
69 #define GPNUM (GPLAST - GPFIRST + 1)
71 #define i (tmp.kb_index)
72 #define s (tmp.kb_table)
73 #define v (tmp.kb_value)
74 static inline int
75 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
77 struct kbentry tmp;
78 ushort *key_map, val, ov;
80 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
81 return -EFAULT;
83 if (!capable(CAP_SYS_TTY_CONFIG))
84 perm = 0;
86 switch (cmd) {
87 case KDGKBENT:
88 key_map = key_maps[s];
89 if (key_map) {
90 val = U(key_map[i]);
91 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
92 val = K_HOLE;
93 } else
94 val = (i ? K_HOLE : K_NOSUCHMAP);
95 return put_user(val, &user_kbe->kb_value);
96 case KDSKBENT:
97 if (!perm)
98 return -EPERM;
99 if (!i && v == K_NOSUCHMAP) {
100 /* deallocate map */
101 key_map = key_maps[s];
102 if (s && key_map) {
103 key_maps[s] = NULL;
104 if (key_map[0] == U(K_ALLOCATED)) {
105 kfree(key_map);
106 keymap_count--;
109 break;
112 if (KTYP(v) < NR_TYPES) {
113 if (KVAL(v) > max_vals[KTYP(v)])
114 return -EINVAL;
115 } else
116 if (kbd->kbdmode != VC_UNICODE)
117 return -EINVAL;
119 /* ++Geert: non-PC keyboards may generate keycode zero */
120 #if !defined(__mc68000__) && !defined(__powerpc__)
121 /* assignment to entry 0 only tests validity of args */
122 if (!i)
123 break;
124 #endif
126 if (!(key_map = key_maps[s])) {
127 int j;
129 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
130 !capable(CAP_SYS_RESOURCE))
131 return -EPERM;
133 key_map = kmalloc(sizeof(plain_map),
134 GFP_KERNEL);
135 if (!key_map)
136 return -ENOMEM;
137 key_maps[s] = key_map;
138 key_map[0] = U(K_ALLOCATED);
139 for (j = 1; j < NR_KEYS; j++)
140 key_map[j] = U(K_HOLE);
141 keymap_count++;
143 ov = U(key_map[i]);
144 if (v == ov)
145 break; /* nothing to do */
147 * Attention Key.
149 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
150 return -EPERM;
151 key_map[i] = U(v);
152 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
153 compute_shiftstate();
154 break;
156 return 0;
158 #undef i
159 #undef s
160 #undef v
162 static inline int
163 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
165 struct kbkeycode tmp;
166 int kc = 0;
168 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
169 return -EFAULT;
170 switch (cmd) {
171 case KDGETKEYCODE:
172 kc = getkeycode(tmp.scancode);
173 if (kc >= 0)
174 kc = put_user(kc, &user_kbkc->keycode);
175 break;
176 case KDSETKEYCODE:
177 if (!perm)
178 return -EPERM;
179 kc = setkeycode(tmp.scancode, tmp.keycode);
180 break;
182 return kc;
185 static inline int
186 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
188 struct kbsentry *kbs;
189 char *p;
190 u_char *q;
191 u_char __user *up;
192 int sz;
193 int delta;
194 char *first_free, *fj, *fnw;
195 int i, j, k;
196 int ret;
198 if (!capable(CAP_SYS_TTY_CONFIG))
199 perm = 0;
201 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
202 if (!kbs) {
203 ret = -ENOMEM;
204 goto reterr;
207 /* we mostly copy too much here (512bytes), but who cares ;) */
208 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
209 ret = -EFAULT;
210 goto reterr;
212 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
213 i = kbs->kb_func;
215 switch (cmd) {
216 case KDGKBSENT:
217 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
218 a struct member */
219 up = user_kdgkb->kb_string;
220 p = func_table[i];
221 if(p)
222 for ( ; *p && sz; p++, sz--)
223 if (put_user(*p, up++)) {
224 ret = -EFAULT;
225 goto reterr;
227 if (put_user('\0', up)) {
228 ret = -EFAULT;
229 goto reterr;
231 kfree(kbs);
232 return ((p && *p) ? -EOVERFLOW : 0);
233 case KDSKBSENT:
234 if (!perm) {
235 ret = -EPERM;
236 goto reterr;
239 q = func_table[i];
240 first_free = funcbufptr + (funcbufsize - funcbufleft);
241 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
243 if (j < MAX_NR_FUNC)
244 fj = func_table[j];
245 else
246 fj = first_free;
248 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
249 if (delta <= funcbufleft) { /* it fits in current buf */
250 if (j < MAX_NR_FUNC) {
251 memmove(fj + delta, fj, first_free - fj);
252 for (k = j; k < MAX_NR_FUNC; k++)
253 if (func_table[k])
254 func_table[k] += delta;
256 if (!q)
257 func_table[i] = fj;
258 funcbufleft -= delta;
259 } else { /* allocate a larger buffer */
260 sz = 256;
261 while (sz < funcbufsize - funcbufleft + delta)
262 sz <<= 1;
263 fnw = kmalloc(sz, GFP_KERNEL);
264 if(!fnw) {
265 ret = -ENOMEM;
266 goto reterr;
269 if (!q)
270 func_table[i] = fj;
271 if (fj > funcbufptr)
272 memmove(fnw, funcbufptr, fj - funcbufptr);
273 for (k = 0; k < j; k++)
274 if (func_table[k])
275 func_table[k] = fnw + (func_table[k] - funcbufptr);
277 if (first_free > fj) {
278 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
279 for (k = j; k < MAX_NR_FUNC; k++)
280 if (func_table[k])
281 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
283 if (funcbufptr != func_buf)
284 kfree(funcbufptr);
285 funcbufptr = fnw;
286 funcbufleft = funcbufleft - delta + sz - funcbufsize;
287 funcbufsize = sz;
289 strcpy(func_table[i], kbs->kb_string);
290 break;
292 ret = 0;
293 reterr:
294 kfree(kbs);
295 return ret;
298 static inline int
299 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
301 struct consolefontdesc cfdarg;
302 int i;
304 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
305 return -EFAULT;
307 switch (cmd) {
308 case PIO_FONTX:
309 if (!perm)
310 return -EPERM;
311 op->op = KD_FONT_OP_SET;
312 op->flags = KD_FONT_FLAG_OLD;
313 op->width = 8;
314 op->height = cfdarg.charheight;
315 op->charcount = cfdarg.charcount;
316 op->data = cfdarg.chardata;
317 return con_font_op(vc_cons[fg_console].d, op);
318 case GIO_FONTX: {
319 op->op = KD_FONT_OP_GET;
320 op->flags = KD_FONT_FLAG_OLD;
321 op->width = 8;
322 op->height = cfdarg.charheight;
323 op->charcount = cfdarg.charcount;
324 op->data = cfdarg.chardata;
325 i = con_font_op(vc_cons[fg_console].d, op);
326 if (i)
327 return i;
328 cfdarg.charheight = op->height;
329 cfdarg.charcount = op->charcount;
330 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
331 return -EFAULT;
332 return 0;
335 return -EINVAL;
338 static inline int
339 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
341 struct unimapdesc tmp;
343 if (copy_from_user(&tmp, user_ud, sizeof tmp))
344 return -EFAULT;
345 if (tmp.entries)
346 if (!access_ok(VERIFY_WRITE, tmp.entries,
347 tmp.entry_ct*sizeof(struct unipair)))
348 return -EFAULT;
349 switch (cmd) {
350 case PIO_UNIMAP:
351 if (!perm)
352 return -EPERM;
353 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
354 case GIO_UNIMAP:
355 if (!perm && fg_console != vc->vc_num)
356 return -EPERM;
357 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
359 return 0;
363 * We handle the console-specific ioctl's here. We allow the
364 * capability to modify any console, not just the fg_console.
366 int vt_ioctl(struct tty_struct *tty, struct file * file,
367 unsigned int cmd, unsigned long arg)
369 struct vc_data *vc = tty->driver_data;
370 struct console_font_op op; /* used in multiple places here */
371 struct kbd_struct * kbd;
372 unsigned int console;
373 unsigned char ucval;
374 void __user *up = (void __user *)arg;
375 int i, perm;
376 int ret = 0;
378 console = vc->vc_num;
380 lock_kernel();
382 if (!vc_cons_allocated(console)) { /* impossible? */
383 ret = -ENOIOCTLCMD;
384 goto out;
389 * To have permissions to do most of the vt ioctls, we either have
390 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
392 perm = 0;
393 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
394 perm = 1;
396 kbd = kbd_table + console;
397 switch (cmd) {
398 case TIOCLINUX:
399 ret = tioclinux(tty, arg);
400 break;
401 case KIOCSOUND:
402 if (!perm)
403 goto eperm;
404 /* FIXME: This is an old broken API but we need to keep it
405 supported and somehow separate the historic advertised
406 tick rate from any real one */
407 if (arg)
408 arg = CLOCK_TICK_RATE / arg;
409 kd_mksound(arg, 0);
410 break;
412 case KDMKTONE:
413 if (!perm)
414 goto eperm;
416 unsigned int ticks, count;
419 * Generate the tone for the appropriate number of ticks.
420 * If the time is zero, turn off sound ourselves.
422 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
423 count = ticks ? (arg & 0xffff) : 0;
424 /* FIXME: This is an old broken API but we need to keep it
425 supported and somehow separate the historic advertised
426 tick rate from any real one */
427 if (count)
428 count = CLOCK_TICK_RATE / count;
429 kd_mksound(count, ticks);
430 break;
433 case KDGKBTYPE:
435 * this is naive.
437 ucval = KB_101;
438 goto setchar;
441 * These cannot be implemented on any machine that implements
442 * ioperm() in user level (such as Alpha PCs) or not at all.
444 * XXX: you should never use these, just call ioperm directly..
446 #ifdef CONFIG_X86
447 case KDADDIO:
448 case KDDELIO:
450 * KDADDIO and KDDELIO may be able to add ports beyond what
451 * we reject here, but to be safe...
453 if (arg < GPFIRST || arg > GPLAST) {
454 ret = -EINVAL;
455 break;
457 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
458 break;
460 case KDENABIO:
461 case KDDISABIO:
462 ret = sys_ioperm(GPFIRST, GPNUM,
463 (cmd == KDENABIO)) ? -ENXIO : 0;
464 break;
465 #endif
467 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
469 case KDKBDREP:
471 struct kbd_repeat kbrep;
473 if (!capable(CAP_SYS_TTY_CONFIG))
474 goto eperm;
476 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
477 ret = -EFAULT;
478 break;
480 ret = kbd_rate(&kbrep);
481 if (ret)
482 break;
483 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
484 ret = -EFAULT;
485 break;
488 case KDSETMODE:
490 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
491 * doesn't do a whole lot. i'm not sure if it should do any
492 * restoration of modes or what...
494 * XXX It should at least call into the driver, fbdev's definitely
495 * need to restore their engine state. --BenH
497 if (!perm)
498 goto eperm;
499 switch (arg) {
500 case KD_GRAPHICS:
501 break;
502 case KD_TEXT0:
503 case KD_TEXT1:
504 arg = KD_TEXT;
505 case KD_TEXT:
506 break;
507 default:
508 ret = -EINVAL;
509 goto out;
511 if (vc->vc_mode == (unsigned char) arg)
512 break;
513 vc->vc_mode = (unsigned char) arg;
514 if (console != fg_console)
515 break;
517 * explicitly blank/unblank the screen if switching modes
519 acquire_console_sem();
520 if (arg == KD_TEXT)
521 do_unblank_screen(1);
522 else
523 do_blank_screen(1);
524 release_console_sem();
525 break;
527 case KDGETMODE:
528 ucval = vc->vc_mode;
529 goto setint;
531 case KDMAPDISP:
532 case KDUNMAPDISP:
534 * these work like a combination of mmap and KDENABIO.
535 * this could be easily finished.
537 ret = -EINVAL;
538 break;
540 case KDSKBMODE:
541 if (!perm)
542 goto eperm;
543 switch(arg) {
544 case K_RAW:
545 kbd->kbdmode = VC_RAW;
546 break;
547 case K_MEDIUMRAW:
548 kbd->kbdmode = VC_MEDIUMRAW;
549 break;
550 case K_XLATE:
551 kbd->kbdmode = VC_XLATE;
552 compute_shiftstate();
553 break;
554 case K_UNICODE:
555 kbd->kbdmode = VC_UNICODE;
556 compute_shiftstate();
557 break;
558 default:
559 ret = -EINVAL;
560 goto out;
562 tty_ldisc_flush(tty);
563 break;
565 case KDGKBMODE:
566 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
567 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
568 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
569 K_XLATE);
570 goto setint;
572 /* this could be folded into KDSKBMODE, but for compatibility
573 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
574 case KDSKBMETA:
575 switch(arg) {
576 case K_METABIT:
577 clr_vc_kbd_mode(kbd, VC_META);
578 break;
579 case K_ESCPREFIX:
580 set_vc_kbd_mode(kbd, VC_META);
581 break;
582 default:
583 ret = -EINVAL;
585 break;
587 case KDGKBMETA:
588 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
589 setint:
590 ret = put_user(ucval, (int __user *)arg);
591 break;
593 case KDGETKEYCODE:
594 case KDSETKEYCODE:
595 if(!capable(CAP_SYS_TTY_CONFIG))
596 perm = 0;
597 ret = do_kbkeycode_ioctl(cmd, up, perm);
598 break;
600 case KDGKBENT:
601 case KDSKBENT:
602 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
603 break;
605 case KDGKBSENT:
606 case KDSKBSENT:
607 ret = do_kdgkb_ioctl(cmd, up, perm);
608 break;
610 case KDGKBDIACR:
612 struct kbdiacrs __user *a = up;
613 struct kbdiacr diacr;
614 int i;
616 if (put_user(accent_table_size, &a->kb_cnt)) {
617 ret = -EFAULT;
618 break;
620 for (i = 0; i < accent_table_size; i++) {
621 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
622 diacr.base = conv_uni_to_8bit(accent_table[i].base);
623 diacr.result = conv_uni_to_8bit(accent_table[i].result);
624 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
625 ret = -EFAULT;
626 break;
629 break;
631 case KDGKBDIACRUC:
633 struct kbdiacrsuc __user *a = up;
635 if (put_user(accent_table_size, &a->kb_cnt))
636 ret = -EFAULT;
637 else if (copy_to_user(a->kbdiacruc, accent_table,
638 accent_table_size*sizeof(struct kbdiacruc)))
639 ret = -EFAULT;
640 break;
643 case KDSKBDIACR:
645 struct kbdiacrs __user *a = up;
646 struct kbdiacr diacr;
647 unsigned int ct;
648 int i;
650 if (!perm)
651 goto eperm;
652 if (get_user(ct,&a->kb_cnt)) {
653 ret = -EFAULT;
654 break;
656 if (ct >= MAX_DIACR) {
657 ret = -EINVAL;
658 break;
660 accent_table_size = ct;
661 for (i = 0; i < ct; i++) {
662 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
663 ret = -EFAULT;
664 break;
666 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
667 accent_table[i].base = conv_8bit_to_uni(diacr.base);
668 accent_table[i].result = conv_8bit_to_uni(diacr.result);
670 break;
673 case KDSKBDIACRUC:
675 struct kbdiacrsuc __user *a = up;
676 unsigned int ct;
678 if (!perm)
679 goto eperm;
680 if (get_user(ct,&a->kb_cnt)) {
681 ret = -EFAULT;
682 break;
684 if (ct >= MAX_DIACR) {
685 ret = -EINVAL;
686 break;
688 accent_table_size = ct;
689 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
690 ret = -EFAULT;
691 break;
694 /* the ioctls below read/set the flags usually shown in the leds */
695 /* don't use them - they will go away without warning */
696 case KDGKBLED:
697 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
698 goto setchar;
700 case KDSKBLED:
701 if (!perm)
702 goto eperm;
703 if (arg & ~0x77) {
704 ret = -EINVAL;
705 break;
707 kbd->ledflagstate = (arg & 7);
708 kbd->default_ledflagstate = ((arg >> 4) & 7);
709 set_leds();
710 break;
712 /* the ioctls below only set the lights, not the functions */
713 /* for those, see KDGKBLED and KDSKBLED above */
714 case KDGETLED:
715 ucval = getledstate();
716 setchar:
717 ret = put_user(ucval, (char __user *)arg);
718 break;
720 case KDSETLED:
721 if (!perm)
722 goto eperm;
723 setledstate(kbd, arg);
724 break;
727 * A process can indicate its willingness to accept signals
728 * generated by pressing an appropriate key combination.
729 * Thus, one can have a daemon that e.g. spawns a new console
730 * upon a keypress and then changes to it.
731 * See also the kbrequest field of inittab(5).
733 case KDSIGACCEPT:
735 if (!perm || !capable(CAP_KILL))
736 goto eperm;
737 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
738 ret = -EINVAL;
739 else {
740 spin_lock_irq(&vt_spawn_con.lock);
741 put_pid(vt_spawn_con.pid);
742 vt_spawn_con.pid = get_pid(task_pid(current));
743 vt_spawn_con.sig = arg;
744 spin_unlock_irq(&vt_spawn_con.lock);
746 break;
749 case VT_SETMODE:
751 struct vt_mode tmp;
753 if (!perm)
754 goto eperm;
755 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
756 ret = -EFAULT;
757 goto out;
759 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
760 ret = -EINVAL;
761 goto out;
763 acquire_console_sem();
764 vc->vt_mode = tmp;
765 /* the frsig is ignored, so we set it to 0 */
766 vc->vt_mode.frsig = 0;
767 put_pid(vc->vt_pid);
768 vc->vt_pid = get_pid(task_pid(current));
769 /* no switch is required -- saw@shade.msu.ru */
770 vc->vt_newvt = -1;
771 release_console_sem();
772 break;
775 case VT_GETMODE:
777 struct vt_mode tmp;
778 int rc;
780 acquire_console_sem();
781 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
782 release_console_sem();
784 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
785 if (rc)
786 ret = -EFAULT;
787 break;
791 * Returns global vt state. Note that VT 0 is always open, since
792 * it's an alias for the current VT, and people can't use it here.
793 * We cannot return state for more than 16 VTs, since v_state is short.
795 case VT_GETSTATE:
797 struct vt_stat __user *vtstat = up;
798 unsigned short state, mask;
800 if (put_user(fg_console + 1, &vtstat->v_active))
801 ret = -EFAULT;
802 else {
803 state = 1; /* /dev/tty0 is always open */
804 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
805 ++i, mask <<= 1)
806 if (VT_IS_IN_USE(i))
807 state |= mask;
808 ret = put_user(state, &vtstat->v_state);
810 break;
814 * Returns the first available (non-opened) console.
816 case VT_OPENQRY:
817 for (i = 0; i < MAX_NR_CONSOLES; ++i)
818 if (! VT_IS_IN_USE(i))
819 break;
820 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
821 goto setint;
824 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
825 * with num >= 1 (switches to vt 0, our console, are not allowed, just
826 * to preserve sanity).
828 case VT_ACTIVATE:
829 if (!perm)
830 goto eperm;
831 if (arg == 0 || arg > MAX_NR_CONSOLES)
832 ret = -ENXIO;
833 else {
834 arg--;
835 acquire_console_sem();
836 ret = vc_allocate(arg);
837 release_console_sem();
838 if (ret)
839 break;
840 set_console(arg);
842 break;
845 * wait until the specified VT has been activated
847 case VT_WAITACTIVE:
848 if (!perm)
849 goto eperm;
850 if (arg == 0 || arg > MAX_NR_CONSOLES)
851 ret = -ENXIO;
852 else
853 ret = vt_waitactive(arg - 1);
854 break;
857 * If a vt is under process control, the kernel will not switch to it
858 * immediately, but postpone the operation until the process calls this
859 * ioctl, allowing the switch to complete.
861 * According to the X sources this is the behavior:
862 * 0: pending switch-from not OK
863 * 1: pending switch-from OK
864 * 2: completed switch-to OK
866 case VT_RELDISP:
867 if (!perm)
868 goto eperm;
870 if (vc->vt_mode.mode != VT_PROCESS) {
871 ret = -EINVAL;
872 break;
875 * Switching-from response
877 acquire_console_sem();
878 if (vc->vt_newvt >= 0) {
879 if (arg == 0)
881 * Switch disallowed, so forget we were trying
882 * to do it.
884 vc->vt_newvt = -1;
886 else {
888 * The current vt has been released, so
889 * complete the switch.
891 int newvt;
892 newvt = vc->vt_newvt;
893 vc->vt_newvt = -1;
894 ret = vc_allocate(newvt);
895 if (ret) {
896 release_console_sem();
897 break;
900 * When we actually do the console switch,
901 * make sure we are atomic with respect to
902 * other console switches..
904 complete_change_console(vc_cons[newvt].d);
906 } else {
908 * Switched-to response
911 * If it's just an ACK, ignore it
913 if (arg != VT_ACKACQ)
914 ret = -EINVAL;
916 release_console_sem();
917 break;
920 * Disallocate memory associated to VT (but leave VT1)
922 case VT_DISALLOCATE:
923 if (arg > MAX_NR_CONSOLES) {
924 ret = -ENXIO;
925 break;
927 if (arg == 0) {
928 /* deallocate all unused consoles, but leave 0 */
929 acquire_console_sem();
930 for (i=1; i<MAX_NR_CONSOLES; i++)
931 if (! VT_BUSY(i))
932 vc_deallocate(i);
933 release_console_sem();
934 } else {
935 /* deallocate a single console, if possible */
936 arg--;
937 if (VT_BUSY(arg))
938 ret = -EBUSY;
939 else if (arg) { /* leave 0 */
940 acquire_console_sem();
941 vc_deallocate(arg);
942 release_console_sem();
945 break;
947 case VT_RESIZE:
949 struct vt_sizes __user *vtsizes = up;
950 struct vc_data *vc;
952 ushort ll,cc;
953 if (!perm)
954 goto eperm;
955 if (get_user(ll, &vtsizes->v_rows) ||
956 get_user(cc, &vtsizes->v_cols))
957 ret = -EFAULT;
958 else {
959 acquire_console_sem();
960 for (i = 0; i < MAX_NR_CONSOLES; i++) {
961 vc = vc_cons[i].d;
963 if (vc) {
964 vc->vc_resize_user = 1;
965 vc_resize(vc_cons[i].d, cc, ll);
968 release_console_sem();
970 break;
973 case VT_RESIZEX:
975 struct vt_consize __user *vtconsize = up;
976 ushort ll,cc,vlin,clin,vcol,ccol;
977 if (!perm)
978 goto eperm;
979 if (!access_ok(VERIFY_READ, vtconsize,
980 sizeof(struct vt_consize))) {
981 ret = -EFAULT;
982 break;
984 /* FIXME: Should check the copies properly */
985 __get_user(ll, &vtconsize->v_rows);
986 __get_user(cc, &vtconsize->v_cols);
987 __get_user(vlin, &vtconsize->v_vlin);
988 __get_user(clin, &vtconsize->v_clin);
989 __get_user(vcol, &vtconsize->v_vcol);
990 __get_user(ccol, &vtconsize->v_ccol);
991 vlin = vlin ? vlin : vc->vc_scan_lines;
992 if (clin) {
993 if (ll) {
994 if (ll != vlin/clin) {
995 /* Parameters don't add up */
996 ret = -EINVAL;
997 break;
999 } else
1000 ll = vlin/clin;
1002 if (vcol && ccol) {
1003 if (cc) {
1004 if (cc != vcol/ccol) {
1005 ret = -EINVAL;
1006 break;
1008 } else
1009 cc = vcol/ccol;
1012 if (clin > 32) {
1013 ret = -EINVAL;
1014 break;
1017 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1018 if (!vc_cons[i].d)
1019 continue;
1020 acquire_console_sem();
1021 if (vlin)
1022 vc_cons[i].d->vc_scan_lines = vlin;
1023 if (clin)
1024 vc_cons[i].d->vc_font.height = clin;
1025 vc_cons[i].d->vc_resize_user = 1;
1026 vc_resize(vc_cons[i].d, cc, ll);
1027 release_console_sem();
1029 break;
1032 case PIO_FONT: {
1033 if (!perm)
1034 goto eperm;
1035 op.op = KD_FONT_OP_SET;
1036 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1037 op.width = 8;
1038 op.height = 0;
1039 op.charcount = 256;
1040 op.data = up;
1041 ret = con_font_op(vc_cons[fg_console].d, &op);
1042 break;
1045 case GIO_FONT: {
1046 op.op = KD_FONT_OP_GET;
1047 op.flags = KD_FONT_FLAG_OLD;
1048 op.width = 8;
1049 op.height = 32;
1050 op.charcount = 256;
1051 op.data = up;
1052 ret = con_font_op(vc_cons[fg_console].d, &op);
1053 break;
1056 case PIO_CMAP:
1057 if (!perm)
1058 ret = -EPERM;
1059 else
1060 ret = con_set_cmap(up);
1061 break;
1063 case GIO_CMAP:
1064 ret = con_get_cmap(up);
1065 break;
1067 case PIO_FONTX:
1068 case GIO_FONTX:
1069 ret = do_fontx_ioctl(cmd, up, perm, &op);
1070 break;
1072 case PIO_FONTRESET:
1074 if (!perm)
1075 goto eperm;
1077 #ifdef BROKEN_GRAPHICS_PROGRAMS
1078 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1079 font is not saved. */
1080 ret = -ENOSYS;
1081 break;
1082 #else
1084 op.op = KD_FONT_OP_SET_DEFAULT;
1085 op.data = NULL;
1086 ret = con_font_op(vc_cons[fg_console].d, &op);
1087 if (ret)
1088 break;
1089 con_set_default_unimap(vc_cons[fg_console].d);
1090 break;
1092 #endif
1095 case KDFONTOP: {
1096 if (copy_from_user(&op, up, sizeof(op))) {
1097 ret = -EFAULT;
1098 break;
1100 if (!perm && op.op != KD_FONT_OP_GET)
1101 goto eperm;
1102 ret = con_font_op(vc, &op);
1103 if (ret)
1104 break;
1105 if (copy_to_user(up, &op, sizeof(op)))
1106 ret = -EFAULT;
1107 break;
1110 case PIO_SCRNMAP:
1111 if (!perm)
1112 ret = -EPERM;
1113 else
1114 ret = con_set_trans_old(up);
1115 break;
1117 case GIO_SCRNMAP:
1118 ret = con_get_trans_old(up);
1119 break;
1121 case PIO_UNISCRNMAP:
1122 if (!perm)
1123 ret = -EPERM;
1124 else
1125 ret = con_set_trans_new(up);
1126 break;
1128 case GIO_UNISCRNMAP:
1129 ret = con_get_trans_new(up);
1130 break;
1132 case PIO_UNIMAPCLR:
1133 { struct unimapinit ui;
1134 if (!perm)
1135 goto eperm;
1136 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1137 if (!ret)
1138 con_clear_unimap(vc, &ui);
1139 break;
1142 case PIO_UNIMAP:
1143 case GIO_UNIMAP:
1144 ret = do_unimap_ioctl(cmd, up, perm, vc);
1145 break;
1147 case VT_LOCKSWITCH:
1148 if (!capable(CAP_SYS_TTY_CONFIG))
1149 goto eperm;
1150 vt_dont_switch = 1;
1151 break;
1152 case VT_UNLOCKSWITCH:
1153 if (!capable(CAP_SYS_TTY_CONFIG))
1154 goto eperm;
1155 vt_dont_switch = 0;
1156 break;
1157 case VT_GETHIFONTMASK:
1158 ret = put_user(vc->vc_hi_font_mask,
1159 (unsigned short __user *)arg);
1160 break;
1161 default:
1162 ret = -ENOIOCTLCMD;
1164 out:
1165 unlock_kernel();
1166 return ret;
1167 eperm:
1168 ret = -EPERM;
1169 goto out;
1173 * Sometimes we want to wait until a particular VT has been activated. We
1174 * do it in a very simple manner. Everybody waits on a single queue and
1175 * get woken up at once. Those that are satisfied go on with their business,
1176 * while those not ready go back to sleep. Seems overkill to add a wait
1177 * to each vt just for this - usually this does nothing!
1179 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1182 * Sleeps until a vt is activated, or the task is interrupted. Returns
1183 * 0 if activation, -EINTR if interrupted by a signal handler.
1185 int vt_waitactive(int vt)
1187 int retval;
1188 DECLARE_WAITQUEUE(wait, current);
1190 add_wait_queue(&vt_activate_queue, &wait);
1191 for (;;) {
1192 retval = 0;
1195 * Synchronize with redraw_screen(). By acquiring the console
1196 * semaphore we make sure that the console switch is completed
1197 * before we return. If we didn't wait for the semaphore, we
1198 * could return at a point where fg_console has already been
1199 * updated, but the console switch hasn't been completed.
1201 acquire_console_sem();
1202 set_current_state(TASK_INTERRUPTIBLE);
1203 if (vt == fg_console) {
1204 release_console_sem();
1205 break;
1207 release_console_sem();
1208 retval = -ERESTARTNOHAND;
1209 if (signal_pending(current))
1210 break;
1211 schedule();
1213 remove_wait_queue(&vt_activate_queue, &wait);
1214 __set_current_state(TASK_RUNNING);
1215 return retval;
1218 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1220 void reset_vc(struct vc_data *vc)
1222 vc->vc_mode = KD_TEXT;
1223 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1224 vc->vt_mode.mode = VT_AUTO;
1225 vc->vt_mode.waitv = 0;
1226 vc->vt_mode.relsig = 0;
1227 vc->vt_mode.acqsig = 0;
1228 vc->vt_mode.frsig = 0;
1229 put_pid(vc->vt_pid);
1230 vc->vt_pid = NULL;
1231 vc->vt_newvt = -1;
1232 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1233 reset_palette(vc);
1236 void vc_SAK(struct work_struct *work)
1238 struct vc *vc_con =
1239 container_of(work, struct vc, SAK_work);
1240 struct vc_data *vc;
1241 struct tty_struct *tty;
1243 acquire_console_sem();
1244 vc = vc_con->d;
1245 if (vc) {
1246 tty = vc->vc_tty;
1248 * SAK should also work in all raw modes and reset
1249 * them properly.
1251 if (tty)
1252 __do_SAK(tty);
1253 reset_vc(vc);
1255 release_console_sem();
1259 * Performs the back end of a vt switch
1261 static void complete_change_console(struct vc_data *vc)
1263 unsigned char old_vc_mode;
1265 last_console = fg_console;
1268 * If we're switching, we could be going from KD_GRAPHICS to
1269 * KD_TEXT mode or vice versa, which means we need to blank or
1270 * unblank the screen later.
1272 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1273 switch_screen(vc);
1276 * This can't appear below a successful kill_pid(). If it did,
1277 * then the *blank_screen operation could occur while X, having
1278 * received acqsig, is waking up on another processor. This
1279 * condition can lead to overlapping accesses to the VGA range
1280 * and the framebuffer (causing system lockups).
1282 * To account for this we duplicate this code below only if the
1283 * controlling process is gone and we've called reset_vc.
1285 if (old_vc_mode != vc->vc_mode) {
1286 if (vc->vc_mode == KD_TEXT)
1287 do_unblank_screen(1);
1288 else
1289 do_blank_screen(1);
1293 * If this new console is under process control, send it a signal
1294 * telling it that it has acquired. Also check if it has died and
1295 * clean up (similar to logic employed in change_console())
1297 if (vc->vt_mode.mode == VT_PROCESS) {
1299 * Send the signal as privileged - kill_pid() will
1300 * tell us if the process has gone or something else
1301 * is awry
1303 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1305 * The controlling process has died, so we revert back to
1306 * normal operation. In this case, we'll also change back
1307 * to KD_TEXT mode. I'm not sure if this is strictly correct
1308 * but it saves the agony when the X server dies and the screen
1309 * remains blanked due to KD_GRAPHICS! It would be nice to do
1310 * this outside of VT_PROCESS but there is no single process
1311 * to account for and tracking tty count may be undesirable.
1313 reset_vc(vc);
1315 if (old_vc_mode != vc->vc_mode) {
1316 if (vc->vc_mode == KD_TEXT)
1317 do_unblank_screen(1);
1318 else
1319 do_blank_screen(1);
1325 * Wake anyone waiting for their VT to activate
1327 vt_wake_waitactive();
1328 return;
1332 * Performs the front-end of a vt switch
1334 void change_console(struct vc_data *new_vc)
1336 struct vc_data *vc;
1338 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1339 return;
1342 * If this vt is in process mode, then we need to handshake with
1343 * that process before switching. Essentially, we store where that
1344 * vt wants to switch to and wait for it to tell us when it's done
1345 * (via VT_RELDISP ioctl).
1347 * We also check to see if the controlling process still exists.
1348 * If it doesn't, we reset this vt to auto mode and continue.
1349 * This is a cheap way to track process control. The worst thing
1350 * that can happen is: we send a signal to a process, it dies, and
1351 * the switch gets "lost" waiting for a response; hopefully, the
1352 * user will try again, we'll detect the process is gone (unless
1353 * the user waits just the right amount of time :-) and revert the
1354 * vt to auto control.
1356 vc = vc_cons[fg_console].d;
1357 if (vc->vt_mode.mode == VT_PROCESS) {
1359 * Send the signal as privileged - kill_pid() will
1360 * tell us if the process has gone or something else
1361 * is awry.
1363 * We need to set vt_newvt *before* sending the signal or we
1364 * have a race.
1366 vc->vt_newvt = new_vc->vc_num;
1367 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1369 * It worked. Mark the vt to switch to and
1370 * return. The process needs to send us a
1371 * VT_RELDISP ioctl to complete the switch.
1373 return;
1377 * The controlling process has died, so we revert back to
1378 * normal operation. In this case, we'll also change back
1379 * to KD_TEXT mode. I'm not sure if this is strictly correct
1380 * but it saves the agony when the X server dies and the screen
1381 * remains blanked due to KD_GRAPHICS! It would be nice to do
1382 * this outside of VT_PROCESS but there is no single process
1383 * to account for and tracking tty count may be undesirable.
1385 reset_vc(vc);
1388 * Fall through to normal (VT_AUTO) handling of the switch...
1393 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1395 if (vc->vc_mode == KD_GRAPHICS)
1396 return;
1398 complete_change_console(new_vc);