patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / vt_ioctl.c
1 /*
2  *  linux/drivers/char/vt_ioctl.c
3  *
4  *  Copyright (C) 1992 obz under the linux copyright
5  *
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
11  */
12
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/tty.h>
18 #include <linux/timer.h>
19 #include <linux/kernel.h>
20 #include <linux/kd.h>
21 #include <linux/vt.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/major.h>
25 #include <linux/fs.h>
26 #include <linux/console.h>
27
28 #include <asm/io.h>
29 #include <asm/uaccess.h>
30
31 #include <linux/kbd_kern.h>
32 #include <linux/vt_kern.h>
33 #include <linux/kbd_diacr.h>
34 #include <linux/selection.h>
35
36 char vt_dont_switch;
37 extern struct tty_driver *console_driver;
38
39 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
40 #define VT_BUSY(i)      (VT_IS_IN_USE(i) || i == fg_console || i == sel_cons)
41
42 /*
43  * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
44  * experimentation and study of X386 SYSV handling.
45  *
46  * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
47  * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
48  * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
49  * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
50  * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
51  * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
52  * to the current console is done by the main ioctl code.
53  */
54
55 struct vt_struct *vt_cons[MAX_NR_CONSOLES];
56
57 /* Keyboard type: Default is KB_101, but can be set by machine
58  * specific code.
59  */
60 unsigned char keyboard_type = KB_101;
61
62 #ifdef CONFIG_X86
63 #include <linux/syscalls.h>
64 #endif
65
66 /*
67  * these are the valid i/o ports we're allowed to change. they map all the
68  * video ports
69  */
70 #define GPFIRST 0x3b4
71 #define GPLAST 0x3df
72 #define GPNUM (GPLAST - GPFIRST + 1)
73
74 #define i (tmp.kb_index)
75 #define s (tmp.kb_table)
76 #define v (tmp.kb_value)
77 static inline int
78 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
79 {
80         struct kbentry tmp;
81         ushort *key_map, val, ov;
82
83         if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
84                 return -EFAULT;
85
86         switch (cmd) {
87         case KDGKBENT:
88                 key_map = key_maps[s];
89                 if (key_map) {
90                     val = U(key_map[i]);
91                     if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
92                         val = K_HOLE;
93                 } else
94                     val = (i ? K_HOLE : K_NOSUCHMAP);
95                 return put_user(val, &user_kbe->kb_value);
96         case KDSKBENT:
97                 if (!perm)
98                         return -EPERM;
99                 if (!i && v == K_NOSUCHMAP) {
100                         /* disallocate map */
101                         key_map = key_maps[s];
102                         if (s && key_map) {
103                             key_maps[s] = 0;
104                             if (key_map[0] == U(K_ALLOCATED)) {
105                                         kfree(key_map);
106                                         keymap_count--;
107                             }
108                         }
109                         break;
110                 }
111
112                 if (KTYP(v) < NR_TYPES) {
113                     if (KVAL(v) > max_vals[KTYP(v)])
114                                 return -EINVAL;
115                 } else
116                     if (kbd->kbdmode != VC_UNICODE)
117                                 return -EINVAL;
118
119                 /* ++Geert: non-PC keyboards may generate keycode zero */
120 #if !defined(__mc68000__) && !defined(__powerpc__)
121                 /* assignment to entry 0 only tests validity of args */
122                 if (!i)
123                         break;
124 #endif
125
126                 if (!(key_map = key_maps[s])) {
127                         int j;
128
129                         if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
130                             !capable(CAP_SYS_RESOURCE))
131                                 return -EPERM;
132
133                         key_map = (ushort *) kmalloc(sizeof(plain_map),
134                                                      GFP_KERNEL);
135                         if (!key_map)
136                                 return -ENOMEM;
137                         key_maps[s] = key_map;
138                         key_map[0] = U(K_ALLOCATED);
139                         for (j = 1; j < NR_KEYS; j++)
140                                 key_map[j] = U(K_HOLE);
141                         keymap_count++;
142                 }
143                 ov = U(key_map[i]);
144                 if (v == ov)
145                         break;  /* nothing to do */
146                 /*
147                  * Attention Key.
148                  */
149                 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
150                         return -EPERM;
151                 key_map[i] = U(v);
152                 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
153                         compute_shiftstate();
154                 break;
155         }
156         return 0;
157 }
158 #undef i
159 #undef s
160 #undef v
161
162 static inline int 
163 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
164 {
165         struct kbkeycode tmp;
166         int kc = 0;
167
168         if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
169                 return -EFAULT;
170         switch (cmd) {
171         case KDGETKEYCODE:
172                 kc = getkeycode(tmp.scancode);
173                 if (kc >= 0)
174                         kc = put_user(kc, &user_kbkc->keycode);
175                 break;
176         case KDSETKEYCODE:
177                 if (!perm)
178                         return -EPERM;
179                 kc = setkeycode(tmp.scancode, tmp.keycode);
180                 break;
181         }
182         return kc;
183 }
184
185 static inline int
186 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
187 {
188         struct kbsentry *kbs;
189         char *p;
190         u_char *q;
191         u_char __user *up;
192         int sz;
193         int delta;
194         char *first_free, *fj, *fnw;
195         int i, j, k;
196         int ret;
197
198         kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
199         if (!kbs) {
200                 ret = -ENOMEM;
201                 goto reterr;
202         }
203
204         /* we mostly copy too much here (512bytes), but who cares ;) */
205         if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
206                 ret = -EFAULT;
207                 goto reterr;
208         }
209         kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
210         i = kbs->kb_func;
211
212         switch (cmd) {
213         case KDGKBSENT:
214                 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
215                                                   a struct member */
216                 up = user_kdgkb->kb_string;
217                 p = func_table[i];
218                 if(p)
219                         for ( ; *p && sz; p++, sz--)
220                                 if (put_user(*p, up++)) {
221                                         ret = -EFAULT;
222                                         goto reterr;
223                                 }
224                 if (put_user('\0', up)) {
225                         ret = -EFAULT;
226                         goto reterr;
227                 }
228                 kfree(kbs);
229                 return ((p && *p) ? -EOVERFLOW : 0);
230         case KDSKBSENT:
231                 if (!perm) {
232                         ret = -EPERM;
233                         goto reterr;
234                 }
235
236                 q = func_table[i];
237                 first_free = funcbufptr + (funcbufsize - funcbufleft);
238                 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) 
239                         ;
240                 if (j < MAX_NR_FUNC)
241                         fj = func_table[j];
242                 else
243                         fj = first_free;
244
245                 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
246                 if (delta <= funcbufleft) {     /* it fits in current buf */
247                     if (j < MAX_NR_FUNC) {
248                         memmove(fj + delta, fj, first_free - fj);
249                         for (k = j; k < MAX_NR_FUNC; k++)
250                             if (func_table[k])
251                                 func_table[k] += delta;
252                     }
253                     if (!q)
254                       func_table[i] = fj;
255                     funcbufleft -= delta;
256                 } else {                        /* allocate a larger buffer */
257                     sz = 256;
258                     while (sz < funcbufsize - funcbufleft + delta)
259                       sz <<= 1;
260                     fnw = (char *) kmalloc(sz, GFP_KERNEL);
261                     if(!fnw) {
262                       ret = -ENOMEM;
263                       goto reterr;
264                     }
265
266                     if (!q)
267                       func_table[i] = fj;
268                     if (fj > funcbufptr)
269                         memmove(fnw, funcbufptr, fj - funcbufptr);
270                     for (k = 0; k < j; k++)
271                       if (func_table[k])
272                         func_table[k] = fnw + (func_table[k] - funcbufptr);
273
274                     if (first_free > fj) {
275                         memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
276                         for (k = j; k < MAX_NR_FUNC; k++)
277                           if (func_table[k])
278                             func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
279                     }
280                     if (funcbufptr != func_buf)
281                       kfree(funcbufptr);
282                     funcbufptr = fnw;
283                     funcbufleft = funcbufleft - delta + sz - funcbufsize;
284                     funcbufsize = sz;
285                 }
286                 strcpy(func_table[i], kbs->kb_string);
287                 break;
288         }
289         ret = 0;
290 reterr:
291         kfree(kbs);
292         return ret;
293 }
294
295 static inline int 
296 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
297 {
298         struct consolefontdesc cfdarg;
299         int i;
300
301         if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) 
302                 return -EFAULT;
303         
304         switch (cmd) {
305         case PIO_FONTX:
306                 if (!perm)
307                         return -EPERM;
308                 op->op = KD_FONT_OP_SET;
309                 op->flags = KD_FONT_FLAG_OLD;
310                 op->width = 8;
311                 op->height = cfdarg.charheight;
312                 op->charcount = cfdarg.charcount;
313                 op->data = cfdarg.chardata;
314                 return con_font_op(fg_console, op);
315         case GIO_FONTX: {
316                 op->op = KD_FONT_OP_GET;
317                 op->flags = KD_FONT_FLAG_OLD;
318                 op->width = 8;
319                 op->height = cfdarg.charheight;
320                 op->charcount = cfdarg.charcount;
321                 op->data = cfdarg.chardata;
322                 i = con_font_op(fg_console, op);
323                 if (i)
324                         return i;
325                 cfdarg.charheight = op->height;
326                 cfdarg.charcount = op->charcount;
327                 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
328                         return -EFAULT;
329                 return 0;
330                 }
331         }
332         return -EINVAL;
333 }
334
335 static inline int 
336 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, unsigned int console)
337 {
338         struct unimapdesc tmp;
339         int i = 0; 
340
341         if (copy_from_user(&tmp, user_ud, sizeof tmp))
342                 return -EFAULT;
343         if (tmp.entries) {
344                 i = verify_area(VERIFY_WRITE, tmp.entries, 
345                                                 tmp.entry_ct*sizeof(struct unipair));
346                 if (i) return i;
347         }
348         switch (cmd) {
349         case PIO_UNIMAP:
350                 if (!perm)
351                         return -EPERM;
352                 return con_set_unimap(console, tmp.entry_ct, tmp.entries);
353         case GIO_UNIMAP:
354                 if (!perm && fg_console != console)
355                         return -EPERM;
356                 return con_get_unimap(console, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
357         }
358         return 0;
359 }
360
361 /*
362  * We handle the console-specific ioctl's here.  We allow the
363  * capability to modify any console, not just the fg_console. 
364  */
365 int vt_ioctl(struct tty_struct *tty, struct file * file,
366              unsigned int cmd, unsigned long arg)
367 {
368         struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
369         struct vc_data *vc = vc_cons[vt->vc_num].d;
370         struct console_font_op op;      /* used in multiple places here */
371         struct kbd_struct * kbd;
372         unsigned int console;
373         unsigned char ucval;
374         void __user *up = (void __user *)arg;
375         int i, perm;
376         
377         console = vt->vc_num;
378
379         if (!vc_cons_allocated(console))        /* impossible? */
380                 return -ENOIOCTLCMD;
381
382         /*
383          * To have permissions to do most of the vt ioctls, we either have
384          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
385          */
386         perm = 0;
387         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
388                 perm = 1;
389  
390         kbd = kbd_table + console;
391         switch (cmd) {
392         case KIOCSOUND:
393                 if (!perm)
394                         return -EPERM;
395                 if (arg)
396                         arg = 1193182 / arg;
397                 kd_mksound(arg, 0);
398                 return 0;
399
400         case KDMKTONE:
401                 if (!perm)
402                         return -EPERM;
403         {
404                 unsigned int ticks, count;
405                 
406                 /*
407                  * Generate the tone for the appropriate number of ticks.
408                  * If the time is zero, turn off sound ourselves.
409                  */
410                 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
411                 count = ticks ? (arg & 0xffff) : 0;
412                 if (count)
413                         count = 1193182 / count;
414                 kd_mksound(count, ticks);
415                 return 0;
416         }
417
418         case KDGKBTYPE:
419                 /*
420                  * this is naive.
421                  */
422                 ucval = keyboard_type;
423                 goto setchar;
424
425                 /*
426                  * These cannot be implemented on any machine that implements
427                  * ioperm() in user level (such as Alpha PCs) or not at all.
428                  *
429                  * XXX: you should never use these, just call ioperm directly..
430                  */
431 #ifdef CONFIG_X86
432         case KDADDIO:
433         case KDDELIO:
434                 /*
435                  * KDADDIO and KDDELIO may be able to add ports beyond what
436                  * we reject here, but to be safe...
437                  */
438                 if (arg < GPFIRST || arg > GPLAST)
439                         return -EINVAL;
440                 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
441
442         case KDENABIO:
443         case KDDISABIO:
444                 return sys_ioperm(GPFIRST, GPNUM,
445                                   (cmd == KDENABIO)) ? -ENXIO : 0;
446 #endif
447
448         /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
449                 
450         case KDKBDREP:
451         {
452                 struct kbd_repeat kbrep;
453                 int err;
454                 
455                 if (!capable(CAP_SYS_TTY_CONFIG))
456                         return -EPERM;
457
458                 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
459                         return -EFAULT;
460                 err = kbd_rate(&kbrep);
461                 if (err)
462                         return err;
463                 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
464                         return -EFAULT;
465                 return 0;
466         }
467
468         case KDSETMODE:
469                 /*
470                  * currently, setting the mode from KD_TEXT to KD_GRAPHICS
471                  * doesn't do a whole lot. i'm not sure if it should do any
472                  * restoration of modes or what...
473                  *
474                  * XXX It should at least call into the driver, fbdev's definitely
475                  * need to restore their engine state. --BenH
476                  */
477                 if (!perm)
478                         return -EPERM;
479                 switch (arg) {
480                 case KD_GRAPHICS:
481                         break;
482                 case KD_TEXT0:
483                 case KD_TEXT1:
484                         arg = KD_TEXT;
485                 case KD_TEXT:
486                         break;
487                 default:
488                         return -EINVAL;
489                 }
490                 if (vt_cons[console]->vc_mode == (unsigned char) arg)
491                         return 0;
492                 vt_cons[console]->vc_mode = (unsigned char) arg;
493                 if (console != fg_console)
494                         return 0;
495                 /*
496                  * explicitly blank/unblank the screen if switching modes
497                  */
498                 acquire_console_sem();
499                 if (arg == KD_TEXT)
500                         do_unblank_screen(1);
501                 else
502                         do_blank_screen(1);
503                 release_console_sem();
504                 return 0;
505
506         case KDGETMODE:
507                 ucval = vt_cons[console]->vc_mode;
508                 goto setint;
509
510         case KDMAPDISP:
511         case KDUNMAPDISP:
512                 /*
513                  * these work like a combination of mmap and KDENABIO.
514                  * this could be easily finished.
515                  */
516                 return -EINVAL;
517
518         case KDSKBMODE:
519                 if (!perm)
520                         return -EPERM;
521                 switch(arg) {
522                   case K_RAW:
523                         kbd->kbdmode = VC_RAW;
524                         break;
525                   case K_MEDIUMRAW:
526                         kbd->kbdmode = VC_MEDIUMRAW;
527                         break;
528                   case K_XLATE:
529                         kbd->kbdmode = VC_XLATE;
530                         compute_shiftstate();
531                         break;
532                   case K_UNICODE:
533                         kbd->kbdmode = VC_UNICODE;
534                         compute_shiftstate();
535                         break;
536                   default:
537                         return -EINVAL;
538                 }
539                 if (tty->ldisc.flush_buffer)
540                         tty->ldisc.flush_buffer(tty);
541                 return 0;
542
543         case KDGKBMODE:
544                 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
545                                  (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
546                                  (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
547                                  K_XLATE);
548                 goto setint;
549
550         /* this could be folded into KDSKBMODE, but for compatibility
551            reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
552         case KDSKBMETA:
553                 switch(arg) {
554                   case K_METABIT:
555                         clr_vc_kbd_mode(kbd, VC_META);
556                         break;
557                   case K_ESCPREFIX:
558                         set_vc_kbd_mode(kbd, VC_META);
559                         break;
560                   default:
561                         return -EINVAL;
562                 }
563                 return 0;
564
565         case KDGKBMETA:
566                 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
567         setint:
568                 return put_user(ucval, (int __user *)arg); 
569
570         case KDGETKEYCODE:
571         case KDSETKEYCODE:
572                 if(!capable(CAP_SYS_TTY_CONFIG))
573                         perm=0;
574                 return do_kbkeycode_ioctl(cmd, up, perm);
575
576         case KDGKBENT:
577         case KDSKBENT:
578                 return do_kdsk_ioctl(cmd, up, perm, kbd);
579
580         case KDGKBSENT:
581         case KDSKBSENT:
582                 return do_kdgkb_ioctl(cmd, up, perm);
583
584         case KDGKBDIACR:
585         {
586                 struct kbdiacrs __user *a = up;
587
588                 if (put_user(accent_table_size, &a->kb_cnt))
589                         return -EFAULT;
590                 if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))
591                         return -EFAULT;
592                 return 0;
593         }
594
595         case KDSKBDIACR:
596         {
597                 struct kbdiacrs __user *a = up;
598                 unsigned int ct;
599
600                 if (!perm)
601                         return -EPERM;
602                 if (get_user(ct,&a->kb_cnt))
603                         return -EFAULT;
604                 if (ct >= MAX_DIACR)
605                         return -EINVAL;
606                 accent_table_size = ct;
607                 if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))
608                         return -EFAULT;
609                 return 0;
610         }
611
612         /* the ioctls below read/set the flags usually shown in the leds */
613         /* don't use them - they will go away without warning */
614         case KDGKBLED:
615                 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
616                 goto setchar;
617
618         case KDSKBLED:
619                 if (!perm)
620                         return -EPERM;
621                 if (arg & ~0x77)
622                         return -EINVAL;
623                 kbd->ledflagstate = (arg & 7);
624                 kbd->default_ledflagstate = ((arg >> 4) & 7);
625                 set_leds();
626                 return 0;
627
628         /* the ioctls below only set the lights, not the functions */
629         /* for those, see KDGKBLED and KDSKBLED above */
630         case KDGETLED:
631                 ucval = getledstate();
632         setchar:
633                 return put_user(ucval, (char __user *)arg);
634
635         case KDSETLED:
636                 if (!perm)
637                   return -EPERM;
638                 setledstate(kbd, arg);
639                 return 0;
640
641         /*
642          * A process can indicate its willingness to accept signals
643          * generated by pressing an appropriate key combination.
644          * Thus, one can have a daemon that e.g. spawns a new console
645          * upon a keypress and then changes to it.
646          * See also the kbrequest field of inittab(5).
647          */
648         case KDSIGACCEPT:
649         {
650                 extern int spawnpid, spawnsig;
651                 if (!perm || !capable(CAP_KILL))
652                   return -EPERM;
653                 if (arg < 1 || arg > _NSIG || arg == SIGKILL)
654                   return -EINVAL;
655                 spawnpid = current->pid;
656                 spawnsig = arg;
657                 return 0;
658         }
659
660         case VT_SETMODE:
661         {
662                 struct vt_mode tmp;
663
664                 if (!perm)
665                         return -EPERM;
666                 if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
667                         return -EFAULT;
668                 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
669                         return -EINVAL;
670                 acquire_console_sem();
671                 vt_cons[console]->vt_mode = tmp;
672                 /* the frsig is ignored, so we set it to 0 */
673                 vt_cons[console]->vt_mode.frsig = 0;
674                 vt_cons[console]->vt_pid = current->pid;
675                 /* no switch is required -- saw@shade.msu.ru */
676                 vt_cons[console]->vt_newvt = -1; 
677                 release_console_sem();
678                 return 0;
679         }
680
681         case VT_GETMODE:
682         {
683                 struct vt_mode tmp;
684                 int rc;
685
686                 acquire_console_sem();
687                 memcpy(&tmp, &vt_cons[console]->vt_mode, sizeof(struct vt_mode));
688                 release_console_sem();
689
690                 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
691                 return rc ? -EFAULT : 0;
692         }
693
694         /*
695          * Returns global vt state. Note that VT 0 is always open, since
696          * it's an alias for the current VT, and people can't use it here.
697          * We cannot return state for more than 16 VTs, since v_state is short.
698          */
699         case VT_GETSTATE:
700         {
701                 struct vt_stat __user *vtstat = up;
702                 unsigned short state, mask;
703
704                 if (put_user(fg_console + 1, &vtstat->v_active))
705                         return -EFAULT;
706                 state = 1;      /* /dev/tty0 is always open */
707                 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
708                         if (VT_IS_IN_USE(i))
709                                 state |= mask;
710                 return put_user(state, &vtstat->v_state);
711         }
712
713         /*
714          * Returns the first available (non-opened) console.
715          */
716         case VT_OPENQRY:
717                 for (i = 0; i < MAX_NR_CONSOLES; ++i)
718                         if (! VT_IS_IN_USE(i))
719                                 break;
720                 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
721                 goto setint;             
722
723         /*
724          * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
725          * with num >= 1 (switches to vt 0, our console, are not allowed, just
726          * to preserve sanity).
727          */
728         case VT_ACTIVATE:
729                 if (!perm)
730                         return -EPERM;
731                 if (arg == 0 || arg > MAX_NR_CONSOLES)
732                         return -ENXIO;
733                 arg--;
734                 acquire_console_sem();
735                 i = vc_allocate(arg);
736                 release_console_sem();
737                 if (i)
738                         return i;
739                 set_console(arg);
740                 return 0;
741
742         /*
743          * wait until the specified VT has been activated
744          */
745         case VT_WAITACTIVE:
746                 if (!perm)
747                         return -EPERM;
748                 if (arg == 0 || arg > MAX_NR_CONSOLES)
749                         return -ENXIO;
750                 return vt_waitactive(arg-1);
751
752         /*
753          * If a vt is under process control, the kernel will not switch to it
754          * immediately, but postpone the operation until the process calls this
755          * ioctl, allowing the switch to complete.
756          *
757          * According to the X sources this is the behavior:
758          *      0:      pending switch-from not OK
759          *      1:      pending switch-from OK
760          *      2:      completed switch-to OK
761          */
762         case VT_RELDISP:
763                 if (!perm)
764                         return -EPERM;
765                 if (vt_cons[console]->vt_mode.mode != VT_PROCESS)
766                         return -EINVAL;
767
768                 /*
769                  * Switching-from response
770                  */
771                 if (vt_cons[console]->vt_newvt >= 0)
772                 {
773                         if (arg == 0)
774                                 /*
775                                  * Switch disallowed, so forget we were trying
776                                  * to do it.
777                                  */
778                                 vt_cons[console]->vt_newvt = -1;
779
780                         else
781                         {
782                                 /*
783                                  * The current vt has been released, so
784                                  * complete the switch.
785                                  */
786                                 int newvt;
787                                 acquire_console_sem();
788                                 newvt = vt_cons[console]->vt_newvt;
789                                 vt_cons[console]->vt_newvt = -1;
790                                 i = vc_allocate(newvt);
791                                 if (i) {
792                                         release_console_sem();
793                                         return i;
794                                 }
795                                 /*
796                                  * When we actually do the console switch,
797                                  * make sure we are atomic with respect to
798                                  * other console switches..
799                                  */
800                                 complete_change_console(newvt);
801                                 release_console_sem();
802                         }
803                 }
804
805                 /*
806                  * Switched-to response
807                  */
808                 else
809                 {
810                         /*
811                          * If it's just an ACK, ignore it
812                          */
813                         if (arg != VT_ACKACQ)
814                                 return -EINVAL;
815                 }
816
817                 return 0;
818
819          /*
820           * Disallocate memory associated to VT (but leave VT1)
821           */
822          case VT_DISALLOCATE:
823                 if (arg > MAX_NR_CONSOLES)
824                         return -ENXIO;
825                 if (arg == 0) {
826                     /* disallocate all unused consoles, but leave 0 */
827                         acquire_console_sem();
828                         for (i=1; i<MAX_NR_CONSOLES; i++)
829                                 if (! VT_BUSY(i))
830                                         vc_disallocate(i);
831                         release_console_sem();
832                 } else {
833                         /* disallocate a single console, if possible */
834                         arg--;
835                         if (VT_BUSY(arg))
836                                 return -EBUSY;
837                         if (arg) {                            /* leave 0 */
838                                 acquire_console_sem();
839                                 vc_disallocate(arg);
840                                 release_console_sem();
841                         }
842                 }
843                 return 0;
844
845         case VT_RESIZE:
846         {
847                 struct vt_sizes __user *vtsizes = up;
848                 ushort ll,cc;
849                 if (!perm)
850                         return -EPERM;
851                 if (get_user(ll, &vtsizes->v_rows) ||
852                     get_user(cc, &vtsizes->v_cols))
853                         return -EFAULT;
854                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
855                         acquire_console_sem();
856                         vc_resize(i, cc, ll);
857                         release_console_sem();
858                 }
859                 return 0;
860         }
861
862         case VT_RESIZEX:
863         {
864                 struct vt_consize __user *vtconsize = up;
865                 ushort ll,cc,vlin,clin,vcol,ccol;
866                 if (!perm)
867                         return -EPERM;
868                 if (verify_area(VERIFY_READ, vtconsize,
869                                 sizeof(struct vt_consize)))
870                         return -EFAULT;
871                 __get_user(ll, &vtconsize->v_rows);
872                 __get_user(cc, &vtconsize->v_cols);
873                 __get_user(vlin, &vtconsize->v_vlin);
874                 __get_user(clin, &vtconsize->v_clin);
875                 __get_user(vcol, &vtconsize->v_vcol);
876                 __get_user(ccol, &vtconsize->v_ccol);
877                 vlin = vlin ? vlin : vc->vc_scan_lines;
878                 if (clin) {
879                         if (ll) {
880                                 if (ll != vlin/clin)
881                                         return -EINVAL; /* Parameters don't add up */
882                         } else 
883                                 ll = vlin/clin;
884                 }
885                 if (vcol && ccol) {
886                         if (cc) {
887                                 if (cc != vcol/ccol)
888                                         return -EINVAL;
889                         } else
890                                 cc = vcol/ccol;
891                 }
892
893                 if (clin > 32)
894                         return -EINVAL;
895                     
896                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
897                         if (!vc_cons[i].d)
898                                 continue;
899                         acquire_console_sem();
900                         if (vlin)
901                                 vc_cons[i].d->vc_scan_lines = vlin;
902                         if (clin)
903                                 vc_cons[i].d->vc_font.height = clin;
904                         vc_resize(i, cc, ll);
905                         release_console_sem();
906                 }
907                 return 0;
908         }
909
910         case PIO_FONT: {
911                 if (!perm)
912                         return -EPERM;
913                 op.op = KD_FONT_OP_SET;
914                 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
915                 op.width = 8;
916                 op.height = 0;
917                 op.charcount = 256;
918                 op.data = (char *) arg;
919                 return con_font_op(fg_console, &op);
920         }
921
922         case GIO_FONT: {
923                 op.op = KD_FONT_OP_GET;
924                 op.flags = KD_FONT_FLAG_OLD;
925                 op.width = 8;
926                 op.height = 32;
927                 op.charcount = 256;
928                 op.data = (char *) arg;
929                 return con_font_op(fg_console, &op);
930         }
931
932         case PIO_CMAP:
933                 if (!perm)
934                         return -EPERM;
935                 return con_set_cmap(up);
936
937         case GIO_CMAP:
938                 return con_get_cmap(up);
939
940         case PIO_FONTX:
941         case GIO_FONTX:
942                 return do_fontx_ioctl(cmd, up, perm, &op);
943
944         case PIO_FONTRESET:
945         {
946                 if (!perm)
947                         return -EPERM;
948
949 #ifdef BROKEN_GRAPHICS_PROGRAMS
950                 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
951                    font is not saved. */
952                 return -ENOSYS;
953 #else
954                 {
955                 op.op = KD_FONT_OP_SET_DEFAULT;
956                 op.data = NULL;
957                 i = con_font_op(fg_console, &op);
958                 if (i) return i;
959                 con_set_default_unimap(fg_console);
960                 return 0;
961                 }
962 #endif
963         }
964
965         case KDFONTOP: {
966                 if (copy_from_user(&op, up, sizeof(op)))
967                         return -EFAULT;
968                 if (!perm && op.op != KD_FONT_OP_GET)
969                         return -EPERM;
970                 i = con_font_op(console, &op);
971                 if (i) return i;
972                 if (copy_to_user(up, &op, sizeof(op)))
973                         return -EFAULT;
974                 return 0;
975         }
976
977         case PIO_SCRNMAP:
978                 if (!perm)
979                         return -EPERM;
980                 return con_set_trans_old(up);
981
982         case GIO_SCRNMAP:
983                 return con_get_trans_old(up);
984
985         case PIO_UNISCRNMAP:
986                 if (!perm)
987                         return -EPERM;
988                 return con_set_trans_new(up);
989
990         case GIO_UNISCRNMAP:
991                 return con_get_trans_new(up);
992
993         case PIO_UNIMAPCLR:
994               { struct unimapinit ui;
995                 if (!perm)
996                         return -EPERM;
997                 i = copy_from_user(&ui, up, sizeof(struct unimapinit));
998                 if (i) return -EFAULT;
999                 con_clear_unimap(console, &ui);
1000                 return 0;
1001               }
1002
1003         case PIO_UNIMAP:
1004         case GIO_UNIMAP:
1005                 return do_unimap_ioctl(cmd, up, perm, console);
1006
1007         case VT_LOCKSWITCH:
1008                 if (!capable(CAP_SYS_TTY_CONFIG))
1009                    return -EPERM;
1010                 vt_dont_switch = 1;
1011                 return 0;
1012         case VT_UNLOCKSWITCH:
1013                 if (!capable(CAP_SYS_TTY_CONFIG))
1014                    return -EPERM;
1015                 vt_dont_switch = 0;
1016                 return 0;
1017         default:
1018                 return -ENOIOCTLCMD;
1019         }
1020 }
1021
1022 /*
1023  * Sometimes we want to wait until a particular VT has been activated. We
1024  * do it in a very simple manner. Everybody waits on a single queue and
1025  * get woken up at once. Those that are satisfied go on with their business,
1026  * while those not ready go back to sleep. Seems overkill to add a wait
1027  * to each vt just for this - usually this does nothing!
1028  */
1029 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1030
1031 /*
1032  * Sleeps until a vt is activated, or the task is interrupted. Returns
1033  * 0 if activation, -EINTR if interrupted.
1034  */
1035 int vt_waitactive(int vt)
1036 {
1037         int retval;
1038         DECLARE_WAITQUEUE(wait, current);
1039
1040         add_wait_queue(&vt_activate_queue, &wait);
1041         for (;;) {
1042                 set_current_state(TASK_INTERRUPTIBLE);
1043                 retval = 0;
1044                 if (vt == fg_console)
1045                         break;
1046                 retval = -EINTR;
1047                 if (signal_pending(current))
1048                         break;
1049                 schedule();
1050         }
1051         remove_wait_queue(&vt_activate_queue, &wait);
1052         current->state = TASK_RUNNING;
1053         return retval;
1054 }
1055
1056 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1057
1058 void reset_vc(unsigned int new_console)
1059 {
1060         vt_cons[new_console]->vc_mode = KD_TEXT;
1061         kbd_table[new_console].kbdmode = VC_XLATE;
1062         vt_cons[new_console]->vt_mode.mode = VT_AUTO;
1063         vt_cons[new_console]->vt_mode.waitv = 0;
1064         vt_cons[new_console]->vt_mode.relsig = 0;
1065         vt_cons[new_console]->vt_mode.acqsig = 0;
1066         vt_cons[new_console]->vt_mode.frsig = 0;
1067         vt_cons[new_console]->vt_pid = -1;
1068         vt_cons[new_console]->vt_newvt = -1;
1069         if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
1070                 reset_palette(new_console) ;
1071 }
1072
1073 /*
1074  * Performs the back end of a vt switch
1075  */
1076 void complete_change_console(unsigned int new_console)
1077 {
1078         unsigned char old_vc_mode;
1079
1080         last_console = fg_console;
1081
1082         /*
1083          * If we're switching, we could be going from KD_GRAPHICS to
1084          * KD_TEXT mode or vice versa, which means we need to blank or
1085          * unblank the screen later.
1086          */
1087         old_vc_mode = vt_cons[fg_console]->vc_mode;
1088         switch_screen(new_console);
1089
1090         /*
1091          * This can't appear below a successful kill_proc().  If it did,
1092          * then the *blank_screen operation could occur while X, having
1093          * received acqsig, is waking up on another processor.  This
1094          * condition can lead to overlapping accesses to the VGA range
1095          * and the framebuffer (causing system lockups).
1096          *
1097          * To account for this we duplicate this code below only if the
1098          * controlling process is gone and we've called reset_vc.
1099          */
1100         if (old_vc_mode != vt_cons[new_console]->vc_mode)
1101         {
1102                 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1103                         do_unblank_screen(1);
1104                 else
1105                         do_blank_screen(1);
1106         }
1107
1108         /*
1109          * If this new console is under process control, send it a signal
1110          * telling it that it has acquired. Also check if it has died and
1111          * clean up (similar to logic employed in change_console())
1112          */
1113         if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
1114         {
1115                 /*
1116                  * Send the signal as privileged - kill_proc() will
1117                  * tell us if the process has gone or something else
1118                  * is awry
1119                  */
1120                 if (kill_proc(vt_cons[new_console]->vt_pid,
1121                               vt_cons[new_console]->vt_mode.acqsig,
1122                               1) != 0)
1123                 {
1124                 /*
1125                  * The controlling process has died, so we revert back to
1126                  * normal operation. In this case, we'll also change back
1127                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1128                  * but it saves the agony when the X server dies and the screen
1129                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1130                  * this outside of VT_PROCESS but there is no single process
1131                  * to account for and tracking tty count may be undesirable.
1132                  */
1133                         reset_vc(new_console);
1134
1135                         if (old_vc_mode != vt_cons[new_console]->vc_mode)
1136                         {
1137                                 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1138                                         do_unblank_screen(1);
1139                                 else
1140                                         do_blank_screen(1);
1141                         }
1142                 }
1143         }
1144
1145         /*
1146          * Wake anyone waiting for their VT to activate
1147          */
1148         vt_wake_waitactive();
1149         return;
1150 }
1151
1152 /*
1153  * Performs the front-end of a vt switch
1154  */
1155 void change_console(unsigned int new_console)
1156 {
1157         if ((new_console == fg_console) || (vt_dont_switch))
1158                 return;
1159         if (!vc_cons_allocated(new_console))
1160                 return;
1161
1162         /*
1163          * If this vt is in process mode, then we need to handshake with
1164          * that process before switching. Essentially, we store where that
1165          * vt wants to switch to and wait for it to tell us when it's done
1166          * (via VT_RELDISP ioctl).
1167          *
1168          * We also check to see if the controlling process still exists.
1169          * If it doesn't, we reset this vt to auto mode and continue.
1170          * This is a cheap way to track process control. The worst thing
1171          * that can happen is: we send a signal to a process, it dies, and
1172          * the switch gets "lost" waiting for a response; hopefully, the
1173          * user will try again, we'll detect the process is gone (unless
1174          * the user waits just the right amount of time :-) and revert the
1175          * vt to auto control.
1176          */
1177         if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
1178         {
1179                 /*
1180                  * Send the signal as privileged - kill_proc() will
1181                  * tell us if the process has gone or something else
1182                  * is awry
1183                  */
1184                 if (kill_proc(vt_cons[fg_console]->vt_pid,
1185                               vt_cons[fg_console]->vt_mode.relsig,
1186                               1) == 0)
1187                 {
1188                         /*
1189                          * It worked. Mark the vt to switch to and
1190                          * return. The process needs to send us a
1191                          * VT_RELDISP ioctl to complete the switch.
1192                          */
1193                         vt_cons[fg_console]->vt_newvt = new_console;
1194                         return;
1195                 }
1196
1197                 /*
1198                  * The controlling process has died, so we revert back to
1199                  * normal operation. In this case, we'll also change back
1200                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1201                  * but it saves the agony when the X server dies and the screen
1202                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1203                  * this outside of VT_PROCESS but there is no single process
1204                  * to account for and tracking tty count may be undesirable.
1205                  */
1206                 reset_vc(fg_console);
1207
1208                 /*
1209                  * Fall through to normal (VT_AUTO) handling of the switch...
1210                  */
1211         }
1212
1213         /*
1214          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1215          */
1216         if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
1217                 return;
1218
1219         complete_change_console(new_console);
1220 }