a92cfffa7b5ed4ef9ea5c8584e1b14545c2c99bc
[linux-flexiantxendom0-natty.git] / drivers / tty / vt / 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/compat.h>
20 #include <linux/module.h>
21 #include <linux/kd.h>
22 #include <linux/vt.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/major.h>
26 #include <linux/fs.h>
27 #include <linux/console.h>
28 #include <linux/consolemap.h>
29 #include <linux/signal.h>
30 #include <linux/smp_lock.h>
31 #include <linux/timex.h>
32
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/kbd_kern.h>
37 #include <linux/vt_kern.h>
38 #include <linux/kbd_diacr.h>
39 #include <linux/selection.h>
40
41 #define max_font_size 65536
42
43 char vt_dont_switch;
44 extern struct tty_driver *console_driver;
45
46 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
47 #define VT_BUSY(i)      (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
48
49 /*
50  * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
51  * experimentation and study of X386 SYSV handling.
52  *
53  * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
54  * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
55  * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
56  * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
57  * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
58  * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
59  * to the current console is done by the main ioctl code.
60  */
61
62 #ifdef CONFIG_X86
63 #include <linux/syscalls.h>
64 #endif
65
66 static void complete_change_console(struct vc_data *vc);
67
68 /*
69  *      User space VT_EVENT handlers
70  */
71
72 struct vt_event_wait {
73         struct list_head list;
74         struct vt_event event;
75         int done;
76 };
77
78 static LIST_HEAD(vt_events);
79 static DEFINE_SPINLOCK(vt_event_lock);
80 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
81
82 /**
83  *      vt_event_post
84  *      @event: the event that occurred
85  *      @old: old console
86  *      @new: new console
87  *
88  *      Post an VT event to interested VT handlers
89  */
90
91 void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
92 {
93         struct list_head *pos, *head;
94         unsigned long flags;
95         int wake = 0;
96
97         spin_lock_irqsave(&vt_event_lock, flags);
98         head = &vt_events;
99
100         list_for_each(pos, head) {
101                 struct vt_event_wait *ve = list_entry(pos,
102                                                 struct vt_event_wait, list);
103                 if (!(ve->event.event & event))
104                         continue;
105                 ve->event.event = event;
106                 /* kernel view is consoles 0..n-1, user space view is
107                    console 1..n with 0 meaning current, so we must bias */
108                 ve->event.oldev = old + 1;
109                 ve->event.newev = new + 1;
110                 wake = 1;
111                 ve->done = 1;
112         }
113         spin_unlock_irqrestore(&vt_event_lock, flags);
114         if (wake)
115                 wake_up_interruptible(&vt_event_waitqueue);
116 }
117
118 /**
119  *      vt_event_wait           -       wait for an event
120  *      @vw: our event
121  *
122  *      Waits for an event to occur which completes our vt_event_wait
123  *      structure. On return the structure has wv->done set to 1 for success
124  *      or 0 if some event such as a signal ended the wait.
125  */
126
127 static void vt_event_wait(struct vt_event_wait *vw)
128 {
129         unsigned long flags;
130         /* Prepare the event */
131         INIT_LIST_HEAD(&vw->list);
132         vw->done = 0;
133         /* Queue our event */
134         spin_lock_irqsave(&vt_event_lock, flags);
135         list_add(&vw->list, &vt_events);
136         spin_unlock_irqrestore(&vt_event_lock, flags);
137         /* Wait for it to pass */
138         wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
139         /* Dequeue it */
140         spin_lock_irqsave(&vt_event_lock, flags);
141         list_del(&vw->list);
142         spin_unlock_irqrestore(&vt_event_lock, flags);
143 }
144
145 /**
146  *      vt_event_wait_ioctl     -       event ioctl handler
147  *      @arg: argument to ioctl
148  *
149  *      Implement the VT_WAITEVENT ioctl using the VT event interface
150  */
151
152 static int vt_event_wait_ioctl(struct vt_event __user *event)
153 {
154         struct vt_event_wait vw;
155
156         if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
157                 return -EFAULT;
158         /* Highest supported event for now */
159         if (vw.event.event & ~VT_MAX_EVENT)
160                 return -EINVAL;
161
162         vt_event_wait(&vw);
163         /* If it occurred report it */
164         if (vw.done) {
165                 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
166                         return -EFAULT;
167                 return 0;
168         }
169         return -EINTR;
170 }
171
172 /**
173  *      vt_waitactive   -       active console wait
174  *      @event: event code
175  *      @n: new console
176  *
177  *      Helper for event waits. Used to implement the legacy
178  *      event waiting ioctls in terms of events
179  */
180
181 int vt_waitactive(int n)
182 {
183         struct vt_event_wait vw;
184         do {
185                 if (n == fg_console + 1)
186                         break;
187                 vw.event.event = VT_EVENT_SWITCH;
188                 vt_event_wait(&vw);
189                 if (vw.done == 0)
190                         return -EINTR;
191         } while (vw.event.newev != n);
192         return 0;
193 }
194
195 /*
196  * these are the valid i/o ports we're allowed to change. they map all the
197  * video ports
198  */
199 #define GPFIRST 0x3b4
200 #define GPLAST 0x3df
201 #define GPNUM (GPLAST - GPFIRST + 1)
202
203 #define i (tmp.kb_index)
204 #define s (tmp.kb_table)
205 #define v (tmp.kb_value)
206 static inline int
207 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
208 {
209         struct kbentry tmp;
210         ushort *key_map, val, ov;
211
212         if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
213                 return -EFAULT;
214
215         if (!capable(CAP_SYS_TTY_CONFIG))
216                 perm = 0;
217
218         switch (cmd) {
219         case KDGKBENT:
220                 key_map = key_maps[s];
221                 if (key_map) {
222                     val = U(key_map[i]);
223                     if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
224                         val = K_HOLE;
225                 } else
226                     val = (i ? K_HOLE : K_NOSUCHMAP);
227                 return put_user(val, &user_kbe->kb_value);
228         case KDSKBENT:
229                 if (!perm)
230                         return -EPERM;
231                 if (!i && v == K_NOSUCHMAP) {
232                         /* deallocate map */
233                         key_map = key_maps[s];
234                         if (s && key_map) {
235                             key_maps[s] = NULL;
236                             if (key_map[0] == U(K_ALLOCATED)) {
237                                         kfree(key_map);
238                                         keymap_count--;
239                             }
240                         }
241                         break;
242                 }
243
244                 if (KTYP(v) < NR_TYPES) {
245                     if (KVAL(v) > max_vals[KTYP(v)])
246                                 return -EINVAL;
247                 } else
248                     if (kbd->kbdmode != VC_UNICODE)
249                                 return -EINVAL;
250
251                 /* ++Geert: non-PC keyboards may generate keycode zero */
252 #if !defined(__mc68000__) && !defined(__powerpc__)
253                 /* assignment to entry 0 only tests validity of args */
254                 if (!i)
255                         break;
256 #endif
257
258                 if (!(key_map = key_maps[s])) {
259                         int j;
260
261                         if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
262                             !capable(CAP_SYS_RESOURCE))
263                                 return -EPERM;
264
265                         key_map = kmalloc(sizeof(plain_map),
266                                                      GFP_KERNEL);
267                         if (!key_map)
268                                 return -ENOMEM;
269                         key_maps[s] = key_map;
270                         key_map[0] = U(K_ALLOCATED);
271                         for (j = 1; j < NR_KEYS; j++)
272                                 key_map[j] = U(K_HOLE);
273                         keymap_count++;
274                 }
275                 ov = U(key_map[i]);
276                 if (v == ov)
277                         break;  /* nothing to do */
278                 /*
279                  * Attention Key.
280                  */
281                 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
282                         return -EPERM;
283                 key_map[i] = U(v);
284                 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
285                         compute_shiftstate();
286                 break;
287         }
288         return 0;
289 }
290 #undef i
291 #undef s
292 #undef v
293
294 static inline int 
295 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
296 {
297         struct kbkeycode tmp;
298         int kc = 0;
299
300         if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
301                 return -EFAULT;
302         switch (cmd) {
303         case KDGETKEYCODE:
304                 kc = getkeycode(tmp.scancode);
305                 if (kc >= 0)
306                         kc = put_user(kc, &user_kbkc->keycode);
307                 break;
308         case KDSETKEYCODE:
309                 if (!perm)
310                         return -EPERM;
311                 kc = setkeycode(tmp.scancode, tmp.keycode);
312                 break;
313         }
314         return kc;
315 }
316
317 static inline int
318 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
319 {
320         struct kbsentry *kbs;
321         char *p;
322         u_char *q;
323         u_char __user *up;
324         int sz;
325         int delta;
326         char *first_free, *fj, *fnw;
327         int i, j, k;
328         int ret;
329
330         if (!capable(CAP_SYS_TTY_CONFIG))
331                 perm = 0;
332
333         kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
334         if (!kbs) {
335                 ret = -ENOMEM;
336                 goto reterr;
337         }
338
339         /* we mostly copy too much here (512bytes), but who cares ;) */
340         if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
341                 ret = -EFAULT;
342                 goto reterr;
343         }
344         kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
345         i = kbs->kb_func;
346
347         switch (cmd) {
348         case KDGKBSENT:
349                 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
350                                                   a struct member */
351                 up = user_kdgkb->kb_string;
352                 p = func_table[i];
353                 if(p)
354                         for ( ; *p && sz; p++, sz--)
355                                 if (put_user(*p, up++)) {
356                                         ret = -EFAULT;
357                                         goto reterr;
358                                 }
359                 if (put_user('\0', up)) {
360                         ret = -EFAULT;
361                         goto reterr;
362                 }
363                 kfree(kbs);
364                 return ((p && *p) ? -EOVERFLOW : 0);
365         case KDSKBSENT:
366                 if (!perm) {
367                         ret = -EPERM;
368                         goto reterr;
369                 }
370
371                 q = func_table[i];
372                 first_free = funcbufptr + (funcbufsize - funcbufleft);
373                 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) 
374                         ;
375                 if (j < MAX_NR_FUNC)
376                         fj = func_table[j];
377                 else
378                         fj = first_free;
379
380                 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
381                 if (delta <= funcbufleft) {     /* it fits in current buf */
382                     if (j < MAX_NR_FUNC) {
383                         memmove(fj + delta, fj, first_free - fj);
384                         for (k = j; k < MAX_NR_FUNC; k++)
385                             if (func_table[k])
386                                 func_table[k] += delta;
387                     }
388                     if (!q)
389                       func_table[i] = fj;
390                     funcbufleft -= delta;
391                 } else {                        /* allocate a larger buffer */
392                     sz = 256;
393                     while (sz < funcbufsize - funcbufleft + delta)
394                       sz <<= 1;
395                     fnw = kmalloc(sz, GFP_KERNEL);
396                     if(!fnw) {
397                       ret = -ENOMEM;
398                       goto reterr;
399                     }
400
401                     if (!q)
402                       func_table[i] = fj;
403                     if (fj > funcbufptr)
404                         memmove(fnw, funcbufptr, fj - funcbufptr);
405                     for (k = 0; k < j; k++)
406                       if (func_table[k])
407                         func_table[k] = fnw + (func_table[k] - funcbufptr);
408
409                     if (first_free > fj) {
410                         memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
411                         for (k = j; k < MAX_NR_FUNC; k++)
412                           if (func_table[k])
413                             func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
414                     }
415                     if (funcbufptr != func_buf)
416                       kfree(funcbufptr);
417                     funcbufptr = fnw;
418                     funcbufleft = funcbufleft - delta + sz - funcbufsize;
419                     funcbufsize = sz;
420                 }
421                 strcpy(func_table[i], kbs->kb_string);
422                 break;
423         }
424         ret = 0;
425 reterr:
426         kfree(kbs);
427         return ret;
428 }
429
430 static inline int 
431 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
432 {
433         struct consolefontdesc cfdarg;
434         int i;
435
436         if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) 
437                 return -EFAULT;
438         
439         switch (cmd) {
440         case PIO_FONTX:
441                 if (!perm)
442                         return -EPERM;
443                 op->op = KD_FONT_OP_SET;
444                 op->flags = KD_FONT_FLAG_OLD;
445                 op->width = 8;
446                 op->height = cfdarg.charheight;
447                 op->charcount = cfdarg.charcount;
448                 op->data = cfdarg.chardata;
449                 return con_font_op(vc_cons[fg_console].d, op);
450         case GIO_FONTX: {
451                 op->op = KD_FONT_OP_GET;
452                 op->flags = KD_FONT_FLAG_OLD;
453                 op->width = 8;
454                 op->height = cfdarg.charheight;
455                 op->charcount = cfdarg.charcount;
456                 op->data = cfdarg.chardata;
457                 i = con_font_op(vc_cons[fg_console].d, op);
458                 if (i)
459                         return i;
460                 cfdarg.charheight = op->height;
461                 cfdarg.charcount = op->charcount;
462                 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
463                         return -EFAULT;
464                 return 0;
465                 }
466         }
467         return -EINVAL;
468 }
469
470 static inline int 
471 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
472 {
473         struct unimapdesc tmp;
474
475         if (copy_from_user(&tmp, user_ud, sizeof tmp))
476                 return -EFAULT;
477         if (tmp.entries)
478                 if (!access_ok(VERIFY_WRITE, tmp.entries,
479                                 tmp.entry_ct*sizeof(struct unipair)))
480                         return -EFAULT;
481         switch (cmd) {
482         case PIO_UNIMAP:
483                 if (!perm)
484                         return -EPERM;
485                 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
486         case GIO_UNIMAP:
487                 if (!perm && fg_console != vc->vc_num)
488                         return -EPERM;
489                 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
490         }
491         return 0;
492 }
493
494
495
496 /*
497  * We handle the console-specific ioctl's here.  We allow the
498  * capability to modify any console, not just the fg_console. 
499  */
500 int vt_ioctl(struct tty_struct *tty, struct file * file,
501              unsigned int cmd, unsigned long arg)
502 {
503         struct vc_data *vc = tty->driver_data;
504         struct console_font_op op;      /* used in multiple places here */
505         struct kbd_struct * kbd;
506         unsigned int console;
507         unsigned char ucval;
508         unsigned int uival;
509         void __user *up = (void __user *)arg;
510         int i, perm;
511         int ret = 0;
512
513         console = vc->vc_num;
514
515         tty_lock();
516
517         if (!vc_cons_allocated(console)) {      /* impossible? */
518                 ret = -ENOIOCTLCMD;
519                 goto out;
520         }
521
522
523         /*
524          * To have permissions to do most of the vt ioctls, we either have
525          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
526          */
527         perm = 0;
528         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
529                 perm = 1;
530  
531         kbd = kbd_table + console;
532         switch (cmd) {
533         case TIOCLINUX:
534                 ret = tioclinux(tty, arg);
535                 break;
536         case KIOCSOUND:
537                 if (!perm)
538                         goto eperm;
539                 /*
540                  * The use of PIT_TICK_RATE is historic, it used to be
541                  * the platform-dependent CLOCK_TICK_RATE between 2.6.12
542                  * and 2.6.36, which was a minor but unfortunate ABI
543                  * change.
544                  */
545                 if (arg)
546                         arg = PIT_TICK_RATE / arg;
547                 kd_mksound(arg, 0);
548                 break;
549
550         case KDMKTONE:
551                 if (!perm)
552                         goto eperm;
553         {
554                 unsigned int ticks, count;
555                 
556                 /*
557                  * Generate the tone for the appropriate number of ticks.
558                  * If the time is zero, turn off sound ourselves.
559                  */
560                 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
561                 count = ticks ? (arg & 0xffff) : 0;
562                 if (count)
563                         count = PIT_TICK_RATE / count;
564                 kd_mksound(count, ticks);
565                 break;
566         }
567
568         case KDGKBTYPE:
569                 /*
570                  * this is naive.
571                  */
572                 ucval = KB_101;
573                 goto setchar;
574
575                 /*
576                  * These cannot be implemented on any machine that implements
577                  * ioperm() in user level (such as Alpha PCs) or not at all.
578                  *
579                  * XXX: you should never use these, just call ioperm directly..
580                  */
581 #ifdef CONFIG_X86
582         case KDADDIO:
583         case KDDELIO:
584                 /*
585                  * KDADDIO and KDDELIO may be able to add ports beyond what
586                  * we reject here, but to be safe...
587                  */
588                 if (arg < GPFIRST || arg > GPLAST) {
589                         ret = -EINVAL;
590                         break;
591                 }
592                 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
593                 break;
594
595         case KDENABIO:
596         case KDDISABIO:
597                 ret = sys_ioperm(GPFIRST, GPNUM,
598                                   (cmd == KDENABIO)) ? -ENXIO : 0;
599                 break;
600 #endif
601
602         /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
603                 
604         case KDKBDREP:
605         {
606                 struct kbd_repeat kbrep;
607                 
608                 if (!capable(CAP_SYS_TTY_CONFIG))
609                         goto eperm;
610
611                 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
612                         ret =  -EFAULT;
613                         break;
614                 }
615                 ret = kbd_rate(&kbrep);
616                 if (ret)
617                         break;
618                 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
619                         ret = -EFAULT;
620                 break;
621         }
622
623         case KDSETMODE:
624                 /*
625                  * currently, setting the mode from KD_TEXT to KD_GRAPHICS
626                  * doesn't do a whole lot. i'm not sure if it should do any
627                  * restoration of modes or what...
628                  *
629                  * XXX It should at least call into the driver, fbdev's definitely
630                  * need to restore their engine state. --BenH
631                  */
632                 if (!perm)
633                         goto eperm;
634                 switch (arg) {
635                 case KD_GRAPHICS:
636                         break;
637                 case KD_TEXT0:
638                 case KD_TEXT1:
639                         arg = KD_TEXT;
640                 case KD_TEXT:
641                         break;
642                 default:
643                         ret = -EINVAL;
644                         goto out;
645                 }
646                 if (vc->vc_mode == (unsigned char) arg)
647                         break;
648                 vc->vc_mode = (unsigned char) arg;
649                 if (console != fg_console)
650                         break;
651                 /*
652                  * explicitly blank/unblank the screen if switching modes
653                  */
654                 console_lock();
655                 if (arg == KD_TEXT)
656                         do_unblank_screen(1);
657                 else
658                         do_blank_screen(1);
659                 console_unlock();
660                 break;
661
662         case KDGETMODE:
663                 uival = vc->vc_mode;
664                 goto setint;
665
666         case KDMAPDISP:
667         case KDUNMAPDISP:
668                 /*
669                  * these work like a combination of mmap and KDENABIO.
670                  * this could be easily finished.
671                  */
672                 ret = -EINVAL;
673                 break;
674
675         case KDSKBMODE:
676                 if (!perm)
677                         goto eperm;
678                 switch(arg) {
679                   case K_RAW:
680                         kbd->kbdmode = VC_RAW;
681                         break;
682                   case K_MEDIUMRAW:
683                         kbd->kbdmode = VC_MEDIUMRAW;
684                         break;
685                   case K_XLATE:
686                         kbd->kbdmode = VC_XLATE;
687                         compute_shiftstate();
688                         break;
689                   case K_UNICODE:
690                         kbd->kbdmode = VC_UNICODE;
691                         compute_shiftstate();
692                         break;
693                   default:
694                         ret = -EINVAL;
695                         goto out;
696                 }
697                 tty_ldisc_flush(tty);
698                 break;
699
700         case KDGKBMODE:
701                 uival = ((kbd->kbdmode == VC_RAW) ? K_RAW :
702                                  (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
703                                  (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
704                                  K_XLATE);
705                 goto setint;
706
707         /* this could be folded into KDSKBMODE, but for compatibility
708            reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
709         case KDSKBMETA:
710                 switch(arg) {
711                   case K_METABIT:
712                         clr_vc_kbd_mode(kbd, VC_META);
713                         break;
714                   case K_ESCPREFIX:
715                         set_vc_kbd_mode(kbd, VC_META);
716                         break;
717                   default:
718                         ret = -EINVAL;
719                 }
720                 break;
721
722         case KDGKBMETA:
723                 uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
724         setint:
725                 ret = put_user(uival, (int __user *)arg);
726                 break;
727
728         case KDGETKEYCODE:
729         case KDSETKEYCODE:
730                 if(!capable(CAP_SYS_TTY_CONFIG))
731                         perm = 0;
732                 ret = do_kbkeycode_ioctl(cmd, up, perm);
733                 break;
734
735         case KDGKBENT:
736         case KDSKBENT:
737                 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
738                 break;
739
740         case KDGKBSENT:
741         case KDSKBSENT:
742                 ret = do_kdgkb_ioctl(cmd, up, perm);
743                 break;
744
745         case KDGKBDIACR:
746         {
747                 struct kbdiacrs __user *a = up;
748                 struct kbdiacr diacr;
749                 int i;
750
751                 if (put_user(accent_table_size, &a->kb_cnt)) {
752                         ret = -EFAULT;
753                         break;
754                 }
755                 for (i = 0; i < accent_table_size; i++) {
756                         diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
757                         diacr.base = conv_uni_to_8bit(accent_table[i].base);
758                         diacr.result = conv_uni_to_8bit(accent_table[i].result);
759                         if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
760                                 ret = -EFAULT;
761                                 break;
762                         }
763                 }
764                 break;
765         }
766         case KDGKBDIACRUC:
767         {
768                 struct kbdiacrsuc __user *a = up;
769
770                 if (put_user(accent_table_size, &a->kb_cnt))
771                         ret = -EFAULT;
772                 else if (copy_to_user(a->kbdiacruc, accent_table,
773                                 accent_table_size*sizeof(struct kbdiacruc)))
774                         ret = -EFAULT;
775                 break;
776         }
777
778         case KDSKBDIACR:
779         {
780                 struct kbdiacrs __user *a = up;
781                 struct kbdiacr diacr;
782                 unsigned int ct;
783                 int i;
784
785                 if (!perm)
786                         goto eperm;
787                 if (get_user(ct,&a->kb_cnt)) {
788                         ret = -EFAULT;
789                         break;
790                 }
791                 if (ct >= MAX_DIACR) {
792                         ret = -EINVAL;
793                         break;
794                 }
795                 accent_table_size = ct;
796                 for (i = 0; i < ct; i++) {
797                         if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
798                                 ret = -EFAULT;
799                                 break;
800                         }
801                         accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
802                         accent_table[i].base = conv_8bit_to_uni(diacr.base);
803                         accent_table[i].result = conv_8bit_to_uni(diacr.result);
804                 }
805                 break;
806         }
807
808         case KDSKBDIACRUC:
809         {
810                 struct kbdiacrsuc __user *a = up;
811                 unsigned int ct;
812
813                 if (!perm)
814                         goto eperm;
815                 if (get_user(ct,&a->kb_cnt)) {
816                         ret = -EFAULT;
817                         break;
818                 }
819                 if (ct >= MAX_DIACR) {
820                         ret = -EINVAL;
821                         break;
822                 }
823                 accent_table_size = ct;
824                 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
825                         ret = -EFAULT;
826                 break;
827         }
828
829         /* the ioctls below read/set the flags usually shown in the leds */
830         /* don't use them - they will go away without warning */
831         case KDGKBLED:
832                 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
833                 goto setchar;
834
835         case KDSKBLED:
836                 if (!perm)
837                         goto eperm;
838                 if (arg & ~0x77) {
839                         ret = -EINVAL;
840                         break;
841                 }
842                 kbd->ledflagstate = (arg & 7);
843                 kbd->default_ledflagstate = ((arg >> 4) & 7);
844                 set_leds();
845                 break;
846
847         /* the ioctls below only set the lights, not the functions */
848         /* for those, see KDGKBLED and KDSKBLED above */
849         case KDGETLED:
850                 ucval = getledstate();
851         setchar:
852                 ret = put_user(ucval, (char __user *)arg);
853                 break;
854
855         case KDSETLED:
856                 if (!perm)
857                         goto eperm;
858                 setledstate(kbd, arg);
859                 break;
860
861         /*
862          * A process can indicate its willingness to accept signals
863          * generated by pressing an appropriate key combination.
864          * Thus, one can have a daemon that e.g. spawns a new console
865          * upon a keypress and then changes to it.
866          * See also the kbrequest field of inittab(5).
867          */
868         case KDSIGACCEPT:
869         {
870                 if (!perm || !capable(CAP_KILL))
871                         goto eperm;
872                 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
873                         ret = -EINVAL;
874                 else {
875                         spin_lock_irq(&vt_spawn_con.lock);
876                         put_pid(vt_spawn_con.pid);
877                         vt_spawn_con.pid = get_pid(task_pid(current));
878                         vt_spawn_con.sig = arg;
879                         spin_unlock_irq(&vt_spawn_con.lock);
880                 }
881                 break;
882         }
883
884         case VT_SETMODE:
885         {
886                 struct vt_mode tmp;
887
888                 if (!perm)
889                         goto eperm;
890                 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
891                         ret = -EFAULT;
892                         goto out;
893                 }
894                 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
895                         ret = -EINVAL;
896                         goto out;
897                 }
898                 console_lock();
899                 vc->vt_mode = tmp;
900                 /* the frsig is ignored, so we set it to 0 */
901                 vc->vt_mode.frsig = 0;
902                 put_pid(vc->vt_pid);
903                 vc->vt_pid = get_pid(task_pid(current));
904                 /* no switch is required -- saw@shade.msu.ru */
905                 vc->vt_newvt = -1;
906                 console_unlock();
907                 break;
908         }
909
910         case VT_GETMODE:
911         {
912                 struct vt_mode tmp;
913                 int rc;
914
915                 console_lock();
916                 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
917                 console_unlock();
918
919                 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
920                 if (rc)
921                         ret = -EFAULT;
922                 break;
923         }
924
925         /*
926          * Returns global vt state. Note that VT 0 is always open, since
927          * it's an alias for the current VT, and people can't use it here.
928          * We cannot return state for more than 16 VTs, since v_state is short.
929          */
930         case VT_GETSTATE:
931         {
932                 struct vt_stat __user *vtstat = up;
933                 unsigned short state, mask;
934
935                 if (put_user(fg_console + 1, &vtstat->v_active))
936                         ret = -EFAULT;
937                 else {
938                         state = 1;      /* /dev/tty0 is always open */
939                         for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
940                                                         ++i, mask <<= 1)
941                                 if (VT_IS_IN_USE(i))
942                                         state |= mask;
943                         ret = put_user(state, &vtstat->v_state);
944                 }
945                 break;
946         }
947
948         /*
949          * Returns the first available (non-opened) console.
950          */
951         case VT_OPENQRY:
952                 for (i = 0; i < MAX_NR_CONSOLES; ++i)
953                         if (! VT_IS_IN_USE(i))
954                                 break;
955                 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
956                 goto setint;             
957
958         /*
959          * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
960          * with num >= 1 (switches to vt 0, our console, are not allowed, just
961          * to preserve sanity).
962          */
963         case VT_ACTIVATE:
964                 if (!perm)
965                         goto eperm;
966                 if (arg == 0 || arg > MAX_NR_CONSOLES)
967                         ret =  -ENXIO;
968                 else {
969                         arg--;
970                         console_lock();
971                         ret = vc_allocate(arg);
972                         console_unlock();
973                         if (ret)
974                                 break;
975                         set_console(arg);
976                 }
977                 break;
978
979         case VT_SETACTIVATE:
980         {
981                 struct vt_setactivate vsa;
982
983                 if (!perm)
984                         goto eperm;
985
986                 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
987                                         sizeof(struct vt_setactivate))) {
988                         ret = -EFAULT;
989                         goto out;
990                 }
991                 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
992                         ret = -ENXIO;
993                 else {
994                         vsa.console--;
995                         console_lock();
996                         ret = vc_allocate(vsa.console);
997                         if (ret == 0) {
998                                 struct vc_data *nvc;
999                                 /* This is safe providing we don't drop the
1000                                    console sem between vc_allocate and
1001                                    finishing referencing nvc */
1002                                 nvc = vc_cons[vsa.console].d;
1003                                 nvc->vt_mode = vsa.mode;
1004                                 nvc->vt_mode.frsig = 0;
1005                                 put_pid(nvc->vt_pid);
1006                                 nvc->vt_pid = get_pid(task_pid(current));
1007                         }
1008                         console_unlock();
1009                         if (ret)
1010                                 break;
1011                         /* Commence switch and lock */
1012                         set_console(arg);
1013                 }
1014         }
1015
1016         /*
1017          * wait until the specified VT has been activated
1018          */
1019         case VT_WAITACTIVE:
1020                 if (!perm)
1021                         goto eperm;
1022                 if (arg == 0 || arg > MAX_NR_CONSOLES)
1023                         ret = -ENXIO;
1024                 else
1025                         ret = vt_waitactive(arg);
1026                 break;
1027
1028         /*
1029          * If a vt is under process control, the kernel will not switch to it
1030          * immediately, but postpone the operation until the process calls this
1031          * ioctl, allowing the switch to complete.
1032          *
1033          * According to the X sources this is the behavior:
1034          *      0:      pending switch-from not OK
1035          *      1:      pending switch-from OK
1036          *      2:      completed switch-to OK
1037          */
1038         case VT_RELDISP:
1039                 if (!perm)
1040                         goto eperm;
1041
1042                 if (vc->vt_mode.mode != VT_PROCESS) {
1043                         ret = -EINVAL;
1044                         break;
1045                 }
1046                 /*
1047                  * Switching-from response
1048                  */
1049                 console_lock();
1050                 if (vc->vt_newvt >= 0) {
1051                         if (arg == 0)
1052                                 /*
1053                                  * Switch disallowed, so forget we were trying
1054                                  * to do it.
1055                                  */
1056                                 vc->vt_newvt = -1;
1057
1058                         else {
1059                                 /*
1060                                  * The current vt has been released, so
1061                                  * complete the switch.
1062                                  */
1063                                 int newvt;
1064                                 newvt = vc->vt_newvt;
1065                                 vc->vt_newvt = -1;
1066                                 ret = vc_allocate(newvt);
1067                                 if (ret) {
1068                                         console_unlock();
1069                                         break;
1070                                 }
1071                                 /*
1072                                  * When we actually do the console switch,
1073                                  * make sure we are atomic with respect to
1074                                  * other console switches..
1075                                  */
1076                                 complete_change_console(vc_cons[newvt].d);
1077                         }
1078                 } else {
1079                         /*
1080                          * Switched-to response
1081                          */
1082                         /*
1083                          * If it's just an ACK, ignore it
1084                          */
1085                         if (arg != VT_ACKACQ)
1086                                 ret = -EINVAL;
1087                 }
1088                 console_unlock();
1089                 break;
1090
1091          /*
1092           * Disallocate memory associated to VT (but leave VT1)
1093           */
1094          case VT_DISALLOCATE:
1095                 if (arg > MAX_NR_CONSOLES) {
1096                         ret = -ENXIO;
1097                         break;
1098                 }
1099                 if (arg == 0) {
1100                     /* deallocate all unused consoles, but leave 0 */
1101                         console_lock();
1102                         for (i=1; i<MAX_NR_CONSOLES; i++)
1103                                 if (! VT_BUSY(i))
1104                                         vc_deallocate(i);
1105                         console_unlock();
1106                 } else {
1107                         /* deallocate a single console, if possible */
1108                         arg--;
1109                         if (VT_BUSY(arg))
1110                                 ret = -EBUSY;
1111                         else if (arg) {                       /* leave 0 */
1112                                 console_lock();
1113                                 vc_deallocate(arg);
1114                                 console_unlock();
1115                         }
1116                 }
1117                 break;
1118
1119         case VT_RESIZE:
1120         {
1121                 struct vt_sizes __user *vtsizes = up;
1122                 struct vc_data *vc;
1123
1124                 ushort ll,cc;
1125                 if (!perm)
1126                         goto eperm;
1127                 if (get_user(ll, &vtsizes->v_rows) ||
1128                     get_user(cc, &vtsizes->v_cols))
1129                         ret = -EFAULT;
1130                 else {
1131                         console_lock();
1132                         for (i = 0; i < MAX_NR_CONSOLES; i++) {
1133                                 vc = vc_cons[i].d;
1134
1135                                 if (vc) {
1136                                         vc->vc_resize_user = 1;
1137                                         vc_resize(vc_cons[i].d, cc, ll);
1138                                 }
1139                         }
1140                         console_unlock();
1141                 }
1142                 break;
1143         }
1144
1145         case VT_RESIZEX:
1146         {
1147                 struct vt_consize __user *vtconsize = up;
1148                 ushort ll,cc,vlin,clin,vcol,ccol;
1149                 if (!perm)
1150                         goto eperm;
1151                 if (!access_ok(VERIFY_READ, vtconsize,
1152                                 sizeof(struct vt_consize))) {
1153                         ret = -EFAULT;
1154                         break;
1155                 }
1156                 /* FIXME: Should check the copies properly */
1157                 __get_user(ll, &vtconsize->v_rows);
1158                 __get_user(cc, &vtconsize->v_cols);
1159                 __get_user(vlin, &vtconsize->v_vlin);
1160                 __get_user(clin, &vtconsize->v_clin);
1161                 __get_user(vcol, &vtconsize->v_vcol);
1162                 __get_user(ccol, &vtconsize->v_ccol);
1163                 vlin = vlin ? vlin : vc->vc_scan_lines;
1164                 if (clin) {
1165                         if (ll) {
1166                                 if (ll != vlin/clin) {
1167                                         /* Parameters don't add up */
1168                                         ret = -EINVAL;
1169                                         break;
1170                                 }
1171                         } else 
1172                                 ll = vlin/clin;
1173                 }
1174                 if (vcol && ccol) {
1175                         if (cc) {
1176                                 if (cc != vcol/ccol) {
1177                                         ret = -EINVAL;
1178                                         break;
1179                                 }
1180                         } else
1181                                 cc = vcol/ccol;
1182                 }
1183
1184                 if (clin > 32) {
1185                         ret =  -EINVAL;
1186                         break;
1187                 }
1188                     
1189                 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1190                         if (!vc_cons[i].d)
1191                                 continue;
1192                         console_lock();
1193                         if (vlin)
1194                                 vc_cons[i].d->vc_scan_lines = vlin;
1195                         if (clin)
1196                                 vc_cons[i].d->vc_font.height = clin;
1197                         vc_cons[i].d->vc_resize_user = 1;
1198                         vc_resize(vc_cons[i].d, cc, ll);
1199                         console_unlock();
1200                 }
1201                 break;
1202         }
1203
1204         case PIO_FONT: {
1205                 if (!perm)
1206                         goto eperm;
1207                 op.op = KD_FONT_OP_SET;
1208                 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1209                 op.width = 8;
1210                 op.height = 0;
1211                 op.charcount = 256;
1212                 op.data = up;
1213                 ret = con_font_op(vc_cons[fg_console].d, &op);
1214                 break;
1215         }
1216
1217         case GIO_FONT: {
1218                 op.op = KD_FONT_OP_GET;
1219                 op.flags = KD_FONT_FLAG_OLD;
1220                 op.width = 8;
1221                 op.height = 32;
1222                 op.charcount = 256;
1223                 op.data = up;
1224                 ret = con_font_op(vc_cons[fg_console].d, &op);
1225                 break;
1226         }
1227
1228         case PIO_CMAP:
1229                 if (!perm)
1230                         ret = -EPERM;
1231                 else
1232                         ret = con_set_cmap(up);
1233                 break;
1234
1235         case GIO_CMAP:
1236                 ret = con_get_cmap(up);
1237                 break;
1238
1239         case PIO_FONTX:
1240         case GIO_FONTX:
1241                 ret = do_fontx_ioctl(cmd, up, perm, &op);
1242                 break;
1243
1244         case PIO_FONTRESET:
1245         {
1246                 if (!perm)
1247                         goto eperm;
1248
1249 #ifdef BROKEN_GRAPHICS_PROGRAMS
1250                 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1251                    font is not saved. */
1252                 ret = -ENOSYS;
1253                 break;
1254 #else
1255                 {
1256                 op.op = KD_FONT_OP_SET_DEFAULT;
1257                 op.data = NULL;
1258                 ret = con_font_op(vc_cons[fg_console].d, &op);
1259                 if (ret)
1260                         break;
1261                 con_set_default_unimap(vc_cons[fg_console].d);
1262                 break;
1263                 }
1264 #endif
1265         }
1266
1267         case KDFONTOP: {
1268                 if (copy_from_user(&op, up, sizeof(op))) {
1269                         ret = -EFAULT;
1270                         break;
1271                 }
1272                 if (!perm && op.op != KD_FONT_OP_GET)
1273                         goto eperm;
1274                 ret = con_font_op(vc, &op);
1275                 if (ret)
1276                         break;
1277                 if (copy_to_user(up, &op, sizeof(op)))
1278                         ret = -EFAULT;
1279                 break;
1280         }
1281
1282         case PIO_SCRNMAP:
1283                 if (!perm)
1284                         ret = -EPERM;
1285                 else
1286                         ret = con_set_trans_old(up);
1287                 break;
1288
1289         case GIO_SCRNMAP:
1290                 ret = con_get_trans_old(up);
1291                 break;
1292
1293         case PIO_UNISCRNMAP:
1294                 if (!perm)
1295                         ret = -EPERM;
1296                 else
1297                         ret = con_set_trans_new(up);
1298                 break;
1299
1300         case GIO_UNISCRNMAP:
1301                 ret = con_get_trans_new(up);
1302                 break;
1303
1304         case PIO_UNIMAPCLR:
1305               { struct unimapinit ui;
1306                 if (!perm)
1307                         goto eperm;
1308                 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1309                 if (ret)
1310                         ret = -EFAULT;
1311                 else
1312                         con_clear_unimap(vc, &ui);
1313                 break;
1314               }
1315
1316         case PIO_UNIMAP:
1317         case GIO_UNIMAP:
1318                 ret = do_unimap_ioctl(cmd, up, perm, vc);
1319                 break;
1320
1321         case VT_LOCKSWITCH:
1322                 if (!capable(CAP_SYS_TTY_CONFIG))
1323                         goto eperm;
1324                 vt_dont_switch = 1;
1325                 break;
1326         case VT_UNLOCKSWITCH:
1327                 if (!capable(CAP_SYS_TTY_CONFIG))
1328                         goto eperm;
1329                 vt_dont_switch = 0;
1330                 break;
1331         case VT_GETHIFONTMASK:
1332                 ret = put_user(vc->vc_hi_font_mask,
1333                                         (unsigned short __user *)arg);
1334                 break;
1335         case VT_WAITEVENT:
1336                 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1337                 break;
1338         default:
1339                 ret = -ENOIOCTLCMD;
1340         }
1341 out:
1342         tty_unlock();
1343         return ret;
1344 eperm:
1345         ret = -EPERM;
1346         goto out;
1347 }
1348
1349 void reset_vc(struct vc_data *vc)
1350 {
1351         vc->vc_mode = KD_TEXT;
1352         kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1353         vc->vt_mode.mode = VT_AUTO;
1354         vc->vt_mode.waitv = 0;
1355         vc->vt_mode.relsig = 0;
1356         vc->vt_mode.acqsig = 0;
1357         vc->vt_mode.frsig = 0;
1358         put_pid(vc->vt_pid);
1359         vc->vt_pid = NULL;
1360         vc->vt_newvt = -1;
1361         if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
1362                 reset_palette(vc);
1363 }
1364
1365 void vc_SAK(struct work_struct *work)
1366 {
1367         struct vc *vc_con =
1368                 container_of(work, struct vc, SAK_work);
1369         struct vc_data *vc;
1370         struct tty_struct *tty;
1371
1372         console_lock();
1373         vc = vc_con->d;
1374         if (vc) {
1375                 tty = vc->port.tty;
1376                 /*
1377                  * SAK should also work in all raw modes and reset
1378                  * them properly.
1379                  */
1380                 if (tty)
1381                         __do_SAK(tty);
1382                 reset_vc(vc);
1383         }
1384         console_unlock();
1385 }
1386
1387 #ifdef CONFIG_COMPAT
1388
1389 struct compat_consolefontdesc {
1390         unsigned short charcount;       /* characters in font (256 or 512) */
1391         unsigned short charheight;      /* scan lines per character (1-32) */
1392         compat_caddr_t chardata;        /* font data in expanded form */
1393 };
1394
1395 static inline int
1396 compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1397                          int perm, struct console_font_op *op)
1398 {
1399         struct compat_consolefontdesc cfdarg;
1400         int i;
1401
1402         if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1403                 return -EFAULT;
1404
1405         switch (cmd) {
1406         case PIO_FONTX:
1407                 if (!perm)
1408                         return -EPERM;
1409                 op->op = KD_FONT_OP_SET;
1410                 op->flags = KD_FONT_FLAG_OLD;
1411                 op->width = 8;
1412                 op->height = cfdarg.charheight;
1413                 op->charcount = cfdarg.charcount;
1414                 op->data = compat_ptr(cfdarg.chardata);
1415                 return con_font_op(vc_cons[fg_console].d, op);
1416         case GIO_FONTX:
1417                 op->op = KD_FONT_OP_GET;
1418                 op->flags = KD_FONT_FLAG_OLD;
1419                 op->width = 8;
1420                 op->height = cfdarg.charheight;
1421                 op->charcount = cfdarg.charcount;
1422                 op->data = compat_ptr(cfdarg.chardata);
1423                 i = con_font_op(vc_cons[fg_console].d, op);
1424                 if (i)
1425                         return i;
1426                 cfdarg.charheight = op->height;
1427                 cfdarg.charcount = op->charcount;
1428                 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1429                         return -EFAULT;
1430                 return 0;
1431         }
1432         return -EINVAL;
1433 }
1434
1435 struct compat_console_font_op {
1436         compat_uint_t op;        /* operation code KD_FONT_OP_* */
1437         compat_uint_t flags;     /* KD_FONT_FLAG_* */
1438         compat_uint_t width, height;     /* font size */
1439         compat_uint_t charcount;
1440         compat_caddr_t data;    /* font data with height fixed to 32 */
1441 };
1442
1443 static inline int
1444 compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1445                          int perm, struct console_font_op *op, struct vc_data *vc)
1446 {
1447         int i;
1448
1449         if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1450                 return -EFAULT;
1451         if (!perm && op->op != KD_FONT_OP_GET)
1452                 return -EPERM;
1453         op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1454         op->flags |= KD_FONT_FLAG_OLD;
1455         i = con_font_op(vc, op);
1456         if (i)
1457                 return i;
1458         ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1459         if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1460                 return -EFAULT;
1461         return 0;
1462 }
1463
1464 struct compat_unimapdesc {
1465         unsigned short entry_ct;
1466         compat_caddr_t entries;
1467 };
1468
1469 static inline int
1470 compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1471                          int perm, struct vc_data *vc)
1472 {
1473         struct compat_unimapdesc tmp;
1474         struct unipair __user *tmp_entries;
1475
1476         if (copy_from_user(&tmp, user_ud, sizeof tmp))
1477                 return -EFAULT;
1478         tmp_entries = compat_ptr(tmp.entries);
1479         if (tmp_entries)
1480                 if (!access_ok(VERIFY_WRITE, tmp_entries,
1481                                 tmp.entry_ct*sizeof(struct unipair)))
1482                         return -EFAULT;
1483         switch (cmd) {
1484         case PIO_UNIMAP:
1485                 if (!perm)
1486                         return -EPERM;
1487                 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1488         case GIO_UNIMAP:
1489                 if (!perm && fg_console != vc->vc_num)
1490                         return -EPERM;
1491                 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1492         }
1493         return 0;
1494 }
1495
1496 long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
1497              unsigned int cmd, unsigned long arg)
1498 {
1499         struct vc_data *vc = tty->driver_data;
1500         struct console_font_op op;      /* used in multiple places here */
1501         struct kbd_struct *kbd;
1502         unsigned int console;
1503         void __user *up = (void __user *)arg;
1504         int perm;
1505         int ret = 0;
1506
1507         console = vc->vc_num;
1508
1509         tty_lock();
1510
1511         if (!vc_cons_allocated(console)) {      /* impossible? */
1512                 ret = -ENOIOCTLCMD;
1513                 goto out;
1514         }
1515
1516         /*
1517          * To have permissions to do most of the vt ioctls, we either have
1518          * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1519          */
1520         perm = 0;
1521         if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1522                 perm = 1;
1523
1524         kbd = kbd_table + console;
1525         switch (cmd) {
1526         /*
1527          * these need special handlers for incompatible data structures
1528          */
1529         case PIO_FONTX:
1530         case GIO_FONTX:
1531                 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1532                 break;
1533
1534         case KDFONTOP:
1535                 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1536                 break;
1537
1538         case PIO_UNIMAP:
1539         case GIO_UNIMAP:
1540                 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1541                 break;
1542
1543         /*
1544          * all these treat 'arg' as an integer
1545          */
1546         case KIOCSOUND:
1547         case KDMKTONE:
1548 #ifdef CONFIG_X86
1549         case KDADDIO:
1550         case KDDELIO:
1551 #endif
1552         case KDSETMODE:
1553         case KDMAPDISP:
1554         case KDUNMAPDISP:
1555         case KDSKBMODE:
1556         case KDSKBMETA:
1557         case KDSKBLED:
1558         case KDSETLED:
1559         case KDSIGACCEPT:
1560         case VT_ACTIVATE:
1561         case VT_WAITACTIVE:
1562         case VT_RELDISP:
1563         case VT_DISALLOCATE:
1564         case VT_RESIZE:
1565         case VT_RESIZEX:
1566                 goto fallback;
1567
1568         /*
1569          * the rest has a compatible data structure behind arg,
1570          * but we have to convert it to a proper 64 bit pointer.
1571          */
1572         default:
1573                 arg = (unsigned long)compat_ptr(arg);
1574                 goto fallback;
1575         }
1576 out:
1577         tty_unlock();
1578         return ret;
1579
1580 fallback:
1581         tty_unlock();
1582         return vt_ioctl(tty, file, cmd, arg);
1583 }
1584
1585
1586 #endif /* CONFIG_COMPAT */
1587
1588
1589 /*
1590  * Performs the back end of a vt switch. Called under the console
1591  * semaphore.
1592  */
1593 static void complete_change_console(struct vc_data *vc)
1594 {
1595         unsigned char old_vc_mode;
1596         int old = fg_console;
1597         struct vc_data *oldvc = vc_cons[fg_console].d;
1598
1599         last_console = fg_console;
1600
1601         /*
1602          * If we're switching, we could be going from KD_GRAPHICS to
1603          * KD_TEXT mode or vice versa, which means we need to blank or
1604          * unblank the screen later.
1605          */
1606         old_vc_mode = oldvc->vc_mode;
1607
1608 #if defined(CONFIG_VGA_CONSOLE)
1609         if (old_vc_mode == KD_TEXT && oldvc->vc_sw == &vga_con &&
1610             oldvc->vc_sw->con_font_get) {
1611                 if (!oldvc->vc_font.data)
1612                         oldvc->vc_font.data = kmalloc(max_font_size, 
1613                                                       GFP_KERNEL);
1614                 lock_kernel();
1615                 oldvc->vc_sw->con_font_get(oldvc, &oldvc->vc_font);
1616                 unlock_kernel();
1617         }
1618 #endif
1619         switch_screen(vc);
1620
1621 #if defined(CONFIG_VGA_CONSOLE)
1622         if (vc->vc_mode == KD_TEXT && vc->vc_sw == &vga_con &&
1623             vc->vc_sw->con_font_set) {
1624                 if (vc->vc_font.data) {
1625                         lock_kernel();
1626                         vc->vc_sw->con_font_set(vc, &vc->vc_font, 0);
1627                         unlock_kernel();
1628                 }
1629         }
1630 #endif
1631         /*
1632          * This can't appear below a successful kill_pid().  If it did,
1633          * then the *blank_screen operation could occur while X, having
1634          * received acqsig, is waking up on another processor.  This
1635          * condition can lead to overlapping accesses to the VGA range
1636          * and the framebuffer (causing system lockups).
1637          *
1638          * To account for this we duplicate this code below only if the
1639          * controlling process is gone and we've called reset_vc.
1640          */
1641         if (old_vc_mode != vc->vc_mode) {
1642                 if (vc->vc_mode == KD_TEXT)
1643                         do_unblank_screen(1);
1644                 else
1645                         do_blank_screen(1);
1646         }
1647
1648         /*
1649          * If this new console is under process control, send it a signal
1650          * telling it that it has acquired. Also check if it has died and
1651          * clean up (similar to logic employed in change_console())
1652          */
1653         if (vc->vt_mode.mode == VT_PROCESS) {
1654                 /*
1655                  * Send the signal as privileged - kill_pid() will
1656                  * tell us if the process has gone or something else
1657                  * is awry
1658                  */
1659                 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1660                 /*
1661                  * The controlling process has died, so we revert back to
1662                  * normal operation. In this case, we'll also change back
1663                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1664                  * but it saves the agony when the X server dies and the screen
1665                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1666                  * this outside of VT_PROCESS but there is no single process
1667                  * to account for and tracking tty count may be undesirable.
1668                  */
1669                         reset_vc(vc);
1670
1671                         if (old_vc_mode != vc->vc_mode) {
1672                                 if (vc->vc_mode == KD_TEXT)
1673                                         do_unblank_screen(1);
1674                                 else
1675                                         do_blank_screen(1);
1676                         }
1677                 }
1678         }
1679
1680         /*
1681          * Wake anyone waiting for their VT to activate
1682          */
1683         vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1684         return;
1685 }
1686
1687 /*
1688  * Performs the front-end of a vt switch
1689  */
1690 void change_console(struct vc_data *new_vc)
1691 {
1692         struct vc_data *vc;
1693
1694         if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1695                 return;
1696
1697         /*
1698          * If this vt is in process mode, then we need to handshake with
1699          * that process before switching. Essentially, we store where that
1700          * vt wants to switch to and wait for it to tell us when it's done
1701          * (via VT_RELDISP ioctl).
1702          *
1703          * We also check to see if the controlling process still exists.
1704          * If it doesn't, we reset this vt to auto mode and continue.
1705          * This is a cheap way to track process control. The worst thing
1706          * that can happen is: we send a signal to a process, it dies, and
1707          * the switch gets "lost" waiting for a response; hopefully, the
1708          * user will try again, we'll detect the process is gone (unless
1709          * the user waits just the right amount of time :-) and revert the
1710          * vt to auto control.
1711          */
1712         vc = vc_cons[fg_console].d;
1713         if (vc->vt_mode.mode == VT_PROCESS) {
1714                 /*
1715                  * Send the signal as privileged - kill_pid() will
1716                  * tell us if the process has gone or something else
1717                  * is awry.
1718                  *
1719                  * We need to set vt_newvt *before* sending the signal or we
1720                  * have a race.
1721                  */
1722                 vc->vt_newvt = new_vc->vc_num;
1723                 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1724                         /*
1725                          * It worked. Mark the vt to switch to and
1726                          * return. The process needs to send us a
1727                          * VT_RELDISP ioctl to complete the switch.
1728                          */
1729                         return;
1730                 }
1731
1732                 /*
1733                  * The controlling process has died, so we revert back to
1734                  * normal operation. In this case, we'll also change back
1735                  * to KD_TEXT mode. I'm not sure if this is strictly correct
1736                  * but it saves the agony when the X server dies and the screen
1737                  * remains blanked due to KD_GRAPHICS! It would be nice to do
1738                  * this outside of VT_PROCESS but there is no single process
1739                  * to account for and tracking tty count may be undesirable.
1740                  */
1741                 reset_vc(vc);
1742
1743                 /*
1744                  * Fall through to normal (VT_AUTO) handling of the switch...
1745                  */
1746         }
1747
1748         /*
1749          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1750          */
1751         if (vc->vc_mode == KD_GRAPHICS)
1752                 return;
1753
1754         complete_change_console(new_vc);
1755 }
1756
1757 /* Perform a kernel triggered VT switch for suspend/resume */
1758
1759 static int disable_vt_switch;
1760
1761 int vt_move_to_console(unsigned int vt, int alloc)
1762 {
1763         int prev;
1764
1765         console_lock();
1766         /* Graphics mode - up to X */
1767         if (disable_vt_switch) {
1768                 console_unlock();
1769                 return 0;
1770         }
1771         prev = fg_console;
1772
1773         if (alloc && vc_allocate(vt)) {
1774                 /* we can't have a free VC for now. Too bad,
1775                  * we don't want to mess the screen for now. */
1776                 console_unlock();
1777                 return -ENOSPC;
1778         }
1779
1780         if (set_console(vt)) {
1781                 /*
1782                  * We're unable to switch to the SUSPEND_CONSOLE.
1783                  * Let the calling function know so it can decide
1784                  * what to do.
1785                  */
1786                 console_unlock();
1787                 return -EIO;
1788         }
1789         console_unlock();
1790         tty_lock();
1791         if (vt_waitactive(vt + 1)) {
1792                 pr_debug("Suspend: Can't switch VCs.");
1793                 tty_unlock();
1794                 return -EINTR;
1795         }
1796         tty_unlock();
1797         return prev;
1798 }
1799
1800 /*
1801  * Normally during a suspend, we allocate a new console and switch to it.
1802  * When we resume, we switch back to the original console.  This switch
1803  * can be slow, so on systems where the framebuffer can handle restoration
1804  * of video registers anyways, there's little point in doing the console
1805  * switch.  This function allows you to disable it by passing it '0'.
1806  */
1807 void pm_set_vt_switch(int do_switch)
1808 {
1809         console_lock();
1810         disable_vt_switch = !do_switch;
1811         console_unlock();
1812 }
1813 EXPORT_SYMBOL(pm_set_vt_switch);