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