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>
21 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/major.h>
25 #include <linux/console.h>
26 #include <linux/consolemap.h>
27 #include <linux/signal.h>
28 #include <linux/smp_lock.h>
29 #include <linux/timex.h>
32 #include <asm/uaccess.h>
34 #include <linux/kbd_kern.h>
35 #include <linux/vt_kern.h>
36 #include <linux/kbd_diacr.h>
37 #include <linux/selection.h>
40 extern struct tty_driver *console_driver;
42 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
43 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
46 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
47 * experimentation and study of X386 SYSV handling.
49 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
50 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
51 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
52 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
53 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
54 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
55 * to the current console is done by the main ioctl code.
59 #include <linux/syscalls.h>
62 static void complete_change_console(struct vc_data *vc);
65 * User space VT_EVENT handlers
68 struct vt_event_wait {
69 struct list_head list;
70 struct vt_event event;
74 static LIST_HEAD(vt_events);
75 static DEFINE_SPINLOCK(vt_event_lock);
76 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
80 * @event: the event that occurred
84 * Post an VT event to interested VT handlers
87 void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
89 struct list_head *pos, *head;
93 spin_lock_irqsave(&vt_event_lock, flags);
96 list_for_each(pos, head) {
97 struct vt_event_wait *ve = list_entry(pos,
98 struct vt_event_wait, list);
99 if (!(ve->event.event & event))
101 ve->event.event = event;
102 /* kernel view is consoles 0..n-1, user space view is
103 console 1..n with 0 meaning current, so we must bias */
104 ve->event.old = old + 1;
105 ve->event.new = new + 1;
109 spin_unlock_irqrestore(&vt_event_lock, flags);
111 wake_up_interruptible(&vt_event_waitqueue);
115 * vt_event_wait - wait for an event
118 * Waits for an event to occur which completes our vt_event_wait
119 * structure. On return the structure has wv->done set to 1 for success
120 * or 0 if some event such as a signal ended the wait.
123 static void vt_event_wait(struct vt_event_wait *vw)
126 /* Prepare the event */
127 INIT_LIST_HEAD(&vw->list);
129 /* Queue our event */
130 spin_lock_irqsave(&vt_event_lock, flags);
131 list_add(&vw->list, &vt_events);
132 spin_unlock_irqrestore(&vt_event_lock, flags);
133 /* Wait for it to pass */
134 wait_event_interruptible(vt_event_waitqueue, vw->done);
136 spin_lock_irqsave(&vt_event_lock, flags);
138 spin_unlock_irqrestore(&vt_event_lock, flags);
142 * vt_event_wait_ioctl - event ioctl handler
143 * @arg: argument to ioctl
145 * Implement the VT_WAITEVENT ioctl using the VT event interface
148 static int vt_event_wait_ioctl(struct vt_event __user *event)
150 struct vt_event_wait vw;
152 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
154 /* Highest supported event for now */
155 if (vw.event.event & ~VT_MAX_EVENT)
159 /* If it occurred report it */
161 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
169 * vt_waitactive - active console wait
173 * Helper for event waits. Used to implement the legacy
174 * event waiting ioctls in terms of events
177 int vt_waitactive(int n)
179 struct vt_event_wait vw;
181 if (n == fg_console + 1)
183 vw.event.event = VT_EVENT_SWITCH;
187 } while (vw.event.new != n);
192 * these are the valid i/o ports we're allowed to change. they map all the
195 #define GPFIRST 0x3b4
197 #define GPNUM (GPLAST - GPFIRST + 1)
199 #define i (tmp.kb_index)
200 #define s (tmp.kb_table)
201 #define v (tmp.kb_value)
203 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
206 ushort *key_map, val, ov;
208 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
211 if (!capable(CAP_SYS_TTY_CONFIG))
216 key_map = key_maps[s];
219 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
222 val = (i ? K_HOLE : K_NOSUCHMAP);
223 return put_user(val, &user_kbe->kb_value);
227 if (!i && v == K_NOSUCHMAP) {
229 key_map = key_maps[s];
232 if (key_map[0] == U(K_ALLOCATED)) {
240 if (KTYP(v) < NR_TYPES) {
241 if (KVAL(v) > max_vals[KTYP(v)])
244 if (kbd->kbdmode != VC_UNICODE)
247 /* ++Geert: non-PC keyboards may generate keycode zero */
248 #if !defined(__mc68000__) && !defined(__powerpc__)
249 /* assignment to entry 0 only tests validity of args */
254 if (!(key_map = key_maps[s])) {
257 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
258 !capable(CAP_SYS_RESOURCE))
261 key_map = kmalloc(sizeof(plain_map),
265 key_maps[s] = key_map;
266 key_map[0] = U(K_ALLOCATED);
267 for (j = 1; j < NR_KEYS; j++)
268 key_map[j] = U(K_HOLE);
273 break; /* nothing to do */
277 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
280 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
281 compute_shiftstate();
291 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
293 struct kbkeycode tmp;
296 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
300 kc = getkeycode(tmp.scancode);
302 kc = put_user(kc, &user_kbkc->keycode);
307 kc = setkeycode(tmp.scancode, tmp.keycode);
314 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
316 struct kbsentry *kbs;
322 char *first_free, *fj, *fnw;
326 if (!capable(CAP_SYS_TTY_CONFIG))
329 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
335 /* we mostly copy too much here (512bytes), but who cares ;) */
336 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
340 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
345 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
347 up = user_kdgkb->kb_string;
350 for ( ; *p && sz; p++, sz--)
351 if (put_user(*p, up++)) {
355 if (put_user('\0', up)) {
360 return ((p && *p) ? -EOVERFLOW : 0);
368 first_free = funcbufptr + (funcbufsize - funcbufleft);
369 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
376 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
377 if (delta <= funcbufleft) { /* it fits in current buf */
378 if (j < MAX_NR_FUNC) {
379 memmove(fj + delta, fj, first_free - fj);
380 for (k = j; k < MAX_NR_FUNC; k++)
382 func_table[k] += delta;
386 funcbufleft -= delta;
387 } else { /* allocate a larger buffer */
389 while (sz < funcbufsize - funcbufleft + delta)
391 fnw = kmalloc(sz, GFP_KERNEL);
400 memmove(fnw, funcbufptr, fj - funcbufptr);
401 for (k = 0; k < j; k++)
403 func_table[k] = fnw + (func_table[k] - funcbufptr);
405 if (first_free > fj) {
406 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
407 for (k = j; k < MAX_NR_FUNC; k++)
409 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
411 if (funcbufptr != func_buf)
414 funcbufleft = funcbufleft - delta + sz - funcbufsize;
417 strcpy(func_table[i], kbs->kb_string);
427 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
429 struct consolefontdesc cfdarg;
432 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
439 op->op = KD_FONT_OP_SET;
440 op->flags = KD_FONT_FLAG_OLD;
442 op->height = cfdarg.charheight;
443 op->charcount = cfdarg.charcount;
444 op->data = cfdarg.chardata;
445 return con_font_op(vc_cons[fg_console].d, op);
447 op->op = KD_FONT_OP_GET;
448 op->flags = KD_FONT_FLAG_OLD;
450 op->height = cfdarg.charheight;
451 op->charcount = cfdarg.charcount;
452 op->data = cfdarg.chardata;
453 i = con_font_op(vc_cons[fg_console].d, op);
456 cfdarg.charheight = op->height;
457 cfdarg.charcount = op->charcount;
458 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
467 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
469 struct unimapdesc tmp;
471 if (copy_from_user(&tmp, user_ud, sizeof tmp))
474 if (!access_ok(VERIFY_WRITE, tmp.entries,
475 tmp.entry_ct*sizeof(struct unipair)))
481 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
483 if (!perm && fg_console != vc->vc_num)
485 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
493 * We handle the console-specific ioctl's here. We allow the
494 * capability to modify any console, not just the fg_console.
496 int vt_ioctl(struct tty_struct *tty, struct file * file,
497 unsigned int cmd, unsigned long arg)
499 struct vc_data *vc = tty->driver_data;
500 struct console_font_op op; /* used in multiple places here */
501 struct kbd_struct * kbd;
502 unsigned int console;
504 void __user *up = (void __user *)arg;
508 console = vc->vc_num;
512 if (!vc_cons_allocated(console)) { /* impossible? */
519 * To have permissions to do most of the vt ioctls, we either have
520 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
523 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
526 kbd = kbd_table + console;
529 ret = tioclinux(tty, arg);
534 /* FIXME: This is an old broken API but we need to keep it
535 supported and somehow separate the historic advertised
536 tick rate from any real one */
538 arg = CLOCK_TICK_RATE / arg;
546 unsigned int ticks, count;
549 * Generate the tone for the appropriate number of ticks.
550 * If the time is zero, turn off sound ourselves.
552 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
553 count = ticks ? (arg & 0xffff) : 0;
554 /* FIXME: This is an old broken API but we need to keep it
555 supported and somehow separate the historic advertised
556 tick rate from any real one */
558 count = CLOCK_TICK_RATE / count;
559 kd_mksound(count, ticks);
571 * These cannot be implemented on any machine that implements
572 * ioperm() in user level (such as Alpha PCs) or not at all.
574 * XXX: you should never use these, just call ioperm directly..
580 * KDADDIO and KDDELIO may be able to add ports beyond what
581 * we reject here, but to be safe...
583 if (arg < GPFIRST || arg > GPLAST) {
587 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
592 ret = sys_ioperm(GPFIRST, GPNUM,
593 (cmd == KDENABIO)) ? -ENXIO : 0;
597 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
601 struct kbd_repeat kbrep;
603 if (!capable(CAP_SYS_TTY_CONFIG))
606 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
610 ret = kbd_rate(&kbrep);
613 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
620 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
621 * doesn't do a whole lot. i'm not sure if it should do any
622 * restoration of modes or what...
624 * XXX It should at least call into the driver, fbdev's definitely
625 * need to restore their engine state. --BenH
641 if (vc->vc_mode == (unsigned char) arg)
643 vc->vc_mode = (unsigned char) arg;
644 if (console != fg_console)
647 * explicitly blank/unblank the screen if switching modes
649 acquire_console_sem();
651 do_unblank_screen(1);
654 release_console_sem();
664 * these work like a combination of mmap and KDENABIO.
665 * this could be easily finished.
675 kbd->kbdmode = VC_RAW;
678 kbd->kbdmode = VC_MEDIUMRAW;
681 kbd->kbdmode = VC_XLATE;
682 compute_shiftstate();
685 kbd->kbdmode = VC_UNICODE;
686 compute_shiftstate();
692 tty_ldisc_flush(tty);
696 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
697 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
698 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
702 /* this could be folded into KDSKBMODE, but for compatibility
703 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
707 clr_vc_kbd_mode(kbd, VC_META);
710 set_vc_kbd_mode(kbd, VC_META);
718 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
720 ret = put_user(ucval, (int __user *)arg);
725 if(!capable(CAP_SYS_TTY_CONFIG))
727 ret = do_kbkeycode_ioctl(cmd, up, perm);
732 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
737 ret = do_kdgkb_ioctl(cmd, up, perm);
742 struct kbdiacrs __user *a = up;
743 struct kbdiacr diacr;
746 if (put_user(accent_table_size, &a->kb_cnt)) {
750 for (i = 0; i < accent_table_size; i++) {
751 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
752 diacr.base = conv_uni_to_8bit(accent_table[i].base);
753 diacr.result = conv_uni_to_8bit(accent_table[i].result);
754 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
763 struct kbdiacrsuc __user *a = up;
765 if (put_user(accent_table_size, &a->kb_cnt))
767 else if (copy_to_user(a->kbdiacruc, accent_table,
768 accent_table_size*sizeof(struct kbdiacruc)))
775 struct kbdiacrs __user *a = up;
776 struct kbdiacr diacr;
782 if (get_user(ct,&a->kb_cnt)) {
786 if (ct >= MAX_DIACR) {
790 accent_table_size = ct;
791 for (i = 0; i < ct; i++) {
792 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
796 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
797 accent_table[i].base = conv_8bit_to_uni(diacr.base);
798 accent_table[i].result = conv_8bit_to_uni(diacr.result);
805 struct kbdiacrsuc __user *a = up;
810 if (get_user(ct,&a->kb_cnt)) {
814 if (ct >= MAX_DIACR) {
818 accent_table_size = ct;
819 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
824 /* the ioctls below read/set the flags usually shown in the leds */
825 /* don't use them - they will go away without warning */
827 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
837 kbd->ledflagstate = (arg & 7);
838 kbd->default_ledflagstate = ((arg >> 4) & 7);
842 /* the ioctls below only set the lights, not the functions */
843 /* for those, see KDGKBLED and KDSKBLED above */
845 ucval = getledstate();
847 ret = put_user(ucval, (char __user *)arg);
853 setledstate(kbd, arg);
857 * A process can indicate its willingness to accept signals
858 * generated by pressing an appropriate key combination.
859 * Thus, one can have a daemon that e.g. spawns a new console
860 * upon a keypress and then changes to it.
861 * See also the kbrequest field of inittab(5).
865 if (!perm || !capable(CAP_KILL))
867 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
870 spin_lock_irq(&vt_spawn_con.lock);
871 put_pid(vt_spawn_con.pid);
872 vt_spawn_con.pid = get_pid(task_pid(current));
873 vt_spawn_con.sig = arg;
874 spin_unlock_irq(&vt_spawn_con.lock);
885 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
889 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
893 acquire_console_sem();
895 /* the frsig is ignored, so we set it to 0 */
896 vc->vt_mode.frsig = 0;
898 vc->vt_pid = get_pid(task_pid(current));
899 /* no switch is required -- saw@shade.msu.ru */
901 release_console_sem();
910 acquire_console_sem();
911 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
912 release_console_sem();
914 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
921 * Returns global vt state. Note that VT 0 is always open, since
922 * it's an alias for the current VT, and people can't use it here.
923 * We cannot return state for more than 16 VTs, since v_state is short.
927 struct vt_stat __user *vtstat = up;
928 unsigned short state, mask;
930 if (put_user(fg_console + 1, &vtstat->v_active))
933 state = 1; /* /dev/tty0 is always open */
934 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
938 ret = put_user(state, &vtstat->v_state);
944 * Returns the first available (non-opened) console.
947 for (i = 0; i < MAX_NR_CONSOLES; ++i)
948 if (! VT_IS_IN_USE(i))
950 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
954 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
955 * with num >= 1 (switches to vt 0, our console, are not allowed, just
956 * to preserve sanity).
961 if (arg == 0 || arg > MAX_NR_CONSOLES)
965 acquire_console_sem();
966 ret = vc_allocate(arg);
967 release_console_sem();
975 * wait until the specified VT has been activated
980 if (arg == 0 || arg > MAX_NR_CONSOLES)
983 ret = vt_waitactive(arg);
987 * If a vt is under process control, the kernel will not switch to it
988 * immediately, but postpone the operation until the process calls this
989 * ioctl, allowing the switch to complete.
991 * According to the X sources this is the behavior:
992 * 0: pending switch-from not OK
993 * 1: pending switch-from OK
994 * 2: completed switch-to OK
1000 if (vc->vt_mode.mode != VT_PROCESS) {
1005 * Switching-from response
1007 acquire_console_sem();
1008 if (vc->vt_newvt >= 0) {
1011 * Switch disallowed, so forget we were trying
1018 * The current vt has been released, so
1019 * complete the switch.
1022 newvt = vc->vt_newvt;
1024 ret = vc_allocate(newvt);
1026 release_console_sem();
1030 * When we actually do the console switch,
1031 * make sure we are atomic with respect to
1032 * other console switches..
1034 complete_change_console(vc_cons[newvt].d);
1038 * Switched-to response
1041 * If it's just an ACK, ignore it
1043 if (arg != VT_ACKACQ)
1046 release_console_sem();
1050 * Disallocate memory associated to VT (but leave VT1)
1052 case VT_DISALLOCATE:
1053 if (arg > MAX_NR_CONSOLES) {
1058 /* deallocate all unused consoles, but leave 0 */
1059 acquire_console_sem();
1060 for (i=1; i<MAX_NR_CONSOLES; i++)
1063 release_console_sem();
1065 /* deallocate a single console, if possible */
1069 else if (arg) { /* leave 0 */
1070 acquire_console_sem();
1072 release_console_sem();
1079 struct vt_sizes __user *vtsizes = up;
1085 if (get_user(ll, &vtsizes->v_rows) ||
1086 get_user(cc, &vtsizes->v_cols))
1089 acquire_console_sem();
1090 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1094 vc->vc_resize_user = 1;
1095 vc_resize(vc_cons[i].d, cc, ll);
1098 release_console_sem();
1105 struct vt_consize __user *vtconsize = up;
1106 ushort ll,cc,vlin,clin,vcol,ccol;
1109 if (!access_ok(VERIFY_READ, vtconsize,
1110 sizeof(struct vt_consize))) {
1114 /* FIXME: Should check the copies properly */
1115 __get_user(ll, &vtconsize->v_rows);
1116 __get_user(cc, &vtconsize->v_cols);
1117 __get_user(vlin, &vtconsize->v_vlin);
1118 __get_user(clin, &vtconsize->v_clin);
1119 __get_user(vcol, &vtconsize->v_vcol);
1120 __get_user(ccol, &vtconsize->v_ccol);
1121 vlin = vlin ? vlin : vc->vc_scan_lines;
1124 if (ll != vlin/clin) {
1125 /* Parameters don't add up */
1134 if (cc != vcol/ccol) {
1147 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1150 acquire_console_sem();
1152 vc_cons[i].d->vc_scan_lines = vlin;
1154 vc_cons[i].d->vc_font.height = clin;
1155 vc_cons[i].d->vc_resize_user = 1;
1156 vc_resize(vc_cons[i].d, cc, ll);
1157 release_console_sem();
1165 op.op = KD_FONT_OP_SET;
1166 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1171 ret = con_font_op(vc_cons[fg_console].d, &op);
1176 op.op = KD_FONT_OP_GET;
1177 op.flags = KD_FONT_FLAG_OLD;
1182 ret = con_font_op(vc_cons[fg_console].d, &op);
1190 ret = con_set_cmap(up);
1194 ret = con_get_cmap(up);
1199 ret = do_fontx_ioctl(cmd, up, perm, &op);
1207 #ifdef BROKEN_GRAPHICS_PROGRAMS
1208 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1209 font is not saved. */
1214 op.op = KD_FONT_OP_SET_DEFAULT;
1216 ret = con_font_op(vc_cons[fg_console].d, &op);
1219 con_set_default_unimap(vc_cons[fg_console].d);
1226 if (copy_from_user(&op, up, sizeof(op))) {
1230 if (!perm && op.op != KD_FONT_OP_GET)
1232 ret = con_font_op(vc, &op);
1235 if (copy_to_user(up, &op, sizeof(op)))
1244 ret = con_set_trans_old(up);
1248 ret = con_get_trans_old(up);
1251 case PIO_UNISCRNMAP:
1255 ret = con_set_trans_new(up);
1258 case GIO_UNISCRNMAP:
1259 ret = con_get_trans_new(up);
1263 { struct unimapinit ui;
1266 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1268 con_clear_unimap(vc, &ui);
1274 ret = do_unimap_ioctl(cmd, up, perm, vc);
1278 if (!capable(CAP_SYS_TTY_CONFIG))
1282 case VT_UNLOCKSWITCH:
1283 if (!capable(CAP_SYS_TTY_CONFIG))
1287 case VT_GETHIFONTMASK:
1288 ret = put_user(vc->vc_hi_font_mask,
1289 (unsigned short __user *)arg);
1292 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1305 void reset_vc(struct vc_data *vc)
1307 vc->vc_mode = KD_TEXT;
1308 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1309 vc->vt_mode.mode = VT_AUTO;
1310 vc->vt_mode.waitv = 0;
1311 vc->vt_mode.relsig = 0;
1312 vc->vt_mode.acqsig = 0;
1313 vc->vt_mode.frsig = 0;
1314 put_pid(vc->vt_pid);
1317 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1321 void vc_SAK(struct work_struct *work)
1324 container_of(work, struct vc, SAK_work);
1326 struct tty_struct *tty;
1328 acquire_console_sem();
1333 * SAK should also work in all raw modes and reset
1340 release_console_sem();
1344 * Performs the back end of a vt switch
1346 static void complete_change_console(struct vc_data *vc)
1348 unsigned char old_vc_mode;
1349 int old = fg_console;
1351 last_console = fg_console;
1354 * If we're switching, we could be going from KD_GRAPHICS to
1355 * KD_TEXT mode or vice versa, which means we need to blank or
1356 * unblank the screen later.
1358 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1362 * This can't appear below a successful kill_pid(). If it did,
1363 * then the *blank_screen operation could occur while X, having
1364 * received acqsig, is waking up on another processor. This
1365 * condition can lead to overlapping accesses to the VGA range
1366 * and the framebuffer (causing system lockups).
1368 * To account for this we duplicate this code below only if the
1369 * controlling process is gone and we've called reset_vc.
1371 if (old_vc_mode != vc->vc_mode) {
1372 if (vc->vc_mode == KD_TEXT)
1373 do_unblank_screen(1);
1379 * If this new console is under process control, send it a signal
1380 * telling it that it has acquired. Also check if it has died and
1381 * clean up (similar to logic employed in change_console())
1383 if (vc->vt_mode.mode == VT_PROCESS) {
1385 * Send the signal as privileged - kill_pid() will
1386 * tell us if the process has gone or something else
1389 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1391 * The controlling process has died, so we revert back to
1392 * normal operation. In this case, we'll also change back
1393 * to KD_TEXT mode. I'm not sure if this is strictly correct
1394 * but it saves the agony when the X server dies and the screen
1395 * remains blanked due to KD_GRAPHICS! It would be nice to do
1396 * this outside of VT_PROCESS but there is no single process
1397 * to account for and tracking tty count may be undesirable.
1401 if (old_vc_mode != vc->vc_mode) {
1402 if (vc->vc_mode == KD_TEXT)
1403 do_unblank_screen(1);
1411 * Wake anyone waiting for their VT to activate
1413 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1418 * Performs the front-end of a vt switch
1420 void change_console(struct vc_data *new_vc)
1424 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1428 * If this vt is in process mode, then we need to handshake with
1429 * that process before switching. Essentially, we store where that
1430 * vt wants to switch to and wait for it to tell us when it's done
1431 * (via VT_RELDISP ioctl).
1433 * We also check to see if the controlling process still exists.
1434 * If it doesn't, we reset this vt to auto mode and continue.
1435 * This is a cheap way to track process control. The worst thing
1436 * that can happen is: we send a signal to a process, it dies, and
1437 * the switch gets "lost" waiting for a response; hopefully, the
1438 * user will try again, we'll detect the process is gone (unless
1439 * the user waits just the right amount of time :-) and revert the
1440 * vt to auto control.
1442 vc = vc_cons[fg_console].d;
1443 if (vc->vt_mode.mode == VT_PROCESS) {
1445 * Send the signal as privileged - kill_pid() will
1446 * tell us if the process has gone or something else
1449 * We need to set vt_newvt *before* sending the signal or we
1452 vc->vt_newvt = new_vc->vc_num;
1453 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1455 * It worked. Mark the vt to switch to and
1456 * return. The process needs to send us a
1457 * VT_RELDISP ioctl to complete the switch.
1463 * The controlling process has died, so we revert back to
1464 * normal operation. In this case, we'll also change back
1465 * to KD_TEXT mode. I'm not sure if this is strictly correct
1466 * but it saves the agony when the X server dies and the screen
1467 * remains blanked due to KD_GRAPHICS! It would be nice to do
1468 * this outside of VT_PROCESS but there is no single process
1469 * to account for and tracking tty count may be undesirable.
1474 * Fall through to normal (VT_AUTO) handling of the switch...
1479 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1481 if (vc->vc_mode == KD_GRAPHICS)
1484 complete_change_console(new_vc);