vt_ioctl.c 40.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  linux/drivers/char/vt_ioctl.c
 *
 *  Copyright (C) 1992 obz under the linux copyright
 *
 *  Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
 *  Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
 *  Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
 *  Some code moved for less code duplication - Andi Kleen - Mar 1997
 *  Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/timer.h>
#include <linux/kernel.h>
19
#include <linux/compat.h>
20
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27
#include <linux/kd.h>
#include <linux/vt.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/console.h>
Samuel Thibault's avatar
Samuel Thibault committed
28
#include <linux/consolemap.h>
29
#include <linux/signal.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
30
#include <linux/smp_lock.h>
31
#include <linux/timex.h>
Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40

#include <asm/io.h>
#include <asm/uaccess.h>

#include <linux/kbd_kern.h>
#include <linux/vt_kern.h>
#include <linux/kbd_diacr.h>
#include <linux/selection.h>

41
char vt_dont_switch;
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
extern struct tty_driver *console_driver;

#define VT_IS_IN_USE(i)	(console_driver->ttys[i] && console_driver->ttys[i]->count)
#define VT_BUSY(i)	(VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)

/*
 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
 * experimentation and study of X386 SYSV handling.
 *
 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
 * to the current console is done by the main ioctl code.
 */

#ifdef CONFIG_X86
#include <linux/syscalls.h>
#endif

static void complete_change_console(struct vc_data *vc);

Alan Cox's avatar
Alan Cox committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 *	User space VT_EVENT handlers
 */

struct vt_event_wait {
	struct list_head list;
	struct vt_event event;
	int done;
};

static LIST_HEAD(vt_events);
static DEFINE_SPINLOCK(vt_event_lock);
static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);

/**
 *	vt_event_post
 *	@event: the event that occurred
 *	@old: old console
 *	@new: new console
 *
 *	Post an VT event to interested VT handlers
 */

void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
{
	struct list_head *pos, *head;
	unsigned long flags;
	int wake = 0;

	spin_lock_irqsave(&vt_event_lock, flags);
	head = &vt_events;

	list_for_each(pos, head) {
		struct vt_event_wait *ve = list_entry(pos,
						struct vt_event_wait, list);
		if (!(ve->event.event & event))
			continue;
		ve->event.event = event;
		/* kernel view is consoles 0..n-1, user space view is
		   console 1..n with 0 meaning current, so we must bias */
106 107
		ve->event.oldev = old + 1;
		ve->event.newev = new + 1;
Alan Cox's avatar
Alan Cox committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		wake = 1;
		ve->done = 1;
	}
	spin_unlock_irqrestore(&vt_event_lock, flags);
	if (wake)
		wake_up_interruptible(&vt_event_waitqueue);
}

/**
 *	vt_event_wait		-	wait for an event
 *	@vw: our event
 *
 *	Waits for an event to occur which completes our vt_event_wait
 *	structure. On return the structure has wv->done set to 1 for success
 *	or 0 if some event such as a signal ended the wait.
 */

static void vt_event_wait(struct vt_event_wait *vw)
{
	unsigned long flags;
	/* Prepare the event */
	INIT_LIST_HEAD(&vw->list);
	vw->done = 0;
	/* Queue our event */
	spin_lock_irqsave(&vt_event_lock, flags);
	list_add(&vw->list, &vt_events);
	spin_unlock_irqrestore(&vt_event_lock, flags);
	/* Wait for it to pass */
136
	wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
Alan Cox's avatar
Alan Cox committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/* Dequeue it */
	spin_lock_irqsave(&vt_event_lock, flags);
	list_del(&vw->list);
	spin_unlock_irqrestore(&vt_event_lock, flags);
}

/**
 *	vt_event_wait_ioctl	-	event ioctl handler
 *	@arg: argument to ioctl
 *
 *	Implement the VT_WAITEVENT ioctl using the VT event interface
 */

static int vt_event_wait_ioctl(struct vt_event __user *event)
{
	struct vt_event_wait vw;

	if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
		return -EFAULT;
	/* Highest supported event for now */
	if (vw.event.event & ~VT_MAX_EVENT)
		return -EINVAL;

	vt_event_wait(&vw);
	/* If it occurred report it */
	if (vw.done) {
		if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
			return -EFAULT;
		return 0;
	}
	return -EINTR;
}

/**
 *	vt_waitactive	-	active console wait
 *	@event: event code
 *	@n: new console
 *
 *	Helper for event waits. Used to implement the legacy
 *	event waiting ioctls in terms of events
 */

int vt_waitactive(int n)
{
	struct vt_event_wait vw;
	do {
		if (n == fg_console + 1)
			break;
		vw.event.event = VT_EVENT_SWITCH;
		vt_event_wait(&vw);
		if (vw.done == 0)
			return -EINTR;
189
	} while (vw.event.newev != n);
Alan Cox's avatar
Alan Cox committed
190 191 192
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/*
 * these are the valid i/o ports we're allowed to change. they map all the
 * video ports
 */
#define GPFIRST 0x3b4
#define GPLAST 0x3df
#define GPNUM (GPLAST - GPFIRST + 1)

#define i (tmp.kb_index)
#define s (tmp.kb_table)
#define v (tmp.kb_value)
static inline int
do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
{
	struct kbentry tmp;
	ushort *key_map, val, ov;

	if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
		return -EFAULT;

213 214 215
	if (!capable(CAP_SYS_TTY_CONFIG))
		perm = 0;

Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229
	switch (cmd) {
	case KDGKBENT:
		key_map = key_maps[s];
		if (key_map) {
		    val = U(key_map[i]);
		    if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
			val = K_HOLE;
		} else
		    val = (i ? K_HOLE : K_NOSUCHMAP);
		return put_user(val, &user_kbe->kb_value);
	case KDSKBENT:
		if (!perm)
			return -EPERM;
		if (!i && v == K_NOSUCHMAP) {
Alan Cox's avatar
Alan Cox committed
230
			/* deallocate map */
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
			key_map = key_maps[s];
			if (s && key_map) {
			    key_maps[s] = NULL;
			    if (key_map[0] == U(K_ALLOCATED)) {
					kfree(key_map);
					keymap_count--;
			    }
			}
			break;
		}

		if (KTYP(v) < NR_TYPES) {
		    if (KVAL(v) > max_vals[KTYP(v)])
				return -EINVAL;
		} else
		    if (kbd->kbdmode != VC_UNICODE)
				return -EINVAL;

		/* ++Geert: non-PC keyboards may generate keycode zero */
#if !defined(__mc68000__) && !defined(__powerpc__)
		/* assignment to entry 0 only tests validity of args */
		if (!i)
			break;
#endif

		if (!(key_map = key_maps[s])) {
			int j;

			if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
			    !capable(CAP_SYS_RESOURCE))
				return -EPERM;

263
			key_map = kmalloc(sizeof(plain_map),
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
						     GFP_KERNEL);
			if (!key_map)
				return -ENOMEM;
			key_maps[s] = key_map;
			key_map[0] = U(K_ALLOCATED);
			for (j = 1; j < NR_KEYS; j++)
				key_map[j] = U(K_HOLE);
			keymap_count++;
		}
		ov = U(key_map[i]);
		if (v == ov)
			break;	/* nothing to do */
		/*
		 * Attention Key.
		 */
		if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
			return -EPERM;
		key_map[i] = U(v);
		if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
			compute_shiftstate();
		break;
	}
	return 0;
}
#undef i
#undef s
#undef v

static inline int 
do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
{
	struct kbkeycode tmp;
	int kc = 0;

	if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
		return -EFAULT;
	switch (cmd) {
	case KDGETKEYCODE:
		kc = getkeycode(tmp.scancode);
		if (kc >= 0)
			kc = put_user(kc, &user_kbkc->keycode);
		break;
	case KDSETKEYCODE:
		if (!perm)
			return -EPERM;
		kc = setkeycode(tmp.scancode, tmp.keycode);
		break;
	}
	return kc;
}

static inline int
do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
{
	struct kbsentry *kbs;
	char *p;
	u_char *q;
	u_char __user *up;
	int sz;
	int delta;
	char *first_free, *fj, *fnw;
	int i, j, k;
	int ret;

Andrew Morton's avatar
Andrew Morton committed
328
	if (!capable(CAP_SYS_TTY_CONFIG))
329
		perm = 0;
Andrew Morton's avatar
Andrew Morton committed
330

Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
	if (!kbs) {
		ret = -ENOMEM;
		goto reterr;
	}

	/* we mostly copy too much here (512bytes), but who cares ;) */
	if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
		ret = -EFAULT;
		goto reterr;
	}
	kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
	i = kbs->kb_func;

	switch (cmd) {
	case KDGKBSENT:
		sz = sizeof(kbs->kb_string) - 1; /* sz should have been
						  a struct member */
		up = user_kdgkb->kb_string;
		p = func_table[i];
		if(p)
			for ( ; *p && sz; p++, sz--)
				if (put_user(*p, up++)) {
					ret = -EFAULT;
					goto reterr;
				}
		if (put_user('\0', up)) {
			ret = -EFAULT;
			goto reterr;
		}
		kfree(kbs);
		return ((p && *p) ? -EOVERFLOW : 0);
	case KDSKBSENT:
		if (!perm) {
			ret = -EPERM;
			goto reterr;
		}

		q = func_table[i];
		first_free = funcbufptr + (funcbufsize - funcbufleft);
		for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) 
			;
		if (j < MAX_NR_FUNC)
			fj = func_table[j];
		else
			fj = first_free;

		delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
		if (delta <= funcbufleft) { 	/* it fits in current buf */
		    if (j < MAX_NR_FUNC) {
			memmove(fj + delta, fj, first_free - fj);
			for (k = j; k < MAX_NR_FUNC; k++)
			    if (func_table[k])
				func_table[k] += delta;
		    }
		    if (!q)
		      func_table[i] = fj;
		    funcbufleft -= delta;
		} else {			/* allocate a larger buffer */
		    sz = 256;
		    while (sz < funcbufsize - funcbufleft + delta)
		      sz <<= 1;
393
		    fnw = kmalloc(sz, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		    if(!fnw) {
		      ret = -ENOMEM;
		      goto reterr;
		    }

		    if (!q)
		      func_table[i] = fj;
		    if (fj > funcbufptr)
			memmove(fnw, funcbufptr, fj - funcbufptr);
		    for (k = 0; k < j; k++)
		      if (func_table[k])
			func_table[k] = fnw + (func_table[k] - funcbufptr);

		    if (first_free > fj) {
			memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
			for (k = j; k < MAX_NR_FUNC; k++)
			  if (func_table[k])
			    func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
		    }
		    if (funcbufptr != func_buf)
		      kfree(funcbufptr);
		    funcbufptr = fnw;
		    funcbufleft = funcbufleft - delta + sz - funcbufsize;
		    funcbufsize = sz;
		}
		strcpy(func_table[i], kbs->kb_string);
		break;
	}
	ret = 0;
reterr:
	kfree(kbs);
	return ret;
}

static inline int 
do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
{
	struct consolefontdesc cfdarg;
	int i;

	if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) 
		return -EFAULT;
 	
	switch (cmd) {
	case PIO_FONTX:
		if (!perm)
			return -EPERM;
		op->op = KD_FONT_OP_SET;
		op->flags = KD_FONT_FLAG_OLD;
		op->width = 8;
		op->height = cfdarg.charheight;
		op->charcount = cfdarg.charcount;
		op->data = cfdarg.chardata;
		return con_font_op(vc_cons[fg_console].d, op);
	case GIO_FONTX: {
		op->op = KD_FONT_OP_GET;
		op->flags = KD_FONT_FLAG_OLD;
		op->width = 8;
		op->height = cfdarg.charheight;
		op->charcount = cfdarg.charcount;
		op->data = cfdarg.chardata;
		i = con_font_op(vc_cons[fg_console].d, op);
		if (i)
			return i;
		cfdarg.charheight = op->height;
		cfdarg.charcount = op->charcount;
		if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
			return -EFAULT;
		return 0;
		}
	}
	return -EINVAL;
}

static inline int 
do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
{
	struct unimapdesc tmp;

	if (copy_from_user(&tmp, user_ud, sizeof tmp))
		return -EFAULT;
	if (tmp.entries)
		if (!access_ok(VERIFY_WRITE, tmp.entries,
				tmp.entry_ct*sizeof(struct unipair)))
			return -EFAULT;
	switch (cmd) {
	case PIO_UNIMAP:
		if (!perm)
			return -EPERM;
		return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
	case GIO_UNIMAP:
		if (!perm && fg_console != vc->vc_num)
			return -EPERM;
		return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
	}
	return 0;
}

Alan Cox's avatar
Alan Cox committed
492 493


Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497 498 499 500
/*
 * We handle the console-specific ioctl's here.  We allow the
 * capability to modify any console, not just the fg_console. 
 */
int vt_ioctl(struct tty_struct *tty, struct file * file,
	     unsigned int cmd, unsigned long arg)
{
Alan Cox's avatar
Alan Cox committed
501
	struct vc_data *vc = tty->driver_data;
Linus Torvalds's avatar
Linus Torvalds committed
502 503 504 505 506 507
	struct console_font_op op;	/* used in multiple places here */
	struct kbd_struct * kbd;
	unsigned int console;
	unsigned char ucval;
	void __user *up = (void __user *)arg;
	int i, perm;
Alan Cox's avatar
Alan Cox committed
508 509
	int ret = 0;

Linus Torvalds's avatar
Linus Torvalds committed
510 511
	console = vc->vc_num;

512
	tty_lock();
Alan Cox's avatar
Alan Cox committed
513 514 515 516 517 518

	if (!vc_cons_allocated(console)) { 	/* impossible? */
		ret = -ENOIOCTLCMD;
		goto out;
	}

Linus Torvalds's avatar
Linus Torvalds committed
519 520 521 522 523 524 525 526 527 528 529

	/*
	 * To have permissions to do most of the vt ioctls, we either have
	 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
	 */
	perm = 0;
	if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
		perm = 1;
 
	kbd = kbd_table + console;
	switch (cmd) {
530
	case TIOCLINUX:
Jiri Slaby's avatar
Jiri Slaby committed
531 532
		ret = tioclinux(tty, arg);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
533 534
	case KIOCSOUND:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
535
			goto eperm;
536 537 538
		/* FIXME: This is an old broken API but we need to keep it
		   supported and somehow separate the historic advertised
		   tick rate from any real one */
Linus Torvalds's avatar
Linus Torvalds committed
539
		if (arg)
540
			arg = CLOCK_TICK_RATE / arg;
Linus Torvalds's avatar
Linus Torvalds committed
541
		kd_mksound(arg, 0);
Alan Cox's avatar
Alan Cox committed
542
		break;
Linus Torvalds's avatar
Linus Torvalds committed
543 544 545

	case KDMKTONE:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
546
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
547 548 549 550 551 552 553 554 555
	{
		unsigned int ticks, count;
		
		/*
		 * Generate the tone for the appropriate number of ticks.
		 * If the time is zero, turn off sound ourselves.
		 */
		ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
		count = ticks ? (arg & 0xffff) : 0;
556 557 558
		/* FIXME: This is an old broken API but we need to keep it
		   supported and somehow separate the historic advertised
		   tick rate from any real one */
Linus Torvalds's avatar
Linus Torvalds committed
559
		if (count)
560
			count = CLOCK_TICK_RATE / count;
Linus Torvalds's avatar
Linus Torvalds committed
561
		kd_mksound(count, ticks);
Alan Cox's avatar
Alan Cox committed
562
		break;
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	}

	case KDGKBTYPE:
		/*
		 * this is naive.
		 */
		ucval = KB_101;
		goto setchar;

		/*
		 * These cannot be implemented on any machine that implements
		 * ioperm() in user level (such as Alpha PCs) or not at all.
		 *
		 * XXX: you should never use these, just call ioperm directly..
		 */
#ifdef CONFIG_X86
	case KDADDIO:
	case KDDELIO:
		/*
		 * KDADDIO and KDDELIO may be able to add ports beyond what
		 * we reject here, but to be safe...
		 */
Alan Cox's avatar
Alan Cox committed
585 586 587 588 589 590
		if (arg < GPFIRST || arg > GPLAST) {
			ret = -EINVAL;
			break;
		}
		ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
591 592 593

	case KDENABIO:
	case KDDISABIO:
Alan Cox's avatar
Alan Cox committed
594
		ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds's avatar
Linus Torvalds committed
595
				  (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox's avatar
Alan Cox committed
596
		break;
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599 600 601 602 603 604 605
#endif

	/* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
		
	case KDKBDREP:
	{
		struct kbd_repeat kbrep;
		
		if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox's avatar
Alan Cox committed
606
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
607

Alan Cox's avatar
Alan Cox committed
608 609 610 611 612 613 614
		if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
			ret =  -EFAULT;
			break;
		}
		ret = kbd_rate(&kbrep);
		if (ret)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
615
		if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox's avatar
Alan Cox committed
616 617
			ret = -EFAULT;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621 622 623 624 625 626 627 628 629
	}

	case KDSETMODE:
		/*
		 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
		 * doesn't do a whole lot. i'm not sure if it should do any
		 * restoration of modes or what...
		 *
		 * XXX It should at least call into the driver, fbdev's definitely
		 * need to restore their engine state. --BenH
		 */
		if (!perm)
Alan Cox's avatar
Alan Cox committed
630
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635 636 637 638 639
		switch (arg) {
		case KD_GRAPHICS:
			break;
		case KD_TEXT0:
		case KD_TEXT1:
			arg = KD_TEXT;
		case KD_TEXT:
			break;
		default:
Alan Cox's avatar
Alan Cox committed
640 641
			ret = -EINVAL;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
642 643
		}
		if (vc->vc_mode == (unsigned char) arg)
Alan Cox's avatar
Alan Cox committed
644
			break;
Linus Torvalds's avatar
Linus Torvalds committed
645 646
		vc->vc_mode = (unsigned char) arg;
		if (console != fg_console)
Alan Cox's avatar
Alan Cox committed
647
			break;
Linus Torvalds's avatar
Linus Torvalds committed
648 649 650 651 652 653 654 655 656
		/*
		 * explicitly blank/unblank the screen if switching modes
		 */
		acquire_console_sem();
		if (arg == KD_TEXT)
			do_unblank_screen(1);
		else
			do_blank_screen(1);
		release_console_sem();
Alan Cox's avatar
Alan Cox committed
657
		break;
Linus Torvalds's avatar
Linus Torvalds committed
658 659 660 661 662 663 664 665 666 667 668

	case KDGETMODE:
		ucval = vc->vc_mode;
		goto setint;

	case KDMAPDISP:
	case KDUNMAPDISP:
		/*
		 * these work like a combination of mmap and KDENABIO.
		 * this could be easily finished.
		 */
Alan Cox's avatar
Alan Cox committed
669 670
		ret = -EINVAL;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
671 672 673

	case KDSKBMODE:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
674
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		switch(arg) {
		  case K_RAW:
			kbd->kbdmode = VC_RAW;
			break;
		  case K_MEDIUMRAW:
			kbd->kbdmode = VC_MEDIUMRAW;
			break;
		  case K_XLATE:
			kbd->kbdmode = VC_XLATE;
			compute_shiftstate();
			break;
		  case K_UNICODE:
			kbd->kbdmode = VC_UNICODE;
			compute_shiftstate();
			break;
		  default:
Alan Cox's avatar
Alan Cox committed
691 692
			ret = -EINVAL;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
693 694
		}
		tty_ldisc_flush(tty);
Alan Cox's avatar
Alan Cox committed
695
		break;
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

	case KDGKBMODE:
		ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
				 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
				 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
				 K_XLATE);
		goto setint;

	/* this could be folded into KDSKBMODE, but for compatibility
	   reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
	case KDSKBMETA:
		switch(arg) {
		  case K_METABIT:
			clr_vc_kbd_mode(kbd, VC_META);
			break;
		  case K_ESCPREFIX:
			set_vc_kbd_mode(kbd, VC_META);
			break;
		  default:
Alan Cox's avatar
Alan Cox committed
715
			ret = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
716
		}
Alan Cox's avatar
Alan Cox committed
717
		break;
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721

	case KDGKBMETA:
		ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
	setint:
Alan Cox's avatar
Alan Cox committed
722 723
		ret = put_user(ucval, (int __user *)arg);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
724 725 726 727

	case KDGETKEYCODE:
	case KDSETKEYCODE:
		if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox's avatar
Alan Cox committed
728 729 730
			perm = 0;
		ret = do_kbkeycode_ioctl(cmd, up, perm);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
731 732 733

	case KDGKBENT:
	case KDSKBENT:
Alan Cox's avatar
Alan Cox committed
734 735
		ret = do_kdsk_ioctl(cmd, up, perm, kbd);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738

	case KDGKBSENT:
	case KDSKBSENT:
Alan Cox's avatar
Alan Cox committed
739 740
		ret = do_kdgkb_ioctl(cmd, up, perm);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
741 742 743 744

	case KDGKBDIACR:
	{
		struct kbdiacrs __user *a = up;
Samuel Thibault's avatar
Samuel Thibault committed
745 746
		struct kbdiacr diacr;
		int i;
Linus Torvalds's avatar
Linus Torvalds committed
747

Alan Cox's avatar
Alan Cox committed
748 749 750 751
		if (put_user(accent_table_size, &a->kb_cnt)) {
			ret = -EFAULT;
			break;
		}
Samuel Thibault's avatar
Samuel Thibault committed
752 753 754 755
		for (i = 0; i < accent_table_size; i++) {
			diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
			diacr.base = conv_uni_to_8bit(accent_table[i].base);
			diacr.result = conv_uni_to_8bit(accent_table[i].result);
Alan Cox's avatar
Alan Cox committed
756 757 758 759
			if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
				ret = -EFAULT;
				break;
			}
Samuel Thibault's avatar
Samuel Thibault committed
760
		}
Alan Cox's avatar
Alan Cox committed
761
		break;
Samuel Thibault's avatar
Samuel Thibault committed
762 763 764 765 766 767
	}
	case KDGKBDIACRUC:
	{
		struct kbdiacrsuc __user *a = up;

		if (put_user(accent_table_size, &a->kb_cnt))
Alan Cox's avatar
Alan Cox committed
768 769 770 771 772
			ret = -EFAULT;
		else if (copy_to_user(a->kbdiacruc, accent_table,
				accent_table_size*sizeof(struct kbdiacruc)))
			ret = -EFAULT;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775 776 777
	}

	case KDSKBDIACR:
	{
		struct kbdiacrs __user *a = up;
Samuel Thibault's avatar
Samuel Thibault committed
778 779 780 781 782
		struct kbdiacr diacr;
		unsigned int ct;
		int i;

		if (!perm)
Alan Cox's avatar
Alan Cox committed
783 784 785 786 787 788 789 790 791
			goto eperm;
		if (get_user(ct,&a->kb_cnt)) {
			ret = -EFAULT;
			break;
		}
		if (ct >= MAX_DIACR) {
			ret = -EINVAL;
			break;
		}
Samuel Thibault's avatar
Samuel Thibault committed
792 793
		accent_table_size = ct;
		for (i = 0; i < ct; i++) {
Alan Cox's avatar
Alan Cox committed
794 795 796 797
			if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
				ret = -EFAULT;
				break;
			}
Samuel Thibault's avatar
Samuel Thibault committed
798 799 800 801
			accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
			accent_table[i].base = conv_8bit_to_uni(diacr.base);
			accent_table[i].result = conv_8bit_to_uni(diacr.result);
		}
Alan Cox's avatar
Alan Cox committed
802
		break;
Samuel Thibault's avatar
Samuel Thibault committed
803 804 805 806 807
	}

	case KDSKBDIACRUC:
	{
		struct kbdiacrsuc __user *a = up;
Linus Torvalds's avatar
Linus Torvalds committed
808 809 810
		unsigned int ct;

		if (!perm)
Alan Cox's avatar
Alan Cox committed
811 812 813 814 815 816 817 818 819
			goto eperm;
		if (get_user(ct,&a->kb_cnt)) {
			ret = -EFAULT;
			break;
		}
		if (ct >= MAX_DIACR) {
			ret = -EINVAL;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
820
		accent_table_size = ct;
Samuel Thibault's avatar
Samuel Thibault committed
821
		if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
Alan Cox's avatar
Alan Cox committed
822 823
			ret = -EFAULT;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
824 825 826 827 828 829 830 831 832 833
	}

	/* the ioctls below read/set the flags usually shown in the leds */
	/* don't use them - they will go away without warning */
	case KDGKBLED:
		ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
		goto setchar;

	case KDSKBLED:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
834 835 836 837 838
			goto eperm;
		if (arg & ~0x77) {
			ret = -EINVAL;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
839 840 841
		kbd->ledflagstate = (arg & 7);
		kbd->default_ledflagstate = ((arg >> 4) & 7);
		set_leds();
Alan Cox's avatar
Alan Cox committed
842
		break;
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846 847 848

	/* the ioctls below only set the lights, not the functions */
	/* for those, see KDGKBLED and KDSKBLED above */
	case KDGETLED:
		ucval = getledstate();
	setchar:
Alan Cox's avatar
Alan Cox committed
849 850
		ret = put_user(ucval, (char __user *)arg);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
851 852 853

	case KDSETLED:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
854
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
855
		setledstate(kbd, arg);
Alan Cox's avatar
Alan Cox committed
856
		break;
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859 860 861 862 863 864 865 866 867

	/*
	 * A process can indicate its willingness to accept signals
	 * generated by pressing an appropriate key combination.
	 * Thus, one can have a daemon that e.g. spawns a new console
	 * upon a keypress and then changes to it.
	 * See also the kbrequest field of inittab(5).
	 */
	case KDSIGACCEPT:
	{
		if (!perm || !capable(CAP_KILL))
Alan Cox's avatar
Alan Cox committed
868
			goto eperm;
869
		if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox's avatar
Alan Cox committed
870 871 872 873 874 875 876 877 878
			ret = -EINVAL;
		else {
			spin_lock_irq(&vt_spawn_con.lock);
			put_pid(vt_spawn_con.pid);
			vt_spawn_con.pid = get_pid(task_pid(current));
			vt_spawn_con.sig = arg;
			spin_unlock_irq(&vt_spawn_con.lock);
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
879 880 881 882 883 884 885
	}

	case VT_SETMODE:
	{
		struct vt_mode tmp;

		if (!perm)
Alan Cox's avatar
Alan Cox committed
886 887 888 889 890
			goto eperm;
		if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
			ret = -EFAULT;
			goto out;
		}
891
		if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
Alan Cox's avatar
Alan Cox committed
892 893 894
			ret = -EINVAL;
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897 898
		acquire_console_sem();
		vc->vt_mode = tmp;
		/* the frsig is ignored, so we set it to 0 */
		vc->vt_mode.frsig = 0;
899 900
		put_pid(vc->vt_pid);
		vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903
		/* no switch is required -- saw@shade.msu.ru */
		vc->vt_newvt = -1;
		release_console_sem();
Alan Cox's avatar
Alan Cox committed
904
		break;
Linus Torvalds's avatar
Linus Torvalds committed
905 906 907 908 909 910 911 912 913 914 915 916
	}

	case VT_GETMODE:
	{
		struct vt_mode tmp;
		int rc;

		acquire_console_sem();
		memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
		release_console_sem();

		rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox's avatar
Alan Cox committed
917 918 919
		if (rc)
			ret = -EFAULT;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
920 921 922 923 924 925 926 927 928 929 930 931 932
	}

	/*
	 * Returns global vt state. Note that VT 0 is always open, since
	 * it's an alias for the current VT, and people can't use it here.
	 * We cannot return state for more than 16 VTs, since v_state is short.
	 */
	case VT_GETSTATE:
	{
		struct vt_stat __user *vtstat = up;
		unsigned short state, mask;

		if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox's avatar
Alan Cox committed
933 934 935 936 937 938 939 940 941 942
			ret = -EFAULT;
		else {
			state = 1;	/* /dev/tty0 is always open */
			for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
							++i, mask <<= 1)
				if (VT_IS_IN_USE(i))
					state |= mask;
			ret = put_user(state, &vtstat->v_state);
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	}

	/*
	 * Returns the first available (non-opened) console.
	 */
	case VT_OPENQRY:
		for (i = 0; i < MAX_NR_CONSOLES; ++i)
			if (! VT_IS_IN_USE(i))
				break;
		ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
		goto setint;		 

	/*
	 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
	 * with num >= 1 (switches to vt 0, our console, are not allowed, just
	 * to preserve sanity).
	 */
	case VT_ACTIVATE:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
962
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
963
		if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox's avatar
Alan Cox committed
964 965 966 967 968 969 970 971 972 973 974
			ret =  -ENXIO;
		else {
			arg--;
			acquire_console_sem();
			ret = vc_allocate(arg);
			release_console_sem();
			if (ret)
				break;
			set_console(arg);
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
975

Alan Cox's avatar
Alan Cox committed
976 977 978 979 980 981 982 983
	case VT_SETACTIVATE:
	{
		struct vt_setactivate vsa;

		if (!perm)
			goto eperm;

		if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
Jiri Slaby's avatar
Jiri Slaby committed
984 985 986 987
					sizeof(struct vt_setactivate))) {
			ret = -EFAULT;
			goto out;
		}
Alan Cox's avatar
Alan Cox committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
			ret = -ENXIO;
		else {
			vsa.console--;
			acquire_console_sem();
			ret = vc_allocate(vsa.console);
			if (ret == 0) {
				struct vc_data *nvc;
				/* This is safe providing we don't drop the
				   console sem between vc_allocate and
				   finishing referencing nvc */
				nvc = vc_cons[vsa.console].d;
				nvc->vt_mode = vsa.mode;
				nvc->vt_mode.frsig = 0;
				put_pid(nvc->vt_pid);
				nvc->vt_pid = get_pid(task_pid(current));
			}
			release_console_sem();
			if (ret)
				break;
			/* Commence switch and lock */
			set_console(arg);
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016 1017
	/*
	 * wait until the specified VT has been activated
	 */
	case VT_WAITACTIVE:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1018
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1019
		if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox's avatar
Alan Cox committed
1020 1021
			ret = -ENXIO;
		else
Alan Cox's avatar
Alan Cox committed
1022
			ret = vt_waitactive(arg);
Alan Cox's avatar
Alan Cox committed
1023
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

	/*
	 * If a vt is under process control, the kernel will not switch to it
	 * immediately, but postpone the operation until the process calls this
	 * ioctl, allowing the switch to complete.
	 *
	 * According to the X sources this is the behavior:
	 *	0:	pending switch-from not OK
	 *	1:	pending switch-from OK
	 *	2:	completed switch-to OK
	 */
	case VT_RELDISP:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1037
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1038

Alan Cox's avatar
Alan Cox committed
1039 1040 1041 1042
		if (vc->vt_mode.mode != VT_PROCESS) {
			ret = -EINVAL;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045
		/*
		 * Switching-from response
		 */
Samuel Ortiz's avatar
Samuel Ortiz committed
1046
		acquire_console_sem();
Linus Torvalds's avatar
Linus Torvalds committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		if (vc->vt_newvt >= 0) {
			if (arg == 0)
				/*
				 * Switch disallowed, so forget we were trying
				 * to do it.
				 */
				vc->vt_newvt = -1;

			else {
				/*
				 * The current vt has been released, so
				 * complete the switch.
				 */
				int newvt;
				newvt = vc->vt_newvt;
				vc->vt_newvt = -1;
Alan Cox's avatar
Alan Cox committed
1063 1064
				ret = vc_allocate(newvt);
				if (ret) {
Linus Torvalds's avatar
Linus Torvalds committed
1065
					release_console_sem();
Alan Cox's avatar
Alan Cox committed
1066
					break;
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069 1070 1071 1072 1073 1074
				}
				/*
				 * When we actually do the console switch,
				 * make sure we are atomic with respect to
				 * other console switches..
				 */
				complete_change_console(vc_cons[newvt].d);
			}
Alan Cox's avatar
Alan Cox committed
1075 1076 1077 1078
		} else {
			/*
			 * Switched-to response
			 */
Linus Torvalds's avatar
Linus Torvalds committed
1079 1080 1081
			/*
			 * If it's just an ACK, ignore it
			 */
Alan Cox's avatar
Alan Cox committed
1082 1083
			if (arg != VT_ACKACQ)
				ret = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1084
		}
Samuel Ortiz's avatar
Samuel Ortiz committed
1085
		release_console_sem();
Alan Cox's avatar
Alan Cox committed
1086
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1087 1088 1089 1090 1091

	 /*
	  * Disallocate memory associated to VT (but leave VT1)
	  */
	 case VT_DISALLOCATE:
Alan Cox's avatar
Alan Cox committed
1092 1093 1094 1095
		if (arg > MAX_NR_CONSOLES) {
			ret = -ENXIO;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1096
		if (arg == 0) {
Alan Cox's avatar
Alan Cox committed
1097
		    /* deallocate all unused consoles, but leave 0 */
Linus Torvalds's avatar
Linus Torvalds committed
1098 1099 1100
			acquire_console_sem();
			for (i=1; i<MAX_NR_CONSOLES; i++)
				if (! VT_BUSY(i))
Alan Cox's avatar
Alan Cox committed
1101
					vc_deallocate(i);
Linus Torvalds's avatar
Linus Torvalds committed
1102 1103
			release_console_sem();
		} else {
Alan Cox's avatar
Alan Cox committed
1104
			/* deallocate a single console, if possible */
Linus Torvalds's avatar
Linus Torvalds committed
1105 1106
			arg--;
			if (VT_BUSY(arg))
Alan Cox's avatar
Alan Cox committed
1107 1108
				ret = -EBUSY;
			else if (arg) {			      /* leave 0 */
Linus Torvalds's avatar
Linus Torvalds committed
1109
				acquire_console_sem();
Alan Cox's avatar
Alan Cox committed
1110
				vc_deallocate(arg);
Linus Torvalds's avatar
Linus Torvalds committed
1111 1112 1113
				release_console_sem();
			}
		}
Alan Cox's avatar
Alan Cox committed
1114
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116 1117 1118

	case VT_RESIZE:
	{
		struct vt_sizes __user *vtsizes = up;
1119 1120
		struct vc_data *vc;

Linus Torvalds's avatar
Linus Torvalds committed
1121 1122
		ushort ll,cc;
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1123
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125
		if (get_user(ll, &vtsizes->v_rows) ||
		    get_user(cc, &vtsizes->v_cols))
Alan Cox's avatar
Alan Cox committed
1126 1127
			ret = -EFAULT;
		else {
1128
			acquire_console_sem();
Alan Cox's avatar
Alan Cox committed
1129 1130
			for (i = 0; i < MAX_NR_CONSOLES; i++) {
				vc = vc_cons[i].d;
1131

Alan Cox's avatar
Alan Cox committed
1132 1133
				if (vc) {
					vc->vc_resize_user = 1;
1134
					vc_resize(vc_cons[i].d, cc, ll);
Alan Cox's avatar
Alan Cox committed
1135
				}
1136
			}
1137
			release_console_sem();
1138
		}
Alan Cox's avatar
Alan Cox committed
1139
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1140 1141 1142 1143 1144 1145 1146
	}

	case VT_RESIZEX:
	{
		struct vt_consize __user *vtconsize = up;
		ushort ll,cc,vlin,clin,vcol,ccol;
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1147
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1148
		if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox's avatar
Alan Cox committed
1149 1150 1151 1152 1153
				sizeof(struct vt_consize))) {
			ret = -EFAULT;
			break;
		}
		/* FIXME: Should check the copies properly */
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155 1156 1157 1158 1159 1160 1161 1162
		__get_user(ll, &vtconsize->v_rows);
		__get_user(cc, &vtconsize->v_cols);
		__get_user(vlin, &vtconsize->v_vlin);
		__get_user(clin, &vtconsize->v_clin);
		__get_user(vcol, &vtconsize->v_vcol);
		__get_user(ccol, &vtconsize->v_ccol);
		vlin = vlin ? vlin : vc->vc_scan_lines;
		if (clin) {
			if (ll) {
Alan Cox's avatar
Alan Cox committed
1163 1164 1165 1166 1167
				if (ll != vlin/clin) {
					/* Parameters don't add up */
					ret = -EINVAL;
					break;
				}
Linus Torvalds's avatar
Linus Torvalds committed
1168 1169 1170 1171 1172
			} else 
				ll = vlin/clin;
		}
		if (vcol && ccol) {
			if (cc) {
Alan Cox's avatar
Alan Cox committed
1173 1174 1175 1176
				if (cc != vcol/ccol) {
					ret = -EINVAL;
					break;
				}
Linus Torvalds's avatar
Linus Torvalds committed
1177 1178 1179 1180
			} else
				cc = vcol/ccol;
		}

Alan Cox's avatar
Alan Cox committed
1181 1182 1183 1184
		if (clin > 32) {
			ret =  -EINVAL;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186 1187 1188 1189 1190 1191 1192 1193
		    
		for (i = 0; i < MAX_NR_CONSOLES; i++) {
			if (!vc_cons[i].d)
				continue;
			acquire_console_sem();
			if (vlin)
				vc_cons[i].d->vc_scan_lines = vlin;
			if (clin)
				vc_cons[i].d->vc_font.height = clin;
1194
			vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196 1197
			vc_resize(vc_cons[i].d, cc, ll);
			release_console_sem();
		}
Alan Cox's avatar
Alan Cox committed
1198
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1199 1200 1201 1202
	}

	case PIO_FONT: {
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1203
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1204 1205 1206 1207 1208 1209
		op.op = KD_FONT_OP_SET;
		op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC;	/* Compatibility */
		op.width = 8;
		op.height = 0;
		op.charcount = 256;
		op.data = up;
Alan Cox's avatar
Alan Cox committed
1210 1211
		ret = con_font_op(vc_cons[fg_console].d, &op);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1212 1213 1214 1215 1216 1217 1218 1219 1220
	}

	case GIO_FONT: {
		op.op = KD_FONT_OP_GET;
		op.flags = KD_FONT_FLAG_OLD;
		op.width = 8;
		op.height = 32;
		op.charcount = 256;
		op.data = up;
Alan Cox's avatar
Alan Cox committed
1221 1222
		ret = con_font_op(vc_cons[fg_console].d, &op);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1223 1224 1225 1226
	}

	case PIO_CMAP:
                if (!perm)
Alan Cox's avatar
Alan Cox committed
1227 1228 1229 1230
			ret = -EPERM;
		else
	                ret = con_set_cmap(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1231 1232

	case GIO_CMAP:
Alan Cox's avatar
Alan Cox committed
1233 1234
                ret = con_get_cmap(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1235 1236 1237

	case PIO_FONTX:
	case GIO_FONTX:
Alan Cox's avatar
Alan Cox committed
1238 1239
		ret = do_fontx_ioctl(cmd, up, perm, &op);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1240 1241 1242 1243

	case PIO_FONTRESET:
	{
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1244
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1245 1246 1247 1248

#ifdef BROKEN_GRAPHICS_PROGRAMS
		/* With BROKEN_GRAPHICS_PROGRAMS defined, the default
		   font is not saved. */
Alan Cox's avatar
Alan Cox committed
1249 1250
		ret = -ENOSYS;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1251 1252 1253 1254
#else
		{
		op.op = KD_FONT_OP_SET_DEFAULT;
		op.data = NULL;
Alan Cox's avatar
Alan Cox committed
1255 1256 1257
		ret = con_font_op(vc_cons[fg_console].d, &op);
		if (ret)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1258
		con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox's avatar
Alan Cox committed
1259
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1260 1261 1262 1263 1264
		}
#endif
	}

	case KDFONTOP: {
Alan Cox's avatar
Alan Cox committed
1265 1266 1267 1268
		if (copy_from_user(&op, up, sizeof(op))) {
			ret = -EFAULT;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1269
		if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox's avatar
Alan Cox committed
1270 1271 1272 1273
			goto eperm;
		ret = con_font_op(vc, &op);
		if (ret)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1274
		if (copy_to_user(up, &op, sizeof(op)))
Alan Cox's avatar
Alan Cox committed
1275 1276
			ret = -EFAULT;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279 1280
	}

	case PIO_SCRNMAP:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1281 1282 1283 1284
			ret = -EPERM;
		else
			ret = con_set_trans_old(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1285 1286

	case GIO_SCRNMAP:
Alan Cox's avatar
Alan Cox committed
1287 1288
		ret = con_get_trans_old(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290 1291

	case PIO_UNISCRNMAP:
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1292 1293 1294 1295
			ret = -EPERM;
		else
			ret = con_set_trans_new(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297

	case GIO_UNISCRNMAP:
Alan Cox's avatar
Alan Cox committed
1298 1299
		ret = con_get_trans_new(up);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301 1302 1303

	case PIO_UNIMAPCLR:
	      { struct unimapinit ui;
		if (!perm)
Alan Cox's avatar
Alan Cox committed
1304 1305
			goto eperm;
		ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1306 1307 1308
		if (ret)
			ret = -EFAULT;
		else
Alan Cox's avatar
Alan Cox committed
1309 1310
			con_clear_unimap(vc, &ui);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1311 1312 1313 1314
	      }

	case PIO_UNIMAP:
	case GIO_UNIMAP:
Alan Cox's avatar
Alan Cox committed
1315 1316
		ret = do_unimap_ioctl(cmd, up, perm, vc);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1317 1318 1319

	case VT_LOCKSWITCH:
		if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox's avatar
Alan Cox committed
1320
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1321
		vt_dont_switch = 1;
Alan Cox's avatar
Alan Cox committed
1322
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1323 1324
	case VT_UNLOCKSWITCH:
		if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox's avatar
Alan Cox committed
1325
			goto eperm;
Linus Torvalds's avatar
Linus Torvalds committed
1326
		vt_dont_switch = 0;
Alan Cox's avatar
Alan Cox committed
1327
		break;
1328
	case VT_GETHIFONTMASK:
Alan Cox's avatar
Alan Cox committed
1329 1330 1331
		ret = put_user(vc->vc_hi_font_mask,
					(unsigned short __user *)arg);
		break;
Alan Cox's avatar
Alan Cox committed
1332 1333 1334
	case VT_WAITEVENT:
		ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1335
	default:
Alan Cox's avatar
Alan Cox committed
1336
		ret = -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1337
	}
Alan Cox's avatar
Alan Cox committed
1338
out:
1339
	tty_unlock();
Alan Cox's avatar
Alan Cox committed
1340 1341 1342 1343
	return ret;
eperm:
	ret = -EPERM;
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1344 1345 1346 1347 1348
}

void reset_vc(struct vc_data *vc)
{
	vc->vc_mode = KD_TEXT;
Bill Nottingham's avatar
Bill Nottingham committed
1349
	kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds's avatar
Linus Torvalds committed
1350 1351 1352 1353 1354
	vc->vt_mode.mode = VT_AUTO;
	vc->vt_mode.waitv = 0;
	vc->vt_mode.relsig = 0;
	vc->vt_mode.acqsig = 0;
	vc->vt_mode.frsig = 0;
1355 1356
	put_pid(vc->vt_pid);
	vc->vt_pid = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358 1359 1360 1361
	vc->vt_newvt = -1;
	if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
		reset_palette(vc);
}

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
void vc_SAK(struct work_struct *work)
{
	struct vc *vc_con =
		container_of(work, struct vc, SAK_work);
	struct vc_data *vc;
	struct tty_struct *tty;

	acquire_console_sem();
	vc = vc_con->d;
	if (vc) {
1372
		tty = vc->port.tty;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
		/*
		 * SAK should also work in all raw modes and reset
		 * them properly.
		 */
		if (tty)
			__do_SAK(tty);
		reset_vc(vc);
	}
	release_console_sem();
}

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
#ifdef CONFIG_COMPAT

struct compat_consolefontdesc {
	unsigned short charcount;       /* characters in font (256 or 512) */
	unsigned short charheight;      /* scan lines per character (1-32) */
	compat_caddr_t chardata;	/* font data in expanded form */
};

static inline int
compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
			 int perm, struct console_font_op *op)
{
	struct compat_consolefontdesc cfdarg;
	int i;

	if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
		return -EFAULT;

	switch (cmd) {
	case PIO_FONTX:
		if (!perm)
			return -EPERM;
		op->op = KD_FONT_OP_SET;
		op->flags = KD_FONT_FLAG_OLD;
		op->width = 8;
		op->height = cfdarg.charheight;
		op->charcount = cfdarg.charcount;
		op->data = compat_ptr(cfdarg.chardata);
		return con_font_op(vc_cons[fg_console].d, op);
	case GIO_FONTX:
		op->op = KD_FONT_OP_GET;
		op->flags = KD_FONT_FLAG_OLD;
		op->width = 8;
		op->height = cfdarg.charheight;
		op->charcount = cfdarg.charcount;
		op->data = compat_ptr(cfdarg.chardata);
		i = con_font_op(vc_cons[fg_console].d, op);
		if (i)
			return i;
		cfdarg.charheight = op->height;
		cfdarg.charcount = op->charcount;
		if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
			return -EFAULT;
		return 0;
	}
	return -EINVAL;
}

struct compat_console_font_op {
	compat_uint_t op;        /* operation code KD_FONT_OP_* */
	compat_uint_t flags;     /* KD_FONT_FLAG_* */
	compat_uint_t width, height;     /* font size */
	compat_uint_t charcount;
	compat_caddr_t data;    /* font data with height fixed to 32 */
};

static inline int
compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
			 int perm, struct console_font_op *op, struct vc_data *vc)
{
	int i;

	if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
		return -EFAULT;
	if (!perm && op->op != KD_FONT_OP_GET)
		return -EPERM;
	op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
	op->flags |= KD_FONT_FLAG_OLD;
	i = con_font_op(vc, op);
	if (i)
		return i;
	((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
	if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
		return -EFAULT;
	return 0;
}

struct compat_unimapdesc {
	unsigned short entry_ct;
	compat_caddr_t entries;
};

static inline int
compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
			 int perm, struct vc_data *vc)
{
	struct compat_unimapdesc tmp;
	struct unipair __user *tmp_entries