Import 2.1.33
[davej-history.git] / drivers / char / vt.c
blob2b8378c0d77dde754dc0d8947e8cc1c2b967343f
1 /*
2 * linux/drivers/char/vt.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
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/tty.h>
16 #include <linux/timer.h>
17 #include <linux/kernel.h>
18 #include <linux/kd.h>
19 #include <linux/vt.h>
20 #include <linux/string.h>
21 #include <linux/malloc.h>
22 #include <linux/major.h>
23 #include <linux/fs.h>
25 #include <asm/io.h>
26 #include <asm/uaccess.h>
28 #include "kbd_kern.h"
29 #include "vt_kern.h"
30 #include "diacr.h"
31 #include "selection.h"
33 char vt_dont_switch = 0;
34 extern struct tty_driver console_driver;
36 #define VT_IS_IN_USE(i) (console_driver.table[i] && console_driver.table[i]->count)
37 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || i == sel_cons)
40 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
41 * experimentation and study of X386 SYSV handling.
43 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
44 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
45 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
46 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
47 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
48 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
49 * to the current console is done by the main ioctl code.
52 struct vt_struct *vt_cons[MAX_NR_CONSOLES];
54 #ifndef __alpha__
55 asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int on);
56 #endif
58 extern int getkeycode(unsigned int scancode);
59 extern int setkeycode(unsigned int scancode, unsigned int keycode);
60 extern void compute_shiftstate(void);
61 extern void complete_change_console(unsigned int new_console);
62 extern int vt_waitactive(int vt);
63 extern void do_blank_screen(int nopowersave);
65 extern unsigned int keymap_count;
68 * routines to load custom translation table, EGA/VGA font and
69 * VGA colour palette from console.c
71 extern int con_set_trans_old(unsigned char * table);
72 extern int con_get_trans_old(unsigned char * table);
73 extern int con_set_trans_new(unsigned short * table);
74 extern int con_get_trans_new(unsigned short * table);
75 extern void con_clear_unimap(struct unimapinit *ui);
76 extern int con_set_unimap(ushort ct, struct unipair *list);
77 extern int con_get_unimap(ushort ct, ushort *uct, struct unipair *list);
78 extern void con_set_default_unimap(void);
79 extern int con_set_font(char * fontmap, int ch512);
80 extern int con_get_font(char * fontmap);
81 extern int con_set_cmap(unsigned char *cmap);
82 extern int con_get_cmap(unsigned char *cmap);
83 extern void reset_palette(int currcons);
84 extern void set_palette(void) ;
85 extern int con_adjust_height(unsigned long fontheight);
87 extern int video_mode_512ch;
88 extern unsigned long video_font_height;
89 extern unsigned long default_font_height;
90 extern unsigned long video_scan_lines;
93 * these are the valid i/o ports we're allowed to change. they map all the
94 * video ports
96 #define GPFIRST 0x3b4
97 #define GPLAST 0x3df
98 #define GPNUM (GPLAST - GPFIRST + 1)
101 * This function is called when the size of the physical screen has been
102 * changed. If either the row or col argument is nonzero, set the appropriate
103 * entry in each winsize structure for all the virtual consoles, then
104 * send SIGWINCH to all processes with a virtual console as controlling
105 * tty.
108 static int
109 kd_size_changed(int row, int col)
111 struct task_struct *p;
112 int i;
114 if ( !row && !col ) return 0;
116 for ( i = 0 ; i < MAX_NR_CONSOLES ; i++ )
118 if ( console_driver.table[i] )
120 if ( row ) console_driver.table[i]->winsize.ws_row = row;
121 if ( col ) console_driver.table[i]->winsize.ws_col = col;
125 for_each_task(p)
127 if ( p->tty && MAJOR(p->tty->device) == TTY_MAJOR &&
128 MINOR(p->tty->device) <= MAX_NR_CONSOLES && MINOR(p->tty->device) )
130 send_sig(SIGWINCH, p, 1);
134 return 0;
138 * Generates sound of some count for some number of clock ticks
139 * [count = 1193180 / frequency]
141 * If freq is 0, will turn off sound, else will turn it on for that time.
142 * If msec is 0, will return immediately, else will sleep for msec time, then
143 * turn sound off.
145 * We use the BEEP_TIMER vector since we're using the same method to
146 * generate sound, and we'll overwrite any beep in progress. That may
147 * be something to fix later, if we like.
149 * We also return immediately, which is what was implied within the X
150 * comments - KDMKTONE doesn't put the process to sleep.
152 static void
153 kd_nosound(unsigned long ignored)
155 /* disable counter 2 */
156 outb(inb_p(0x61)&0xFC, 0x61);
157 return;
160 void
161 _kd_mksound(unsigned int hz, unsigned int ticks)
163 static struct timer_list sound_timer = { NULL, NULL, 0, 0,
164 kd_nosound };
166 unsigned int count = 0;
168 if (hz > 20 && hz < 32767)
169 count = 1193180 / hz;
171 cli();
172 del_timer(&sound_timer);
173 if (count) {
174 /* enable counter 2 */
175 outb_p(inb_p(0x61)|3, 0x61);
176 /* set command for counter 2, 2 byte write */
177 outb_p(0xB6, 0x43);
178 /* select desired HZ */
179 outb_p(count & 0xff, 0x42);
180 outb((count >> 8) & 0xff, 0x42);
182 if (ticks) {
183 sound_timer.expires = jiffies+ticks;
184 add_timer(&sound_timer);
186 } else
187 kd_nosound(0);
188 sti();
189 return;
192 void (*kd_mksound)(unsigned int hz, unsigned int ticks) = _kd_mksound;
195 #define i (tmp.kb_index)
196 #define s (tmp.kb_table)
197 #define v (tmp.kb_value)
198 static inline int
199 do_kdsk_ioctl(int cmd, struct kbentry *user_kbe, int perm, struct kbd_struct *kbd)
201 struct kbentry tmp;
202 ushort *key_map, val, ov;
204 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
205 return -EFAULT;
206 if (i >= NR_KEYS || s >= MAX_NR_KEYMAPS)
207 return -EINVAL;
209 switch (cmd) {
210 case KDGKBENT:
211 key_map = key_maps[s];
212 if (key_map) {
213 val = U(key_map[i]);
214 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
215 val = K_HOLE;
216 } else
217 val = (i ? K_HOLE : K_NOSUCHMAP);
218 return __put_user(val, &user_kbe->kb_value);
219 case KDSKBENT:
220 if (!perm)
221 return -EPERM;
222 if (!i && v == K_NOSUCHMAP) {
223 /* disallocate map */
224 key_map = key_maps[s];
225 if (s && key_map) {
226 key_maps[s] = 0;
227 if (key_map[0] == U(K_ALLOCATED)) {
228 kfree_s(key_map, sizeof(plain_map));
229 keymap_count--;
232 break;
235 if (KTYP(v) < NR_TYPES) {
236 if (KVAL(v) > max_vals[KTYP(v)])
237 return -EINVAL;
238 } else
239 if (kbd->kbdmode != VC_UNICODE)
240 return -EINVAL;
242 /* assignment to entry 0 only tests validity of args */
243 if (!i)
244 break;
246 if (!(key_map = key_maps[s])) {
247 int j;
249 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && !suser())
250 return -EPERM;
252 key_map = (ushort *) kmalloc(sizeof(plain_map),
253 GFP_KERNEL);
254 if (!key_map)
255 return -ENOMEM;
256 key_maps[s] = key_map;
257 key_map[0] = U(K_ALLOCATED);
258 for (j = 1; j < NR_KEYS; j++)
259 key_map[j] = U(K_HOLE);
260 keymap_count++;
262 ov = U(key_map[i]);
263 if (v == ov)
264 break; /* nothing to do */
266 * Attention Key.
268 if (((ov == K_SAK) || (v == K_SAK)) && !suser())
269 return -EPERM;
270 key_map[i] = U(v);
271 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
272 compute_shiftstate();
273 break;
275 return 0;
277 #undef i
278 #undef s
279 #undef v
281 static inline int
282 do_kbkeycode_ioctl(int cmd, struct kbkeycode *user_kbkc, int perm)
284 struct kbkeycode tmp;
285 int kc = 0;
287 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
288 return -EFAULT;
289 switch (cmd) {
290 case KDGETKEYCODE:
291 kc = getkeycode(tmp.scancode);
292 if (kc >= 0)
293 kc = __put_user(kc, &user_kbkc->keycode);
294 break;
295 case KDSETKEYCODE:
296 if (!perm)
297 return -EPERM;
298 kc = setkeycode(tmp.scancode, tmp.keycode);
299 break;
301 return kc;
304 static inline int
305 do_kdgkb_ioctl(int cmd, struct kbsentry *user_kdgkb, int perm)
307 struct kbsentry tmp;
308 char *p;
309 u_char *q;
310 int sz;
311 int delta;
312 char *first_free, *fj, *fnw;
313 int j, k, i = 0;
315 /* we mostly copy too much here (512bytes), but who cares ;) */
316 if (copy_from_user(&tmp, user_kdgkb, sizeof(struct kbsentry)))
317 return -EFAULT;
318 tmp.kb_string[sizeof(tmp.kb_string)-1] = '\0';
319 if (tmp.kb_func >= MAX_NR_FUNC)
320 return -EINVAL;
322 switch (cmd) {
323 case KDGKBSENT:
324 sz = sizeof(tmp.kb_string) - 1; /* sz should have been
325 a struct member */
326 q = user_kdgkb->kb_string;
327 p = func_table[i];
328 if(p)
329 for ( ; *p && sz; p++, sz--)
330 __put_user(*p, q++);
331 __put_user('\0', q);
332 return ((p && *p) ? -EOVERFLOW : 0);
333 case KDSKBSENT:
334 if (!perm)
335 return -EPERM;
337 q = func_table[i];
338 first_free = funcbufptr + (funcbufsize - funcbufleft);
339 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
341 if (j < MAX_NR_FUNC)
342 fj = func_table[j];
343 else
344 fj = first_free;
346 delta = (q ? -strlen(q) : 1) + strlen(tmp.kb_string);
347 if (delta <= funcbufleft) { /* it fits in current buf */
348 if (j < MAX_NR_FUNC) {
349 memmove(fj + delta, fj, first_free - fj);
350 for (k = j; k < MAX_NR_FUNC; k++)
351 if (func_table[k])
352 func_table[k] += delta;
354 if (!q)
355 func_table[i] = fj;
356 funcbufleft -= delta;
357 } else { /* allocate a larger buffer */
358 sz = 256;
359 while (sz < funcbufsize - funcbufleft + delta)
360 sz <<= 1;
361 fnw = (char *) kmalloc(sz, GFP_KERNEL);
362 if(!fnw)
363 return -ENOMEM;
365 if (!q)
366 func_table[i] = fj;
367 if (fj > funcbufptr)
368 memmove(fnw, funcbufptr, fj - funcbufptr);
369 for (k = 0; k < j; k++)
370 if (func_table[k])
371 func_table[k] = fnw + (func_table[k] - funcbufptr);
373 if (first_free > fj) {
374 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
375 for (k = j; k < MAX_NR_FUNC; k++)
376 if (func_table[k])
377 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
379 if (funcbufptr != func_buf)
380 kfree_s(funcbufptr, funcbufsize);
381 funcbufptr = fnw;
382 funcbufleft = funcbufleft - delta + sz - funcbufsize;
383 funcbufsize = sz;
385 strcpy(func_table[i], tmp.kb_string);
386 break;
388 return 0;
391 static inline int
392 do_fontx_ioctl(int cmd, struct consolefontdesc *user_cfd, int perm)
394 int nchar;
395 struct consolefontdesc cfdarg;
396 int i = 0;
398 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
399 return -EFAULT;
400 if (vt_cons[fg_console]->vc_mode != KD_TEXT)
401 return -EINVAL;
403 switch (cmd) {
404 case PIO_FONTX:
405 if (!perm)
406 return -EPERM;
407 if ( cfdarg.charcount == 256 ||
408 cfdarg.charcount == 512 ) {
409 i = con_set_font(cfdarg.chardata,
410 cfdarg.charcount == 512);
411 if (i)
412 return i;
413 i = con_adjust_height(cfdarg.charheight);
414 return (i <= 0) ? i : kd_size_changed(i, 0);
415 } else
416 return -EINVAL;
417 case GIO_FONTX:
418 i = cfdarg.charcount;
419 cfdarg.charcount = nchar = video_mode_512ch ? 512 : 256;
420 cfdarg.charheight = video_font_height;
421 __copy_to_user(user_cfd, &cfdarg,
422 sizeof(struct consolefontdesc));
423 if ( cfdarg.chardata )
425 if ( i < nchar )
426 return -ENOMEM;
427 return con_get_font(cfdarg.chardata);
428 } else
429 return 0;
431 return 0;
434 static inline int
435 do_unimap_ioctl(int cmd, struct unimapdesc *user_ud,int perm)
437 struct unimapdesc tmp;
438 int i = 0;
440 if (copy_from_user(&tmp, user_ud, sizeof tmp))
441 return -EFAULT;
442 if (tmp.entries) {
443 i = verify_area(VERIFY_WRITE, tmp.entries,
444 tmp.entry_ct*sizeof(struct unipair));
445 if (i) return i;
447 switch (cmd) {
448 case PIO_UNIMAP:
449 if (!perm)
450 return -EPERM;
451 return con_set_unimap(tmp.entry_ct, tmp.entries);
452 case GIO_UNIMAP:
453 return con_get_unimap(tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
455 return 0;
459 * We handle the console-specific ioctl's here. We allow the
460 * capability to modify any console, not just the fg_console.
462 int vt_ioctl(struct tty_struct *tty, struct file * file,
463 unsigned int cmd, unsigned long arg)
465 int i, perm;
466 unsigned int console;
467 unsigned char ucval;
468 struct kbd_struct * kbd;
469 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
471 console = vt->vc_num;
473 if (!vc_cons_allocated(console)) /* impossible? */
474 return -ENOIOCTLCMD;
477 * To have permissions to do most of the vt ioctls, we either have
478 * to be the owner of the tty, or super-user.
480 perm = 0;
481 if (current->tty == tty || suser())
482 perm = 1;
484 kbd = kbd_table + console;
485 switch (cmd) {
486 case KIOCSOUND:
487 if (!perm)
488 return -EPERM;
489 if (arg)
490 arg = 1193180 / arg;
491 kd_mksound(arg, 0);
492 return 0;
494 case KDMKTONE:
495 if (!perm)
496 return -EPERM;
498 unsigned int ticks, count;
501 * Generate the tone for the appropriate number of ticks.
502 * If the time is zero, turn off sound ourselves.
504 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
505 if ((arg & 0xffff) == 0 ) arg |= 1; /* jp: huh? */
506 count = ticks ? (1193180 / (arg & 0xffff)) : 0;
507 kd_mksound(count, ticks);
508 return 0;
511 case KDGKBTYPE:
513 * this is naive.
515 ucval = KB_101;
516 goto setchar;
518 #ifndef __alpha__
520 * These cannot be implemented on any machine that implements
521 * ioperm() in user level (such as Alpha PCs).
523 case KDADDIO:
524 case KDDELIO:
526 * KDADDIO and KDDELIO may be able to add ports beyond what
527 * we reject here, but to be safe...
529 if (arg < GPFIRST || arg > GPLAST)
530 return -EINVAL;
531 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
533 case KDENABIO:
534 case KDDISABIO:
535 return sys_ioperm(GPFIRST, GPNUM,
536 (cmd == KDENABIO)) ? -ENXIO : 0;
537 #endif
539 case KDSETMODE:
541 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
542 * doesn't do a whole lot. i'm not sure if it should do any
543 * restoration of modes or what...
545 if (!perm)
546 return -EPERM;
547 switch (arg) {
548 case KD_GRAPHICS:
549 break;
550 case KD_TEXT0:
551 case KD_TEXT1:
552 arg = KD_TEXT;
553 case KD_TEXT:
554 break;
555 default:
556 return -EINVAL;
558 if (vt_cons[console]->vc_mode == (unsigned char) arg)
559 return 0;
560 vt_cons[console]->vc_mode = (unsigned char) arg;
561 if (console != fg_console)
562 return 0;
564 * explicitly blank/unblank the screen if switching modes
566 if (arg == KD_TEXT)
567 do_unblank_screen();
568 else
569 do_blank_screen(1);
570 return 0;
572 case KDGETMODE:
573 ucval = vt_cons[console]->vc_mode;
574 goto setint;
576 case KDMAPDISP:
577 case KDUNMAPDISP:
579 * these work like a combination of mmap and KDENABIO.
580 * this could be easily finished.
582 return -EINVAL;
584 case KDSKBMODE:
585 if (!perm)
586 return -EPERM;
587 switch(arg) {
588 case K_RAW:
589 kbd->kbdmode = VC_RAW;
590 break;
591 case K_MEDIUMRAW:
592 kbd->kbdmode = VC_MEDIUMRAW;
593 break;
594 case K_XLATE:
595 kbd->kbdmode = VC_XLATE;
596 compute_shiftstate();
597 break;
598 case K_UNICODE:
599 kbd->kbdmode = VC_UNICODE;
600 compute_shiftstate();
601 break;
602 default:
603 return -EINVAL;
605 if (tty->ldisc.flush_buffer)
606 tty->ldisc.flush_buffer(tty);
607 return 0;
609 case KDGKBMODE:
610 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
611 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
612 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
613 K_XLATE);
614 goto setint;
616 /* this could be folded into KDSKBMODE, but for compatibility
617 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
618 case KDSKBMETA:
619 switch(arg) {
620 case K_METABIT:
621 clr_vc_kbd_mode(kbd, VC_META);
622 break;
623 case K_ESCPREFIX:
624 set_vc_kbd_mode(kbd, VC_META);
625 break;
626 default:
627 return -EINVAL;
629 return 0;
631 case KDGKBMETA:
632 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
633 setint:
634 return put_user(ucval, (int *)arg);
636 case KDGETKEYCODE:
637 case KDSETKEYCODE:
638 return do_kbkeycode_ioctl(cmd, (struct kbkeycode *)arg, perm);
640 case KDGKBENT:
641 case KDSKBENT:
642 return do_kdsk_ioctl(cmd, (struct kbentry *)arg, perm, kbd);
644 case KDGKBSENT:
645 case KDSKBSENT:
646 return do_kdgkb_ioctl(cmd, (struct kbsentry *)arg, perm);
648 case KDGKBDIACR:
650 struct kbdiacrs *a = (struct kbdiacrs *)arg;
652 i = verify_area(VERIFY_WRITE, (void *) a, sizeof(struct kbdiacrs));
653 if (i)
654 return i;
655 __put_user(accent_table_size, &a->kb_cnt);
656 __copy_to_user(a->kbdiacr, accent_table,
657 accent_table_size*sizeof(struct kbdiacr));
658 return 0;
661 case KDSKBDIACR:
663 struct kbdiacrs *a = (struct kbdiacrs *)arg;
664 unsigned int ct;
666 if (!perm)
667 return -EPERM;
668 i = verify_area(VERIFY_READ, (void *) a, sizeof(struct kbdiacrs));
669 if (i)
670 return i;
671 __get_user(ct,&a->kb_cnt);
672 if (ct >= MAX_DIACR)
673 return -EINVAL;
674 accent_table_size = ct;
675 __copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr));
676 return 0;
679 /* the ioctls below read/set the flags usually shown in the leds */
680 /* don't use them - they will go away without warning */
681 case KDGKBLED:
682 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
683 goto setchar;
685 case KDSKBLED:
686 if (!perm)
687 return -EPERM;
688 if (arg & ~0x77)
689 return -EINVAL;
690 kbd->ledflagstate = (arg & 7);
691 kbd->default_ledflagstate = ((arg >> 4) & 7);
692 set_leds();
693 return 0;
695 /* the ioctls below only set the lights, not the functions */
696 /* for those, see KDGKBLED and KDSKBLED above */
697 case KDGETLED:
698 ucval = getledstate();
699 setchar:
700 return put_user(ucval, (char*)arg);
702 case KDSETLED:
703 if (!perm)
704 return -EPERM;
705 setledstate(kbd, arg);
706 return 0;
709 * A process can indicate its willingness to accept signals
710 * generated by pressing an appropriate key combination.
711 * Thus, one can have a daemon that e.g. spawns a new console
712 * upon a keypress and then changes to it.
713 * Probably init should be changed to do this (and have a
714 * field ks (`keyboard signal') in inittab describing the
715 * desired action), so that the number of background daemons
716 * does not increase.
718 case KDSIGACCEPT:
720 extern int spawnpid, spawnsig;
721 if (!perm)
722 return -EPERM;
723 if (arg < 1 || arg > NSIG || arg == SIGKILL)
724 return -EINVAL;
725 spawnpid = current->pid;
726 spawnsig = arg;
727 return 0;
730 case VT_SETMODE:
732 struct vt_mode tmp;
734 if (!perm)
735 return -EPERM;
736 if (copy_from_user(&tmp, (void*)arg, sizeof(struct vt_mode)))
737 return -EFAULT;
738 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
739 return -EINVAL;
740 vt_cons[console]->vt_mode = tmp;
741 /* the frsig is ignored, so we set it to 0 */
742 vt_cons[console]->vt_mode.frsig = 0;
743 vt_cons[console]->vt_pid = current->pid;
744 /* no switch is required -- saw@shade.msu.ru */
745 vt_cons[console]->vt_newvt = -1;
746 return 0;
749 case VT_GETMODE:
750 return copy_to_user((void*)arg, &(vt_cons[console]->vt_mode),
751 sizeof(struct vt_mode)) ? -EFAULT : 0;
754 * Returns global vt state. Note that VT 0 is always open, since
755 * it's an alias for the current VT, and people can't use it here.
756 * We cannot return state for more than 16 VTs, since v_state is short.
758 case VT_GETSTATE:
760 struct vt_stat *vtstat = (struct vt_stat *)arg;
761 unsigned short state, mask;
763 i = verify_area(VERIFY_WRITE,(void *)vtstat, sizeof(struct vt_stat));
764 if (i)
765 return i;
766 __put_user(fg_console + 1, &vtstat->v_active);
767 state = 1; /* /dev/tty0 is always open */
768 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
769 if (VT_IS_IN_USE(i))
770 state |= mask;
771 return __put_user(state, &vtstat->v_state);
775 * Returns the first available (non-opened) console.
777 case VT_OPENQRY:
778 for (i = 0; i < MAX_NR_CONSOLES; ++i)
779 if (! VT_IS_IN_USE(i))
780 break;
781 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
782 goto setint;
785 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
786 * with num >= 1 (switches to vt 0, our console, are not allowed, just
787 * to preserve sanity).
789 case VT_ACTIVATE:
790 if (!perm)
791 return -EPERM;
792 if (arg == 0 || arg > MAX_NR_CONSOLES)
793 return -ENXIO;
794 arg--;
795 i = vc_allocate(arg);
796 if (i)
797 return i;
798 set_console(arg);
799 return 0;
802 * wait until the specified VT has been activated
804 case VT_WAITACTIVE:
805 if (!perm)
806 return -EPERM;
807 if (arg == 0 || arg > MAX_NR_CONSOLES)
808 return -ENXIO;
809 return vt_waitactive(arg-1);
812 * If a vt is under process control, the kernel will not switch to it
813 * immediately, but postpone the operation until the process calls this
814 * ioctl, allowing the switch to complete.
816 * According to the X sources this is the behavior:
817 * 0: pending switch-from not OK
818 * 1: pending switch-from OK
819 * 2: completed switch-to OK
821 case VT_RELDISP:
822 if (!perm)
823 return -EPERM;
824 if (vt_cons[console]->vt_mode.mode != VT_PROCESS)
825 return -EINVAL;
828 * Switching-from response
830 if (vt_cons[console]->vt_newvt >= 0)
832 if (arg == 0)
834 * Switch disallowed, so forget we were trying
835 * to do it.
837 vt_cons[console]->vt_newvt = -1;
839 else
842 * The current vt has been released, so
843 * complete the switch.
845 int newvt = vt_cons[console]->vt_newvt;
846 vt_cons[console]->vt_newvt = -1;
847 i = vc_allocate(newvt);
848 if (i)
849 return i;
851 * When we actually do the console switch,
852 * make sure we are atomic with respect to
853 * other console switches..
855 start_bh_atomic();
856 complete_change_console(newvt);
857 end_bh_atomic();
862 * Switched-to response
864 else
867 * If it's just an ACK, ignore it
869 if (arg != VT_ACKACQ)
870 return -EINVAL;
873 return 0;
876 * Disallocate memory associated to VT (but leave VT1)
878 case VT_DISALLOCATE:
879 if (arg > MAX_NR_CONSOLES)
880 return -ENXIO;
881 if (arg == 0) {
882 /* disallocate all unused consoles, but leave 0 */
883 for (i=1; i<MAX_NR_CONSOLES; i++)
884 if (! VT_BUSY(i))
885 vc_disallocate(i);
886 } else {
887 /* disallocate a single console, if possible */
888 arg--;
889 if (VT_BUSY(arg))
890 return -EBUSY;
891 if (arg) /* leave 0 */
892 vc_disallocate(arg);
894 return 0;
896 case VT_RESIZE:
898 struct vt_sizes *vtsizes = (struct vt_sizes *) arg;
899 ushort ll,cc;
900 if (!perm)
901 return -EPERM;
902 i = verify_area(VERIFY_READ, (void *)vtsizes, sizeof(struct vt_sizes));
903 if (i)
904 return i;
905 __get_user(ll, &vtsizes->v_rows);
906 __get_user(cc, &vtsizes->v_cols);
907 i = vc_resize(ll, cc);
908 return i ? i : kd_size_changed(ll, cc);
911 case VT_RESIZEX:
913 struct vt_consize *vtconsize = (struct vt_consize *) arg;
914 ushort ll,cc,vlin,clin,vcol,ccol;
915 if (!perm)
916 return -EPERM;
917 i = verify_area(VERIFY_READ, (void *)vtconsize, sizeof(struct vt_consize));
918 if (i)
919 return i;
920 __get_user(ll, &vtconsize->v_rows);
921 __get_user(cc, &vtconsize->v_cols);
922 __get_user(vlin, &vtconsize->v_vlin);
923 __get_user(clin, &vtconsize->v_clin);
924 __get_user(vcol, &vtconsize->v_vcol);
925 __get_user(ccol, &vtconsize->v_ccol);
926 vlin = vlin ? vlin : video_scan_lines;
927 if ( clin )
929 if ( ll )
931 if ( ll != vlin/clin )
932 return -EINVAL; /* Parameters don't add up */
934 else
935 ll = vlin/clin;
937 if ( vcol && ccol )
939 if ( cc )
941 if ( cc != vcol/ccol )
942 return -EINVAL;
944 else
945 cc = vcol/ccol;
948 if ( clin > 32 )
949 return -EINVAL;
951 if ( vlin )
952 video_scan_lines = vlin;
953 if ( clin )
954 video_font_height = clin;
956 i = vc_resize(ll, cc);
957 if (i)
958 return i;
960 kd_size_changed(ll, cc);
961 return 0;
964 case PIO_FONT:
965 if (!perm)
966 return -EPERM;
967 if (vt_cons[fg_console]->vc_mode != KD_TEXT)
968 return -EINVAL;
969 return con_set_font((char *)arg, 0);
970 /* con_set_font() defined in console.c */
972 case GIO_FONT:
973 if (vt_cons[fg_console]->vc_mode != KD_TEXT ||
974 video_mode_512ch)
975 return -EINVAL;
976 return con_get_font((char *)arg);
977 /* con_get_font() defined in console.c */
979 case PIO_CMAP:
980 if (!perm)
981 return -EPERM;
982 return con_set_cmap((char *)arg);
983 /* con_set_cmap() defined in console.c */
985 case GIO_CMAP:
986 return con_get_cmap((char *)arg);
987 /* con_get_cmap() defined in console.c */
989 case PIO_FONTX:
990 case GIO_FONTX:
991 return do_fontx_ioctl(cmd, (struct consolefontdesc *)arg, perm);
993 case PIO_FONTRESET:
995 if (!perm)
996 return -EPERM;
997 if (vt_cons[fg_console]->vc_mode != KD_TEXT)
998 return -EINVAL;
1000 #ifdef BROKEN_GRAPHICS_PROGRAMS
1001 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1002 font is not saved. */
1003 return -ENOSYS;
1004 #else
1006 i = con_set_font(NULL, 0); /* Set font to default */
1007 if (i) return i;
1009 i = con_adjust_height(default_font_height);
1010 if ( i > 0 ) kd_size_changed(i, 0);
1011 con_set_default_unimap();
1013 return 0;
1014 #endif
1017 case PIO_SCRNMAP:
1018 if (!perm)
1019 return -EPERM;
1020 return con_set_trans_old((unsigned char *)arg);
1022 case GIO_SCRNMAP:
1023 return con_get_trans_old((unsigned char *)arg);
1025 case PIO_UNISCRNMAP:
1026 if (!perm)
1027 return -EPERM;
1028 return con_set_trans_new((unsigned short *)arg);
1030 case GIO_UNISCRNMAP:
1031 return con_get_trans_new((unsigned short *)arg);
1033 case PIO_UNIMAPCLR:
1034 { struct unimapinit ui;
1035 if (!perm)
1036 return -EPERM;
1037 i = copy_from_user(&ui, (void *)arg, sizeof(struct unimapinit));
1038 if (i) return -EFAULT;
1039 con_clear_unimap(&ui);
1040 return 0;
1043 case PIO_UNIMAP:
1044 case GIO_UNIMAP:
1045 return do_unimap_ioctl(cmd, (struct unimapdesc *)arg, perm);
1047 case VT_LOCKSWITCH:
1048 if (!suser())
1049 return -EPERM;
1050 vt_dont_switch = 1;
1051 return 0;
1052 case VT_UNLOCKSWITCH:
1053 if (!suser())
1054 return -EPERM;
1055 vt_dont_switch = 0;
1056 return 0;
1057 default:
1058 return -ENOIOCTLCMD;
1063 * Sometimes we want to wait until a particular VT has been activated. We
1064 * do it in a very simple manner. Everybody waits on a single queue and
1065 * get woken up at once. Those that are satisfied go on with their business,
1066 * while those not ready go back to sleep. Seems overkill to add a wait
1067 * to each vt just for this - usually this does nothing!
1069 static struct wait_queue *vt_activate_queue = NULL;
1072 * Sleeps until a vt is activated, or the task is interrupted. Returns
1073 * 0 if activation, -EINTR if interrupted.
1075 int vt_waitactive(int vt)
1077 int retval;
1078 struct wait_queue wait = { current, NULL };
1080 add_wait_queue(&vt_activate_queue, &wait);
1081 for (;;) {
1082 current->state = TASK_INTERRUPTIBLE;
1083 retval = 0;
1084 if (vt == fg_console)
1085 break;
1086 retval = -EINTR;
1087 if (current->signal & ~current->blocked)
1088 break;
1089 schedule();
1091 remove_wait_queue(&vt_activate_queue, &wait);
1092 current->state = TASK_RUNNING;
1093 return retval;
1096 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1098 void reset_vc(unsigned int new_console)
1100 vt_cons[new_console]->vc_mode = KD_TEXT;
1101 kbd_table[new_console].kbdmode = VC_XLATE;
1102 vt_cons[new_console]->vt_mode.mode = VT_AUTO;
1103 vt_cons[new_console]->vt_mode.waitv = 0;
1104 vt_cons[new_console]->vt_mode.relsig = 0;
1105 vt_cons[new_console]->vt_mode.acqsig = 0;
1106 vt_cons[new_console]->vt_mode.frsig = 0;
1107 vt_cons[new_console]->vt_pid = -1;
1108 vt_cons[new_console]->vt_newvt = -1;
1109 reset_palette (new_console) ;
1113 * Performs the back end of a vt switch
1115 void complete_change_console(unsigned int new_console)
1117 unsigned char old_vc_mode;
1119 if ((new_console == fg_console) || (vt_dont_switch))
1120 return;
1121 if (!vc_cons_allocated(new_console))
1122 return;
1123 last_console = fg_console;
1126 * If we're switching, we could be going from KD_GRAPHICS to
1127 * KD_TEXT mode or vice versa, which means we need to blank or
1128 * unblank the screen later.
1130 old_vc_mode = vt_cons[fg_console]->vc_mode;
1131 update_screen(new_console);
1134 * If this new console is under process control, send it a signal
1135 * telling it that it has acquired. Also check if it has died and
1136 * clean up (similar to logic employed in change_console())
1138 if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
1141 * Send the signal as privileged - kill_proc() will
1142 * tell us if the process has gone or something else
1143 * is awry
1145 if (kill_proc(vt_cons[new_console]->vt_pid,
1146 vt_cons[new_console]->vt_mode.acqsig,
1147 1) != 0)
1150 * The controlling process has died, so we revert back to
1151 * normal operation. In this case, we'll also change back
1152 * to KD_TEXT mode. I'm not sure if this is strictly correct
1153 * but it saves the agony when the X server dies and the screen
1154 * remains blanked due to KD_GRAPHICS! It would be nice to do
1155 * this outside of VT_PROCESS but there is no single process
1156 * to account for and tracking tty count may be undesirable.
1158 reset_vc(new_console);
1163 * We do this here because the controlling process above may have
1164 * gone, and so there is now a new vc_mode
1166 if (old_vc_mode != vt_cons[new_console]->vc_mode)
1168 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1169 do_unblank_screen();
1170 else
1171 do_blank_screen(1);
1174 /* Set the colour palette for this VT */
1175 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1176 set_palette() ;
1178 #ifdef CONFIG_SUN_CONSOLE
1179 if (old_vc_mode != vt_cons[new_console]->vc_mode)
1181 extern void set_cursor(int currcons);
1182 extern void hide_cursor(void);
1184 if (old_vc_mode == KD_GRAPHICS)
1186 extern void sun_clear_margin(void);
1187 extern void render_screen(void);
1189 sun_clear_margin();
1190 render_screen();
1191 set_cursor(fg_console);
1193 else
1194 hide_cursor();
1196 #endif
1198 * Wake anyone waiting for their VT to activate
1200 vt_wake_waitactive();
1201 return;
1205 * Performs the front-end of a vt switch
1207 void change_console(unsigned int new_console)
1209 if ((new_console == fg_console) || (vt_dont_switch))
1210 return;
1211 if (!vc_cons_allocated(new_console))
1212 return;
1215 * If this vt is in process mode, then we need to handshake with
1216 * that process before switching. Essentially, we store where that
1217 * vt wants to switch to and wait for it to tell us when it's done
1218 * (via VT_RELDISP ioctl).
1220 * We also check to see if the controlling process still exists.
1221 * If it doesn't, we reset this vt to auto mode and continue.
1222 * This is a cheap way to track process control. The worst thing
1223 * that can happen is: we send a signal to a process, it dies, and
1224 * the switch gets "lost" waiting for a response; hopefully, the
1225 * user will try again, we'll detect the process is gone (unless
1226 * the user waits just the right amount of time :-) and revert the
1227 * vt to auto control.
1229 if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
1232 * Send the signal as privileged - kill_proc() will
1233 * tell us if the process has gone or something else
1234 * is awry
1236 if (kill_proc(vt_cons[fg_console]->vt_pid,
1237 vt_cons[fg_console]->vt_mode.relsig,
1238 1) == 0)
1241 * It worked. Mark the vt to switch to and
1242 * return. The process needs to send us a
1243 * VT_RELDISP ioctl to complete the switch.
1245 vt_cons[fg_console]->vt_newvt = new_console;
1246 return;
1250 * The controlling process has died, so we revert back to
1251 * normal operation. In this case, we'll also change back
1252 * to KD_TEXT mode. I'm not sure if this is strictly correct
1253 * but it saves the agony when the X server dies and the screen
1254 * remains blanked due to KD_GRAPHICS! It would be nice to do
1255 * this outside of VT_PROCESS but there is no single process
1256 * to account for and tracking tty count may be undesirable.
1258 reset_vc(fg_console);
1261 * Fall through to normal (VT_AUTO) handling of the switch...
1266 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1268 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
1269 return;
1271 complete_change_console(new_console);