- David Miller: sparc and net updates. Fix merge_segments.
[davej-history.git] / drivers / char / vt.c
blob4165a37e337a1ad3c31273bf709c78147c9b4066
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/config.h>
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/kd.h>
20 #include <linux/vt.h>
21 #include <linux/string.h>
22 #include <linux/malloc.h>
23 #include <linux/major.h>
24 #include <linux/fs.h>
25 #include <linux/console.h>
27 #include <asm/io.h>
28 #include <asm/uaccess.h>
30 #include <linux/kbd_kern.h>
31 #include <linux/vt_kern.h>
32 #include <linux/kbd_diacr.h>
33 #include <linux/selection.h>
35 #ifdef CONFIG_FB_COMPAT_XPMAC
36 #include <asm/vc_ioctl.h>
37 #endif /* CONFIG_FB_COMPAT_XPMAC */
39 char vt_dont_switch;
40 extern struct tty_driver console_driver;
42 #define VT_IS_IN_USE(i) (console_driver.table[i] && console_driver.table[i]->count)
43 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || i == sel_cons)
46 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
47 * experimentation and study of X386 SYSV handling.
49 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
50 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
51 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
52 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
53 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
54 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
55 * to the current console is done by the main ioctl code.
58 struct vt_struct *vt_cons[MAX_NR_CONSOLES];
60 /* Keyboard type: Default is KB_101, but can be set by machine
61 * specific code.
63 unsigned char keyboard_type = KB_101;
65 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__) && !defined(__arm__) && !defined(__sh__)
66 asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int on);
67 #endif
69 unsigned int video_font_height;
70 unsigned int default_font_height;
71 unsigned int video_scan_lines;
74 * these are the valid i/o ports we're allowed to change. they map all the
75 * video ports
77 #define GPFIRST 0x3b4
78 #define GPLAST 0x3df
79 #define GPNUM (GPLAST - GPFIRST + 1)
82 * Generates sound of some frequency for some number of clock ticks
84 * If freq is 0, will turn off sound, else will turn it on for that time.
85 * If msec is 0, will return immediately, else will sleep for msec time, then
86 * turn sound off.
88 * We also return immediately, which is what was implied within the X
89 * comments - KDMKTONE doesn't put the process to sleep.
92 #if defined(__i386__) || defined(__alpha__) || defined(__powerpc__) \
93 || (defined(__mips__) && !defined(CONFIG_SGI_IP22)) \
94 || (defined(__arm__) && defined(CONFIG_HOST_FOOTBRIDGE))
96 static void
97 kd_nosound(unsigned long ignored)
99 /* disable counter 2 */
100 outb(inb_p(0x61)&0xFC, 0x61);
101 return;
104 void
105 _kd_mksound(unsigned int hz, unsigned int ticks)
107 static struct timer_list sound_timer = { function: kd_nosound };
108 unsigned int count = 0;
109 unsigned long flags;
111 if (hz > 20 && hz < 32767)
112 count = 1193180 / hz;
114 save_flags(flags);
115 cli();
116 del_timer(&sound_timer);
117 if (count) {
118 /* enable counter 2 */
119 outb_p(inb_p(0x61)|3, 0x61);
120 /* set command for counter 2, 2 byte write */
121 outb_p(0xB6, 0x43);
122 /* select desired HZ */
123 outb_p(count & 0xff, 0x42);
124 outb((count >> 8) & 0xff, 0x42);
126 if (ticks) {
127 sound_timer.expires = jiffies+ticks;
128 add_timer(&sound_timer);
130 } else
131 kd_nosound(0);
132 restore_flags(flags);
133 return;
136 #else
138 void
139 _kd_mksound(unsigned int hz, unsigned int ticks)
143 #endif
145 void (*kd_mksound)(unsigned int hz, unsigned int ticks) = _kd_mksound;
148 #define i (tmp.kb_index)
149 #define s (tmp.kb_table)
150 #define v (tmp.kb_value)
151 static inline int
152 do_kdsk_ioctl(int cmd, struct kbentry *user_kbe, int perm, struct kbd_struct *kbd)
154 struct kbentry tmp;
155 ushort *key_map, val, ov;
157 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
158 return -EFAULT;
159 if (i >= NR_KEYS || s >= MAX_NR_KEYMAPS)
160 return -EINVAL;
162 switch (cmd) {
163 case KDGKBENT:
164 key_map = key_maps[s];
165 if (key_map) {
166 val = U(key_map[i]);
167 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
168 val = K_HOLE;
169 } else
170 val = (i ? K_HOLE : K_NOSUCHMAP);
171 return put_user(val, &user_kbe->kb_value);
172 case KDSKBENT:
173 if (!perm)
174 return -EPERM;
175 if (!i && v == K_NOSUCHMAP) {
176 /* disallocate map */
177 key_map = key_maps[s];
178 if (s && key_map) {
179 key_maps[s] = 0;
180 if (key_map[0] == U(K_ALLOCATED)) {
181 kfree(key_map);
182 keymap_count--;
185 break;
188 if (KTYP(v) < NR_TYPES) {
189 if (KVAL(v) > max_vals[KTYP(v)])
190 return -EINVAL;
191 } else
192 if (kbd->kbdmode != VC_UNICODE)
193 return -EINVAL;
195 /* ++Geert: non-PC keyboards may generate keycode zero */
196 #if !defined(__mc68000__) && !defined(__powerpc__)
197 /* assignment to entry 0 only tests validity of args */
198 if (!i)
199 break;
200 #endif
202 if (!(key_map = key_maps[s])) {
203 int j;
205 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
206 !capable(CAP_SYS_RESOURCE))
207 return -EPERM;
209 key_map = (ushort *) kmalloc(sizeof(plain_map),
210 GFP_KERNEL);
211 if (!key_map)
212 return -ENOMEM;
213 key_maps[s] = key_map;
214 key_map[0] = U(K_ALLOCATED);
215 for (j = 1; j < NR_KEYS; j++)
216 key_map[j] = U(K_HOLE);
217 keymap_count++;
219 ov = U(key_map[i]);
220 if (v == ov)
221 break; /* nothing to do */
223 * Attention Key.
225 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
226 return -EPERM;
227 key_map[i] = U(v);
228 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
229 compute_shiftstate();
230 break;
232 return 0;
234 #undef i
235 #undef s
236 #undef v
238 static inline int
239 do_kbkeycode_ioctl(int cmd, struct kbkeycode *user_kbkc, int perm)
241 struct kbkeycode tmp;
242 int kc = 0;
244 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
245 return -EFAULT;
246 switch (cmd) {
247 case KDGETKEYCODE:
248 kc = getkeycode(tmp.scancode);
249 if (kc >= 0)
250 kc = put_user(kc, &user_kbkc->keycode);
251 break;
252 case KDSETKEYCODE:
253 if (!perm)
254 return -EPERM;
255 kc = setkeycode(tmp.scancode, tmp.keycode);
256 break;
258 return kc;
261 static inline int
262 do_kdgkb_ioctl(int cmd, struct kbsentry *user_kdgkb, int perm)
264 struct kbsentry tmp;
265 char *p;
266 u_char *q;
267 int sz;
268 int delta;
269 char *first_free, *fj, *fnw;
270 int i, j, k;
272 /* we mostly copy too much here (512bytes), but who cares ;) */
273 if (copy_from_user(&tmp, user_kdgkb, sizeof(struct kbsentry)))
274 return -EFAULT;
275 tmp.kb_string[sizeof(tmp.kb_string)-1] = '\0';
276 if (tmp.kb_func >= MAX_NR_FUNC)
277 return -EINVAL;
278 i = tmp.kb_func;
280 switch (cmd) {
281 case KDGKBSENT:
282 sz = sizeof(tmp.kb_string) - 1; /* sz should have been
283 a struct member */
284 q = user_kdgkb->kb_string;
285 p = func_table[i];
286 if(p)
287 for ( ; *p && sz; p++, sz--)
288 put_user(*p, q++);
289 put_user('\0', q);
290 return ((p && *p) ? -EOVERFLOW : 0);
291 case KDSKBSENT:
292 if (!perm)
293 return -EPERM;
295 q = func_table[i];
296 first_free = funcbufptr + (funcbufsize - funcbufleft);
297 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
299 if (j < MAX_NR_FUNC)
300 fj = func_table[j];
301 else
302 fj = first_free;
304 delta = (q ? -strlen(q) : 1) + strlen(tmp.kb_string);
305 if (delta <= funcbufleft) { /* it fits in current buf */
306 if (j < MAX_NR_FUNC) {
307 memmove(fj + delta, fj, first_free - fj);
308 for (k = j; k < MAX_NR_FUNC; k++)
309 if (func_table[k])
310 func_table[k] += delta;
312 if (!q)
313 func_table[i] = fj;
314 funcbufleft -= delta;
315 } else { /* allocate a larger buffer */
316 sz = 256;
317 while (sz < funcbufsize - funcbufleft + delta)
318 sz <<= 1;
319 fnw = (char *) kmalloc(sz, GFP_KERNEL);
320 if(!fnw)
321 return -ENOMEM;
323 if (!q)
324 func_table[i] = fj;
325 if (fj > funcbufptr)
326 memmove(fnw, funcbufptr, fj - funcbufptr);
327 for (k = 0; k < j; k++)
328 if (func_table[k])
329 func_table[k] = fnw + (func_table[k] - funcbufptr);
331 if (first_free > fj) {
332 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
333 for (k = j; k < MAX_NR_FUNC; k++)
334 if (func_table[k])
335 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
337 if (funcbufptr != func_buf)
338 kfree(funcbufptr);
339 funcbufptr = fnw;
340 funcbufleft = funcbufleft - delta + sz - funcbufsize;
341 funcbufsize = sz;
343 strcpy(func_table[i], tmp.kb_string);
344 break;
346 return 0;
349 static inline int
350 do_fontx_ioctl(int cmd, struct consolefontdesc *user_cfd, int perm)
352 struct consolefontdesc cfdarg;
353 struct console_font_op op;
354 int i;
356 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
357 return -EFAULT;
359 switch (cmd) {
360 case PIO_FONTX:
361 if (!perm)
362 return -EPERM;
363 op.op = KD_FONT_OP_SET;
364 op.flags = KD_FONT_FLAG_OLD;
365 op.width = 8;
366 op.height = cfdarg.charheight;
367 op.charcount = cfdarg.charcount;
368 op.data = cfdarg.chardata;
369 return con_font_op(fg_console, &op);
370 case GIO_FONTX: {
371 op.op = KD_FONT_OP_GET;
372 op.flags = KD_FONT_FLAG_OLD;
373 op.width = 8;
374 op.height = cfdarg.charheight;
375 op.charcount = cfdarg.charcount;
376 op.data = cfdarg.chardata;
377 i = con_font_op(fg_console, &op);
378 if (i)
379 return i;
380 cfdarg.charheight = op.height;
381 cfdarg.charcount = op.charcount;
382 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
383 return -EFAULT;
384 return 0;
387 return -EINVAL;
390 static inline int
391 do_unimap_ioctl(int cmd, struct unimapdesc *user_ud,int perm)
393 struct unimapdesc tmp;
394 int i = 0;
396 if (copy_from_user(&tmp, user_ud, sizeof tmp))
397 return -EFAULT;
398 if (tmp.entries) {
399 i = verify_area(VERIFY_WRITE, tmp.entries,
400 tmp.entry_ct*sizeof(struct unipair));
401 if (i) return i;
403 switch (cmd) {
404 case PIO_UNIMAP:
405 if (!perm)
406 return -EPERM;
407 return con_set_unimap(fg_console, tmp.entry_ct, tmp.entries);
408 case GIO_UNIMAP:
409 return con_get_unimap(fg_console, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
411 return 0;
415 * We handle the console-specific ioctl's here. We allow the
416 * capability to modify any console, not just the fg_console.
418 int vt_ioctl(struct tty_struct *tty, struct file * file,
419 unsigned int cmd, unsigned long arg)
421 int i, perm;
422 unsigned int console;
423 unsigned char ucval;
424 struct kbd_struct * kbd;
425 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
427 console = vt->vc_num;
429 if (!vc_cons_allocated(console)) /* impossible? */
430 return -ENOIOCTLCMD;
433 * To have permissions to do most of the vt ioctls, we either have
434 * to be the owner of the tty, or super-user.
436 perm = 0;
437 if (current->tty == tty || suser())
438 perm = 1;
440 kbd = kbd_table + console;
441 switch (cmd) {
442 case KIOCSOUND:
443 if (!perm)
444 return -EPERM;
445 if (arg)
446 arg = 1193180 / arg;
447 kd_mksound(arg, 0);
448 return 0;
450 case KDMKTONE:
451 if (!perm)
452 return -EPERM;
454 unsigned int ticks, count;
457 * Generate the tone for the appropriate number of ticks.
458 * If the time is zero, turn off sound ourselves.
460 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
461 count = ticks ? (arg & 0xffff) : 0;
462 if (count)
463 count = 1193180 / count;
464 kd_mksound(count, ticks);
465 return 0;
468 case KDGKBTYPE:
470 * this is naive.
472 ucval = keyboard_type;
473 goto setchar;
475 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__) && !defined(__arm__) && !defined(__sh__)
477 * These cannot be implemented on any machine that implements
478 * ioperm() in user level (such as Alpha PCs).
480 case KDADDIO:
481 case KDDELIO:
483 * KDADDIO and KDDELIO may be able to add ports beyond what
484 * we reject here, but to be safe...
486 if (arg < GPFIRST || arg > GPLAST)
487 return -EINVAL;
488 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
490 case KDENABIO:
491 case KDDISABIO:
492 return sys_ioperm(GPFIRST, GPNUM,
493 (cmd == KDENABIO)) ? -ENXIO : 0;
494 #endif
496 case KDSETMODE:
498 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
499 * doesn't do a whole lot. i'm not sure if it should do any
500 * restoration of modes or what...
502 if (!perm)
503 return -EPERM;
504 switch (arg) {
505 case KD_GRAPHICS:
506 break;
507 case KD_TEXT0:
508 case KD_TEXT1:
509 arg = KD_TEXT;
510 case KD_TEXT:
511 break;
512 default:
513 return -EINVAL;
515 if (vt_cons[console]->vc_mode == (unsigned char) arg)
516 return 0;
517 vt_cons[console]->vc_mode = (unsigned char) arg;
518 if (console != fg_console)
519 return 0;
521 * explicitly blank/unblank the screen if switching modes
523 if (arg == KD_TEXT)
524 unblank_screen();
525 else
526 do_blank_screen(1);
527 return 0;
529 case KDGETMODE:
530 ucval = vt_cons[console]->vc_mode;
531 goto setint;
533 case KDMAPDISP:
534 case KDUNMAPDISP:
536 * these work like a combination of mmap and KDENABIO.
537 * this could be easily finished.
539 return -EINVAL;
541 case KDSKBMODE:
542 if (!perm)
543 return -EPERM;
544 switch(arg) {
545 case K_RAW:
546 kbd->kbdmode = VC_RAW;
547 break;
548 case K_MEDIUMRAW:
549 kbd->kbdmode = VC_MEDIUMRAW;
550 break;
551 case K_XLATE:
552 kbd->kbdmode = VC_XLATE;
553 compute_shiftstate();
554 break;
555 case K_UNICODE:
556 kbd->kbdmode = VC_UNICODE;
557 compute_shiftstate();
558 break;
559 default:
560 return -EINVAL;
562 if (tty->ldisc.flush_buffer)
563 tty->ldisc.flush_buffer(tty);
564 return 0;
566 case KDGKBMODE:
567 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
568 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
569 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
570 K_XLATE);
571 goto setint;
573 /* this could be folded into KDSKBMODE, but for compatibility
574 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
575 case KDSKBMETA:
576 switch(arg) {
577 case K_METABIT:
578 clr_vc_kbd_mode(kbd, VC_META);
579 break;
580 case K_ESCPREFIX:
581 set_vc_kbd_mode(kbd, VC_META);
582 break;
583 default:
584 return -EINVAL;
586 return 0;
588 case KDGKBMETA:
589 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
590 setint:
591 return put_user(ucval, (int *)arg);
593 case KDGETKEYCODE:
594 case KDSETKEYCODE:
595 if(!capable(CAP_SYS_ADMIN))
596 perm=0;
597 return do_kbkeycode_ioctl(cmd, (struct kbkeycode *)arg, perm);
599 case KDGKBENT:
600 case KDSKBENT:
601 return do_kdsk_ioctl(cmd, (struct kbentry *)arg, perm, kbd);
603 case KDGKBSENT:
604 case KDSKBSENT:
605 return do_kdgkb_ioctl(cmd, (struct kbsentry *)arg, perm);
607 case KDGKBDIACR:
609 struct kbdiacrs *a = (struct kbdiacrs *)arg;
611 if (put_user(accent_table_size, &a->kb_cnt))
612 return -EFAULT;
613 if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))
614 return -EFAULT;
615 return 0;
618 case KDSKBDIACR:
620 struct kbdiacrs *a = (struct kbdiacrs *)arg;
621 unsigned int ct;
623 if (!perm)
624 return -EPERM;
625 if (get_user(ct,&a->kb_cnt))
626 return -EFAULT;
627 if (ct >= MAX_DIACR)
628 return -EINVAL;
629 accent_table_size = ct;
630 if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))
631 return -EFAULT;
632 return 0;
635 /* the ioctls below read/set the flags usually shown in the leds */
636 /* don't use them - they will go away without warning */
637 case KDGKBLED:
638 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
639 goto setchar;
641 case KDSKBLED:
642 if (!perm)
643 return -EPERM;
644 if (arg & ~0x77)
645 return -EINVAL;
646 kbd->ledflagstate = (arg & 7);
647 kbd->default_ledflagstate = ((arg >> 4) & 7);
648 set_leds();
649 return 0;
651 /* the ioctls below only set the lights, not the functions */
652 /* for those, see KDGKBLED and KDSKBLED above */
653 case KDGETLED:
654 ucval = getledstate();
655 setchar:
656 return put_user(ucval, (char*)arg);
658 case KDSETLED:
659 if (!perm)
660 return -EPERM;
661 setledstate(kbd, arg);
662 return 0;
665 * A process can indicate its willingness to accept signals
666 * generated by pressing an appropriate key combination.
667 * Thus, one can have a daemon that e.g. spawns a new console
668 * upon a keypress and then changes to it.
669 * Probably init should be changed to do this (and have a
670 * field ks (`keyboard signal') in inittab describing the
671 * desired action), so that the number of background daemons
672 * does not increase.
674 case KDSIGACCEPT:
676 extern int spawnpid, spawnsig;
677 if (!perm || !capable(CAP_KILL))
678 return -EPERM;
679 if (arg < 1 || arg > _NSIG || arg == SIGKILL)
680 return -EINVAL;
681 spawnpid = current->pid;
682 spawnsig = arg;
683 return 0;
686 case VT_SETMODE:
688 struct vt_mode tmp;
690 if (!perm)
691 return -EPERM;
692 if (copy_from_user(&tmp, (void*)arg, sizeof(struct vt_mode)))
693 return -EFAULT;
694 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
695 return -EINVAL;
696 vt_cons[console]->vt_mode = tmp;
697 /* the frsig is ignored, so we set it to 0 */
698 vt_cons[console]->vt_mode.frsig = 0;
699 vt_cons[console]->vt_pid = current->pid;
700 /* no switch is required -- saw@shade.msu.ru */
701 vt_cons[console]->vt_newvt = -1;
702 return 0;
705 case VT_GETMODE:
706 return copy_to_user((void*)arg, &(vt_cons[console]->vt_mode),
707 sizeof(struct vt_mode)) ? -EFAULT : 0;
710 * Returns global vt state. Note that VT 0 is always open, since
711 * it's an alias for the current VT, and people can't use it here.
712 * We cannot return state for more than 16 VTs, since v_state is short.
714 case VT_GETSTATE:
716 struct vt_stat *vtstat = (struct vt_stat *)arg;
717 unsigned short state, mask;
719 i = verify_area(VERIFY_WRITE,(void *)vtstat, sizeof(struct vt_stat));
720 if (i)
721 return i;
722 put_user(fg_console + 1, &vtstat->v_active);
723 state = 1; /* /dev/tty0 is always open */
724 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
725 if (VT_IS_IN_USE(i))
726 state |= mask;
727 return put_user(state, &vtstat->v_state);
731 * Returns the first available (non-opened) console.
733 case VT_OPENQRY:
734 for (i = 0; i < MAX_NR_CONSOLES; ++i)
735 if (! VT_IS_IN_USE(i))
736 break;
737 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
738 goto setint;
741 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
742 * with num >= 1 (switches to vt 0, our console, are not allowed, just
743 * to preserve sanity).
745 case VT_ACTIVATE:
746 if (!perm)
747 return -EPERM;
748 if (arg == 0 || arg > MAX_NR_CONSOLES)
749 return -ENXIO;
750 arg--;
751 i = vc_allocate(arg);
752 if (i)
753 return i;
754 set_console(arg);
755 return 0;
758 * wait until the specified VT has been activated
760 case VT_WAITACTIVE:
761 if (!perm)
762 return -EPERM;
763 if (arg == 0 || arg > MAX_NR_CONSOLES)
764 return -ENXIO;
765 return vt_waitactive(arg-1);
768 * If a vt is under process control, the kernel will not switch to it
769 * immediately, but postpone the operation until the process calls this
770 * ioctl, allowing the switch to complete.
772 * According to the X sources this is the behavior:
773 * 0: pending switch-from not OK
774 * 1: pending switch-from OK
775 * 2: completed switch-to OK
777 case VT_RELDISP:
778 if (!perm)
779 return -EPERM;
780 if (vt_cons[console]->vt_mode.mode != VT_PROCESS)
781 return -EINVAL;
784 * Switching-from response
786 if (vt_cons[console]->vt_newvt >= 0)
788 if (arg == 0)
790 * Switch disallowed, so forget we were trying
791 * to do it.
793 vt_cons[console]->vt_newvt = -1;
795 else
798 * The current vt has been released, so
799 * complete the switch.
801 int newvt = vt_cons[console]->vt_newvt;
802 vt_cons[console]->vt_newvt = -1;
803 i = vc_allocate(newvt);
804 if (i)
805 return i;
807 * When we actually do the console switch,
808 * make sure we are atomic with respect to
809 * other console switches..
811 spin_lock_irq(&console_lock);
812 complete_change_console(newvt);
813 spin_unlock_irq(&console_lock);
818 * Switched-to response
820 else
823 * If it's just an ACK, ignore it
825 if (arg != VT_ACKACQ)
826 return -EINVAL;
829 return 0;
832 * Disallocate memory associated to VT (but leave VT1)
834 case VT_DISALLOCATE:
835 if (arg > MAX_NR_CONSOLES)
836 return -ENXIO;
837 if (arg == 0) {
838 /* disallocate all unused consoles, but leave 0 */
839 for (i=1; i<MAX_NR_CONSOLES; i++)
840 if (! VT_BUSY(i))
841 vc_disallocate(i);
842 } else {
843 /* disallocate a single console, if possible */
844 arg--;
845 if (VT_BUSY(arg))
846 return -EBUSY;
847 if (arg) /* leave 0 */
848 vc_disallocate(arg);
850 return 0;
852 case VT_RESIZE:
854 struct vt_sizes *vtsizes = (struct vt_sizes *) arg;
855 ushort ll,cc;
856 if (!perm)
857 return -EPERM;
858 i = verify_area(VERIFY_READ, (void *)vtsizes, sizeof(struct vt_sizes));
859 if (i)
860 return i;
861 get_user(ll, &vtsizes->v_rows);
862 get_user(cc, &vtsizes->v_cols);
863 return vc_resize_all(ll, cc);
866 case VT_RESIZEX:
868 struct vt_consize *vtconsize = (struct vt_consize *) arg;
869 ushort ll,cc,vlin,clin,vcol,ccol;
870 if (!perm)
871 return -EPERM;
872 i = verify_area(VERIFY_READ, (void *)vtconsize, sizeof(struct vt_consize));
873 if (i)
874 return i;
875 get_user(ll, &vtconsize->v_rows);
876 get_user(cc, &vtconsize->v_cols);
877 get_user(vlin, &vtconsize->v_vlin);
878 get_user(clin, &vtconsize->v_clin);
879 get_user(vcol, &vtconsize->v_vcol);
880 get_user(ccol, &vtconsize->v_ccol);
881 vlin = vlin ? vlin : video_scan_lines;
882 if ( clin )
884 if ( ll )
886 if ( ll != vlin/clin )
887 return -EINVAL; /* Parameters don't add up */
889 else
890 ll = vlin/clin;
892 if ( vcol && ccol )
894 if ( cc )
896 if ( cc != vcol/ccol )
897 return -EINVAL;
899 else
900 cc = vcol/ccol;
903 if ( clin > 32 )
904 return -EINVAL;
906 if ( vlin )
907 video_scan_lines = vlin;
908 if ( clin )
909 video_font_height = clin;
911 return vc_resize_all(ll, cc);
914 case PIO_FONT: {
915 struct console_font_op op;
916 if (!perm)
917 return -EPERM;
918 op.op = KD_FONT_OP_SET;
919 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
920 op.width = 8;
921 op.height = 0;
922 op.charcount = 256;
923 op.data = (char *) arg;
924 return con_font_op(fg_console, &op);
927 case GIO_FONT: {
928 struct console_font_op op;
929 op.op = KD_FONT_OP_GET;
930 op.flags = KD_FONT_FLAG_OLD;
931 op.width = 8;
932 op.height = 32;
933 op.charcount = 256;
934 op.data = (char *) arg;
935 return con_font_op(fg_console, &op);
938 case PIO_CMAP:
939 if (!perm)
940 return -EPERM;
941 return con_set_cmap((char *)arg);
943 case GIO_CMAP:
944 return con_get_cmap((char *)arg);
946 case PIO_FONTX:
947 case GIO_FONTX:
948 return do_fontx_ioctl(cmd, (struct consolefontdesc *)arg, perm);
950 case PIO_FONTRESET:
952 if (!perm)
953 return -EPERM;
955 #ifdef BROKEN_GRAPHICS_PROGRAMS
956 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
957 font is not saved. */
958 return -ENOSYS;
959 #else
961 struct console_font_op op;
962 op.op = KD_FONT_OP_SET_DEFAULT;
963 op.data = NULL;
964 i = con_font_op(fg_console, &op);
965 if (i) return i;
966 con_set_default_unimap(fg_console);
967 return 0;
969 #endif
972 case KDFONTOP: {
973 struct console_font_op op;
974 if (copy_from_user(&op, (void *) arg, sizeof(op)))
975 return -EFAULT;
976 if (!perm && op.op != KD_FONT_OP_GET)
977 return -EPERM;
978 i = con_font_op(console, &op);
979 if (i) return i;
980 if (copy_to_user((void *) arg, &op, sizeof(op)))
981 return -EFAULT;
982 return 0;
985 case PIO_SCRNMAP:
986 if (!perm)
987 return -EPERM;
988 return con_set_trans_old((unsigned char *)arg);
990 case GIO_SCRNMAP:
991 return con_get_trans_old((unsigned char *)arg);
993 case PIO_UNISCRNMAP:
994 if (!perm)
995 return -EPERM;
996 return con_set_trans_new((unsigned short *)arg);
998 case GIO_UNISCRNMAP:
999 return con_get_trans_new((unsigned short *)arg);
1001 case PIO_UNIMAPCLR:
1002 { struct unimapinit ui;
1003 if (!perm)
1004 return -EPERM;
1005 i = copy_from_user(&ui, (void *)arg, sizeof(struct unimapinit));
1006 if (i) return -EFAULT;
1007 con_clear_unimap(fg_console, &ui);
1008 return 0;
1011 case PIO_UNIMAP:
1012 case GIO_UNIMAP:
1013 return do_unimap_ioctl(cmd, (struct unimapdesc *)arg, perm);
1015 case VT_LOCKSWITCH:
1016 if (!suser())
1017 return -EPERM;
1018 vt_dont_switch = 1;
1019 return 0;
1020 case VT_UNLOCKSWITCH:
1021 if (!suser())
1022 return -EPERM;
1023 vt_dont_switch = 0;
1024 return 0;
1025 #ifdef CONFIG_FB_COMPAT_XPMAC
1026 case VC_GETMODE:
1028 struct vc_mode mode;
1030 i = verify_area(VERIFY_WRITE, (void *) arg,
1031 sizeof(struct vc_mode));
1032 if (i == 0)
1033 i = console_getmode(&mode);
1034 if (i)
1035 return i;
1036 if (copy_to_user((void *) arg, &mode, sizeof(mode)))
1037 return -EFAULT;
1038 return 0;
1040 case VC_SETMODE:
1041 case VC_INQMODE:
1043 struct vc_mode mode;
1045 if (!perm)
1046 return -EPERM;
1047 i = verify_area(VERIFY_READ, (void *) arg,
1048 sizeof(struct vc_mode));
1049 if (i)
1050 return i;
1051 if (copy_from_user(&mode, (void *) arg, sizeof(mode)))
1052 return -EFAULT;
1053 return console_setmode(&mode, cmd == VC_SETMODE);
1055 case VC_SETCMAP:
1057 unsigned char cmap[3][256], *p;
1058 int n_entries, cmap_size, i, j;
1060 if (!perm)
1061 return -EPERM;
1062 if (arg == (unsigned long) VC_POWERMODE_INQUIRY
1063 || arg <= VESA_POWERDOWN) {
1064 /* compatibility hack: VC_POWERMODE
1065 was changed from 0x766a to 0x766c */
1066 return console_powermode((int) arg);
1068 i = verify_area(VERIFY_READ, (void *) arg,
1069 sizeof(int));
1070 if (i)
1071 return i;
1072 if (get_user(cmap_size, (int *) arg))
1073 return -EFAULT;
1074 if (cmap_size % 3)
1075 return -EINVAL;
1076 n_entries = cmap_size / 3;
1077 if ((unsigned) n_entries > 256)
1078 return -EINVAL;
1079 p = (unsigned char *) (arg + sizeof(int));
1080 for (j = 0; j < n_entries; ++j)
1081 for (i = 0; i < 3; ++i)
1082 if (get_user(cmap[i][j], p++))
1083 return -EFAULT;
1084 return console_setcmap(n_entries, cmap[0],
1085 cmap[1], cmap[2]);
1087 case VC_GETCMAP:
1088 /* not implemented yet */
1089 return -ENOIOCTLCMD;
1090 case VC_POWERMODE:
1091 if (!perm)
1092 return -EPERM;
1093 return console_powermode((int) arg);
1094 #endif /* CONFIG_FB_COMPAT_XPMAC */
1095 default:
1096 return -ENOIOCTLCMD;
1101 * Sometimes we want to wait until a particular VT has been activated. We
1102 * do it in a very simple manner. Everybody waits on a single queue and
1103 * get woken up at once. Those that are satisfied go on with their business,
1104 * while those not ready go back to sleep. Seems overkill to add a wait
1105 * to each vt just for this - usually this does nothing!
1107 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1110 * Sleeps until a vt is activated, or the task is interrupted. Returns
1111 * 0 if activation, -EINTR if interrupted.
1113 int vt_waitactive(int vt)
1115 int retval;
1116 DECLARE_WAITQUEUE(wait, current);
1118 add_wait_queue(&vt_activate_queue, &wait);
1119 for (;;) {
1120 set_current_state(TASK_INTERRUPTIBLE);
1121 retval = 0;
1122 if (vt == fg_console)
1123 break;
1124 retval = -EINTR;
1125 if (signal_pending(current))
1126 break;
1127 schedule();
1129 remove_wait_queue(&vt_activate_queue, &wait);
1130 current->state = TASK_RUNNING;
1131 return retval;
1134 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1136 void reset_vc(unsigned int new_console)
1138 vt_cons[new_console]->vc_mode = KD_TEXT;
1139 kbd_table[new_console].kbdmode = VC_XLATE;
1140 vt_cons[new_console]->vt_mode.mode = VT_AUTO;
1141 vt_cons[new_console]->vt_mode.waitv = 0;
1142 vt_cons[new_console]->vt_mode.relsig = 0;
1143 vt_cons[new_console]->vt_mode.acqsig = 0;
1144 vt_cons[new_console]->vt_mode.frsig = 0;
1145 vt_cons[new_console]->vt_pid = -1;
1146 vt_cons[new_console]->vt_newvt = -1;
1147 reset_palette (new_console) ;
1151 * Performs the back end of a vt switch
1153 void complete_change_console(unsigned int new_console)
1155 unsigned char old_vc_mode;
1157 last_console = fg_console;
1160 * If we're switching, we could be going from KD_GRAPHICS to
1161 * KD_TEXT mode or vice versa, which means we need to blank or
1162 * unblank the screen later.
1164 old_vc_mode = vt_cons[fg_console]->vc_mode;
1165 switch_screen(new_console);
1168 * If this new console is under process control, send it a signal
1169 * telling it that it has acquired. Also check if it has died and
1170 * clean up (similar to logic employed in change_console())
1172 if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
1175 * Send the signal as privileged - kill_proc() will
1176 * tell us if the process has gone or something else
1177 * is awry
1179 if (kill_proc(vt_cons[new_console]->vt_pid,
1180 vt_cons[new_console]->vt_mode.acqsig,
1181 1) != 0)
1184 * The controlling process has died, so we revert back to
1185 * normal operation. In this case, we'll also change back
1186 * to KD_TEXT mode. I'm not sure if this is strictly correct
1187 * but it saves the agony when the X server dies and the screen
1188 * remains blanked due to KD_GRAPHICS! It would be nice to do
1189 * this outside of VT_PROCESS but there is no single process
1190 * to account for and tracking tty count may be undesirable.
1192 reset_vc(new_console);
1197 * We do this here because the controlling process above may have
1198 * gone, and so there is now a new vc_mode
1200 if (old_vc_mode != vt_cons[new_console]->vc_mode)
1202 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1203 unblank_screen();
1204 else
1205 do_blank_screen(1);
1209 * Wake anyone waiting for their VT to activate
1211 vt_wake_waitactive();
1212 return;
1216 * Performs the front-end of a vt switch
1218 void change_console(unsigned int new_console)
1220 if ((new_console == fg_console) || (vt_dont_switch))
1221 return;
1222 if (!vc_cons_allocated(new_console))
1223 return;
1226 * If this vt is in process mode, then we need to handshake with
1227 * that process before switching. Essentially, we store where that
1228 * vt wants to switch to and wait for it to tell us when it's done
1229 * (via VT_RELDISP ioctl).
1231 * We also check to see if the controlling process still exists.
1232 * If it doesn't, we reset this vt to auto mode and continue.
1233 * This is a cheap way to track process control. The worst thing
1234 * that can happen is: we send a signal to a process, it dies, and
1235 * the switch gets "lost" waiting for a response; hopefully, the
1236 * user will try again, we'll detect the process is gone (unless
1237 * the user waits just the right amount of time :-) and revert the
1238 * vt to auto control.
1240 if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
1243 * Send the signal as privileged - kill_proc() will
1244 * tell us if the process has gone or something else
1245 * is awry
1247 if (kill_proc(vt_cons[fg_console]->vt_pid,
1248 vt_cons[fg_console]->vt_mode.relsig,
1249 1) == 0)
1252 * It worked. Mark the vt to switch to and
1253 * return. The process needs to send us a
1254 * VT_RELDISP ioctl to complete the switch.
1256 vt_cons[fg_console]->vt_newvt = new_console;
1257 return;
1261 * The controlling process has died, so we revert back to
1262 * normal operation. In this case, we'll also change back
1263 * to KD_TEXT mode. I'm not sure if this is strictly correct
1264 * but it saves the agony when the X server dies and the screen
1265 * remains blanked due to KD_GRAPHICS! It would be nice to do
1266 * this outside of VT_PROCESS but there is no single process
1267 * to account for and tracking tty count may be undesirable.
1269 reset_vc(fg_console);
1272 * Fall through to normal (VT_AUTO) handling of the switch...
1277 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1279 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
1280 return;
1282 complete_change_console(new_console);