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/compat.h>
20 #include <linux/module.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/major.h>
27 #include <linux/console.h>
28 #include <linux/consolemap.h>
29 #include <linux/signal.h>
30 #include <linux/smp_lock.h>
31 #include <linux/timex.h>
34 #include <asm/uaccess.h>
36 #include <linux/kbd_kern.h>
37 #include <linux/vt_kern.h>
38 #include <linux/kbd_diacr.h>
39 #include <linux/selection.h>
42 extern struct tty_driver
*console_driver
;
44 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
45 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
48 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
49 * experimentation and study of X386 SYSV handling.
51 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
52 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
53 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
54 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
55 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
56 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
57 * to the current console is done by the main ioctl code.
61 #include <linux/syscalls.h>
64 static void complete_change_console(struct vc_data
*vc
);
67 * User space VT_EVENT handlers
70 struct vt_event_wait
{
71 struct list_head list
;
72 struct vt_event event
;
76 static LIST_HEAD(vt_events
);
77 static DEFINE_SPINLOCK(vt_event_lock
);
78 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue
);
82 * @event: the event that occurred
86 * Post an VT event to interested VT handlers
89 void vt_event_post(unsigned int event
, unsigned int old
, unsigned int new)
91 struct list_head
*pos
, *head
;
95 spin_lock_irqsave(&vt_event_lock
, flags
);
98 list_for_each(pos
, head
) {
99 struct vt_event_wait
*ve
= list_entry(pos
,
100 struct vt_event_wait
, list
);
101 if (!(ve
->event
.event
& event
))
103 ve
->event
.event
= event
;
104 /* kernel view is consoles 0..n-1, user space view is
105 console 1..n with 0 meaning current, so we must bias */
106 ve
->event
.oldev
= old
+ 1;
107 ve
->event
.newev
= new + 1;
111 spin_unlock_irqrestore(&vt_event_lock
, flags
);
113 wake_up_interruptible(&vt_event_waitqueue
);
117 * vt_event_wait - wait for an event
120 * Waits for an event to occur which completes our vt_event_wait
121 * structure. On return the structure has wv->done set to 1 for success
122 * or 0 if some event such as a signal ended the wait.
125 static void vt_event_wait(struct vt_event_wait
*vw
)
128 /* Prepare the event */
129 INIT_LIST_HEAD(&vw
->list
);
131 /* Queue our event */
132 spin_lock_irqsave(&vt_event_lock
, flags
);
133 list_add(&vw
->list
, &vt_events
);
134 spin_unlock_irqrestore(&vt_event_lock
, flags
);
135 /* Wait for it to pass */
136 wait_event_interruptible(vt_event_waitqueue
, vw
->done
);
138 spin_lock_irqsave(&vt_event_lock
, flags
);
140 spin_unlock_irqrestore(&vt_event_lock
, flags
);
144 * vt_event_wait_ioctl - event ioctl handler
145 * @arg: argument to ioctl
147 * Implement the VT_WAITEVENT ioctl using the VT event interface
150 static int vt_event_wait_ioctl(struct vt_event __user
*event
)
152 struct vt_event_wait vw
;
154 if (copy_from_user(&vw
.event
, event
, sizeof(struct vt_event
)))
156 /* Highest supported event for now */
157 if (vw
.event
.event
& ~VT_MAX_EVENT
)
161 /* If it occurred report it */
163 if (copy_to_user(event
, &vw
.event
, sizeof(struct vt_event
)))
171 * vt_waitactive - active console wait
175 * Helper for event waits. Used to implement the legacy
176 * event waiting ioctls in terms of events
179 int vt_waitactive(int n
)
181 struct vt_event_wait vw
;
183 if (n
== fg_console
+ 1)
185 vw
.event
.event
= VT_EVENT_SWITCH
;
189 } while (vw
.event
.newev
!= n
);
194 * these are the valid i/o ports we're allowed to change. they map all the
197 #define GPFIRST 0x3b4
199 #define GPNUM (GPLAST - GPFIRST + 1)
201 #define i (tmp.kb_index)
202 #define s (tmp.kb_table)
203 #define v (tmp.kb_value)
205 do_kdsk_ioctl(int cmd
, struct kbentry __user
*user_kbe
, int perm
, struct kbd_struct
*kbd
)
208 ushort
*key_map
, val
, ov
;
210 if (copy_from_user(&tmp
, user_kbe
, sizeof(struct kbentry
)))
213 if (!capable(CAP_SYS_TTY_CONFIG
))
218 key_map
= key_maps
[s
];
221 if (kbd
->kbdmode
!= VC_UNICODE
&& KTYP(val
) >= NR_TYPES
)
224 val
= (i
? K_HOLE
: K_NOSUCHMAP
);
225 return put_user(val
, &user_kbe
->kb_value
);
229 if (!i
&& v
== K_NOSUCHMAP
) {
231 key_map
= key_maps
[s
];
234 if (key_map
[0] == U(K_ALLOCATED
)) {
242 if (KTYP(v
) < NR_TYPES
) {
243 if (KVAL(v
) > max_vals
[KTYP(v
)])
246 if (kbd
->kbdmode
!= VC_UNICODE
)
249 /* ++Geert: non-PC keyboards may generate keycode zero */
250 #if !defined(__mc68000__) && !defined(__powerpc__)
251 /* assignment to entry 0 only tests validity of args */
256 if (!(key_map
= key_maps
[s
])) {
259 if (keymap_count
>= MAX_NR_OF_USER_KEYMAPS
&&
260 !capable(CAP_SYS_RESOURCE
))
263 key_map
= kmalloc(sizeof(plain_map
),
267 key_maps
[s
] = key_map
;
268 key_map
[0] = U(K_ALLOCATED
);
269 for (j
= 1; j
< NR_KEYS
; j
++)
270 key_map
[j
] = U(K_HOLE
);
275 break; /* nothing to do */
279 if (((ov
== K_SAK
) || (v
== K_SAK
)) && !capable(CAP_SYS_ADMIN
))
282 if (!s
&& (KTYP(ov
) == KT_SHIFT
|| KTYP(v
) == KT_SHIFT
))
283 compute_shiftstate();
293 do_kbkeycode_ioctl(int cmd
, struct kbkeycode __user
*user_kbkc
, int perm
)
295 struct kbkeycode tmp
;
298 if (copy_from_user(&tmp
, user_kbkc
, sizeof(struct kbkeycode
)))
302 kc
= getkeycode(tmp
.scancode
);
304 kc
= put_user(kc
, &user_kbkc
->keycode
);
309 kc
= setkeycode(tmp
.scancode
, tmp
.keycode
);
316 do_kdgkb_ioctl(int cmd
, struct kbsentry __user
*user_kdgkb
, int perm
)
318 struct kbsentry
*kbs
;
324 char *first_free
, *fj
, *fnw
;
328 if (!capable(CAP_SYS_TTY_CONFIG
))
331 kbs
= kmalloc(sizeof(*kbs
), GFP_KERNEL
);
337 /* we mostly copy too much here (512bytes), but who cares ;) */
338 if (copy_from_user(kbs
, user_kdgkb
, sizeof(struct kbsentry
))) {
342 kbs
->kb_string
[sizeof(kbs
->kb_string
)-1] = '\0';
347 sz
= sizeof(kbs
->kb_string
) - 1; /* sz should have been
349 up
= user_kdgkb
->kb_string
;
352 for ( ; *p
&& sz
; p
++, sz
--)
353 if (put_user(*p
, up
++)) {
357 if (put_user('\0', up
)) {
362 return ((p
&& *p
) ? -EOVERFLOW
: 0);
370 first_free
= funcbufptr
+ (funcbufsize
- funcbufleft
);
371 for (j
= i
+1; j
< MAX_NR_FUNC
&& !func_table
[j
]; j
++)
378 delta
= (q
? -strlen(q
) : 1) + strlen(kbs
->kb_string
);
379 if (delta
<= funcbufleft
) { /* it fits in current buf */
380 if (j
< MAX_NR_FUNC
) {
381 memmove(fj
+ delta
, fj
, first_free
- fj
);
382 for (k
= j
; k
< MAX_NR_FUNC
; k
++)
384 func_table
[k
] += delta
;
388 funcbufleft
-= delta
;
389 } else { /* allocate a larger buffer */
391 while (sz
< funcbufsize
- funcbufleft
+ delta
)
393 fnw
= kmalloc(sz
, GFP_KERNEL
);
402 memmove(fnw
, funcbufptr
, fj
- funcbufptr
);
403 for (k
= 0; k
< j
; k
++)
405 func_table
[k
] = fnw
+ (func_table
[k
] - funcbufptr
);
407 if (first_free
> fj
) {
408 memmove(fnw
+ (fj
- funcbufptr
) + delta
, fj
, first_free
- fj
);
409 for (k
= j
; k
< MAX_NR_FUNC
; k
++)
411 func_table
[k
] = fnw
+ (func_table
[k
] - funcbufptr
) + delta
;
413 if (funcbufptr
!= func_buf
)
416 funcbufleft
= funcbufleft
- delta
+ sz
- funcbufsize
;
419 strcpy(func_table
[i
], kbs
->kb_string
);
429 do_fontx_ioctl(int cmd
, struct consolefontdesc __user
*user_cfd
, int perm
, struct console_font_op
*op
)
431 struct consolefontdesc cfdarg
;
434 if (copy_from_user(&cfdarg
, user_cfd
, sizeof(struct consolefontdesc
)))
441 op
->op
= KD_FONT_OP_SET
;
442 op
->flags
= KD_FONT_FLAG_OLD
;
444 op
->height
= cfdarg
.charheight
;
445 op
->charcount
= cfdarg
.charcount
;
446 op
->data
= cfdarg
.chardata
;
447 return con_font_op(vc_cons
[fg_console
].d
, op
);
449 op
->op
= KD_FONT_OP_GET
;
450 op
->flags
= KD_FONT_FLAG_OLD
;
452 op
->height
= cfdarg
.charheight
;
453 op
->charcount
= cfdarg
.charcount
;
454 op
->data
= cfdarg
.chardata
;
455 i
= con_font_op(vc_cons
[fg_console
].d
, op
);
458 cfdarg
.charheight
= op
->height
;
459 cfdarg
.charcount
= op
->charcount
;
460 if (copy_to_user(user_cfd
, &cfdarg
, sizeof(struct consolefontdesc
)))
469 do_unimap_ioctl(int cmd
, struct unimapdesc __user
*user_ud
, int perm
, struct vc_data
*vc
)
471 struct unimapdesc tmp
;
473 if (copy_from_user(&tmp
, user_ud
, sizeof tmp
))
476 if (!access_ok(VERIFY_WRITE
, tmp
.entries
,
477 tmp
.entry_ct
*sizeof(struct unipair
)))
483 return con_set_unimap(vc
, tmp
.entry_ct
, tmp
.entries
);
485 if (!perm
&& fg_console
!= vc
->vc_num
)
487 return con_get_unimap(vc
, tmp
.entry_ct
, &(user_ud
->entry_ct
), tmp
.entries
);
495 * We handle the console-specific ioctl's here. We allow the
496 * capability to modify any console, not just the fg_console.
498 int vt_ioctl(struct tty_struct
*tty
, struct file
* file
,
499 unsigned int cmd
, unsigned long arg
)
501 struct vc_data
*vc
= tty
->driver_data
;
502 struct console_font_op op
; /* used in multiple places here */
503 struct kbd_struct
* kbd
;
504 unsigned int console
;
507 void __user
*up
= (void __user
*)arg
;
511 console
= vc
->vc_num
;
515 if (!vc_cons_allocated(console
)) { /* impossible? */
522 * To have permissions to do most of the vt ioctls, we either have
523 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
526 if (current
->signal
->tty
== tty
|| capable(CAP_SYS_TTY_CONFIG
))
529 kbd
= kbd_table
+ console
;
532 ret
= tioclinux(tty
, arg
);
537 /* FIXME: This is an old broken API but we need to keep it
538 supported and somehow separate the historic advertised
539 tick rate from any real one */
541 arg
= CLOCK_TICK_RATE
/ arg
;
549 unsigned int ticks
, count
;
552 * Generate the tone for the appropriate number of ticks.
553 * If the time is zero, turn off sound ourselves.
555 ticks
= HZ
* ((arg
>> 16) & 0xffff) / 1000;
556 count
= ticks
? (arg
& 0xffff) : 0;
557 /* FIXME: This is an old broken API but we need to keep it
558 supported and somehow separate the historic advertised
559 tick rate from any real one */
561 count
= CLOCK_TICK_RATE
/ count
;
562 kd_mksound(count
, ticks
);
574 * These cannot be implemented on any machine that implements
575 * ioperm() in user level (such as Alpha PCs) or not at all.
577 * XXX: you should never use these, just call ioperm directly..
583 * KDADDIO and KDDELIO may be able to add ports beyond what
584 * we reject here, but to be safe...
586 if (arg
< GPFIRST
|| arg
> GPLAST
) {
590 ret
= sys_ioperm(arg
, 1, (cmd
== KDADDIO
)) ? -ENXIO
: 0;
595 ret
= sys_ioperm(GPFIRST
, GPNUM
,
596 (cmd
== KDENABIO
)) ? -ENXIO
: 0;
600 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
604 struct kbd_repeat kbrep
;
606 if (!capable(CAP_SYS_TTY_CONFIG
))
609 if (copy_from_user(&kbrep
, up
, sizeof(struct kbd_repeat
))) {
613 ret
= kbd_rate(&kbrep
);
616 if (copy_to_user(up
, &kbrep
, sizeof(struct kbd_repeat
)))
623 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
624 * doesn't do a whole lot. i'm not sure if it should do any
625 * restoration of modes or what...
627 * XXX It should at least call into the driver, fbdev's definitely
628 * need to restore their engine state. --BenH
644 if (vc
->vc_mode
== (unsigned char) arg
)
646 vc
->vc_mode
= (unsigned char) arg
;
647 if (console
!= fg_console
)
650 * explicitly blank/unblank the screen if switching modes
652 acquire_console_sem();
654 do_unblank_screen(1);
657 release_console_sem();
667 * these work like a combination of mmap and KDENABIO.
668 * this could be easily finished.
678 kbd
->kbdmode
= VC_RAW
;
681 kbd
->kbdmode
= VC_MEDIUMRAW
;
684 kbd
->kbdmode
= VC_XLATE
;
685 compute_shiftstate();
688 kbd
->kbdmode
= VC_UNICODE
;
689 compute_shiftstate();
695 tty_ldisc_flush(tty
);
699 uival
= ((kbd
->kbdmode
== VC_RAW
) ? K_RAW
:
700 (kbd
->kbdmode
== VC_MEDIUMRAW
) ? K_MEDIUMRAW
:
701 (kbd
->kbdmode
== VC_UNICODE
) ? K_UNICODE
:
705 /* this could be folded into KDSKBMODE, but for compatibility
706 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
710 clr_vc_kbd_mode(kbd
, VC_META
);
713 set_vc_kbd_mode(kbd
, VC_META
);
721 uival
= (vc_kbd_mode(kbd
, VC_META
) ? K_ESCPREFIX
: K_METABIT
);
723 ret
= put_user(uival
, (int __user
*)arg
);
728 if(!capable(CAP_SYS_TTY_CONFIG
))
730 ret
= do_kbkeycode_ioctl(cmd
, up
, perm
);
735 ret
= do_kdsk_ioctl(cmd
, up
, perm
, kbd
);
740 ret
= do_kdgkb_ioctl(cmd
, up
, perm
);
745 struct kbdiacrs __user
*a
= up
;
746 struct kbdiacr diacr
;
749 if (put_user(accent_table_size
, &a
->kb_cnt
)) {
753 for (i
= 0; i
< accent_table_size
; i
++) {
754 diacr
.diacr
= conv_uni_to_8bit(accent_table
[i
].diacr
);
755 diacr
.base
= conv_uni_to_8bit(accent_table
[i
].base
);
756 diacr
.result
= conv_uni_to_8bit(accent_table
[i
].result
);
757 if (copy_to_user(a
->kbdiacr
+ i
, &diacr
, sizeof(struct kbdiacr
))) {
766 struct kbdiacrsuc __user
*a
= up
;
768 if (put_user(accent_table_size
, &a
->kb_cnt
))
770 else if (copy_to_user(a
->kbdiacruc
, accent_table
,
771 accent_table_size
*sizeof(struct kbdiacruc
)))
778 struct kbdiacrs __user
*a
= up
;
779 struct kbdiacr diacr
;
785 if (get_user(ct
,&a
->kb_cnt
)) {
789 if (ct
>= MAX_DIACR
) {
793 accent_table_size
= ct
;
794 for (i
= 0; i
< ct
; i
++) {
795 if (copy_from_user(&diacr
, a
->kbdiacr
+ i
, sizeof(struct kbdiacr
))) {
799 accent_table
[i
].diacr
= conv_8bit_to_uni(diacr
.diacr
);
800 accent_table
[i
].base
= conv_8bit_to_uni(diacr
.base
);
801 accent_table
[i
].result
= conv_8bit_to_uni(diacr
.result
);
808 struct kbdiacrsuc __user
*a
= up
;
813 if (get_user(ct
,&a
->kb_cnt
)) {
817 if (ct
>= MAX_DIACR
) {
821 accent_table_size
= ct
;
822 if (copy_from_user(accent_table
, a
->kbdiacruc
, ct
*sizeof(struct kbdiacruc
)))
827 /* the ioctls below read/set the flags usually shown in the leds */
828 /* don't use them - they will go away without warning */
830 ucval
= kbd
->ledflagstate
| (kbd
->default_ledflagstate
<< 4);
840 kbd
->ledflagstate
= (arg
& 7);
841 kbd
->default_ledflagstate
= ((arg
>> 4) & 7);
845 /* the ioctls below only set the lights, not the functions */
846 /* for those, see KDGKBLED and KDSKBLED above */
848 ucval
= getledstate();
850 ret
= put_user(ucval
, (char __user
*)arg
);
856 setledstate(kbd
, arg
);
860 * A process can indicate its willingness to accept signals
861 * generated by pressing an appropriate key combination.
862 * Thus, one can have a daemon that e.g. spawns a new console
863 * upon a keypress and then changes to it.
864 * See also the kbrequest field of inittab(5).
868 if (!perm
|| !capable(CAP_KILL
))
870 if (!valid_signal(arg
) || arg
< 1 || arg
== SIGKILL
)
873 spin_lock_irq(&vt_spawn_con
.lock
);
874 put_pid(vt_spawn_con
.pid
);
875 vt_spawn_con
.pid
= get_pid(task_pid(current
));
876 vt_spawn_con
.sig
= arg
;
877 spin_unlock_irq(&vt_spawn_con
.lock
);
888 if (copy_from_user(&tmp
, up
, sizeof(struct vt_mode
))) {
892 if (tmp
.mode
!= VT_AUTO
&& tmp
.mode
!= VT_PROCESS
) {
896 acquire_console_sem();
898 /* the frsig is ignored, so we set it to 0 */
899 vc
->vt_mode
.frsig
= 0;
901 vc
->vt_pid
= get_pid(task_pid(current
));
902 /* no switch is required -- saw@shade.msu.ru */
904 release_console_sem();
913 acquire_console_sem();
914 memcpy(&tmp
, &vc
->vt_mode
, sizeof(struct vt_mode
));
915 release_console_sem();
917 rc
= copy_to_user(up
, &tmp
, sizeof(struct vt_mode
));
924 * Returns global vt state. Note that VT 0 is always open, since
925 * it's an alias for the current VT, and people can't use it here.
926 * We cannot return state for more than 16 VTs, since v_state is short.
930 struct vt_stat __user
*vtstat
= up
;
931 unsigned short state
, mask
;
933 if (put_user(fg_console
+ 1, &vtstat
->v_active
))
936 state
= 1; /* /dev/tty0 is always open */
937 for (i
= 0, mask
= 2; i
< MAX_NR_CONSOLES
&& mask
;
941 ret
= put_user(state
, &vtstat
->v_state
);
947 * Returns the first available (non-opened) console.
950 for (i
= 0; i
< MAX_NR_CONSOLES
; ++i
)
951 if (! VT_IS_IN_USE(i
))
953 uival
= i
< MAX_NR_CONSOLES
? (i
+1) : -1;
957 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
958 * with num >= 1 (switches to vt 0, our console, are not allowed, just
959 * to preserve sanity).
964 if (arg
== 0 || arg
> MAX_NR_CONSOLES
)
968 acquire_console_sem();
969 ret
= vc_allocate(arg
);
970 release_console_sem();
979 struct vt_setactivate vsa
;
984 if (copy_from_user(&vsa
, (struct vt_setactivate __user
*)arg
,
985 sizeof(struct vt_setactivate
))) {
989 if (vsa
.console
== 0 || vsa
.console
> MAX_NR_CONSOLES
)
993 acquire_console_sem();
994 ret
= vc_allocate(vsa
.console
);
997 /* This is safe providing we don't drop the
998 console sem between vc_allocate and
999 finishing referencing nvc */
1000 nvc
= vc_cons
[vsa
.console
].d
;
1001 nvc
->vt_mode
= vsa
.mode
;
1002 nvc
->vt_mode
.frsig
= 0;
1003 put_pid(nvc
->vt_pid
);
1004 nvc
->vt_pid
= get_pid(task_pid(current
));
1006 release_console_sem();
1009 /* Commence switch and lock */
1015 * wait until the specified VT has been activated
1020 if (arg
== 0 || arg
> MAX_NR_CONSOLES
)
1023 ret
= vt_waitactive(arg
);
1027 * If a vt is under process control, the kernel will not switch to it
1028 * immediately, but postpone the operation until the process calls this
1029 * ioctl, allowing the switch to complete.
1031 * According to the X sources this is the behavior:
1032 * 0: pending switch-from not OK
1033 * 1: pending switch-from OK
1034 * 2: completed switch-to OK
1040 if (vc
->vt_mode
.mode
!= VT_PROCESS
) {
1045 * Switching-from response
1047 acquire_console_sem();
1048 if (vc
->vt_newvt
>= 0) {
1051 * Switch disallowed, so forget we were trying
1058 * The current vt has been released, so
1059 * complete the switch.
1062 newvt
= vc
->vt_newvt
;
1064 ret
= vc_allocate(newvt
);
1066 release_console_sem();
1070 * When we actually do the console switch,
1071 * make sure we are atomic with respect to
1072 * other console switches..
1074 complete_change_console(vc_cons
[newvt
].d
);
1078 * Switched-to response
1081 * If it's just an ACK, ignore it
1083 if (arg
!= VT_ACKACQ
)
1086 release_console_sem();
1090 * Disallocate memory associated to VT (but leave VT1)
1092 case VT_DISALLOCATE
:
1093 if (arg
> MAX_NR_CONSOLES
) {
1098 /* deallocate all unused consoles, but leave 0 */
1099 acquire_console_sem();
1100 for (i
=1; i
<MAX_NR_CONSOLES
; i
++)
1103 release_console_sem();
1105 /* deallocate a single console, if possible */
1109 else if (arg
) { /* leave 0 */
1110 acquire_console_sem();
1112 release_console_sem();
1119 struct vt_sizes __user
*vtsizes
= up
;
1125 if (get_user(ll
, &vtsizes
->v_rows
) ||
1126 get_user(cc
, &vtsizes
->v_cols
))
1129 acquire_console_sem();
1130 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
1134 vc
->vc_resize_user
= 1;
1135 vc_resize(vc_cons
[i
].d
, cc
, ll
);
1138 release_console_sem();
1145 struct vt_consize __user
*vtconsize
= up
;
1146 ushort ll
,cc
,vlin
,clin
,vcol
,ccol
;
1149 if (!access_ok(VERIFY_READ
, vtconsize
,
1150 sizeof(struct vt_consize
))) {
1154 /* FIXME: Should check the copies properly */
1155 __get_user(ll
, &vtconsize
->v_rows
);
1156 __get_user(cc
, &vtconsize
->v_cols
);
1157 __get_user(vlin
, &vtconsize
->v_vlin
);
1158 __get_user(clin
, &vtconsize
->v_clin
);
1159 __get_user(vcol
, &vtconsize
->v_vcol
);
1160 __get_user(ccol
, &vtconsize
->v_ccol
);
1161 vlin
= vlin
? vlin
: vc
->vc_scan_lines
;
1164 if (ll
!= vlin
/clin
) {
1165 /* Parameters don't add up */
1174 if (cc
!= vcol
/ccol
) {
1187 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++) {
1190 acquire_console_sem();
1192 vc_cons
[i
].d
->vc_scan_lines
= vlin
;
1194 vc_cons
[i
].d
->vc_font
.height
= clin
;
1195 vc_cons
[i
].d
->vc_resize_user
= 1;
1196 vc_resize(vc_cons
[i
].d
, cc
, ll
);
1197 release_console_sem();
1205 op
.op
= KD_FONT_OP_SET
;
1206 op
.flags
= KD_FONT_FLAG_OLD
| KD_FONT_FLAG_DONT_RECALC
; /* Compatibility */
1211 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
1216 op
.op
= KD_FONT_OP_GET
;
1217 op
.flags
= KD_FONT_FLAG_OLD
;
1222 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
1230 ret
= con_set_cmap(up
);
1234 ret
= con_get_cmap(up
);
1239 ret
= do_fontx_ioctl(cmd
, up
, perm
, &op
);
1247 #ifdef BROKEN_GRAPHICS_PROGRAMS
1248 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1249 font is not saved. */
1254 op
.op
= KD_FONT_OP_SET_DEFAULT
;
1256 ret
= con_font_op(vc_cons
[fg_console
].d
, &op
);
1259 con_set_default_unimap(vc_cons
[fg_console
].d
);
1266 if (copy_from_user(&op
, up
, sizeof(op
))) {
1270 if (!perm
&& op
.op
!= KD_FONT_OP_GET
)
1272 ret
= con_font_op(vc
, &op
);
1275 if (copy_to_user(up
, &op
, sizeof(op
)))
1284 ret
= con_set_trans_old(up
);
1288 ret
= con_get_trans_old(up
);
1291 case PIO_UNISCRNMAP
:
1295 ret
= con_set_trans_new(up
);
1298 case GIO_UNISCRNMAP
:
1299 ret
= con_get_trans_new(up
);
1303 { struct unimapinit ui
;
1306 ret
= copy_from_user(&ui
, up
, sizeof(struct unimapinit
));
1308 con_clear_unimap(vc
, &ui
);
1314 ret
= do_unimap_ioctl(cmd
, up
, perm
, vc
);
1318 if (!capable(CAP_SYS_TTY_CONFIG
))
1322 case VT_UNLOCKSWITCH
:
1323 if (!capable(CAP_SYS_TTY_CONFIG
))
1327 case VT_GETHIFONTMASK
:
1328 ret
= put_user(vc
->vc_hi_font_mask
,
1329 (unsigned short __user
*)arg
);
1332 ret
= vt_event_wait_ioctl((struct vt_event __user
*)arg
);
1345 void reset_vc(struct vc_data
*vc
)
1347 vc
->vc_mode
= KD_TEXT
;
1348 kbd_table
[vc
->vc_num
].kbdmode
= default_utf8
? VC_UNICODE
: VC_XLATE
;
1349 vc
->vt_mode
.mode
= VT_AUTO
;
1350 vc
->vt_mode
.waitv
= 0;
1351 vc
->vt_mode
.relsig
= 0;
1352 vc
->vt_mode
.acqsig
= 0;
1353 vc
->vt_mode
.frsig
= 0;
1354 put_pid(vc
->vt_pid
);
1357 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1361 void vc_SAK(struct work_struct
*work
)
1364 container_of(work
, struct vc
, SAK_work
);
1366 struct tty_struct
*tty
;
1368 acquire_console_sem();
1373 * SAK should also work in all raw modes and reset
1380 release_console_sem();
1383 #ifdef CONFIG_COMPAT
1385 struct compat_consolefontdesc
{
1386 unsigned short charcount
; /* characters in font (256 or 512) */
1387 unsigned short charheight
; /* scan lines per character (1-32) */
1388 compat_caddr_t chardata
; /* font data in expanded form */
1392 compat_fontx_ioctl(int cmd
, struct compat_consolefontdesc __user
*user_cfd
,
1393 int perm
, struct console_font_op
*op
)
1395 struct compat_consolefontdesc cfdarg
;
1398 if (copy_from_user(&cfdarg
, user_cfd
, sizeof(struct compat_consolefontdesc
)))
1405 op
->op
= KD_FONT_OP_SET
;
1406 op
->flags
= KD_FONT_FLAG_OLD
;
1408 op
->height
= cfdarg
.charheight
;
1409 op
->charcount
= cfdarg
.charcount
;
1410 op
->data
= compat_ptr(cfdarg
.chardata
);
1411 return con_font_op(vc_cons
[fg_console
].d
, op
);
1413 op
->op
= KD_FONT_OP_GET
;
1414 op
->flags
= KD_FONT_FLAG_OLD
;
1416 op
->height
= cfdarg
.charheight
;
1417 op
->charcount
= cfdarg
.charcount
;
1418 op
->data
= compat_ptr(cfdarg
.chardata
);
1419 i
= con_font_op(vc_cons
[fg_console
].d
, op
);
1422 cfdarg
.charheight
= op
->height
;
1423 cfdarg
.charcount
= op
->charcount
;
1424 if (copy_to_user(user_cfd
, &cfdarg
, sizeof(struct compat_consolefontdesc
)))
1431 struct compat_console_font_op
{
1432 compat_uint_t op
; /* operation code KD_FONT_OP_* */
1433 compat_uint_t flags
; /* KD_FONT_FLAG_* */
1434 compat_uint_t width
, height
; /* font size */
1435 compat_uint_t charcount
;
1436 compat_caddr_t data
; /* font data with height fixed to 32 */
1440 compat_kdfontop_ioctl(struct compat_console_font_op __user
*fontop
,
1441 int perm
, struct console_font_op
*op
, struct vc_data
*vc
)
1445 if (copy_from_user(op
, fontop
, sizeof(struct compat_console_font_op
)))
1447 if (!perm
&& op
->op
!= KD_FONT_OP_GET
)
1449 op
->data
= compat_ptr(((struct compat_console_font_op
*)op
)->data
);
1450 op
->flags
|= KD_FONT_FLAG_OLD
;
1451 i
= con_font_op(vc
, op
);
1454 ((struct compat_console_font_op
*)op
)->data
= (unsigned long)op
->data
;
1455 if (copy_to_user(fontop
, op
, sizeof(struct compat_console_font_op
)))
1460 struct compat_unimapdesc
{
1461 unsigned short entry_ct
;
1462 compat_caddr_t entries
;
1466 compat_unimap_ioctl(unsigned int cmd
, struct compat_unimapdesc __user
*user_ud
,
1467 int perm
, struct vc_data
*vc
)
1469 struct compat_unimapdesc tmp
;
1470 struct unipair __user
*tmp_entries
;
1472 if (copy_from_user(&tmp
, user_ud
, sizeof tmp
))
1474 tmp_entries
= compat_ptr(tmp
.entries
);
1476 if (!access_ok(VERIFY_WRITE
, tmp_entries
,
1477 tmp
.entry_ct
*sizeof(struct unipair
)))
1483 return con_set_unimap(vc
, tmp
.entry_ct
, tmp_entries
);
1485 if (!perm
&& fg_console
!= vc
->vc_num
)
1487 return con_get_unimap(vc
, tmp
.entry_ct
, &(user_ud
->entry_ct
), tmp_entries
);
1492 long vt_compat_ioctl(struct tty_struct
*tty
, struct file
* file
,
1493 unsigned int cmd
, unsigned long arg
)
1495 struct vc_data
*vc
= tty
->driver_data
;
1496 struct console_font_op op
; /* used in multiple places here */
1497 struct kbd_struct
*kbd
;
1498 unsigned int console
;
1499 void __user
*up
= (void __user
*)arg
;
1503 console
= vc
->vc_num
;
1507 if (!vc_cons_allocated(console
)) { /* impossible? */
1513 * To have permissions to do most of the vt ioctls, we either have
1514 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1517 if (current
->signal
->tty
== tty
|| capable(CAP_SYS_TTY_CONFIG
))
1520 kbd
= kbd_table
+ console
;
1523 * these need special handlers for incompatible data structures
1527 ret
= compat_fontx_ioctl(cmd
, up
, perm
, &op
);
1531 ret
= compat_kdfontop_ioctl(up
, perm
, &op
, vc
);
1536 ret
= compat_unimap_ioctl(cmd
, up
, perm
, vc
);
1540 * all these treat 'arg' as an integer
1559 case VT_DISALLOCATE
:
1565 * the rest has a compatible data structure behind arg,
1566 * but we have to convert it to a proper 64 bit pointer.
1569 arg
= (unsigned long)compat_ptr(arg
);
1578 return vt_ioctl(tty
, file
, cmd
, arg
);
1582 #endif /* CONFIG_COMPAT */
1586 * Performs the back end of a vt switch. Called under the console
1589 static void complete_change_console(struct vc_data
*vc
)
1591 unsigned char old_vc_mode
;
1592 int old
= fg_console
;
1594 last_console
= fg_console
;
1597 * If we're switching, we could be going from KD_GRAPHICS to
1598 * KD_TEXT mode or vice versa, which means we need to blank or
1599 * unblank the screen later.
1601 old_vc_mode
= vc_cons
[fg_console
].d
->vc_mode
;
1605 * This can't appear below a successful kill_pid(). If it did,
1606 * then the *blank_screen operation could occur while X, having
1607 * received acqsig, is waking up on another processor. This
1608 * condition can lead to overlapping accesses to the VGA range
1609 * and the framebuffer (causing system lockups).
1611 * To account for this we duplicate this code below only if the
1612 * controlling process is gone and we've called reset_vc.
1614 if (old_vc_mode
!= vc
->vc_mode
) {
1615 if (vc
->vc_mode
== KD_TEXT
)
1616 do_unblank_screen(1);
1622 * If this new console is under process control, send it a signal
1623 * telling it that it has acquired. Also check if it has died and
1624 * clean up (similar to logic employed in change_console())
1626 if (vc
->vt_mode
.mode
== VT_PROCESS
) {
1628 * Send the signal as privileged - kill_pid() will
1629 * tell us if the process has gone or something else
1632 if (kill_pid(vc
->vt_pid
, vc
->vt_mode
.acqsig
, 1) != 0) {
1634 * The controlling process has died, so we revert back to
1635 * normal operation. In this case, we'll also change back
1636 * to KD_TEXT mode. I'm not sure if this is strictly correct
1637 * but it saves the agony when the X server dies and the screen
1638 * remains blanked due to KD_GRAPHICS! It would be nice to do
1639 * this outside of VT_PROCESS but there is no single process
1640 * to account for and tracking tty count may be undesirable.
1644 if (old_vc_mode
!= vc
->vc_mode
) {
1645 if (vc
->vc_mode
== KD_TEXT
)
1646 do_unblank_screen(1);
1654 * Wake anyone waiting for their VT to activate
1656 vt_event_post(VT_EVENT_SWITCH
, old
, vc
->vc_num
);
1661 * Performs the front-end of a vt switch
1663 void change_console(struct vc_data
*new_vc
)
1667 if (!new_vc
|| new_vc
->vc_num
== fg_console
|| vt_dont_switch
)
1671 * If this vt is in process mode, then we need to handshake with
1672 * that process before switching. Essentially, we store where that
1673 * vt wants to switch to and wait for it to tell us when it's done
1674 * (via VT_RELDISP ioctl).
1676 * We also check to see if the controlling process still exists.
1677 * If it doesn't, we reset this vt to auto mode and continue.
1678 * This is a cheap way to track process control. The worst thing
1679 * that can happen is: we send a signal to a process, it dies, and
1680 * the switch gets "lost" waiting for a response; hopefully, the
1681 * user will try again, we'll detect the process is gone (unless
1682 * the user waits just the right amount of time :-) and revert the
1683 * vt to auto control.
1685 vc
= vc_cons
[fg_console
].d
;
1686 if (vc
->vt_mode
.mode
== VT_PROCESS
) {
1688 * Send the signal as privileged - kill_pid() will
1689 * tell us if the process has gone or something else
1692 * We need to set vt_newvt *before* sending the signal or we
1695 vc
->vt_newvt
= new_vc
->vc_num
;
1696 if (kill_pid(vc
->vt_pid
, vc
->vt_mode
.relsig
, 1) == 0) {
1698 * It worked. Mark the vt to switch to and
1699 * return. The process needs to send us a
1700 * VT_RELDISP ioctl to complete the switch.
1706 * The controlling process has died, so we revert back to
1707 * normal operation. In this case, we'll also change back
1708 * to KD_TEXT mode. I'm not sure if this is strictly correct
1709 * but it saves the agony when the X server dies and the screen
1710 * remains blanked due to KD_GRAPHICS! It would be nice to do
1711 * this outside of VT_PROCESS but there is no single process
1712 * to account for and tracking tty count may be undesirable.
1717 * Fall through to normal (VT_AUTO) handling of the switch...
1722 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1724 if (vc
->vc_mode
== KD_GRAPHICS
)
1727 complete_change_console(new_vc
);
1730 /* Perform a kernel triggered VT switch for suspend/resume */
1732 static int disable_vt_switch
;
1734 int vt_move_to_console(unsigned int vt
, int alloc
)
1738 acquire_console_sem();
1739 /* Graphics mode - up to X */
1740 if (disable_vt_switch
) {
1741 release_console_sem();
1746 if (alloc
&& vc_allocate(vt
)) {
1747 /* we can't have a free VC for now. Too bad,
1748 * we don't want to mess the screen for now. */
1749 release_console_sem();
1753 if (set_console(vt
)) {
1755 * We're unable to switch to the SUSPEND_CONSOLE.
1756 * Let the calling function know so it can decide
1759 release_console_sem();
1762 release_console_sem();
1763 if (vt_waitactive(vt
+ 1)) {
1764 pr_debug("Suspend: Can't switch VCs.");
1771 * Normally during a suspend, we allocate a new console and switch to it.
1772 * When we resume, we switch back to the original console. This switch
1773 * can be slow, so on systems where the framebuffer can handle restoration
1774 * of video registers anyways, there's little point in doing the console
1775 * switch. This function allows you to disable it by passing it '0'.
1777 void pm_set_vt_switch(int do_switch
)
1779 acquire_console_sem();
1780 disable_vt_switch
= !do_switch
;
1781 release_console_sem();
1783 EXPORT_SYMBOL(pm_set_vt_switch
);