vt_ioctl.c 32.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  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>
17
#include <linux/compat.h>
18
#include <linux/module.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25
#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>
S
Samuel Thibault 已提交
26
#include <linux/consolemap.h>
27
#include <linux/signal.h>
28
#include <linux/suspend.h>
29
#include <linux/timex.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38

#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>

39
char vt_dont_switch;
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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);

A
Alan Cox 已提交
64 65 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
/*
 *	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 */
A
Alan Cox 已提交
104 105
		ve->event.oldev = old + 1;
		ve->event.newev = new + 1;
A
Alan Cox 已提交
106 107 108 109 110 111 112 113
		wake = 1;
		ve->done = 1;
	}
	spin_unlock_irqrestore(&vt_event_lock, flags);
	if (wake)
		wake_up_interruptible(&vt_event_waitqueue);
}

R
Rabin Vincent 已提交
114
static void __vt_event_queue(struct vt_event_wait *vw)
A
Alan Cox 已提交
115 116 117 118 119 120 121 122 123
{
	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);
R
Rabin Vincent 已提交
124 125 126 127
}

static void __vt_event_wait(struct vt_event_wait *vw)
{
A
Alan Cox 已提交
128
	/* Wait for it to pass */
129
	wait_event_interruptible(vt_event_waitqueue, vw->done);
R
Rabin Vincent 已提交
130 131 132 133 134 135
}

static void __vt_event_dequeue(struct vt_event_wait *vw)
{
	unsigned long flags;

A
Alan Cox 已提交
136 137 138 139 140 141
	/* Dequeue it */
	spin_lock_irqsave(&vt_event_lock, flags);
	list_del(&vw->list);
	spin_unlock_irqrestore(&vt_event_lock, flags);
}

R
Rabin Vincent 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/**
 *	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)
{
	__vt_event_queue(vw);
	__vt_event_wait(vw);
	__vt_event_dequeue(vw);
}

A
Alan Cox 已提交
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 189 190 191 192 193 194 195 196 197 198
/**
 *	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 {
		vw.event.event = VT_EVENT_SWITCH;
R
Rabin Vincent 已提交
199 200 201 202 203 204 205
		__vt_event_queue(&vw);
		if (n == fg_console + 1) {
			__vt_event_dequeue(&vw);
			break;
		}
		__vt_event_wait(&vw);
		__vt_event_dequeue(&vw);
A
Alan Cox 已提交
206 207
		if (vw.done == 0)
			return -EINTR;
A
Alan Cox 已提交
208
	} while (vw.event.newev != n);
A
Alan Cox 已提交
209 210 211
	return 0;
}

L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
 * 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)



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;
}

A
Alan Cox 已提交
286 287


L
Linus Torvalds 已提交
288 289 290 291
/*
 * We handle the console-specific ioctl's here.  We allow the
 * capability to modify any console, not just the fg_console. 
 */
292
int vt_ioctl(struct tty_struct *tty,
L
Linus Torvalds 已提交
293 294
	     unsigned int cmd, unsigned long arg)
{
A
Alan Cox 已提交
295
	struct vc_data *vc = tty->driver_data;
L
Linus Torvalds 已提交
296 297 298
	struct console_font_op op;	/* used in multiple places here */
	unsigned int console;
	unsigned char ucval;
299
	unsigned int uival;
L
Linus Torvalds 已提交
300 301
	void __user *up = (void __user *)arg;
	int i, perm;
A
Alan Cox 已提交
302 303
	int ret = 0;

L
Linus Torvalds 已提交
304 305
	console = vc->vc_num;

A
Alan Cox 已提交
306 307 308 309 310 311

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

L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321

	/*
	 * 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;
 
	switch (cmd) {
A
Alan Cox 已提交
322
	case TIOCLINUX:
J
Jiri Slaby 已提交
323 324
		ret = tioclinux(tty, arg);
		break;
L
Linus Torvalds 已提交
325 326
	case KIOCSOUND:
		if (!perm)
327
			return -EPERM;
328 329 330 331
		/*
		 * The use of PIT_TICK_RATE is historic, it used to be
		 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
		 * and 2.6.36, which was a minor but unfortunate ABI
332
		 * change. kd_mksound is locked by the input layer.
333
		 */
L
Linus Torvalds 已提交
334
		if (arg)
335
			arg = PIT_TICK_RATE / arg;
L
Linus Torvalds 已提交
336
		kd_mksound(arg, 0);
A
Alan Cox 已提交
337
		break;
L
Linus Torvalds 已提交
338 339 340

	case KDMKTONE:
		if (!perm)
341
			return -EPERM;
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351
	{
		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;
		if (count)
352
			count = PIT_TICK_RATE / count;
L
Linus Torvalds 已提交
353
		kd_mksound(count, ticks);
A
Alan Cox 已提交
354
		break;
L
Linus Torvalds 已提交
355 356 357 358
	}

	case KDGKBTYPE:
		/*
359
		 * this is naïve.
L
Linus Torvalds 已提交
360 361
		 */
		ucval = KB_101;
A
Alan Cox 已提交
362 363
		ret = put_user(ucval, (char __user *)arg);
		break;
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376

		/*
		 * 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...
377 378
		 *
		 * These are locked internally via sys_ioperm
L
Linus Torvalds 已提交
379
		 */
A
Alan Cox 已提交
380 381 382 383 384 385
		if (arg < GPFIRST || arg > GPLAST) {
			ret = -EINVAL;
			break;
		}
		ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
		break;
L
Linus Torvalds 已提交
386 387 388

	case KDENABIO:
	case KDDISABIO:
A
Alan Cox 已提交
389
		ret = sys_ioperm(GPFIRST, GPNUM,
L
Linus Torvalds 已提交
390
				  (cmd == KDENABIO)) ? -ENXIO : 0;
A
Alan Cox 已提交
391
		break;
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400
#endif

	/* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
		
	case KDKBDREP:
	{
		struct kbd_repeat kbrep;
		
		if (!capable(CAP_SYS_TTY_CONFIG))
401
			return -EPERM;
L
Linus Torvalds 已提交
402

A
Alan Cox 已提交
403 404 405 406 407 408 409
		if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
			ret =  -EFAULT;
			break;
		}
		ret = kbd_rate(&kbrep);
		if (ret)
			break;
L
Linus Torvalds 已提交
410
		if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
A
Alan Cox 已提交
411 412
			ret = -EFAULT;
		break;
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423 424
	}

	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)
425
			return -EPERM;
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434
		switch (arg) {
		case KD_GRAPHICS:
			break;
		case KD_TEXT0:
		case KD_TEXT1:
			arg = KD_TEXT;
		case KD_TEXT:
			break;
		default:
A
Alan Cox 已提交
435 436
			ret = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
437
		}
438
		/* FIXME: this needs the console lock extending */
L
Linus Torvalds 已提交
439
		if (vc->vc_mode == (unsigned char) arg)
A
Alan Cox 已提交
440
			break;
L
Linus Torvalds 已提交
441 442
		vc->vc_mode = (unsigned char) arg;
		if (console != fg_console)
A
Alan Cox 已提交
443
			break;
L
Linus Torvalds 已提交
444 445 446
		/*
		 * explicitly blank/unblank the screen if switching modes
		 */
447
		console_lock();
L
Linus Torvalds 已提交
448 449 450 451
		if (arg == KD_TEXT)
			do_unblank_screen(1);
		else
			do_blank_screen(1);
452
		console_unlock();
A
Alan Cox 已提交
453
		break;
L
Linus Torvalds 已提交
454 455

	case KDGETMODE:
456
		uival = vc->vc_mode;
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464
		goto setint;

	case KDMAPDISP:
	case KDUNMAPDISP:
		/*
		 * these work like a combination of mmap and KDENABIO.
		 * this could be easily finished.
		 */
A
Alan Cox 已提交
465 466
		ret = -EINVAL;
		break;
L
Linus Torvalds 已提交
467 468 469

	case KDSKBMODE:
		if (!perm)
470
			return -EPERM;
A
Alan Cox 已提交
471 472 473
		ret = vt_do_kdskbmode(console, arg);
		if (ret == 0)
			tty_ldisc_flush(tty);
A
Alan Cox 已提交
474
		break;
L
Linus Torvalds 已提交
475 476

	case KDGKBMODE:
A
Alan Cox 已提交
477 478 479
		uival = vt_do_kdgkbmode(console);
		ret = put_user(uival, (int __user *)arg);
		break;
L
Linus Torvalds 已提交
480 481 482 483

	/* this could be folded into KDSKBMODE, but for compatibility
	   reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
	case KDSKBMETA:
A
Alan Cox 已提交
484
		ret = vt_do_kdskbmeta(console, arg);
A
Alan Cox 已提交
485
		break;
L
Linus Torvalds 已提交
486 487

	case KDGKBMETA:
A
Alan Cox 已提交
488 489
		/* FIXME: should review whether this is worth locking */
		uival = vt_do_kdgkbmeta(console);
L
Linus Torvalds 已提交
490
	setint:
491
		ret = put_user(uival, (int __user *)arg);
A
Alan Cox 已提交
492
		break;
L
Linus Torvalds 已提交
493 494 495 496

	case KDGETKEYCODE:
	case KDSETKEYCODE:
		if(!capable(CAP_SYS_TTY_CONFIG))
A
Alan Cox 已提交
497
			perm = 0;
A
Alan Cox 已提交
498
		ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
A
Alan Cox 已提交
499
		break;
L
Linus Torvalds 已提交
500 501 502

	case KDGKBENT:
	case KDSKBENT:
A
Alan Cox 已提交
503
		ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
A
Alan Cox 已提交
504
		break;
L
Linus Torvalds 已提交
505 506 507

	case KDGKBSENT:
	case KDSKBSENT:
A
Alan Cox 已提交
508
		ret = vt_do_kdgkb_ioctl(cmd, up, perm);
A
Alan Cox 已提交
509
		break;
L
Linus Torvalds 已提交
510

A
Alan Cox 已提交
511 512
	/* Diacritical processing. Handled in keyboard.c as it has
	   to operate on the keyboard locks and structures */
L
Linus Torvalds 已提交
513
	case KDGKBDIACR:
S
Samuel Thibault 已提交
514
	case KDGKBDIACRUC:
L
Linus Torvalds 已提交
515
	case KDSKBDIACR:
S
Samuel Thibault 已提交
516
	case KDSKBDIACRUC:
A
Alan Cox 已提交
517
		ret = vt_do_diacrit(cmd, up, perm);
A
Alan Cox 已提交
518
		break;
L
Linus Torvalds 已提交
519 520 521 522 523 524 525

	/* the ioctls below read/set the flags usually shown in the leds */
	/* don't use them - they will go away without warning */
	case KDGKBLED:
	case KDSKBLED:
	case KDGETLED:
	case KDSETLED:
A
Alan Cox 已提交
526
		ret = vt_do_kdskled(console, cmd, arg, perm);
A
Alan Cox 已提交
527
		break;
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536 537 538

	/*
	 * 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))
539
			return -EPERM;
540
		if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
A
Alan Cox 已提交
541 542 543 544 545 546 547 548 549
			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;
L
Linus Torvalds 已提交
550 551 552 553 554 555 556
	}

	case VT_SETMODE:
	{
		struct vt_mode tmp;

		if (!perm)
557
			return -EPERM;
A
Alan Cox 已提交
558 559 560 561
		if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
			ret = -EFAULT;
			goto out;
		}
562
		if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
A
Alan Cox 已提交
563 564 565
			ret = -EINVAL;
			goto out;
		}
566
		console_lock();
L
Linus Torvalds 已提交
567 568 569
		vc->vt_mode = tmp;
		/* the frsig is ignored, so we set it to 0 */
		vc->vt_mode.frsig = 0;
570 571
		put_pid(vc->vt_pid);
		vc->vt_pid = get_pid(task_pid(current));
L
Linus Torvalds 已提交
572 573
		/* no switch is required -- saw@shade.msu.ru */
		vc->vt_newvt = -1;
574
		console_unlock();
A
Alan Cox 已提交
575
		break;
L
Linus Torvalds 已提交
576 577 578 579 580 581 582
	}

	case VT_GETMODE:
	{
		struct vt_mode tmp;
		int rc;

583
		console_lock();
L
Linus Torvalds 已提交
584
		memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
585
		console_unlock();
L
Linus Torvalds 已提交
586 587

		rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
A
Alan Cox 已提交
588 589 590
		if (rc)
			ret = -EFAULT;
		break;
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602
	}

	/*
	 * 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;

603
		/* Review: FIXME: Console lock ? */
L
Linus Torvalds 已提交
604
		if (put_user(fg_console + 1, &vtstat->v_active))
A
Alan Cox 已提交
605 606 607 608 609 610 611 612 613 614
			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;
L
Linus Torvalds 已提交
615 616 617 618 619 620
	}

	/*
	 * Returns the first available (non-opened) console.
	 */
	case VT_OPENQRY:
621
		/* FIXME: locking ? - but then this is a stupid API */
L
Linus Torvalds 已提交
622 623 624
		for (i = 0; i < MAX_NR_CONSOLES; ++i)
			if (! VT_IS_IN_USE(i))
				break;
625
		uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634
		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)
635
			return -EPERM;
L
Linus Torvalds 已提交
636
		if (arg == 0 || arg > MAX_NR_CONSOLES)
A
Alan Cox 已提交
637 638 639
			ret =  -ENXIO;
		else {
			arg--;
640
			console_lock();
A
Alan Cox 已提交
641
			ret = vc_allocate(arg);
642
			console_unlock();
A
Alan Cox 已提交
643 644 645 646 647
			if (ret)
				break;
			set_console(arg);
		}
		break;
L
Linus Torvalds 已提交
648

A
Alan Cox 已提交
649 650 651 652 653
	case VT_SETACTIVATE:
	{
		struct vt_setactivate vsa;

		if (!perm)
654
			return -EPERM;
A
Alan Cox 已提交
655 656

		if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
J
Jiri Slaby 已提交
657 658 659 660
					sizeof(struct vt_setactivate))) {
			ret = -EFAULT;
			goto out;
		}
A
Alan Cox 已提交
661 662 663 664
		if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
			ret = -ENXIO;
		else {
			vsa.console--;
665
			console_lock();
A
Alan Cox 已提交
666 667 668 669 670 671 672 673 674 675 676 677
			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));
			}
678
			console_unlock();
A
Alan Cox 已提交
679 680 681
			if (ret)
				break;
			/* Commence switch and lock */
682
			/* Review set_console locks */
683
			set_console(vsa.console);
A
Alan Cox 已提交
684
		}
685
		break;
A
Alan Cox 已提交
686 687
	}

L
Linus Torvalds 已提交
688 689 690 691 692
	/*
	 * wait until the specified VT has been activated
	 */
	case VT_WAITACTIVE:
		if (!perm)
693
			return -EPERM;
L
Linus Torvalds 已提交
694
		if (arg == 0 || arg > MAX_NR_CONSOLES)
A
Alan Cox 已提交
695
			ret = -ENXIO;
696
		else
A
Alan Cox 已提交
697
			ret = vt_waitactive(arg);
A
Alan Cox 已提交
698
		break;
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711

	/*
	 * 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)
712
			return -EPERM;
L
Linus Torvalds 已提交
713

714
		console_lock();
A
Alan Cox 已提交
715
		if (vc->vt_mode.mode != VT_PROCESS) {
716
			console_unlock();
A
Alan Cox 已提交
717 718 719
			ret = -EINVAL;
			break;
		}
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
		/*
		 * Switching-from response
		 */
		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;
A
Alan Cox 已提交
739 740
				ret = vc_allocate(newvt);
				if (ret) {
741
					console_unlock();
A
Alan Cox 已提交
742
					break;
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750
				}
				/*
				 * 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);
			}
A
Alan Cox 已提交
751 752 753 754
		} else {
			/*
			 * Switched-to response
			 */
L
Linus Torvalds 已提交
755 756 757
			/*
			 * If it's just an ACK, ignore it
			 */
A
Alan Cox 已提交
758 759
			if (arg != VT_ACKACQ)
				ret = -EINVAL;
L
Linus Torvalds 已提交
760
		}
761
		console_unlock();
A
Alan Cox 已提交
762
		break;
L
Linus Torvalds 已提交
763 764 765 766 767

	 /*
	  * Disallocate memory associated to VT (but leave VT1)
	  */
	 case VT_DISALLOCATE:
A
Alan Cox 已提交
768 769 770 771
		if (arg > MAX_NR_CONSOLES) {
			ret = -ENXIO;
			break;
		}
L
Linus Torvalds 已提交
772
		if (arg == 0) {
A
Alan Cox 已提交
773
		    /* deallocate all unused consoles, but leave 0 */
774
			console_lock();
L
Linus Torvalds 已提交
775 776
			for (i=1; i<MAX_NR_CONSOLES; i++)
				if (! VT_BUSY(i))
A
Alan Cox 已提交
777
					vc_deallocate(i);
778
			console_unlock();
L
Linus Torvalds 已提交
779
		} else {
A
Alan Cox 已提交
780
			/* deallocate a single console, if possible */
L
Linus Torvalds 已提交
781 782
			arg--;
			if (VT_BUSY(arg))
A
Alan Cox 已提交
783 784
				ret = -EBUSY;
			else if (arg) {			      /* leave 0 */
785
				console_lock();
A
Alan Cox 已提交
786
				vc_deallocate(arg);
787
				console_unlock();
L
Linus Torvalds 已提交
788 789
			}
		}
A
Alan Cox 已提交
790
		break;
L
Linus Torvalds 已提交
791 792 793 794

	case VT_RESIZE:
	{
		struct vt_sizes __user *vtsizes = up;
795 796
		struct vc_data *vc;

L
Linus Torvalds 已提交
797 798
		ushort ll,cc;
		if (!perm)
799
			return -EPERM;
L
Linus Torvalds 已提交
800 801
		if (get_user(ll, &vtsizes->v_rows) ||
		    get_user(cc, &vtsizes->v_cols))
A
Alan Cox 已提交
802 803
			ret = -EFAULT;
		else {
804
			console_lock();
A
Alan Cox 已提交
805 806
			for (i = 0; i < MAX_NR_CONSOLES; i++) {
				vc = vc_cons[i].d;
807

A
Alan Cox 已提交
808 809
				if (vc) {
					vc->vc_resize_user = 1;
810
					/* FIXME: review v tty lock */
811
					vc_resize(vc_cons[i].d, cc, ll);
A
Alan Cox 已提交
812
				}
813
			}
814
			console_unlock();
815
		}
A
Alan Cox 已提交
816
		break;
L
Linus Torvalds 已提交
817 818 819 820 821 822 823
	}

	case VT_RESIZEX:
	{
		struct vt_consize __user *vtconsize = up;
		ushort ll,cc,vlin,clin,vcol,ccol;
		if (!perm)
824
			return -EPERM;
L
Linus Torvalds 已提交
825
		if (!access_ok(VERIFY_READ, vtconsize,
A
Alan Cox 已提交
826 827 828 829 830
				sizeof(struct vt_consize))) {
			ret = -EFAULT;
			break;
		}
		/* FIXME: Should check the copies properly */
L
Linus Torvalds 已提交
831 832 833 834 835 836 837 838 839
		__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) {
A
Alan Cox 已提交
840 841 842 843 844
				if (ll != vlin/clin) {
					/* Parameters don't add up */
					ret = -EINVAL;
					break;
				}
L
Linus Torvalds 已提交
845 846 847 848 849
			} else 
				ll = vlin/clin;
		}
		if (vcol && ccol) {
			if (cc) {
A
Alan Cox 已提交
850 851 852 853
				if (cc != vcol/ccol) {
					ret = -EINVAL;
					break;
				}
L
Linus Torvalds 已提交
854 855 856 857
			} else
				cc = vcol/ccol;
		}

A
Alan Cox 已提交
858 859 860 861
		if (clin > 32) {
			ret =  -EINVAL;
			break;
		}
L
Linus Torvalds 已提交
862 863 864 865
		    
		for (i = 0; i < MAX_NR_CONSOLES; i++) {
			if (!vc_cons[i].d)
				continue;
866
			console_lock();
L
Linus Torvalds 已提交
867 868 869 870
			if (vlin)
				vc_cons[i].d->vc_scan_lines = vlin;
			if (clin)
				vc_cons[i].d->vc_font.height = clin;
871
			vc_cons[i].d->vc_resize_user = 1;
L
Linus Torvalds 已提交
872
			vc_resize(vc_cons[i].d, cc, ll);
873
			console_unlock();
L
Linus Torvalds 已提交
874
		}
A
Alan Cox 已提交
875
		break;
L
Linus Torvalds 已提交
876 877 878 879
	}

	case PIO_FONT: {
		if (!perm)
880
			return -EPERM;
L
Linus Torvalds 已提交
881 882 883 884 885 886
		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;
A
Alan Cox 已提交
887 888
		ret = con_font_op(vc_cons[fg_console].d, &op);
		break;
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896 897
	}

	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;
A
Alan Cox 已提交
898 899
		ret = con_font_op(vc_cons[fg_console].d, &op);
		break;
L
Linus Torvalds 已提交
900 901 902 903
	}

	case PIO_CMAP:
                if (!perm)
A
Alan Cox 已提交
904 905 906 907
			ret = -EPERM;
		else
	                ret = con_set_cmap(up);
		break;
L
Linus Torvalds 已提交
908 909

	case GIO_CMAP:
A
Alan Cox 已提交
910 911
                ret = con_get_cmap(up);
		break;
L
Linus Torvalds 已提交
912 913 914

	case PIO_FONTX:
	case GIO_FONTX:
A
Alan Cox 已提交
915 916
		ret = do_fontx_ioctl(cmd, up, perm, &op);
		break;
L
Linus Torvalds 已提交
917 918 919 920

	case PIO_FONTRESET:
	{
		if (!perm)
921
			return -EPERM;
L
Linus Torvalds 已提交
922 923 924 925

#ifdef BROKEN_GRAPHICS_PROGRAMS
		/* With BROKEN_GRAPHICS_PROGRAMS defined, the default
		   font is not saved. */
A
Alan Cox 已提交
926 927
		ret = -ENOSYS;
		break;
L
Linus Torvalds 已提交
928 929 930 931
#else
		{
		op.op = KD_FONT_OP_SET_DEFAULT;
		op.data = NULL;
A
Alan Cox 已提交
932 933 934
		ret = con_font_op(vc_cons[fg_console].d, &op);
		if (ret)
			break;
935
		console_lock();
L
Linus Torvalds 已提交
936
		con_set_default_unimap(vc_cons[fg_console].d);
937
		console_unlock();
A
Alan Cox 已提交
938
		break;
L
Linus Torvalds 已提交
939 940 941 942 943
		}
#endif
	}

	case KDFONTOP: {
A
Alan Cox 已提交
944 945 946 947
		if (copy_from_user(&op, up, sizeof(op))) {
			ret = -EFAULT;
			break;
		}
L
Linus Torvalds 已提交
948
		if (!perm && op.op != KD_FONT_OP_GET)
949
			return -EPERM;
A
Alan Cox 已提交
950 951 952
		ret = con_font_op(vc, &op);
		if (ret)
			break;
L
Linus Torvalds 已提交
953
		if (copy_to_user(up, &op, sizeof(op)))
A
Alan Cox 已提交
954 955
			ret = -EFAULT;
		break;
L
Linus Torvalds 已提交
956 957 958 959
	}

	case PIO_SCRNMAP:
		if (!perm)
A
Alan Cox 已提交
960
			ret = -EPERM;
961
		else
A
Alan Cox 已提交
962 963
			ret = con_set_trans_old(up);
		break;
L
Linus Torvalds 已提交
964 965

	case GIO_SCRNMAP:
A
Alan Cox 已提交
966 967
		ret = con_get_trans_old(up);
		break;
L
Linus Torvalds 已提交
968 969 970

	case PIO_UNISCRNMAP:
		if (!perm)
A
Alan Cox 已提交
971
			ret = -EPERM;
972
		else
A
Alan Cox 已提交
973 974
			ret = con_set_trans_new(up);
		break;
L
Linus Torvalds 已提交
975 976

	case GIO_UNISCRNMAP:
A
Alan Cox 已提交
977 978
		ret = con_get_trans_new(up);
		break;
L
Linus Torvalds 已提交
979 980 981 982

	case PIO_UNIMAPCLR:
	      { struct unimapinit ui;
		if (!perm)
983
			return -EPERM;
A
Alan Cox 已提交
984
		ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
985 986
		if (ret)
			ret = -EFAULT;
987
		else
A
Alan Cox 已提交
988 989
			con_clear_unimap(vc, &ui);
		break;
L
Linus Torvalds 已提交
990 991 992 993
	      }

	case PIO_UNIMAP:
	case GIO_UNIMAP:
A
Alan Cox 已提交
994 995
		ret = do_unimap_ioctl(cmd, up, perm, vc);
		break;
L
Linus Torvalds 已提交
996 997 998

	case VT_LOCKSWITCH:
		if (!capable(CAP_SYS_TTY_CONFIG))
999
			return -EPERM;
L
Linus Torvalds 已提交
1000
		vt_dont_switch = 1;
A
Alan Cox 已提交
1001
		break;
L
Linus Torvalds 已提交
1002 1003
	case VT_UNLOCKSWITCH:
		if (!capable(CAP_SYS_TTY_CONFIG))
1004
			return -EPERM;
L
Linus Torvalds 已提交
1005
		vt_dont_switch = 0;
A
Alan Cox 已提交
1006
		break;
1007
	case VT_GETHIFONTMASK:
A
Alan Cox 已提交
1008 1009 1010
		ret = put_user(vc->vc_hi_font_mask,
					(unsigned short __user *)arg);
		break;
A
Alan Cox 已提交
1011 1012 1013
	case VT_WAITEVENT:
		ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
		break;
L
Linus Torvalds 已提交
1014
	default:
A
Alan Cox 已提交
1015
		ret = -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1016
	}
A
Alan Cox 已提交
1017 1018
out:
	return ret;
L
Linus Torvalds 已提交
1019 1020 1021 1022 1023
}

void reset_vc(struct vc_data *vc)
{
	vc->vc_mode = KD_TEXT;
A
Alan Cox 已提交
1024
	vt_reset_unicode(vc->vc_num);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029
	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;
1030 1031
	put_pid(vc->vt_pid);
	vc->vt_pid = NULL;
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036
	vc->vt_newvt = -1;
	if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */
		reset_palette(vc);
}

1037 1038 1039 1040 1041 1042 1043
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;

1044
	console_lock();
1045 1046
	vc = vc_con->d;
	if (vc) {
A
Alan Cox 已提交
1047
		/* FIXME: review tty ref counting */
1048
		tty = vc->port.tty;
1049 1050 1051 1052 1053 1054 1055 1056
		/*
		 * SAK should also work in all raw modes and reset
		 * them properly.
		 */
		if (tty)
			__do_SAK(tty);
		reset_vc(vc);
	}
1057
	console_unlock();
1058 1059
}

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
#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);
	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;

	if (copy_from_user(&tmp, user_ud, sizeof tmp))
		return -EFAULT;
	tmp_entries = compat_ptr(tmp.entries);
	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;
}

1168
long vt_compat_ioctl(struct tty_struct *tty,
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	     unsigned int cmd, unsigned long arg)
{
	struct vc_data *vc = tty->driver_data;
	struct console_font_op op;	/* used in multiple places here */
	unsigned int console;
	void __user *up = (void __user *)arg;
	int perm;
	int ret = 0;

	console = vc->vc_num;

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

	/*
	 * 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;

	switch (cmd) {
	/*
	 * these need special handlers for incompatible data structures
	 */
	case PIO_FONTX:
	case GIO_FONTX:
		ret = compat_fontx_ioctl(cmd, up, perm, &op);
		break;

	case KDFONTOP:
		ret = compat_kdfontop_ioctl(up, perm, &op, vc);
		break;

	case PIO_UNIMAP:
	case GIO_UNIMAP:
A
Andreas Schwab 已提交
1208
		ret = compat_unimap_ioctl(cmd, up, perm, vc);
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
		break;

	/*
	 * all these treat 'arg' as an integer
	 */
	case KIOCSOUND:
	case KDMKTONE:
#ifdef CONFIG_X86
	case KDADDIO:
	case KDDELIO:
#endif
	case KDSETMODE:
	case KDMAPDISP:
	case KDUNMAPDISP:
	case KDSKBMODE:
	case KDSKBMETA:
	case KDSKBLED:
	case KDSETLED:
	case KDSIGACCEPT:
	case VT_ACTIVATE:
	case VT_WAITACTIVE:
	case VT_RELDISP:
	case VT_DISALLOCATE:
	case VT_RESIZE:
	case VT_RESIZEX:
		goto fallback;

	/*
	 * the rest has a compatible data structure behind arg,
	 * but we have to convert it to a proper 64 bit pointer.
	 */
	default:
		arg = (unsigned long)compat_ptr(arg);
		goto fallback;
	}
out:
	return ret;

fallback:
1248
	return vt_ioctl(tty, cmd, arg);
1249 1250 1251 1252 1253 1254
}


#endif /* CONFIG_COMPAT */


L
Linus Torvalds 已提交
1255
/*
A
Alan Cox 已提交
1256 1257
 * Performs the back end of a vt switch. Called under the console
 * semaphore.
L
Linus Torvalds 已提交
1258 1259 1260 1261
 */
static void complete_change_console(struct vc_data *vc)
{
	unsigned char old_vc_mode;
A
Alan Cox 已提交
1262
	int old = fg_console;
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	last_console = fg_console;

	/*
	 * If we're switching, we could be going from KD_GRAPHICS to
	 * KD_TEXT mode or vice versa, which means we need to blank or
	 * unblank the screen later.
	 */
	old_vc_mode = vc_cons[fg_console].d->vc_mode;
	switch_screen(vc);

	/*
1275
	 * This can't appear below a successful kill_pid().  If it did,
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	 * then the *blank_screen operation could occur while X, having
	 * received acqsig, is waking up on another processor.  This
	 * condition can lead to overlapping accesses to the VGA range
	 * and the framebuffer (causing system lockups).
	 *
	 * To account for this we duplicate this code below only if the
	 * controlling process is gone and we've called reset_vc.
	 */
	if (old_vc_mode != vc->vc_mode) {
		if (vc->vc_mode == KD_TEXT)
			do_unblank_screen(1);
		else
			do_blank_screen(1);
	}

	/*
	 * If this new console is under process control, send it a signal
	 * telling it that it has acquired. Also check if it has died and
	 * clean up (similar to logic employed in change_console())
	 */
1296
	if (vc->vt_mode.mode == VT_PROCESS) {
L
Linus Torvalds 已提交
1297
		/*
1298
		 * Send the signal as privileged - kill_pid() will
L
Linus Torvalds 已提交
1299 1300 1301
		 * tell us if the process has gone or something else
		 * is awry
		 */
1302
		if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
		/*
		 * The controlling process has died, so we revert back to
		 * normal operation. In this case, we'll also change back
		 * to KD_TEXT mode. I'm not sure if this is strictly correct
		 * but it saves the agony when the X server dies and the screen
		 * remains blanked due to KD_GRAPHICS! It would be nice to do
		 * this outside of VT_PROCESS but there is no single process
		 * to account for and tracking tty count may be undesirable.
		 */
			reset_vc(vc);

			if (old_vc_mode != vc->vc_mode) {
				if (vc->vc_mode == KD_TEXT)
					do_unblank_screen(1);
				else
					do_blank_screen(1);
			}
		}
	}

	/*
	 * Wake anyone waiting for their VT to activate
	 */
A
Alan Cox 已提交
1326
	vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	return;
}

/*
 * Performs the front-end of a vt switch
 */
void change_console(struct vc_data *new_vc)
{
	struct vc_data *vc;

	if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
		return;

	/*
	 * If this vt is in process mode, then we need to handshake with
	 * that process before switching. Essentially, we store where that
	 * vt wants to switch to and wait for it to tell us when it's done
	 * (via VT_RELDISP ioctl).
	 *
	 * We also check to see if the controlling process still exists.
	 * If it doesn't, we reset this vt to auto mode and continue.
	 * This is a cheap way to track process control. The worst thing
	 * that can happen is: we send a signal to a process, it dies, and
	 * the switch gets "lost" waiting for a response; hopefully, the
	 * user will try again, we'll detect the process is gone (unless
	 * the user waits just the right amount of time :-) and revert the
	 * vt to auto control.
	 */
	vc = vc_cons[fg_console].d;
1356
	if (vc->vt_mode.mode == VT_PROCESS) {
L
Linus Torvalds 已提交
1357
		/*
1358
		 * Send the signal as privileged - kill_pid() will
L
Linus Torvalds 已提交
1359
		 * tell us if the process has gone or something else
1360 1361 1362 1363
		 * is awry.
		 *
		 * We need to set vt_newvt *before* sending the signal or we
		 * have a race.
L
Linus Torvalds 已提交
1364
		 */
1365
		vc->vt_newvt = new_vc->vc_num;
1366
		if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
L
Linus Torvalds 已提交
1367
			/*
1368 1369 1370
			 * It worked. Mark the vt to switch to and
			 * return. The process needs to send us a
			 * VT_RELDISP ioctl to complete the switch.
L
Linus Torvalds 已提交
1371
			 */
1372
			return;
L
Linus Torvalds 已提交
1373 1374 1375
		}

		/*
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
		 * The controlling process has died, so we revert back to
		 * normal operation. In this case, we'll also change back
		 * to KD_TEXT mode. I'm not sure if this is strictly correct
		 * but it saves the agony when the X server dies and the screen
		 * remains blanked due to KD_GRAPHICS! It would be nice to do
		 * this outside of VT_PROCESS but there is no single process
		 * to account for and tracking tty count may be undesirable.
		 */
		reset_vc(vc);

		/*
		 * Fall through to normal (VT_AUTO) handling of the switch...
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		 */
	}

	/*
	 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
	 */
	if (vc->vc_mode == KD_GRAPHICS)
		return;

	complete_change_console(new_vc);
}
1399 1400 1401 1402 1403 1404 1405 1406 1407

/* Perform a kernel triggered VT switch for suspend/resume */

static int disable_vt_switch;

int vt_move_to_console(unsigned int vt, int alloc)
{
	int prev;

1408
	console_lock();
1409 1410
	/* Graphics mode - up to X */
	if (disable_vt_switch) {
1411
		console_unlock();
1412 1413 1414 1415 1416 1417 1418
		return 0;
	}
	prev = fg_console;

	if (alloc && vc_allocate(vt)) {
		/* we can't have a free VC for now. Too bad,
		 * we don't want to mess the screen for now. */
1419
		console_unlock();
1420 1421 1422 1423 1424 1425 1426 1427 1428
		return -ENOSPC;
	}

	if (set_console(vt)) {
		/*
		 * We're unable to switch to the SUSPEND_CONSOLE.
		 * Let the calling function know so it can decide
		 * what to do.
		 */
1429
		console_unlock();
1430 1431
		return -EIO;
	}
1432
	console_unlock();
1433
	if (vt_waitactive(vt + 1)) {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
		pr_debug("Suspend: Can't switch VCs.");
		return -EINTR;
	}
	return prev;
}

/*
 * Normally during a suspend, we allocate a new console and switch to it.
 * When we resume, we switch back to the original console.  This switch
 * can be slow, so on systems where the framebuffer can handle restoration
 * of video registers anyways, there's little point in doing the console
 * switch.  This function allows you to disable it by passing it '0'.
 */
void pm_set_vt_switch(int do_switch)
{
1449
	console_lock();
1450
	disable_vt_switch = !do_switch;
1451
	console_unlock();
1452 1453
}
EXPORT_SYMBOL(pm_set_vt_switch);