2 * Copyright (C) 1992 obz under the linux copyright
4 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
5 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
6 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
7 * Some code moved for less code duplication - Andi Kleen - Mar 1997
8 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/tty.h>
15 #include <linux/timer.h>
16 #include <linux/kernel.h>
17 #include <linux/compat.h>
18 #include <linux/module.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/timex.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>
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.
58 #include <linux/syscalls.h>
61 static void complete_change_console(struct vc_data
*vc
);
64 * User space VT_EVENT handlers
67 struct vt_event_wait
{
68 struct list_head list
;
69 struct vt_event event
;
73 static LIST_HEAD(vt_events
);
74 static DEFINE_SPINLOCK(vt_event_lock
);
75 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue
);
79 * @event: the event that occurred
83 * Post an VT event to interested VT handlers
86 void vt_event_post(unsigned int event
, unsigned int old
, unsigned int new)
88 struct list_head
*pos
, *head
;
92 spin_lock_irqsave(&vt_event_lock
, flags
);
95 list_for_each(pos
, head
) {
96 struct vt_event_wait
*ve
= list_entry(pos
,
97 struct vt_event_wait
, list
);
98 if (!(ve
->event
.event
& event
))
100 ve
->event
.event
= event
;
101 /* kernel view is consoles 0..n-1, user space view is
102 console 1..n with 0 meaning current, so we must bias */
103 ve
->event
.oldev
= old
+ 1;
104 ve
->event
.newev
= new + 1;
108 spin_unlock_irqrestore(&vt_event_lock
, flags
);
110 wake_up_interruptible(&vt_event_waitqueue
);
113 static void __vt_event_queue(struct vt_event_wait
*vw
)
116 /* Prepare the event */
117 INIT_LIST_HEAD(&vw
->list
);
119 /* Queue our event */
120 spin_lock_irqsave(&vt_event_lock
, flags
);
121 list_add(&vw
->list
, &vt_events
);
122 spin_unlock_irqrestore(&vt_event_lock
, flags
);
125 static void __vt_event_wait(struct vt_event_wait
*vw
)
127 /* Wait for it to pass */
128 wait_event_interruptible(vt_event_waitqueue
, vw
->done
);
131 static void __vt_event_dequeue(struct vt_event_wait
*vw
)
136 spin_lock_irqsave(&vt_event_lock
, flags
);
138 spin_unlock_irqrestore(&vt_event_lock
, flags
);
142 * vt_event_wait - wait for an event
145 * Waits for an event to occur which completes our vt_event_wait
146 * structure. On return the structure has wv->done set to 1 for success
147 * or 0 if some event such as a signal ended the wait.
150 static void vt_event_wait(struct vt_event_wait
*vw
)
152 __vt_event_queue(vw
);
154 __vt_event_dequeue(vw
);
158 * vt_event_wait_ioctl - event ioctl handler
159 * @arg: argument to ioctl
161 * Implement the VT_WAITEVENT ioctl using the VT event interface
164 static int vt_event_wait_ioctl(struct vt_event __user
*event
)
166 struct vt_event_wait vw
;
168 if (copy_from_user(&vw
.event
, event
, sizeof(struct vt_event
)))
170 /* Highest supported event for now */
171 if (vw
.event
.event
& ~VT_MAX_EVENT
)
175 /* If it occurred report it */
177 if (copy_to_user(event
, &vw
.event
, sizeof(struct vt_event
)))
185 * vt_waitactive - active console wait
189 * Helper for event waits. Used to implement the legacy
190 * event waiting ioctls in terms of events
193 int vt_waitactive(int n
)
195 struct vt_event_wait vw
;
197 vw
.event
.event
= VT_EVENT_SWITCH
;
198 __vt_event_queue(&vw
);
199 if (n
== fg_console
+ 1) {
200 __vt_event_dequeue(&vw
);
203 __vt_event_wait(&vw
);
204 __vt_event_dequeue(&vw
);
207 } while (vw
.event
.newev
!= n
);
212 * these are the valid i/o ports we're allowed to change. they map all the
215 #define GPFIRST 0x3b4
217 #define GPNUM (GPLAST - GPFIRST + 1)
222 do_fontx_ioctl(int cmd
, struct consolefontdesc __user
*user_cfd
, int perm
, struct console_font_op
*op
)
224 struct consolefontdesc cfdarg
;
227 if (copy_from_user(&cfdarg
, user_cfd
, sizeof(struct consolefontdesc
)))
234 op
->op
= KD_FONT_OP_SET
;
235 op
->flags
= KD_FONT_FLAG_OLD
;
237 op
->height
= cfdarg
.charheight
;
238 op
->charcount
= cfdarg
.charcount
;
239 op
->data
= cfdarg
.chardata
;
240 return con_font_op(vc_cons
[fg_console
].d
, op
);
242 op
->op
= KD_FONT_OP_GET
;
243 op
->flags
= KD_FONT_FLAG_OLD
;
245 op
->height
= cfdarg
.charheight
;
246 op
->charcount
= cfdarg
.charcount
;
247 op
->data
= cfdarg
.chardata
;
248 i
= con_font_op(vc_cons
[fg_console
].d
, op
);
251 cfdarg
.charheight
= op
->height
;
252 cfdarg
.charcount
= op
->charcount
;
253 if (copy_to_user(user_cfd
, &cfdarg
, sizeof(struct consolefontdesc
)))
262 do_unimap_ioctl(int cmd
, struct unimapdesc __user
*user_ud
, int perm
, struct vc_data
*vc
)
264 struct unimapdesc tmp
;
266 if (copy_from_user(&tmp
, user_ud
, sizeof tmp
))
269 if (!access_ok(VERIFY_WRITE
, tmp
.entries
,
270 tmp
.entry_ct
*sizeof(struct unipair
)))
276 return con_set_unimap(vc
, tmp
.entry_ct
, tmp
.entries
);
278 if (!perm
&& fg_console
!= vc
->vc_num
)
280 return con_get_unimap(vc
, tmp
.entry_ct
, &(user_ud
->entry_ct
), tmp
.entries
);
288 * We handle the console-specific ioctl's here. We allow the
289 * capability to modify any console, not just the fg_console.
291 int vt_ioctl(struct tty_struct
*tty
,
292 unsigned int cmd
, unsigned long arg
)
294 struct vc_data
*vc
= tty
->driver_data
;
295 struct console_font_op op
; /* used in multiple places here */
296 unsigned int console
;
299 void __user
*up
= (void __user
*)arg
;
303 console
= vc
->vc_num
;
306 if (!vc_cons_allocated(console
)) { /* impossible? */
313 * To have permissions to do most of the vt ioctls, we either have
314 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
317 if (current
->signal
->tty
== tty
|| capable(CAP_SYS_TTY_CONFIG
))
322 ret
= tioclinux(tty
, arg
);
328 * The use of PIT_TICK_RATE is historic, it used to be
329 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
330 * and 2.6.36, which was a minor but unfortunate ABI
331 * change. kd_mksound is locked by the input layer.
334 arg
= PIT_TICK_RATE
/ arg
;
342 unsigned int ticks
, count
;
345 * Generate the tone for the appropriate number of ticks.
346 * If the time is zero, turn off sound ourselves.
348 ticks
= HZ
* ((arg
>> 16) & 0xffff) / 1000;
349 count
= ticks
? (arg
& 0xffff) : 0;
351 count
= PIT_TICK_RATE
/ count
;
352 kd_mksound(count
, ticks
);
361 ret
= put_user(ucval
, (char __user
*)arg
);
365 * These cannot be implemented on any machine that implements
366 * ioperm() in user level (such as Alpha PCs) or not at all.
368 * XXX: you should never use these, just call ioperm directly..
374 * KDADDIO and KDDELIO may be able to add ports beyond what
375 * we reject here, but to be safe...
377 * These are locked internally via sys_ioperm
379 if (arg
< GPFIRST
|| arg
> GPLAST
) {
383 ret
= sys_ioperm(arg
, 1, (cmd
== KDADDIO
)) ? -ENXIO
: 0;
388 ret
= sys_ioperm(GPFIRST
, GPNUM
,
389 (cmd
== KDENABIO
)) ? -ENXIO
: 0;
393 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
397 struct kbd_repeat kbrep
;
399 if (!capable(CAP_SYS_TTY_CONFIG
))
402 if (copy_from_user(&kbrep
, up
, sizeof(struct kbd_repeat
))) {
406 ret
= kbd_rate(&kbrep
);
409 if (copy_to_user(up
, &kbrep
, sizeof(struct kbd_repeat
)))
416 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
417 * doesn't do a whole lot. i'm not sure if it should do any
418 * restoration of modes or what...
420 * XXX It should at least call into the driver, fbdev's definitely
421 * need to restore their engine state. --BenH
437 /* FIXME: this needs the console lock extending */
438 if (vc
->vc_mode
== (unsigned char) arg
)
440 vc
->vc_mode
= (unsigned char) arg
;
441 if (console
!= fg_console
)
444 * explicitly blank/unblank the screen if switching modes
448 do_unblank_screen(1);
461 * these work like a combination of mmap and KDENABIO.
462 * this could be easily finished.
470 ret
= vt_do_kdskbmode(console
, arg
);
472 tty_ldisc_flush(tty
);
476 uival
= vt_do_kdgkbmode(console
);
477 ret
= put_user(uival
, (int __user
*)arg
);
480 /* this could be folded into KDSKBMODE, but for compatibility
481 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
483 ret
= vt_do_kdskbmeta(console
, arg
);
487 /* FIXME: should review whether this is worth locking */
488 uival
= vt_do_kdgkbmeta(console
);
490 ret
= put_user(uival
, (int __user
*)arg
);
495 if(!capable(CAP_SYS_TTY_CONFIG
))
497 ret
= vt_do_kbkeycode_ioctl(cmd
, up
, perm
);
502 ret
= vt_do_kdsk_ioctl(cmd
, up
, perm
, console
);
507 ret
= vt_do_kdgkb_ioctl(cmd
, up
, perm
);
510 /* Diacritical processing. Handled in keyboard.c as it has
511 to operate on the keyboard locks and structures */
516 ret
= vt_do_diacrit(cmd
, up
, perm
);
519 /* the ioctls below read/set the flags usually shown in the leds */
520 /* don't use them - they will go away without warning */
525 ret
= vt_do_kdskled(console
, cmd
, arg
, perm
);
529 * A process can indicate its willingness to accept signals
530 * generated by pressing an appropriate key combination.
531 * Thus, one can have a daemon that e.g. spawns a new console
532 * upon a keypress and then changes to it.
533 * See also the kbrequest field of inittab(5).
537 if (!perm
|| !capable(CAP_KILL
))
539 if (!valid_signal(arg
) || arg
< 1 || arg
== SIGKILL
)
542 spin_lock_irq(&vt_spawn_con
.lock
);
543 put_pid(vt_spawn_con
.pid
);
544 vt_spawn_con
.pid
= get_pid(task_pid(current
));
545 vt_spawn_con
.sig
= arg
;
546 spin_unlock_irq(&vt_spawn_con
.lock
);
557 if (copy_from_user(&tmp
, up
, sizeof(struct vt_mode
))) {
561 if (tmp
.mode
!= VT_AUTO
&& tmp
.mode
!= VT_PROCESS
) {
567 /* the frsig is ignored, so we set it to 0 */
568 vc
->vt_mode
.frsig
= 0;
570 vc
->vt_pid
= get_pid(task_pid(current
));
571 /* no switch is required -- saw@shade.msu.ru */
583 memcpy(&tmp
, &vc
->vt_mode
, sizeof(struct vt_mode
));
586 rc
= copy_to_user(up
, &tmp
, sizeof(struct vt_mode
));
593 * Returns global vt state. Note that VT 0 is always open, since
594 * it's an alias for the current VT, and people can't use it here.
595 * We cannot return state for more than 16 VTs, since v_state is short.
599 struct vt_stat __user
*vtstat
= up
;
600 unsigned short state
, mask
;
602 /* Review: FIXME: Console lock ? */
603 if (put_user(fg_console
+ 1, &vtstat
->v_active
))
606 state
= 1; /* /dev/tty0 is always open */
607 for (i
= 0, mask
= 2; i
< MAX_NR_CONSOLES
&& mask
;
611 ret
= put_user(state
, &vtstat
->v_state
);
617 * Returns the first available (non-opened) console.
620 /* FIXME: locking ? - but then this is a stupid API */
621 for (i
= 0; i
< MAX_NR_CONSOLES
; ++i
)
622 if (! VT_IS_IN_USE(i
))
624 uival
= i
< MAX_NR_CONSOLES
? (i
+1) : -1;
628 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
629 * with num >= 1 (switches to vt 0, our console, are not allowed, just
630 * to preserve sanity).
635 if (arg
== 0 || arg
> MAX_NR_CONSOLES
)
640 ret
= vc_allocate(arg
);
650 struct vt_setactivate vsa
;
655 if (copy_from_user(&vsa
, (struct vt_setactivate __user
*)arg
,
656 sizeof(struct vt_setactivate
))) {
660 if (vsa
.console
== 0 || vsa
.console
> MAX_NR_CONSOLES
)
665 ret
= vc_allocate(vsa
.console
);
668 /* This is safe providing we don't drop the
669 console sem between vc_allocate and
670 finishing referencing nvc */
671 nvc
= vc_cons
[vsa
.console
].d
;
672 nvc
->vt_mode
= vsa
.mode
;
673 nvc
->vt_mode
.frsig
= 0;
674 put_pid(nvc
->vt_pid
);
675 nvc
->vt_pid
= get_pid(task_pid(current
));
680 /* Commence switch and lock */
681 /* Review set_console locks */
682 set_console(vsa
.console
);
688 * wait until the specified VT has been activated
693 if (arg
== 0 || arg
> MAX_NR_CONSOLES
)
696 ret
= vt_waitactive(arg
);
700 * If a vt is under process control, the kernel will not switch to it
701 * immediately, but postpone the operation until the process calls this
702 * ioctl, allowing the switch to complete.
704 * According to the X sources this is the behavior:
705 * 0: pending switch-from not OK
706 * 1: pending switch-from OK
707 * 2: completed switch-to OK
714 if (vc
->vt_mode
.mode
!= VT_PROCESS
) {
720 * Switching-from response
722 if (vc
->vt_newvt
>= 0) {
725 * Switch disallowed, so forget we were trying
732 * The current vt has been released, so
733 * complete the switch.
736 newvt
= vc
->vt_newvt
;
738 ret
= vc_allocate(newvt
);
744 * When we actually do the console switch,
745 * make sure we are atomic with respect to
746 * other console switches..
748 complete_change_console(vc_cons
[newvt
].d
);
752 * Switched-to response
755 * If it's just an ACK, ignore it
757 if (arg
!= VT_ACKACQ
)
764 * Disallocate memory associated to VT (but leave VT1)
767 if (arg
> MAX_NR_CONSOLES
) {
772 /* deallocate all unused consoles, but leave 0 */
774 for (i
=1; i
<MAX_NR_CONSOLES
; i
++)
779 /* deallocate a single console, if possible */
783 else if (arg
) { /* leave 0 */
793 struct vt_sizes __user
*vtsizes
= up
;
799 if (get_user(ll
, &vtsizes
->v_rows
) ||
800 get_user(cc
, &vtsizes
->v_cols
))
804 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
808 vc
->vc_resize_user
= 1;
809 /* FIXME: review v tty lock */
810 vc_resize(vc_cons
[i
].d
, cc
, ll
);
820 struct vt_consize __user
*vtconsize
= up
;
821 ushort ll
,cc
,vlin
,clin
,vcol
,ccol
;
824 if (!access_ok(VERIFY_READ
, vtconsize
,
825 sizeof(struct vt_consize
))) {
829 /* FIXME: Should check the copies properly */
830 __get_user(ll
, &vtconsize
->v_rows
);
831 __get_user(cc
, &vtconsize
->v_cols
);
832 __get_user(vlin
, &vtconsize
->v_vlin
);
833 __get_user(clin
, &vtconsize
->v_clin
);
834 __get_user(vcol
, &vtconsize
->v_vcol
);
835 __get_user(ccol
, &vtconsize
->v_ccol
);
836 vlin
= vlin
? vlin
: vc
->vc_scan_lines
;
839 if (ll
!= vlin
/clin
) {
840 /* Parameters don't add up */
849 if (cc
!= vcol
/ccol
) {
862 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
867 vc_cons
[i
].d
->vc_scan_lines
= vlin
;
869 vc_cons
[i
].d
->vc_font
.height
= clin
;
870 vc_cons
[i
].d
->vc_resize_user
= 1;
871 vc_resize(vc_cons
[i
].d
, cc
, ll
);
880 op
.op
= KD_FONT_OP_SET
;
881 op
.flags
= KD_FONT_FLAG_OLD
| KD_FONT_FLAG_DONT_RECALC
; /* Compatibility */
886 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
891 op
.op
= KD_FONT_OP_GET
;
892 op
.flags
= KD_FONT_FLAG_OLD
;
897 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
905 ret
= con_set_cmap(up
);
909 ret
= con_get_cmap(up
);
914 ret
= do_fontx_ioctl(cmd
, up
, perm
, &op
);
922 #ifdef BROKEN_GRAPHICS_PROGRAMS
923 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
924 font is not saved. */
929 op
.op
= KD_FONT_OP_SET_DEFAULT
;
931 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
935 con_set_default_unimap(vc_cons
[fg_console
].d
);
943 if (copy_from_user(&op
, up
, sizeof(op
))) {
947 if (!perm
&& op
.op
!= KD_FONT_OP_GET
)
949 ret
= con_font_op(vc
, &op
);
952 if (copy_to_user(up
, &op
, sizeof(op
)))
961 ret
= con_set_trans_old(up
);
965 ret
= con_get_trans_old(up
);
972 ret
= con_set_trans_new(up
);
976 ret
= con_get_trans_new(up
);
980 { struct unimapinit ui
;
983 ret
= copy_from_user(&ui
, up
, sizeof(struct unimapinit
));
987 con_clear_unimap(vc
, &ui
);
993 ret
= do_unimap_ioctl(cmd
, up
, perm
, vc
);
997 if (!capable(CAP_SYS_TTY_CONFIG
))
1001 case VT_UNLOCKSWITCH
:
1002 if (!capable(CAP_SYS_TTY_CONFIG
))
1006 case VT_GETHIFONTMASK
:
1007 ret
= put_user(vc
->vc_hi_font_mask
,
1008 (unsigned short __user
*)arg
);
1011 ret
= vt_event_wait_ioctl((struct vt_event __user
*)arg
);
1020 void reset_vc(struct vc_data
*vc
)
1022 vc
->vc_mode
= KD_TEXT
;
1023 vt_reset_unicode(vc
->vc_num
);
1024 vc
->vt_mode
.mode
= VT_AUTO
;
1025 vc
->vt_mode
.waitv
= 0;
1026 vc
->vt_mode
.relsig
= 0;
1027 vc
->vt_mode
.acqsig
= 0;
1028 vc
->vt_mode
.frsig
= 0;
1029 put_pid(vc
->vt_pid
);
1032 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1036 void vc_SAK(struct work_struct
*work
)
1039 container_of(work
, struct vc
, SAK_work
);
1041 struct tty_struct
*tty
;
1046 /* FIXME: review tty ref counting */
1049 * SAK should also work in all raw modes and reset
1059 #ifdef CONFIG_COMPAT
1061 struct compat_consolefontdesc
{
1062 unsigned short charcount
; /* characters in font (256 or 512) */
1063 unsigned short charheight
; /* scan lines per character (1-32) */
1064 compat_caddr_t chardata
; /* font data in expanded form */
1068 compat_fontx_ioctl(int cmd
, struct compat_consolefontdesc __user
*user_cfd
,
1069 int perm
, struct console_font_op
*op
)
1071 struct compat_consolefontdesc cfdarg
;
1074 if (copy_from_user(&cfdarg
, user_cfd
, sizeof(struct compat_consolefontdesc
)))
1081 op
->op
= KD_FONT_OP_SET
;
1082 op
->flags
= KD_FONT_FLAG_OLD
;
1084 op
->height
= cfdarg
.charheight
;
1085 op
->charcount
= cfdarg
.charcount
;
1086 op
->data
= compat_ptr(cfdarg
.chardata
);
1087 return con_font_op(vc_cons
[fg_console
].d
, op
);
1089 op
->op
= KD_FONT_OP_GET
;
1090 op
->flags
= KD_FONT_FLAG_OLD
;
1092 op
->height
= cfdarg
.charheight
;
1093 op
->charcount
= cfdarg
.charcount
;
1094 op
->data
= compat_ptr(cfdarg
.chardata
);
1095 i
= con_font_op(vc_cons
[fg_console
].d
, op
);
1098 cfdarg
.charheight
= op
->height
;
1099 cfdarg
.charcount
= op
->charcount
;
1100 if (copy_to_user(user_cfd
, &cfdarg
, sizeof(struct compat_consolefontdesc
)))
1107 struct compat_console_font_op
{
1108 compat_uint_t op
; /* operation code KD_FONT_OP_* */
1109 compat_uint_t flags
; /* KD_FONT_FLAG_* */
1110 compat_uint_t width
, height
; /* font size */
1111 compat_uint_t charcount
;
1112 compat_caddr_t data
; /* font data with height fixed to 32 */
1116 compat_kdfontop_ioctl(struct compat_console_font_op __user
*fontop
,
1117 int perm
, struct console_font_op
*op
, struct vc_data
*vc
)
1121 if (copy_from_user(op
, fontop
, sizeof(struct compat_console_font_op
)))
1123 if (!perm
&& op
->op
!= KD_FONT_OP_GET
)
1125 op
->data
= compat_ptr(((struct compat_console_font_op
*)op
)->data
);
1126 i
= con_font_op(vc
, op
);
1129 ((struct compat_console_font_op
*)op
)->data
= (unsigned long)op
->data
;
1130 if (copy_to_user(fontop
, op
, sizeof(struct compat_console_font_op
)))
1135 struct compat_unimapdesc
{
1136 unsigned short entry_ct
;
1137 compat_caddr_t entries
;
1141 compat_unimap_ioctl(unsigned int cmd
, struct compat_unimapdesc __user
*user_ud
,
1142 int perm
, struct vc_data
*vc
)
1144 struct compat_unimapdesc tmp
;
1145 struct unipair __user
*tmp_entries
;
1147 if (copy_from_user(&tmp
, user_ud
, sizeof tmp
))
1149 tmp_entries
= compat_ptr(tmp
.entries
);
1151 if (!access_ok(VERIFY_WRITE
, tmp_entries
,
1152 tmp
.entry_ct
*sizeof(struct unipair
)))
1158 return con_set_unimap(vc
, tmp
.entry_ct
, tmp_entries
);
1160 if (!perm
&& fg_console
!= vc
->vc_num
)
1162 return con_get_unimap(vc
, tmp
.entry_ct
, &(user_ud
->entry_ct
), tmp_entries
);
1167 long vt_compat_ioctl(struct tty_struct
*tty
,
1168 unsigned int cmd
, unsigned long arg
)
1170 struct vc_data
*vc
= tty
->driver_data
;
1171 struct console_font_op op
; /* used in multiple places here */
1172 unsigned int console
;
1173 void __user
*up
= (void __user
*)arg
;
1177 console
= vc
->vc_num
;
1179 if (!vc_cons_allocated(console
)) { /* impossible? */
1185 * To have permissions to do most of the vt ioctls, we either have
1186 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1189 if (current
->signal
->tty
== tty
|| capable(CAP_SYS_TTY_CONFIG
))
1194 * these need special handlers for incompatible data structures
1198 ret
= compat_fontx_ioctl(cmd
, up
, perm
, &op
);
1202 ret
= compat_kdfontop_ioctl(up
, perm
, &op
, vc
);
1207 ret
= compat_unimap_ioctl(cmd
, up
, perm
, vc
);
1211 * all these treat 'arg' as an integer
1230 case VT_DISALLOCATE
:
1236 * the rest has a compatible data structure behind arg,
1237 * but we have to convert it to a proper 64 bit pointer.
1240 arg
= (unsigned long)compat_ptr(arg
);
1247 return vt_ioctl(tty
, cmd
, arg
);
1251 #endif /* CONFIG_COMPAT */
1255 * Performs the back end of a vt switch. Called under the console
1258 static void complete_change_console(struct vc_data
*vc
)
1260 unsigned char old_vc_mode
;
1261 int old
= fg_console
;
1263 last_console
= fg_console
;
1266 * If we're switching, we could be going from KD_GRAPHICS to
1267 * KD_TEXT mode or vice versa, which means we need to blank or
1268 * unblank the screen later.
1270 old_vc_mode
= vc_cons
[fg_console
].d
->vc_mode
;
1274 * This can't appear below a successful kill_pid(). If it did,
1275 * then the *blank_screen operation could occur while X, having
1276 * received acqsig, is waking up on another processor. This
1277 * condition can lead to overlapping accesses to the VGA range
1278 * and the framebuffer (causing system lockups).
1280 * To account for this we duplicate this code below only if the
1281 * controlling process is gone and we've called reset_vc.
1283 if (old_vc_mode
!= vc
->vc_mode
) {
1284 if (vc
->vc_mode
== KD_TEXT
)
1285 do_unblank_screen(1);
1291 * If this new console is under process control, send it a signal
1292 * telling it that it has acquired. Also check if it has died and
1293 * clean up (similar to logic employed in change_console())
1295 if (vc
->vt_mode
.mode
== VT_PROCESS
) {
1297 * Send the signal as privileged - kill_pid() will
1298 * tell us if the process has gone or something else
1301 if (kill_pid(vc
->vt_pid
, vc
->vt_mode
.acqsig
, 1) != 0) {
1303 * The controlling process has died, so we revert back to
1304 * normal operation. In this case, we'll also change back
1305 * to KD_TEXT mode. I'm not sure if this is strictly correct
1306 * but it saves the agony when the X server dies and the screen
1307 * remains blanked due to KD_GRAPHICS! It would be nice to do
1308 * this outside of VT_PROCESS but there is no single process
1309 * to account for and tracking tty count may be undesirable.
1313 if (old_vc_mode
!= vc
->vc_mode
) {
1314 if (vc
->vc_mode
== KD_TEXT
)
1315 do_unblank_screen(1);
1323 * Wake anyone waiting for their VT to activate
1325 vt_event_post(VT_EVENT_SWITCH
, old
, vc
->vc_num
);
1330 * Performs the front-end of a vt switch
1332 void change_console(struct vc_data
*new_vc
)
1336 if (!new_vc
|| new_vc
->vc_num
== fg_console
|| vt_dont_switch
)
1340 * If this vt is in process mode, then we need to handshake with
1341 * that process before switching. Essentially, we store where that
1342 * vt wants to switch to and wait for it to tell us when it's done
1343 * (via VT_RELDISP ioctl).
1345 * We also check to see if the controlling process still exists.
1346 * If it doesn't, we reset this vt to auto mode and continue.
1347 * This is a cheap way to track process control. The worst thing
1348 * that can happen is: we send a signal to a process, it dies, and
1349 * the switch gets "lost" waiting for a response; hopefully, the
1350 * user will try again, we'll detect the process is gone (unless
1351 * the user waits just the right amount of time :-) and revert the
1352 * vt to auto control.
1354 vc
= vc_cons
[fg_console
].d
;
1355 if (vc
->vt_mode
.mode
== VT_PROCESS
) {
1357 * Send the signal as privileged - kill_pid() will
1358 * tell us if the process has gone or something else
1361 * We need to set vt_newvt *before* sending the signal or we
1364 vc
->vt_newvt
= new_vc
->vc_num
;
1365 if (kill_pid(vc
->vt_pid
, vc
->vt_mode
.relsig
, 1) == 0) {
1367 * It worked. Mark the vt to switch to and
1368 * return. The process needs to send us a
1369 * VT_RELDISP ioctl to complete the switch.
1375 * The controlling process has died, so we revert back to
1376 * normal operation. In this case, we'll also change back
1377 * to KD_TEXT mode. I'm not sure if this is strictly correct
1378 * but it saves the agony when the X server dies and the screen
1379 * remains blanked due to KD_GRAPHICS! It would be nice to do
1380 * this outside of VT_PROCESS but there is no single process
1381 * to account for and tracking tty count may be undesirable.
1386 * Fall through to normal (VT_AUTO) handling of the switch...
1391 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1393 if (vc
->vc_mode
== KD_GRAPHICS
)
1396 complete_change_console(new_vc
);
1399 /* Perform a kernel triggered VT switch for suspend/resume */
1401 static int disable_vt_switch
;
1403 int vt_move_to_console(unsigned int vt
, int alloc
)
1408 /* Graphics mode - up to X */
1409 if (disable_vt_switch
) {
1415 if (alloc
&& vc_allocate(vt
)) {
1416 /* we can't have a free VC for now. Too bad,
1417 * we don't want to mess the screen for now. */
1422 if (set_console(vt
)) {
1424 * We're unable to switch to the SUSPEND_CONSOLE.
1425 * Let the calling function know so it can decide
1432 if (vt_waitactive(vt
+ 1)) {
1433 pr_debug("Suspend: Can't switch VCs.");
1440 * Normally during a suspend, we allocate a new console and switch to it.
1441 * When we resume, we switch back to the original console. This switch
1442 * can be slow, so on systems where the framebuffer can handle restoration
1443 * of video registers anyways, there's little point in doing the console
1444 * switch. This function allows you to disable it by passing it '0'.
1446 void pm_set_vt_switch(int do_switch
)
1449 disable_vt_switch
= !do_switch
;
1452 EXPORT_SYMBOL(pm_set_vt_switch
);