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/module.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/major.h>
26 #include <linux/console.h>
27 #include <linux/consolemap.h>
28 #include <linux/signal.h>
29 #include <linux/smp_lock.h>
30 #include <linux/timex.h>
33 #include <asm/uaccess.h>
35 #include <linux/kbd_kern.h>
36 #include <linux/vt_kern.h>
37 #include <linux/kbd_diacr.h>
38 #include <linux/selection.h>
41 extern struct tty_driver *console_driver;
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)
47 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
48 * experimentation and study of X386 SYSV handling.
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.
60 #include <linux/syscalls.h>
63 static void complete_change_console(struct vc_data *vc);
66 * User space VT_EVENT handlers
69 struct vt_event_wait {
70 struct list_head list;
71 struct vt_event event;
75 static LIST_HEAD(vt_events);
76 static DEFINE_SPINLOCK(vt_event_lock);
77 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
81 * @event: the event that occurred
85 * Post an VT event to interested VT handlers
88 void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
90 struct list_head *pos, *head;
94 spin_lock_irqsave(&vt_event_lock, flags);
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))
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 */
105 ve->event.old = old + 1;
106 ve->event.new = new + 1;
110 spin_unlock_irqrestore(&vt_event_lock, flags);
112 wake_up_interruptible(&vt_event_waitqueue);
116 * vt_event_wait - wait for an event
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.
124 static void vt_event_wait(struct vt_event_wait *vw)
127 /* Prepare the event */
128 INIT_LIST_HEAD(&vw->list);
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 */
135 wait_event_interruptible(vt_event_waitqueue, vw->done);
137 spin_lock_irqsave(&vt_event_lock, flags);
139 spin_unlock_irqrestore(&vt_event_lock, flags);
143 * vt_event_wait_ioctl - event ioctl handler
144 * @arg: argument to ioctl
146 * Implement the VT_WAITEVENT ioctl using the VT event interface
149 static int vt_event_wait_ioctl(struct vt_event __user *event)
151 struct vt_event_wait vw;
153 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
155 /* Highest supported event for now */
156 if (vw.event.event & ~VT_MAX_EVENT)
160 /* If it occurred report it */
162 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
170 * vt_waitactive - active console wait
174 * Helper for event waits. Used to implement the legacy
175 * event waiting ioctls in terms of events
178 int vt_waitactive(int n)
180 struct vt_event_wait vw;
182 if (n == fg_console + 1)
184 vw.event.event = VT_EVENT_SWITCH;
188 } while (vw.event.new != n);
193 * these are the valid i/o ports we're allowed to change. they map all the
196 #define GPFIRST 0x3b4
198 #define GPNUM (GPLAST - GPFIRST + 1)
200 #define i (tmp.kb_index)
201 #define s (tmp.kb_table)
202 #define v (tmp.kb_value)
204 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
207 ushort *key_map, val, ov;
209 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
212 if (!capable(CAP_SYS_TTY_CONFIG))
217 key_map = key_maps[s];
220 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
223 val = (i ? K_HOLE : K_NOSUCHMAP);
224 return put_user(val, &user_kbe->kb_value);
228 if (!i && v == K_NOSUCHMAP) {
230 key_map = key_maps[s];
233 if (key_map[0] == U(K_ALLOCATED)) {
241 if (KTYP(v) < NR_TYPES) {
242 if (KVAL(v) > max_vals[KTYP(v)])
245 if (kbd->kbdmode != VC_UNICODE)
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 */
255 if (!(key_map = key_maps[s])) {
258 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
259 !capable(CAP_SYS_RESOURCE))
262 key_map = kmalloc(sizeof(plain_map),
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);
274 break; /* nothing to do */
278 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
281 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
282 compute_shiftstate();
292 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
294 struct kbkeycode tmp;
297 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
301 kc = getkeycode(tmp.scancode);
303 kc = put_user(kc, &user_kbkc->keycode);
308 kc = setkeycode(tmp.scancode, tmp.keycode);
315 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
317 struct kbsentry *kbs;
323 char *first_free, *fj, *fnw;
327 if (!capable(CAP_SYS_TTY_CONFIG))
330 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
336 /* we mostly copy too much here (512bytes), but who cares ;) */
337 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
341 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
346 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
348 up = user_kdgkb->kb_string;
351 for ( ; *p && sz; p++, sz--)
352 if (put_user(*p, up++)) {
356 if (put_user('\0', up)) {
361 return ((p && *p) ? -EOVERFLOW : 0);
369 first_free = funcbufptr + (funcbufsize - funcbufleft);
370 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
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++)
383 func_table[k] += delta;
387 funcbufleft -= delta;
388 } else { /* allocate a larger buffer */
390 while (sz < funcbufsize - funcbufleft + delta)
392 fnw = kmalloc(sz, GFP_KERNEL);
401 memmove(fnw, funcbufptr, fj - funcbufptr);
402 for (k = 0; k < j; k++)
404 func_table[k] = fnw + (func_table[k] - funcbufptr);
406 if (first_free > fj) {
407 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
408 for (k = j; k < MAX_NR_FUNC; k++)
410 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
412 if (funcbufptr != func_buf)
415 funcbufleft = funcbufleft - delta + sz - funcbufsize;
418 strcpy(func_table[i], kbs->kb_string);
428 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
430 struct consolefontdesc cfdarg;
433 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
440 op->op = KD_FONT_OP_SET;
441 op->flags = KD_FONT_FLAG_OLD;
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);
448 op->op = KD_FONT_OP_GET;
449 op->flags = KD_FONT_FLAG_OLD;
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);
457 cfdarg.charheight = op->height;
458 cfdarg.charcount = op->charcount;
459 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
468 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
470 struct unimapdesc tmp;
472 if (copy_from_user(&tmp, user_ud, sizeof tmp))
475 if (!access_ok(VERIFY_WRITE, tmp.entries,
476 tmp.entry_ct*sizeof(struct unipair)))
482 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
484 if (!perm && fg_console != vc->vc_num)
486 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
494 * We handle the console-specific ioctl's here. We allow the
495 * capability to modify any console, not just the fg_console.
497 int vt_ioctl(struct tty_struct *tty, struct file * file,
498 unsigned int cmd, unsigned long arg)
500 struct vc_data *vc = tty->driver_data;
501 struct console_font_op op; /* used in multiple places here */
502 struct kbd_struct * kbd;
503 unsigned int console;
505 void __user *up = (void __user *)arg;
509 console = vc->vc_num;
513 if (!vc_cons_allocated(console)) { /* impossible? */
520 * To have permissions to do most of the vt ioctls, we either have
521 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
524 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
527 kbd = kbd_table + console;
530 ret = tioclinux(tty, arg);
535 /* FIXME: This is an old broken API but we need to keep it
536 supported and somehow separate the historic advertised
537 tick rate from any real one */
539 arg = CLOCK_TICK_RATE / arg;
547 unsigned int ticks, count;
550 * Generate the tone for the appropriate number of ticks.
551 * If the time is zero, turn off sound ourselves.
553 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
554 count = ticks ? (arg & 0xffff) : 0;
555 /* FIXME: This is an old broken API but we need to keep it
556 supported and somehow separate the historic advertised
557 tick rate from any real one */
559 count = CLOCK_TICK_RATE / count;
560 kd_mksound(count, ticks);
572 * These cannot be implemented on any machine that implements
573 * ioperm() in user level (such as Alpha PCs) or not at all.
575 * XXX: you should never use these, just call ioperm directly..
581 * KDADDIO and KDDELIO may be able to add ports beyond what
582 * we reject here, but to be safe...
584 if (arg < GPFIRST || arg > GPLAST) {
588 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
593 ret = sys_ioperm(GPFIRST, GPNUM,
594 (cmd == KDENABIO)) ? -ENXIO : 0;
598 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
602 struct kbd_repeat kbrep;
604 if (!capable(CAP_SYS_TTY_CONFIG))
607 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
611 ret = kbd_rate(&kbrep);
614 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
621 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
622 * doesn't do a whole lot. i'm not sure if it should do any
623 * restoration of modes or what...
625 * XXX It should at least call into the driver, fbdev's definitely
626 * need to restore their engine state. --BenH
642 if (vc->vc_mode == (unsigned char) arg)
644 vc->vc_mode = (unsigned char) arg;
645 if (console != fg_console)
648 * explicitly blank/unblank the screen if switching modes
650 acquire_console_sem();
652 do_unblank_screen(1);
655 release_console_sem();
665 * these work like a combination of mmap and KDENABIO.
666 * this could be easily finished.
676 kbd->kbdmode = VC_RAW;
679 kbd->kbdmode = VC_MEDIUMRAW;
682 kbd->kbdmode = VC_XLATE;
683 compute_shiftstate();
686 kbd->kbdmode = VC_UNICODE;
687 compute_shiftstate();
693 tty_ldisc_flush(tty);
697 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
698 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
699 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
703 /* this could be folded into KDSKBMODE, but for compatibility
704 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
708 clr_vc_kbd_mode(kbd, VC_META);
711 set_vc_kbd_mode(kbd, VC_META);
719 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
721 ret = put_user(ucval, (int __user *)arg);
726 if(!capable(CAP_SYS_TTY_CONFIG))
728 ret = do_kbkeycode_ioctl(cmd, up, perm);
733 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
738 ret = do_kdgkb_ioctl(cmd, up, perm);
743 struct kbdiacrs __user *a = up;
744 struct kbdiacr diacr;
747 if (put_user(accent_table_size, &a->kb_cnt)) {
751 for (i = 0; i < accent_table_size; i++) {
752 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
753 diacr.base = conv_uni_to_8bit(accent_table[i].base);
754 diacr.result = conv_uni_to_8bit(accent_table[i].result);
755 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
764 struct kbdiacrsuc __user *a = up;
766 if (put_user(accent_table_size, &a->kb_cnt))
768 else if (copy_to_user(a->kbdiacruc, accent_table,
769 accent_table_size*sizeof(struct kbdiacruc)))
776 struct kbdiacrs __user *a = up;
777 struct kbdiacr diacr;
783 if (get_user(ct,&a->kb_cnt)) {
787 if (ct >= MAX_DIACR) {
791 accent_table_size = ct;
792 for (i = 0; i < ct; i++) {
793 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
797 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
798 accent_table[i].base = conv_8bit_to_uni(diacr.base);
799 accent_table[i].result = conv_8bit_to_uni(diacr.result);
806 struct kbdiacrsuc __user *a = up;
811 if (get_user(ct,&a->kb_cnt)) {
815 if (ct >= MAX_DIACR) {
819 accent_table_size = ct;
820 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
825 /* the ioctls below read/set the flags usually shown in the leds */
826 /* don't use them - they will go away without warning */
828 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
838 kbd->ledflagstate = (arg & 7);
839 kbd->default_ledflagstate = ((arg >> 4) & 7);
843 /* the ioctls below only set the lights, not the functions */
844 /* for those, see KDGKBLED and KDSKBLED above */
846 ucval = getledstate();
848 ret = put_user(ucval, (char __user *)arg);
854 setledstate(kbd, arg);
858 * A process can indicate its willingness to accept signals
859 * generated by pressing an appropriate key combination.
860 * Thus, one can have a daemon that e.g. spawns a new console
861 * upon a keypress and then changes to it.
862 * See also the kbrequest field of inittab(5).
866 if (!perm || !capable(CAP_KILL))
868 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
871 spin_lock_irq(&vt_spawn_con.lock);
872 put_pid(vt_spawn_con.pid);
873 vt_spawn_con.pid = get_pid(task_pid(current));
874 vt_spawn_con.sig = arg;
875 spin_unlock_irq(&vt_spawn_con.lock);
886 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
890 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
894 acquire_console_sem();
896 /* the frsig is ignored, so we set it to 0 */
897 vc->vt_mode.frsig = 0;
899 vc->vt_pid = get_pid(task_pid(current));
900 /* no switch is required -- saw@shade.msu.ru */
902 release_console_sem();
911 acquire_console_sem();
912 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
913 release_console_sem();
915 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
922 * Returns global vt state. Note that VT 0 is always open, since
923 * it's an alias for the current VT, and people can't use it here.
924 * We cannot return state for more than 16 VTs, since v_state is short.
928 struct vt_stat __user *vtstat = up;
929 unsigned short state, mask;
931 if (put_user(fg_console + 1, &vtstat->v_active))
934 state = 1; /* /dev/tty0 is always open */
935 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
939 ret = put_user(state, &vtstat->v_state);
945 * Returns the first available (non-opened) console.
948 for (i = 0; i < MAX_NR_CONSOLES; ++i)
949 if (! VT_IS_IN_USE(i))
951 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
955 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
956 * with num >= 1 (switches to vt 0, our console, are not allowed, just
957 * to preserve sanity).
962 if (arg == 0 || arg > MAX_NR_CONSOLES)
966 acquire_console_sem();
967 ret = vc_allocate(arg);
968 release_console_sem();
977 struct vt_setactivate vsa;
982 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
983 sizeof(struct vt_setactivate)))
985 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
989 acquire_console_sem();
990 ret = vc_allocate(vsa.console);
993 /* This is safe providing we don't drop the
994 console sem between vc_allocate and
995 finishing referencing nvc */
996 nvc = vc_cons[vsa.console].d;
997 nvc->vt_mode = vsa.mode;
998 nvc->vt_mode.frsig = 0;
999 put_pid(nvc->vt_pid);
1000 nvc->vt_pid = get_pid(task_pid(current));
1002 release_console_sem();
1005 /* Commence switch and lock */
1011 * wait until the specified VT has been activated
1016 if (arg == 0 || arg > MAX_NR_CONSOLES)
1019 ret = vt_waitactive(arg);
1023 * If a vt is under process control, the kernel will not switch to it
1024 * immediately, but postpone the operation until the process calls this
1025 * ioctl, allowing the switch to complete.
1027 * According to the X sources this is the behavior:
1028 * 0: pending switch-from not OK
1029 * 1: pending switch-from OK
1030 * 2: completed switch-to OK
1036 if (vc->vt_mode.mode != VT_PROCESS) {
1041 * Switching-from response
1043 acquire_console_sem();
1044 if (vc->vt_newvt >= 0) {
1047 * Switch disallowed, so forget we were trying
1054 * The current vt has been released, so
1055 * complete the switch.
1058 newvt = vc->vt_newvt;
1060 ret = vc_allocate(newvt);
1062 release_console_sem();
1066 * When we actually do the console switch,
1067 * make sure we are atomic with respect to
1068 * other console switches..
1070 complete_change_console(vc_cons[newvt].d);
1074 * Switched-to response
1077 * If it's just an ACK, ignore it
1079 if (arg != VT_ACKACQ)
1082 release_console_sem();
1086 * Disallocate memory associated to VT (but leave VT1)
1088 case VT_DISALLOCATE:
1089 if (arg > MAX_NR_CONSOLES) {
1094 /* deallocate all unused consoles, but leave 0 */
1095 acquire_console_sem();
1096 for (i=1; i<MAX_NR_CONSOLES; i++)
1099 release_console_sem();
1101 /* deallocate a single console, if possible */
1105 else if (arg) { /* leave 0 */
1106 acquire_console_sem();
1108 release_console_sem();
1115 struct vt_sizes __user *vtsizes = up;
1121 if (get_user(ll, &vtsizes->v_rows) ||
1122 get_user(cc, &vtsizes->v_cols))
1125 acquire_console_sem();
1126 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1130 vc->vc_resize_user = 1;
1131 vc_resize(vc_cons[i].d, cc, ll);
1134 release_console_sem();
1141 struct vt_consize __user *vtconsize = up;
1142 ushort ll,cc,vlin,clin,vcol,ccol;
1145 if (!access_ok(VERIFY_READ, vtconsize,
1146 sizeof(struct vt_consize))) {
1150 /* FIXME: Should check the copies properly */
1151 __get_user(ll, &vtconsize->v_rows);
1152 __get_user(cc, &vtconsize->v_cols);
1153 __get_user(vlin, &vtconsize->v_vlin);
1154 __get_user(clin, &vtconsize->v_clin);
1155 __get_user(vcol, &vtconsize->v_vcol);
1156 __get_user(ccol, &vtconsize->v_ccol);
1157 vlin = vlin ? vlin : vc->vc_scan_lines;
1160 if (ll != vlin/clin) {
1161 /* Parameters don't add up */
1170 if (cc != vcol/ccol) {
1183 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1186 acquire_console_sem();
1188 vc_cons[i].d->vc_scan_lines = vlin;
1190 vc_cons[i].d->vc_font.height = clin;
1191 vc_cons[i].d->vc_resize_user = 1;
1192 vc_resize(vc_cons[i].d, cc, ll);
1193 release_console_sem();
1201 op.op = KD_FONT_OP_SET;
1202 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1207 ret = con_font_op(vc_cons[fg_console].d, &op);
1212 op.op = KD_FONT_OP_GET;
1213 op.flags = KD_FONT_FLAG_OLD;
1218 ret = con_font_op(vc_cons[fg_console].d, &op);
1226 ret = con_set_cmap(up);
1230 ret = con_get_cmap(up);
1235 ret = do_fontx_ioctl(cmd, up, perm, &op);
1243 #ifdef BROKEN_GRAPHICS_PROGRAMS
1244 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1245 font is not saved. */
1250 op.op = KD_FONT_OP_SET_DEFAULT;
1252 ret = con_font_op(vc_cons[fg_console].d, &op);
1255 con_set_default_unimap(vc_cons[fg_console].d);
1262 if (copy_from_user(&op, up, sizeof(op))) {
1266 if (!perm && op.op != KD_FONT_OP_GET)
1268 ret = con_font_op(vc, &op);
1271 if (copy_to_user(up, &op, sizeof(op)))
1280 ret = con_set_trans_old(up);
1284 ret = con_get_trans_old(up);
1287 case PIO_UNISCRNMAP:
1291 ret = con_set_trans_new(up);
1294 case GIO_UNISCRNMAP:
1295 ret = con_get_trans_new(up);
1299 { struct unimapinit ui;
1302 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1304 con_clear_unimap(vc, &ui);
1310 ret = do_unimap_ioctl(cmd, up, perm, vc);
1314 if (!capable(CAP_SYS_TTY_CONFIG))
1318 case VT_UNLOCKSWITCH:
1319 if (!capable(CAP_SYS_TTY_CONFIG))
1323 case VT_GETHIFONTMASK:
1324 ret = put_user(vc->vc_hi_font_mask,
1325 (unsigned short __user *)arg);
1328 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1341 void reset_vc(struct vc_data *vc)
1343 vc->vc_mode = KD_TEXT;
1344 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1345 vc->vt_mode.mode = VT_AUTO;
1346 vc->vt_mode.waitv = 0;
1347 vc->vt_mode.relsig = 0;
1348 vc->vt_mode.acqsig = 0;
1349 vc->vt_mode.frsig = 0;
1350 put_pid(vc->vt_pid);
1353 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1357 void vc_SAK(struct work_struct *work)
1360 container_of(work, struct vc, SAK_work);
1362 struct tty_struct *tty;
1364 acquire_console_sem();
1369 * SAK should also work in all raw modes and reset
1376 release_console_sem();
1380 * Performs the back end of a vt switch. Called under the console
1383 static void complete_change_console(struct vc_data *vc)
1385 unsigned char old_vc_mode;
1386 int old = fg_console;
1388 last_console = fg_console;
1391 * If we're switching, we could be going from KD_GRAPHICS to
1392 * KD_TEXT mode or vice versa, which means we need to blank or
1393 * unblank the screen later.
1395 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1399 * This can't appear below a successful kill_pid(). If it did,
1400 * then the *blank_screen operation could occur while X, having
1401 * received acqsig, is waking up on another processor. This
1402 * condition can lead to overlapping accesses to the VGA range
1403 * and the framebuffer (causing system lockups).
1405 * To account for this we duplicate this code below only if the
1406 * controlling process is gone and we've called reset_vc.
1408 if (old_vc_mode != vc->vc_mode) {
1409 if (vc->vc_mode == KD_TEXT)
1410 do_unblank_screen(1);
1416 * If this new console is under process control, send it a signal
1417 * telling it that it has acquired. Also check if it has died and
1418 * clean up (similar to logic employed in change_console())
1420 if (vc->vt_mode.mode == VT_PROCESS) {
1422 * Send the signal as privileged - kill_pid() will
1423 * tell us if the process has gone or something else
1426 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1428 * The controlling process has died, so we revert back to
1429 * normal operation. In this case, we'll also change back
1430 * to KD_TEXT mode. I'm not sure if this is strictly correct
1431 * but it saves the agony when the X server dies and the screen
1432 * remains blanked due to KD_GRAPHICS! It would be nice to do
1433 * this outside of VT_PROCESS but there is no single process
1434 * to account for and tracking tty count may be undesirable.
1438 if (old_vc_mode != vc->vc_mode) {
1439 if (vc->vc_mode == KD_TEXT)
1440 do_unblank_screen(1);
1448 * Wake anyone waiting for their VT to activate
1450 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1455 * Performs the front-end of a vt switch
1457 void change_console(struct vc_data *new_vc)
1461 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1465 * If this vt is in process mode, then we need to handshake with
1466 * that process before switching. Essentially, we store where that
1467 * vt wants to switch to and wait for it to tell us when it's done
1468 * (via VT_RELDISP ioctl).
1470 * We also check to see if the controlling process still exists.
1471 * If it doesn't, we reset this vt to auto mode and continue.
1472 * This is a cheap way to track process control. The worst thing
1473 * that can happen is: we send a signal to a process, it dies, and
1474 * the switch gets "lost" waiting for a response; hopefully, the
1475 * user will try again, we'll detect the process is gone (unless
1476 * the user waits just the right amount of time :-) and revert the
1477 * vt to auto control.
1479 vc = vc_cons[fg_console].d;
1480 if (vc->vt_mode.mode == VT_PROCESS) {
1482 * Send the signal as privileged - kill_pid() will
1483 * tell us if the process has gone or something else
1486 * We need to set vt_newvt *before* sending the signal or we
1489 vc->vt_newvt = new_vc->vc_num;
1490 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1492 * It worked. Mark the vt to switch to and
1493 * return. The process needs to send us a
1494 * VT_RELDISP ioctl to complete the switch.
1500 * The controlling process has died, so we revert back to
1501 * normal operation. In this case, we'll also change back
1502 * to KD_TEXT mode. I'm not sure if this is strictly correct
1503 * but it saves the agony when the X server dies and the screen
1504 * remains blanked due to KD_GRAPHICS! It would be nice to do
1505 * this outside of VT_PROCESS but there is no single process
1506 * to account for and tracking tty count may be undesirable.
1511 * Fall through to normal (VT_AUTO) handling of the switch...
1516 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1518 if (vc->vc_mode == KD_GRAPHICS)
1521 complete_change_console(new_vc);
1524 /* Perform a kernel triggered VT switch for suspend/resume */
1526 static int disable_vt_switch;
1528 int vt_move_to_console(unsigned int vt, int alloc)
1532 acquire_console_sem();
1533 /* Graphics mode - up to X */
1534 if (disable_vt_switch) {
1535 release_console_sem();
1540 if (alloc && vc_allocate(vt)) {
1541 /* we can't have a free VC for now. Too bad,
1542 * we don't want to mess the screen for now. */
1543 release_console_sem();
1547 if (set_console(vt)) {
1549 * We're unable to switch to the SUSPEND_CONSOLE.
1550 * Let the calling function know so it can decide
1553 release_console_sem();
1556 release_console_sem();
1557 if (vt_waitactive(vt)) {
1558 pr_debug("Suspend: Can't switch VCs.");
1565 * Normally during a suspend, we allocate a new console and switch to it.
1566 * When we resume, we switch back to the original console. This switch
1567 * can be slow, so on systems where the framebuffer can handle restoration
1568 * of video registers anyways, there's little point in doing the console
1569 * switch. This function allows you to disable it by passing it '0'.
1571 void pm_set_vt_switch(int do_switch)
1573 acquire_console_sem();
1574 disable_vt_switch = !do_switch;
1575 release_console_sem();
1577 EXPORT_SYMBOL(pm_set_vt_switch);