ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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_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_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_kdgkb, int perm)
187 {
188         struct kbsentry *kbs;
189         char *p;
190         u_char *q;
191         int sz;
192         int delta;
193         char *first_free, *fj, *fnw;
194         int i, j, k;
195         int ret;
196
197         kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
198         if (!kbs) {
199                 ret = -ENOMEM;
200                 goto reterr;
201         }
202
203         /* we mostly copy too much here (512bytes), but who cares ;) */
204         if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
205                 ret = -EFAULT;
206                 goto reterr;
207         }
208         kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
209         i = kbs->kb_func;
210
211         switch (cmd) {
212         case KDGKBSENT:
213                 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
214                                                   a struct member */
215                 q = user_kdgkb->kb_string;
216                 p = func_table[i];
217                 if(p)
218                         for ( ; *p && sz; p++, sz--)
219                                 if (put_user(*p, q++)) {
220                                         ret = -EFAULT;
221                                         goto reterr;
222                                 }
223                 if (put_user('\0', q)) {
224                         ret = -EFAULT;
225                         goto reterr;
226                 }
227                 kfree(kbs);
228                 return ((p && *p) ? -EOVERFLOW : 0);
229         case KDSKBSENT:
230                 if (!perm) {
231                         ret = -EPERM;
232                         goto reterr;
233                 }
234
235                 q = func_table[i];
236                 first_free = funcbufptr + (funcbufsize - funcbufleft);
237                 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) 
238                         ;
239                 if (j < MAX_NR_FUNC)
240                         fj = func_table[j];
241                 else
242                         fj = first_free;
243
244                 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
245                 if (delta <= funcbufleft) {     /* it fits in current buf */
246                     if (j < MAX_NR_FUNC) {
247                         memmove(fj + delta, fj, first_free - fj);
248                         for (k = j; k < MAX_NR_FUNC; k++)
249                             if (func_table[k])
250                                 func_table[k] += delta;
251                     }
252                     if (!q)
253                       func_table[i] = fj;
254                     funcbufleft -= delta;
255                 } else {                        /* allocate a larger buffer */
256                     sz = 256;
257                     while (sz < funcbufsize - funcbufleft + delta)
258                       sz <<= 1;
259                     fnw = (char *) kmalloc(sz, GFP_KERNEL);
260                     if(!fnw) {
261                       ret = -ENOMEM;
262                       goto reterr;
263                     }
264
265                     if (!q)
266                       func_table[i] = fj;
267                     if (fj > funcbufptr)
268                         memmove(fnw, funcbufptr, fj - funcbufptr);
269                     for (k = 0; k < j; k++)
270                       if (func_table[k])
271                         func_table[k] = fnw + (func_table[k] - funcbufptr);
272
273                     if (first_free > fj) {
274                         memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
275                         for (k = j; k < MAX_NR_FUNC; k++)
276                           if (func_table[k])
277                             func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
278                     }
279                     if (funcbufptr != func_buf)
280                       kfree(funcbufptr);
281                     funcbufptr = fnw;
282                     funcbufleft = funcbufleft - delta + sz - funcbufsize;
283                     funcbufsize = sz;
284                 }
285                 strcpy(func_table[i], kbs->kb_string);
286                 break;
287         }
288         ret = 0;
289 reterr:
290         kfree(kbs);
291         return ret;
292 }
293
294 static inline int 
295 do_fontx_ioctl(int cmd, struct consolefontdesc *user_cfd, int perm, struct console_font_op *op)
296 {
297         struct consolefontdesc cfdarg;
298         int i;
299
300         if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) 
301                 return -EFAULT;
302         
303         switch (cmd) {
304         case PIO_FONTX:
305                 if (!perm)
306                         return -EPERM;
307                 op->op = KD_FONT_OP_SET;
308                 op->flags = KD_FONT_FLAG_OLD;
309                 op->width = 8;
310                 op->height = cfdarg.charheight;
311                 op->charcount = cfdarg.charcount;
312                 op->data = cfdarg.chardata;
313                 return con_font_op(fg_console, op);
314         case GIO_FONTX: {
315                 op->op = KD_FONT_OP_GET;
316                 op->flags = KD_FONT_FLAG_OLD;
317                 op->width = 8;
318                 op->height = cfdarg.charheight;
319                 op->charcount = cfdarg.charcount;
320                 op->data = cfdarg.chardata;
321                 i = con_font_op(fg_console, op);
322                 if (i)
323                         return i;
324                 cfdarg.charheight = op->height;
325                 cfdarg.charcount = op->charcount;
326                 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
327                         return -EFAULT;
328                 return 0;
329                 }
330         }
331         return -EINVAL;
332 }
333
334 static inline int 
335 do_unimap_ioctl(int cmd, struct unimapdesc *user_ud, int perm, unsigned int console)
336 {
337         struct unimapdesc tmp;
338         int i = 0; 
339
340         if (copy_from_user(&tmp, user_ud, sizeof tmp))
341                 return -EFAULT;
342         if (tmp.entries) {
343                 i = verify_area(VERIFY_WRITE, tmp.entries, 
344                                                 tmp.entry_ct*sizeof(struct unipair));
345                 if (i) return i;
346         }
347         switch (cmd) {
348         case PIO_UNIMAP:
349                 if (!perm)
350                         return -EPERM;
351                 return con_set_unimap(console, tmp.entry_ct, tmp.entries);
352         case GIO_UNIMAP:
353                 if (!perm && fg_console != console)
354                         return -EPERM;
355                 return con_get_unimap(console, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
356         }
357         return 0;
358 }
359
360 /*
361  * We handle the console-specific ioctl's here.  We allow the
362  * capability to modify any console, not just the fg_console. 
363  */
364 int vt_ioctl(struct tty_struct *tty, struct file * file,
365              unsigned int cmd, unsigned long arg)
366 {
367         struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
368         struct vc_data *vc = vc_cons[vt->vc_num].d;
369         struct console_font_op op;      /* used in multiple places here */
370         struct kbd_struct * kbd;
371         unsigned int console;
372         unsigned char ucval;
373         int i, perm;
374         
375         console = vt->vc_num;
376
377         if (!vc_cons_allocated(console))        /* impossible? */
378                 return -ENOIOCTLCMD;
379
380         /*
381          * To have permissions to do most of the vt ioctls, we either have
382          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
383          */
384         perm = 0;
385         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
386                 perm = 1;
387  
388         kbd = kbd_table + console;
389         switch (cmd) {
390         case KIOCSOUND:
391                 if (!perm)
392                         return -EPERM;
393                 if (arg)
394                         arg = 1193182 / arg;
395                 kd_mksound(arg, 0);
396                 return 0;
397
398         case KDMKTONE:
399                 if (!perm)
400                         return -EPERM;
401         {
402                 unsigned int ticks, count;
403                 
404                 /*
405                  * Generate the tone for the appropriate number of ticks.
406                  * If the time is zero, turn off sound ourselves.
407                  */
408                 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
409                 count = ticks ? (arg & 0xffff) : 0;
410                 if (count)
411                         count = 1193182 / count;
412                 kd_mksound(count, ticks);
413                 return 0;
414         }
415
416         case KDGKBTYPE:
417                 /*
418                  * this is naive.
419                  */
420                 ucval = keyboard_type;
421                 goto setchar;
422
423                 /*
424                  * These cannot be implemented on any machine that implements
425                  * ioperm() in user level (such as Alpha PCs) or not at all.
426                  *
427                  * XXX: you should never use these, just call ioperm directly..
428                  */
429 #ifdef CONFIG_X86
430         case KDADDIO:
431         case KDDELIO:
432                 /*
433                  * KDADDIO and KDDELIO may be able to add ports beyond what
434                  * we reject here, but to be safe...
435                  */
436                 if (arg < GPFIRST || arg > GPLAST)
437                         return -EINVAL;
438                 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
439
440         case KDENABIO:
441         case KDDISABIO:
442                 return sys_ioperm(GPFIRST, GPNUM,
443                                   (cmd == KDENABIO)) ? -ENXIO : 0;
444 #endif
445
446         /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
447                 
448         case KDKBDREP:
449         {
450                 struct kbd_repeat kbrep;
451                 int err;
452                 
453                 if (!capable(CAP_SYS_TTY_CONFIG))
454                         return -EPERM;
455
456                 if (copy_from_user(&kbrep, (void *)arg,
457                                    sizeof(struct kbd_repeat)))
458                         return -EFAULT;
459                 err = kbd_rate(&kbrep);
460                 if (err)
461                         return err;
462                 if (copy_to_user((void *)arg, &kbrep,
463                                  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 *)arg); 
569
570         case KDGETKEYCODE:
571         case KDSETKEYCODE:
572                 if(!capable(CAP_SYS_TTY_CONFIG))
573                         perm=0;
574                 return do_kbkeycode_ioctl(cmd, (struct kbkeycode *)arg, perm);
575
576         case KDGKBENT:
577         case KDSKBENT:
578                 return do_kdsk_ioctl(cmd, (struct kbentry *)arg, perm, kbd);
579
580         case KDGKBSENT:
581         case KDSKBSENT:
582                 return do_kdgkb_ioctl(cmd, (struct kbsentry *)arg, perm);
583
584         case KDGKBDIACR:
585         {
586                 struct kbdiacrs *a = (struct kbdiacrs *)arg;
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 *a = (struct kbdiacrs *)arg;
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*)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          * Probably init should be changed to do this (and have a
647          * field ks (`keyboard signal') in inittab describing the
648          * desired action), so that the number of background daemons
649          * does not increase.
650          */
651         case KDSIGACCEPT:
652         {
653                 extern int spawnpid, spawnsig;
654                 if (!perm || !capable(CAP_KILL))
655                   return -EPERM;
656                 if (arg < 1 || arg > _NSIG || arg == SIGKILL)
657                   return -EINVAL;
658                 spawnpid = current->pid;
659                 spawnsig = arg;
660                 return 0;
661         }
662
663         case VT_SETMODE:
664         {
665                 struct vt_mode tmp;
666
667                 if (!perm)
668                         return -EPERM;
669                 if (copy_from_user(&tmp, (void*)arg, sizeof(struct vt_mode)))
670                         return -EFAULT;
671                 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
672                         return -EINVAL;
673                 acquire_console_sem();
674                 vt_cons[console]->vt_mode = tmp;
675                 /* the frsig is ignored, so we set it to 0 */
676                 vt_cons[console]->vt_mode.frsig = 0;
677                 vt_cons[console]->vt_pid = current->pid;
678                 /* no switch is required -- saw@shade.msu.ru */
679                 vt_cons[console]->vt_newvt = -1; 
680                 release_console_sem();
681                 return 0;
682         }
683
684         case VT_GETMODE:
685         {
686                 struct vt_mode tmp;
687                 int rc;
688
689                 acquire_console_sem();
690                 memcpy(&tmp, &vt_cons[console]->vt_mode, sizeof(struct vt_mode));
691                 release_console_sem();
692
693                 rc = copy_to_user((void*)arg, &tmp, sizeof(struct vt_mode));
694                 return rc ? -EFAULT : 0;
695         }
696
697         /*
698          * Returns global vt state. Note that VT 0 is always open, since
699          * it's an alias for the current VT, and people can't use it here.
700          * We cannot return state for more than 16 VTs, since v_state is short.
701          */
702         case VT_GETSTATE:
703         {
704                 struct vt_stat *vtstat = (struct vt_stat *)arg;
705                 unsigned short state, mask;
706
707                 if (put_user(fg_console + 1, &vtstat->v_active))
708                         return -EFAULT;
709                 state = 1;      /* /dev/tty0 is always open */
710                 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
711                         if (VT_IS_IN_USE(i))
712                                 state |= mask;
713                 return put_user(state, &vtstat->v_state);
714         }
715
716         /*
717          * Returns the first available (non-opened) console.
718          */
719         case VT_OPENQRY:
720                 for (i = 0; i < MAX_NR_CONSOLES; ++i)
721                         if (! VT_IS_IN_USE(i))
722                                 break;
723                 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
724                 goto setint;             
725
726         /*
727          * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
728          * with num >= 1 (switches to vt 0, our console, are not allowed, just
729          * to preserve sanity).
730          */
731         case VT_ACTIVATE:
732                 if (!perm)
733                         return -EPERM;
734                 if (arg == 0 || arg > MAX_NR_CONSOLES)
735                         return -ENXIO;
736                 arg--;
737                 acquire_console_sem();
738                 i = vc_allocate(arg);
739                 release_console_sem();
740                 if (i)
741                         return i;
742                 set_console(arg);
743                 return 0;
744
745         /*
746          * wait until the specified VT has been activated
747          */
748         case VT_WAITACTIVE:
749                 if (!perm)
750                         return -EPERM;
751                 if (arg == 0 || arg > MAX_NR_CONSOLES)
752                         return -ENXIO;
753                 return vt_waitactive(arg-1);
754
755         /*
756          * If a vt is under process control, the kernel will not switch to it
757          * immediately, but postpone the operation until the process calls this
758          * ioctl, allowing the switch to complete.
759          *
760          * According to the X sources this is the behavior:
761          *      0:      pending switch-from not OK
762          *      1:      pending switch-from OK
763          *      2:      completed switch-to OK
764          */
765         case VT_RELDISP:
766                 if (!perm)
767                         return -EPERM;
768                 if (vt_cons[console]->vt_mode.mode != VT_PROCESS)
769                         return -EINVAL;
770
771                 /*
772                  * Switching-from response
773                  */
774                 if (vt_cons[console]->vt_newvt >= 0)
775                 {
776                         if (arg == 0)
777                                 /*
778                                  * Switch disallowed, so forget we were trying
779                                  * to do it.
780                                  */
781                                 vt_cons[console]->vt_newvt = -1;
782
783                         else
784                         {
785                                 /*
786                                  * The current vt has been released, so
787                                  * complete the switch.
788                                  */
789                                 int newvt;
790                                 acquire_console_sem();
791                                 newvt = vt_cons[console]->vt_newvt;
792                                 vt_cons[console]->vt_newvt = -1;
793                                 i = vc_allocate(newvt);
794                                 if (i) {
795                                         release_console_sem();
796                                         return i;
797                                 }
798                                 /*
799                                  * When we actually do the console switch,
800                                  * make sure we are atomic with respect to
801                                  * other console switches..
802                                  */
803                                 complete_change_console(newvt);
804                                 release_console_sem();
805                         }
806                 }
807
808                 /*
809                  * Switched-to response
810                  */
811                 else
812                 {
813                         /*
814                          * If it's just an ACK, ignore it
815                          */
816                         if (arg != VT_ACKACQ)
817                                 return -EINVAL;
818                 }
819
820                 return 0;
821
822          /*
823           * Disallocate memory associated to VT (but leave VT1)
824           */
825          case VT_DISALLOCATE:
826                 if (arg > MAX_NR_CONSOLES)
827                         return -ENXIO;
828                 if (arg == 0) {
829                     /* disallocate all unused consoles, but leave 0 */
830                         acquire_console_sem();
831                         for (i=1; i<MAX_NR_CONSOLES; i++)
832                                 if (! VT_BUSY(i))
833                                         vc_disallocate(i);
834                         release_console_sem();
835                 } else {
836                         /* disallocate a single console, if possible */
837                         arg--;
838                         if (VT_BUSY(arg))
839                                 return -EBUSY;
840                         if (arg) {                            /* leave 0 */
841                                 acquire_console_sem();
842                                 vc_disallocate(arg);
843                                 release_console_sem();
844                         }
845                 }
846                 return 0;
847
848         case VT_RESIZE:
849         {
850                 struct vt_sizes *vtsizes = (struct vt_sizes *) arg;
851                 ushort ll,cc;
852                 if (!perm)
853                         return -EPERM;
854                 if (get_user(ll, &vtsizes->v_rows) ||
855                     get_user(cc, &vtsizes->v_cols))
856                         return -EFAULT;
857                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
858                         acquire_console_sem();
859                         vc_resize(i, cc, ll);
860                         release_console_sem();
861                 }
862                 return 0;
863         }
864
865         case VT_RESIZEX:
866         {
867                 struct vt_consize *vtconsize = (struct vt_consize *) arg;
868                 ushort ll,cc,vlin,clin,vcol,ccol;
869                 if (!perm)
870                         return -EPERM;
871                 if (verify_area(VERIFY_READ, (void *)vtconsize,
872                                 sizeof(struct vt_consize)))
873                         return -EFAULT;
874                 __get_user(ll, &vtconsize->v_rows);
875                 __get_user(cc, &vtconsize->v_cols);
876                 __get_user(vlin, &vtconsize->v_vlin);
877                 __get_user(clin, &vtconsize->v_clin);
878                 __get_user(vcol, &vtconsize->v_vcol);
879                 __get_user(ccol, &vtconsize->v_ccol);
880                 vlin = vlin ? vlin : vc->vc_scan_lines;
881                 if (clin) {
882                         if (ll) {
883                                 if (ll != vlin/clin)
884                                         return -EINVAL; /* Parameters don't add up */
885                         } else 
886                                 ll = vlin/clin;
887                 }
888                 if (vcol && ccol) {
889                         if (cc) {
890                                 if (cc != vcol/ccol)
891                                         return -EINVAL;
892                         } else
893                                 cc = vcol/ccol;
894                 }
895
896                 if (clin > 32)
897                         return -EINVAL;
898                     
899                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
900                         if (!vc_cons[i].d)
901                                 continue;
902                         acquire_console_sem();
903                         if (vlin)
904                                 vc_cons[i].d->vc_scan_lines = vlin;
905                         if (clin)
906                                 vc_cons[i].d->vc_font.height = clin;
907                         vc_resize(i, cc, ll);
908                         release_console_sem();
909                 }
910                 return 0;
911         }
912
913         case PIO_FONT: {
914                 if (!perm)
915                         return -EPERM;
916                 op.op = KD_FONT_OP_SET;
917                 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
918                 op.width = 8;
919                 op.height = 0;
920                 op.charcount = 256;
921                 op.data = (char *) arg;
922                 return con_font_op(fg_console, &op);
923         }
924
925         case GIO_FONT: {
926                 op.op = KD_FONT_OP_GET;
927                 op.flags = KD_FONT_FLAG_OLD;
928                 op.width = 8;
929                 op.height = 32;
930                 op.charcount = 256;
931                 op.data = (char *) arg;
932                 return con_font_op(fg_console, &op);
933         }
934
935         case PIO_CMAP:
936                 if (!perm)
937                         return -EPERM;
938                 return con_set_cmap((char *)arg);
939
940         case GIO_CMAP:
941                 return con_get_cmap((char *)arg);
942
943         case PIO_FONTX:
944         case GIO_FONTX:
945                 return do_fontx_ioctl(cmd, (struct consolefontdesc *)arg, perm, &op);
946
947         case PIO_FONTRESET:
948         {
949                 if (!perm)
950                         return -EPERM;
951
952 #ifdef BROKEN_GRAPHICS_PROGRAMS
953                 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
954                    font is not saved. */
955                 return -ENOSYS;
956 #else
957                 {
958                 op.op = KD_FONT_OP_SET_DEFAULT;
959                 op.data = NULL;
960                 i = con_font_op(fg_console, &op);
961                 if (i) return i;
962                 con_set_default_unimap(fg_console);
963                 return 0;
964                 }
965 #endif
966         }
967
968         case KDFONTOP: {
969                 if (copy_from_user(&op, (void *) arg, sizeof(op)))
970                         return -EFAULT;
971                 if (!perm && op.op != KD_FONT_OP_GET)
972                         return -EPERM;
973                 i = con_font_op(console, &op);
974                 if (i) return i;
975                 if (copy_to_user((void *) arg, &op, sizeof(op)))
976                         return -EFAULT;
977                 return 0;
978         }
979
980         case PIO_SCRNMAP:
981                 if (!perm)
982                         return -EPERM;
983                 return con_set_trans_old((unsigned char *)arg);
984
985         case GIO_SCRNMAP:
986                 return con_get_trans_old((unsigned char *)arg);
987
988         case PIO_UNISCRNMAP:
989                 if (!perm)
990                         return -EPERM;
991                 return con_set_trans_new((unsigned short *)arg);
992
993         case GIO_UNISCRNMAP:
994                 return con_get_trans_new((unsigned short *)arg);
995
996         case PIO_UNIMAPCLR:
997               { struct unimapinit ui;
998                 if (!perm)
999                         return -EPERM;
1000                 i = copy_from_user(&ui, (void *)arg, sizeof(struct unimapinit));
1001                 if (i) return -EFAULT;
1002                 con_clear_unimap(console, &ui);
1003                 return 0;
1004               }
1005
1006         case PIO_UNIMAP:
1007         case GIO_UNIMAP:
1008                 return do_unimap_ioctl(cmd, (struct unimapdesc *)arg, perm, console);
1009
1010         case VT_LOCKSWITCH:
1011                 if (!capable(CAP_SYS_TTY_CONFIG))
1012                    return -EPERM;
1013                 vt_dont_switch = 1;
1014                 return 0;
1015         case VT_UNLOCKSWITCH:
1016                 if (!capable(CAP_SYS_TTY_CONFIG))
1017                    return -EPERM;
1018                 vt_dont_switch = 0;
1019                 return 0;
1020         default:
1021                 return -ENOIOCTLCMD;
1022         }
1023 }
1024
1025 /*
1026  * Sometimes we want to wait until a particular VT has been activated. We
1027  * do it in a very simple manner. Everybody waits on a single queue and
1028  * get woken up at once. Those that are satisfied go on with their business,
1029  * while those not ready go back to sleep. Seems overkill to add a wait
1030  * to each vt just for this - usually this does nothing!
1031  */
1032 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1033
1034 /*
1035  * Sleeps until a vt is activated, or the task is interrupted. Returns
1036  * 0 if activation, -EINTR if interrupted.
1037  */
1038 int vt_waitactive(int vt)
1039 {
1040         int retval;
1041         DECLARE_WAITQUEUE(wait, current);
1042
1043         add_wait_queue(&vt_activate_queue, &wait);
1044         for (;;) {
1045                 set_current_state(TASK_INTERRUPTIBLE);
1046                 retval = 0;
1047                 if (vt == fg_console)
1048                         break;
1049                 retval = -EINTR;
1050                 if (signal_pending(current))
1051                         break;
1052                 schedule();
1053         }
1054         remove_wait_queue(&vt_activate_queue, &wait);
1055         current->state = TASK_RUNNING;
1056         return retval;
1057 }
1058
1059 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1060
1061 void reset_vc(unsigned int new_console)
1062 {
1063         vt_cons[new_console]->vc_mode = KD_TEXT;
1064         kbd_table[new_console].kbdmode = VC_XLATE;
1065         vt_cons[new_console]->vt_mode.mode = VT_AUTO;
1066         vt_cons[new_console]->vt_mode.waitv = 0;
1067         vt_cons[new_console]->vt_mode.relsig = 0;
1068         vt_cons[new_console]->vt_mode.acqsig = 0;
1069         vt_cons[new_console]->vt_mode.frsig = 0;
1070         vt_cons[new_console]->vt_pid = -1;
1071         vt_cons[new_console]->vt_newvt = -1;
1072         if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
1073                 reset_palette(new_console) ;
1074 }
1075
1076 /*
1077  * Performs the back end of a vt switch
1078  */
1079 void complete_change_console(unsigned int new_console)
1080 {
1081         unsigned char old_vc_mode;
1082
1083         last_console = fg_console;
1084
1085         /*
1086          * If we're switching, we could be going from KD_GRAPHICS to
1087          * KD_TEXT mode or vice versa, which means we need to blank or
1088          * unblank the screen later.
1089          */
1090         old_vc_mode = vt_cons[fg_console]->vc_mode;
1091         switch_screen(new_console);
1092
1093         /*
1094          * This can't appear below a successful kill_proc().  If it did,
1095          * then the *blank_screen operation could occur while X, having
1096          * received acqsig, is waking up on another processor.  This
1097          * condition can lead to overlapping accesses to the VGA range
1098          * and the framebuffer (causing system lockups).
1099          *
1100          * To account for this we duplicate this code below only if the
1101          * controlling process is gone and we've called reset_vc.
1102          */
1103         if (old_vc_mode != vt_cons[new_console]->vc_mode)
1104         {
1105                 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1106                         do_unblank_screen(1);
1107                 else
1108                         do_blank_screen(1);
1109         }
1110
1111         /*
1112          * If this new console is under process control, send it a signal
1113          * telling it that it has acquired. Also check if it has died and
1114          * clean up (similar to logic employed in change_console())
1115          */
1116         if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
1117         {
1118                 /*
1119                  * Send the signal as privileged - kill_proc() will
1120                  * tell us if the process has gone or something else
1121                  * is awry
1122                  */
1123                 if (kill_proc(vt_cons[new_console]->vt_pid,
1124                               vt_cons[new_console]->vt_mode.acqsig,
1125                               1) != 0)
1126                 {
1127                 /*
1128                  * The controlling process has died, so we revert back to
1129                  * normal operation. In this case, we'll also change back
1130                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1131                  * but it saves the agony when the X server dies and the screen
1132                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1133                  * this outside of VT_PROCESS but there is no single process
1134                  * to account for and tracking tty count may be undesirable.
1135                  */
1136                         reset_vc(new_console);
1137
1138                         if (old_vc_mode != vt_cons[new_console]->vc_mode)
1139                         {
1140                                 if (vt_cons[new_console]->vc_mode == KD_TEXT)
1141                                         do_unblank_screen(1);
1142                                 else
1143                                         do_blank_screen(1);
1144                         }
1145                 }
1146         }
1147
1148         /*
1149          * Wake anyone waiting for their VT to activate
1150          */
1151         vt_wake_waitactive();
1152         return;
1153 }
1154
1155 /*
1156  * Performs the front-end of a vt switch
1157  */
1158 void change_console(unsigned int new_console)
1159 {
1160         if ((new_console == fg_console) || (vt_dont_switch))
1161                 return;
1162         if (!vc_cons_allocated(new_console))
1163                 return;
1164
1165         /*
1166          * If this vt is in process mode, then we need to handshake with
1167          * that process before switching. Essentially, we store where that
1168          * vt wants to switch to and wait for it to tell us when it's done
1169          * (via VT_RELDISP ioctl).
1170          *
1171          * We also check to see if the controlling process still exists.
1172          * If it doesn't, we reset this vt to auto mode and continue.
1173          * This is a cheap way to track process control. The worst thing
1174          * that can happen is: we send a signal to a process, it dies, and
1175          * the switch gets "lost" waiting for a response; hopefully, the
1176          * user will try again, we'll detect the process is gone (unless
1177          * the user waits just the right amount of time :-) and revert the
1178          * vt to auto control.
1179          */
1180         if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
1181         {
1182                 /*
1183                  * Send the signal as privileged - kill_proc() will
1184                  * tell us if the process has gone or something else
1185                  * is awry
1186                  */
1187                 if (kill_proc(vt_cons[fg_console]->vt_pid,
1188                               vt_cons[fg_console]->vt_mode.relsig,
1189                               1) == 0)
1190                 {
1191                         /*
1192                          * It worked. Mark the vt to switch to and
1193                          * return. The process needs to send us a
1194                          * VT_RELDISP ioctl to complete the switch.
1195                          */
1196                         vt_cons[fg_console]->vt_newvt = new_console;
1197                         return;
1198                 }
1199
1200                 /*
1201                  * The controlling process has died, so we revert back to
1202                  * normal operation. In this case, we'll also change back
1203                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1204                  * but it saves the agony when the X server dies and the screen
1205                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1206                  * this outside of VT_PROCESS but there is no single process
1207                  * to account for and tracking tty count may be undesirable.
1208                  */
1209                 reset_vc(fg_console);
1210
1211                 /*
1212                  * Fall through to normal (VT_AUTO) handling of the switch...
1213                  */
1214         }
1215
1216         /*
1217          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1218          */
1219         if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
1220                 return;
1221
1222         complete_change_console(new_console);
1223 }