File: /usr/src/linux/drivers/char/vt.c

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