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