blob: 28ca0aa8664fbab9c015348d004f41dad5447ebd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1992 obz under the linux copyright
3 *
4 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
5 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
6 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
7 * Some code moved for less code duplication - Andi Kleen - Mar 1997
8 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/errno.h>
13#include <linux/sched.h>
14#include <linux/tty.h>
15#include <linux/timer.h>
16#include <linux/kernel.h>
Arnd Bergmanne9216652009-08-06 15:09:28 +020017#include <linux/compat.h>
Alan Cox8d233552009-09-19 13:13:25 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kd.h>
20#include <linux/vt.h>
21#include <linux/string.h>
22#include <linux/slab.h>
23#include <linux/major.h>
24#include <linux/fs.h>
25#include <linux/console.h>
Samuel Thibault04c71972007-10-16 23:27:04 -070026#include <linux/consolemap.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070027#include <linux/signal.h>
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -070028#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/io.h>
31#include <asm/uaccess.h>
32
33#include <linux/kbd_kern.h>
34#include <linux/vt_kern.h>
35#include <linux/kbd_diacr.h>
36#include <linux/selection.h>
37
Andrew Johnsonb257bc02007-03-16 13:38:24 -080038char vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039extern struct tty_driver *console_driver;
40
41#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
42#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
43
44/*
45 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
46 * experimentation and study of X386 SYSV handling.
47 *
48 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
49 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
50 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
51 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
52 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
53 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
54 * to the current console is done by the main ioctl code.
55 */
56
57#ifdef CONFIG_X86
58#include <linux/syscalls.h>
59#endif
60
61static void complete_change_console(struct vc_data *vc);
62
63/*
Alan Cox8b92e872009-09-19 13:13:24 -070064 * User space VT_EVENT handlers
65 */
66
67struct vt_event_wait {
68 struct list_head list;
69 struct vt_event event;
70 int done;
71};
72
73static LIST_HEAD(vt_events);
74static DEFINE_SPINLOCK(vt_event_lock);
75static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
76
77/**
78 * vt_event_post
79 * @event: the event that occurred
80 * @old: old console
81 * @new: new console
82 *
83 * Post an VT event to interested VT handlers
84 */
85
86void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
87{
88 struct list_head *pos, *head;
89 unsigned long flags;
90 int wake = 0;
91
92 spin_lock_irqsave(&vt_event_lock, flags);
93 head = &vt_events;
94
95 list_for_each(pos, head) {
96 struct vt_event_wait *ve = list_entry(pos,
97 struct vt_event_wait, list);
98 if (!(ve->event.event & event))
99 continue;
100 ve->event.event = event;
101 /* kernel view is consoles 0..n-1, user space view is
102 console 1..n with 0 meaning current, so we must bias */
Alan Cox308efab2009-11-19 13:30:36 +0000103 ve->event.oldev = old + 1;
104 ve->event.newev = new + 1;
Alan Cox8b92e872009-09-19 13:13:24 -0700105 wake = 1;
106 ve->done = 1;
107 }
108 spin_unlock_irqrestore(&vt_event_lock, flags);
109 if (wake)
110 wake_up_interruptible(&vt_event_waitqueue);
111}
112
113/**
114 * vt_event_wait - wait for an event
115 * @vw: our event
116 *
117 * Waits for an event to occur which completes our vt_event_wait
118 * structure. On return the structure has wv->done set to 1 for success
119 * or 0 if some event such as a signal ended the wait.
120 */
121
122static void vt_event_wait(struct vt_event_wait *vw)
123{
124 unsigned long flags;
125 /* Prepare the event */
126 INIT_LIST_HEAD(&vw->list);
127 vw->done = 0;
128 /* Queue our event */
129 spin_lock_irqsave(&vt_event_lock, flags);
130 list_add(&vw->list, &vt_events);
131 spin_unlock_irqrestore(&vt_event_lock, flags);
132 /* Wait for it to pass */
Arnd Bergmannbe1bc282010-06-01 22:53:05 +0200133 wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
Alan Cox8b92e872009-09-19 13:13:24 -0700134 /* Dequeue it */
135 spin_lock_irqsave(&vt_event_lock, flags);
136 list_del(&vw->list);
137 spin_unlock_irqrestore(&vt_event_lock, flags);
138}
139
140/**
141 * vt_event_wait_ioctl - event ioctl handler
142 * @arg: argument to ioctl
143 *
144 * Implement the VT_WAITEVENT ioctl using the VT event interface
145 */
146
147static int vt_event_wait_ioctl(struct vt_event __user *event)
148{
149 struct vt_event_wait vw;
150
151 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
152 return -EFAULT;
153 /* Highest supported event for now */
154 if (vw.event.event & ~VT_MAX_EVENT)
155 return -EINVAL;
156
157 vt_event_wait(&vw);
158 /* If it occurred report it */
159 if (vw.done) {
160 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
161 return -EFAULT;
162 return 0;
163 }
164 return -EINTR;
165}
166
167/**
168 * vt_waitactive - active console wait
169 * @event: event code
170 * @n: new console
171 *
172 * Helper for event waits. Used to implement the legacy
173 * event waiting ioctls in terms of events
174 */
175
176int vt_waitactive(int n)
177{
178 struct vt_event_wait vw;
179 do {
180 if (n == fg_console + 1)
181 break;
182 vw.event.event = VT_EVENT_SWITCH;
183 vt_event_wait(&vw);
184 if (vw.done == 0)
185 return -EINTR;
Alan Cox308efab2009-11-19 13:30:36 +0000186 } while (vw.event.newev != n);
Alan Cox8b92e872009-09-19 13:13:24 -0700187 return 0;
188}
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * these are the valid i/o ports we're allowed to change. they map all the
192 * video ports
193 */
194#define GPFIRST 0x3b4
195#define GPLAST 0x3df
196#define GPNUM (GPLAST - GPFIRST + 1)
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200static inline int
201do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
202{
203 struct consolefontdesc cfdarg;
204 int i;
205
206 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
207 return -EFAULT;
208
209 switch (cmd) {
210 case PIO_FONTX:
211 if (!perm)
212 return -EPERM;
213 op->op = KD_FONT_OP_SET;
214 op->flags = KD_FONT_FLAG_OLD;
215 op->width = 8;
216 op->height = cfdarg.charheight;
217 op->charcount = cfdarg.charcount;
218 op->data = cfdarg.chardata;
219 return con_font_op(vc_cons[fg_console].d, op);
220 case GIO_FONTX: {
221 op->op = KD_FONT_OP_GET;
222 op->flags = KD_FONT_FLAG_OLD;
223 op->width = 8;
224 op->height = cfdarg.charheight;
225 op->charcount = cfdarg.charcount;
226 op->data = cfdarg.chardata;
227 i = con_font_op(vc_cons[fg_console].d, op);
228 if (i)
229 return i;
230 cfdarg.charheight = op->height;
231 cfdarg.charcount = op->charcount;
232 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
233 return -EFAULT;
234 return 0;
235 }
236 }
237 return -EINVAL;
238}
239
240static inline int
241do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
242{
243 struct unimapdesc tmp;
244
245 if (copy_from_user(&tmp, user_ud, sizeof tmp))
246 return -EFAULT;
247 if (tmp.entries)
248 if (!access_ok(VERIFY_WRITE, tmp.entries,
249 tmp.entry_ct*sizeof(struct unipair)))
250 return -EFAULT;
251 switch (cmd) {
252 case PIO_UNIMAP:
253 if (!perm)
254 return -EPERM;
255 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
256 case GIO_UNIMAP:
257 if (!perm && fg_console != vc->vc_num)
258 return -EPERM;
259 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
260 }
261 return 0;
262}
263
Alan Cox8b92e872009-09-19 13:13:24 -0700264
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * We handle the console-specific ioctl's here. We allow the
268 * capability to modify any console, not just the fg_console.
269 */
Alan Cox6caa76b2011-02-14 16:27:22 +0000270int vt_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned int cmd, unsigned long arg)
272{
Alan Coxc9f19e92009-01-02 13:47:26 +0000273 struct vc_data *vc = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct console_font_op op; /* used in multiple places here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned int console;
276 unsigned char ucval;
Graham Gower1e0ad282010-10-27 15:33:00 -0700277 unsigned int uival;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 void __user *up = (void __user *)arg;
279 int i, perm;
Alan Cox9cc3c222008-04-30 00:53:26 -0700280 int ret = 0;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 console = vc->vc_num;
283
Arnd Bergmannec79d602010-06-01 22:53:01 +0200284 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700285
286 if (!vc_cons_allocated(console)) { /* impossible? */
287 ret = -ENOIOCTLCMD;
288 goto out;
289 }
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * To have permissions to do most of the vt ioctls, we either have
294 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
295 */
296 perm = 0;
297 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
298 perm = 1;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100301 case TIOCLINUX:
Jiri Slabya1159022009-06-22 18:42:18 +0100302 ret = tioclinux(tty, arg);
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case KIOCSOUND:
305 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700306 goto eperm;
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700307 /*
308 * The use of PIT_TICK_RATE is historic, it used to be
309 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
310 * and 2.6.36, which was a minor but unfortunate ABI
311 * change.
312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (arg)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700314 arg = PIT_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 case KDMKTONE:
319 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700320 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 {
322 unsigned int ticks, count;
323
324 /*
325 * Generate the tone for the appropriate number of ticks.
326 * If the time is zero, turn off sound ourselves.
327 */
328 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
329 count = ticks ? (arg & 0xffff) : 0;
330 if (count)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700331 count = PIT_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 case KDGKBTYPE:
337 /*
338 * this is naive.
339 */
340 ucval = KB_101;
Alan Cox079c9532012-02-28 14:49:23 +0000341 ret = put_user(ucval, (char __user *)arg);
342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * These cannot be implemented on any machine that implements
346 * ioperm() in user level (such as Alpha PCs) or not at all.
347 *
348 * XXX: you should never use these, just call ioperm directly..
349 */
350#ifdef CONFIG_X86
351 case KDADDIO:
352 case KDDELIO:
353 /*
354 * KDADDIO and KDDELIO may be able to add ports beyond what
355 * we reject here, but to be safe...
356 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700357 if (arg < GPFIRST || arg > GPLAST) {
358 ret = -EINVAL;
359 break;
360 }
361 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 case KDENABIO:
365 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700366 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
370
371 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
372
373 case KDKBDREP:
374 {
375 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700378 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox9cc3c222008-04-30 00:53:26 -0700380 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
381 ret = -EFAULT;
382 break;
383 }
384 ret = kbd_rate(&kbrep);
385 if (ret)
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700388 ret = -EFAULT;
389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 case KDSETMODE:
393 /*
394 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
395 * doesn't do a whole lot. i'm not sure if it should do any
396 * restoration of modes or what...
397 *
398 * XXX It should at least call into the driver, fbdev's definitely
399 * need to restore their engine state. --BenH
400 */
401 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700402 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 switch (arg) {
404 case KD_GRAPHICS:
405 break;
406 case KD_TEXT0:
407 case KD_TEXT1:
408 arg = KD_TEXT;
409 case KD_TEXT:
410 break;
411 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700412 ret = -EINVAL;
413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 vc->vc_mode = (unsigned char) arg;
418 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * explicitly blank/unblank the screen if switching modes
422 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800423 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (arg == KD_TEXT)
425 do_unblank_screen(1);
426 else
427 do_blank_screen(1);
Torben Hohnac751ef2011-01-25 15:07:35 -0800428 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 case KDGETMODE:
Graham Gower1e0ad282010-10-27 15:33:00 -0700432 uival = vc->vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto setint;
434
435 case KDMAPDISP:
436 case KDUNMAPDISP:
437 /*
438 * these work like a combination of mmap and KDENABIO.
439 * this could be easily finished.
440 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700441 ret = -EINVAL;
442 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 case KDSKBMODE:
445 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700446 goto eperm;
Alan Cox079c9532012-02-28 14:49:23 +0000447 ret = vt_do_kdskbmode(console, arg);
448 if (ret == 0)
449 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 case KDGKBMODE:
Alan Cox079c9532012-02-28 14:49:23 +0000453 uival = vt_do_kdgkbmode(console);
454 ret = put_user(uival, (int __user *)arg);
455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* this could be folded into KDSKBMODE, but for compatibility
458 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
459 case KDSKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000460 ret = vt_do_kdskbmeta(console, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 case KDGKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000464 /* FIXME: should review whether this is worth locking */
465 uival = vt_do_kdgkbmeta(console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 setint:
Graham Gower1e0ad282010-10-27 15:33:00 -0700467 ret = put_user(uival, (int __user *)arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 case KDGETKEYCODE:
471 case KDSETKEYCODE:
472 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700473 perm = 0;
Alan Cox079c9532012-02-28 14:49:23 +0000474 ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 case KDGKBENT:
478 case KDSKBENT:
Alan Cox079c9532012-02-28 14:49:23 +0000479 ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
Alan Cox9cc3c222008-04-30 00:53:26 -0700480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 case KDGKBSENT:
483 case KDSKBSENT:
Alan Cox079c9532012-02-28 14:49:23 +0000484 ret = vt_do_kdgkb_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Alan Cox247ff8e2012-02-24 12:47:11 +0000487 /* Diacritical processing. Handled in keyboard.c as it has
488 to operate on the keyboard locks and structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 case KDGKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700490 case KDGKBDIACRUC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 case KDSKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700492 case KDSKBDIACRUC:
Alan Cox247ff8e2012-02-24 12:47:11 +0000493 ret = vt_do_diacrit(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* the ioctls below read/set the flags usually shown in the leds */
497 /* don't use them - they will go away without warning */
498 case KDGKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case KDSKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 case KDGETLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 case KDSETLED:
Alan Cox079c9532012-02-28 14:49:23 +0000502 ret = vt_do_kdskled(console, cmd, arg, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /*
506 * A process can indicate its willingness to accept signals
507 * generated by pressing an appropriate key combination.
508 * Thus, one can have a daemon that e.g. spawns a new console
509 * upon a keypress and then changes to it.
510 * See also the kbrequest field of inittab(5).
511 */
512 case KDSIGACCEPT:
513 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!perm || !capable(CAP_KILL))
Alan Cox9cc3c222008-04-30 00:53:26 -0700515 goto eperm;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700516 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700517 ret = -EINVAL;
518 else {
519 spin_lock_irq(&vt_spawn_con.lock);
520 put_pid(vt_spawn_con.pid);
521 vt_spawn_con.pid = get_pid(task_pid(current));
522 vt_spawn_con.sig = arg;
523 spin_unlock_irq(&vt_spawn_con.lock);
524 }
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 case VT_SETMODE:
529 {
530 struct vt_mode tmp;
531
532 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700533 goto eperm;
534 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
535 ret = -EFAULT;
536 goto out;
537 }
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -0700538 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700539 ret = -EINVAL;
540 goto out;
541 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800542 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 vc->vt_mode = tmp;
544 /* the frsig is ignored, so we set it to 0 */
545 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800546 put_pid(vc->vt_pid);
547 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* no switch is required -- saw@shade.msu.ru */
549 vc->vt_newvt = -1;
Torben Hohnac751ef2011-01-25 15:07:35 -0800550 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 case VT_GETMODE:
555 {
556 struct vt_mode tmp;
557 int rc;
558
Torben Hohnac751ef2011-01-25 15:07:35 -0800559 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
Torben Hohnac751ef2011-01-25 15:07:35 -0800561 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700564 if (rc)
565 ret = -EFAULT;
566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 /*
570 * Returns global vt state. Note that VT 0 is always open, since
571 * it's an alias for the current VT, and people can't use it here.
572 * We cannot return state for more than 16 VTs, since v_state is short.
573 */
574 case VT_GETSTATE:
575 {
576 struct vt_stat __user *vtstat = up;
577 unsigned short state, mask;
578
579 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700580 ret = -EFAULT;
581 else {
582 state = 1; /* /dev/tty0 is always open */
583 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
584 ++i, mask <<= 1)
585 if (VT_IS_IN_USE(i))
586 state |= mask;
587 ret = put_user(state, &vtstat->v_state);
588 }
589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 /*
593 * Returns the first available (non-opened) console.
594 */
595 case VT_OPENQRY:
596 for (i = 0; i < MAX_NR_CONSOLES; ++i)
597 if (! VT_IS_IN_USE(i))
598 break;
Graham Gower1e0ad282010-10-27 15:33:00 -0700599 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto setint;
601
602 /*
603 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
604 * with num >= 1 (switches to vt 0, our console, are not allowed, just
605 * to preserve sanity).
606 */
607 case VT_ACTIVATE:
608 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700609 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700611 ret = -ENXIO;
612 else {
613 arg--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800614 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700615 ret = vc_allocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800616 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700617 if (ret)
618 break;
619 set_console(arg);
620 }
621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Alan Coxd3b5cff2009-09-19 13:13:26 -0700623 case VT_SETACTIVATE:
624 {
625 struct vt_setactivate vsa;
626
627 if (!perm)
628 goto eperm;
629
630 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
Jiri Slabya09efb02009-10-01 15:43:59 -0700631 sizeof(struct vt_setactivate))) {
632 ret = -EFAULT;
633 goto out;
634 }
Alan Coxd3b5cff2009-09-19 13:13:26 -0700635 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
636 ret = -ENXIO;
637 else {
638 vsa.console--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800639 console_lock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700640 ret = vc_allocate(vsa.console);
641 if (ret == 0) {
642 struct vc_data *nvc;
643 /* This is safe providing we don't drop the
644 console sem between vc_allocate and
645 finishing referencing nvc */
646 nvc = vc_cons[vsa.console].d;
647 nvc->vt_mode = vsa.mode;
648 nvc->vt_mode.frsig = 0;
649 put_pid(nvc->vt_pid);
650 nvc->vt_pid = get_pid(task_pid(current));
651 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800652 console_unlock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700653 if (ret)
654 break;
655 /* Commence switch and lock */
Jiri Olsad6378372011-02-11 15:39:28 +0100656 set_console(vsa.console);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700657 }
Jiri Olsad6378372011-02-11 15:39:28 +0100658 break;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * wait until the specified VT has been activated
663 */
664 case VT_WAITACTIVE:
665 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700666 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700668 ret = -ENXIO;
669 else
Alan Cox8b92e872009-09-19 13:13:24 -0700670 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700671 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
674 * If a vt is under process control, the kernel will not switch to it
675 * immediately, but postpone the operation until the process calls this
676 * ioctl, allowing the switch to complete.
677 *
678 * According to the X sources this is the behavior:
679 * 0: pending switch-from not OK
680 * 1: pending switch-from OK
681 * 2: completed switch-to OK
682 */
683 case VT_RELDISP:
684 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700685 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Alan Cox9cc3c222008-04-30 00:53:26 -0700687 if (vc->vt_mode.mode != VT_PROCESS) {
688 ret = -EINVAL;
689 break;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Switching-from response
693 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800694 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (vc->vt_newvt >= 0) {
696 if (arg == 0)
697 /*
698 * Switch disallowed, so forget we were trying
699 * to do it.
700 */
701 vc->vt_newvt = -1;
702
703 else {
704 /*
705 * The current vt has been released, so
706 * complete the switch.
707 */
708 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 newvt = vc->vt_newvt;
710 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700711 ret = vc_allocate(newvt);
712 if (ret) {
Torben Hohnac751ef2011-01-25 15:07:35 -0800713 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 /*
717 * When we actually do the console switch,
718 * make sure we are atomic with respect to
719 * other console switches..
720 */
721 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700723 } else {
724 /*
725 * Switched-to response
726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
728 * If it's just an ACK, ignore it
729 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700730 if (arg != VT_ACKACQ)
731 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800733 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Disallocate memory associated to VT (but leave VT1)
738 */
739 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -0700740 if (arg > MAX_NR_CONSOLES) {
741 ret = -ENXIO;
742 break;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -0700745 /* deallocate all unused consoles, but leave 0 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800746 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 for (i=1; i<MAX_NR_CONSOLES; i++)
748 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -0700749 vc_deallocate(i);
Torben Hohnac751ef2011-01-25 15:07:35 -0800750 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
Alan Coxca9bda02006-09-29 02:00:03 -0700752 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 arg--;
754 if (VT_BUSY(arg))
Alan Cox9cc3c222008-04-30 00:53:26 -0700755 ret = -EBUSY;
756 else if (arg) { /* leave 0 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800757 console_lock();
Alan Coxca9bda02006-09-29 02:00:03 -0700758 vc_deallocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800759 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 case VT_RESIZE:
765 {
766 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700767 struct vc_data *vc;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ushort ll,cc;
770 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700771 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (get_user(ll, &vtsizes->v_rows) ||
773 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -0700774 ret = -EFAULT;
775 else {
Torben Hohnac751ef2011-01-25 15:07:35 -0800776 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700777 for (i = 0; i < MAX_NR_CONSOLES; i++) {
778 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700779
Alan Cox9cc3c222008-04-30 00:53:26 -0700780 if (vc) {
781 vc->vc_resize_user = 1;
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100782 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -0700783 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700784 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800785 console_unlock();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700786 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 case VT_RESIZEX:
791 {
792 struct vt_consize __user *vtconsize = up;
793 ushort ll,cc,vlin,clin,vcol,ccol;
794 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700795 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox9cc3c222008-04-30 00:53:26 -0700797 sizeof(struct vt_consize))) {
798 ret = -EFAULT;
799 break;
800 }
801 /* FIXME: Should check the copies properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 __get_user(ll, &vtconsize->v_rows);
803 __get_user(cc, &vtconsize->v_cols);
804 __get_user(vlin, &vtconsize->v_vlin);
805 __get_user(clin, &vtconsize->v_clin);
806 __get_user(vcol, &vtconsize->v_vcol);
807 __get_user(ccol, &vtconsize->v_ccol);
808 vlin = vlin ? vlin : vc->vc_scan_lines;
809 if (clin) {
810 if (ll) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700811 if (ll != vlin/clin) {
812 /* Parameters don't add up */
813 ret = -EINVAL;
814 break;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 } else
817 ll = vlin/clin;
818 }
819 if (vcol && ccol) {
820 if (cc) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700821 if (cc != vcol/ccol) {
822 ret = -EINVAL;
823 break;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else
826 cc = vcol/ccol;
827 }
828
Alan Cox9cc3c222008-04-30 00:53:26 -0700829 if (clin > 32) {
830 ret = -EINVAL;
831 break;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 for (i = 0; i < MAX_NR_CONSOLES; i++) {
835 if (!vc_cons[i].d)
836 continue;
Torben Hohnac751ef2011-01-25 15:07:35 -0800837 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (vlin)
839 vc_cons[i].d->vc_scan_lines = vlin;
840 if (clin)
841 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700842 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 vc_resize(vc_cons[i].d, cc, ll);
Torben Hohnac751ef2011-01-25 15:07:35 -0800844 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 case PIO_FONT: {
850 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700851 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 op.op = KD_FONT_OP_SET;
853 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
854 op.width = 8;
855 op.height = 0;
856 op.charcount = 256;
857 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700858 ret = con_font_op(vc_cons[fg_console].d, &op);
859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
862 case GIO_FONT: {
863 op.op = KD_FONT_OP_GET;
864 op.flags = KD_FONT_FLAG_OLD;
865 op.width = 8;
866 op.height = 32;
867 op.charcount = 256;
868 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700869 ret = con_font_op(vc_cons[fg_console].d, &op);
870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 case PIO_CMAP:
874 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700875 ret = -EPERM;
876 else
877 ret = con_set_cmap(up);
878 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700881 ret = con_get_cmap(up);
882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 case PIO_FONTX:
885 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -0700886 ret = do_fontx_ioctl(cmd, up, perm, &op);
887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 case PIO_FONTRESET:
890 {
891 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700892 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894#ifdef BROKEN_GRAPHICS_PROGRAMS
895 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
896 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -0700897 ret = -ENOSYS;
898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#else
900 {
901 op.op = KD_FONT_OP_SET_DEFAULT;
902 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -0700903 ret = con_font_op(vc_cons[fg_console].d, &op);
904 if (ret)
905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox9cc3c222008-04-30 00:53:26 -0700907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909#endif
910 }
911
912 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -0700913 if (copy_from_user(&op, up, sizeof(op))) {
914 ret = -EFAULT;
915 break;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox9cc3c222008-04-30 00:53:26 -0700918 goto eperm;
919 ret = con_font_op(vc, &op);
920 if (ret)
921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700923 ret = -EFAULT;
924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 case PIO_SCRNMAP:
928 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700929 ret = -EPERM;
930 else
931 ret = con_set_trans_old(up);
932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700935 ret = con_get_trans_old(up);
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 case PIO_UNISCRNMAP:
939 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700940 ret = -EPERM;
941 else
942 ret = con_set_trans_new(up);
943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700946 ret = con_get_trans_new(up);
947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 case PIO_UNIMAPCLR:
950 { struct unimapinit ui;
951 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700952 goto eperm;
953 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
Dan Carpenter3fde85d2010-06-04 12:20:46 +0200954 if (ret)
955 ret = -EFAULT;
956 else
Alan Cox9cc3c222008-04-30 00:53:26 -0700957 con_clear_unimap(vc, &ui);
958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
961 case PIO_UNIMAP:
962 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700963 ret = do_unimap_ioctl(cmd, up, perm, vc);
964 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 case VT_LOCKSWITCH:
967 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700968 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 case VT_UNLOCKSWITCH:
972 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700973 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700975 break;
Samuel Thibault533475d2006-08-27 01:23:39 -0700976 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -0700977 ret = put_user(vc->vc_hi_font_mask,
978 (unsigned short __user *)arg);
979 break;
Alan Cox8b92e872009-09-19 13:13:24 -0700980 case VT_WAITEVENT:
981 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700984 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700986out:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200987 tty_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700988 return ret;
989eperm:
990 ret = -EPERM;
991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994void reset_vc(struct vc_data *vc)
995{
996 vc->vc_mode = KD_TEXT;
Alan Cox079c9532012-02-28 14:49:23 +0000997 vt_reset_unicode(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 vc->vt_mode.mode = VT_AUTO;
999 vc->vt_mode.waitv = 0;
1000 vc->vt_mode.relsig = 0;
1001 vc->vt_mode.acqsig = 0;
1002 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001003 put_pid(vc->vt_pid);
1004 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 vc->vt_newvt = -1;
1006 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1007 reset_palette(vc);
1008}
1009
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001010void vc_SAK(struct work_struct *work)
1011{
1012 struct vc *vc_con =
1013 container_of(work, struct vc, SAK_work);
1014 struct vc_data *vc;
1015 struct tty_struct *tty;
1016
Torben Hohnac751ef2011-01-25 15:07:35 -08001017 console_lock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001018 vc = vc_con->d;
1019 if (vc) {
Alan Cox079c9532012-02-28 14:49:23 +00001020 /* FIXME: review tty ref counting */
Alan Cox8ce73262010-06-01 22:52:56 +02001021 tty = vc->port.tty;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001022 /*
1023 * SAK should also work in all raw modes and reset
1024 * them properly.
1025 */
1026 if (tty)
1027 __do_SAK(tty);
1028 reset_vc(vc);
1029 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001030 console_unlock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001031}
1032
Arnd Bergmanne9216652009-08-06 15:09:28 +02001033#ifdef CONFIG_COMPAT
1034
1035struct compat_consolefontdesc {
1036 unsigned short charcount; /* characters in font (256 or 512) */
1037 unsigned short charheight; /* scan lines per character (1-32) */
1038 compat_caddr_t chardata; /* font data in expanded form */
1039};
1040
1041static inline int
1042compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1043 int perm, struct console_font_op *op)
1044{
1045 struct compat_consolefontdesc cfdarg;
1046 int i;
1047
1048 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1049 return -EFAULT;
1050
1051 switch (cmd) {
1052 case PIO_FONTX:
1053 if (!perm)
1054 return -EPERM;
1055 op->op = KD_FONT_OP_SET;
1056 op->flags = KD_FONT_FLAG_OLD;
1057 op->width = 8;
1058 op->height = cfdarg.charheight;
1059 op->charcount = cfdarg.charcount;
1060 op->data = compat_ptr(cfdarg.chardata);
1061 return con_font_op(vc_cons[fg_console].d, op);
1062 case GIO_FONTX:
1063 op->op = KD_FONT_OP_GET;
1064 op->flags = KD_FONT_FLAG_OLD;
1065 op->width = 8;
1066 op->height = cfdarg.charheight;
1067 op->charcount = cfdarg.charcount;
1068 op->data = compat_ptr(cfdarg.chardata);
1069 i = con_font_op(vc_cons[fg_console].d, op);
1070 if (i)
1071 return i;
1072 cfdarg.charheight = op->height;
1073 cfdarg.charcount = op->charcount;
1074 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1075 return -EFAULT;
1076 return 0;
1077 }
1078 return -EINVAL;
1079}
1080
1081struct compat_console_font_op {
1082 compat_uint_t op; /* operation code KD_FONT_OP_* */
1083 compat_uint_t flags; /* KD_FONT_FLAG_* */
1084 compat_uint_t width, height; /* font size */
1085 compat_uint_t charcount;
1086 compat_caddr_t data; /* font data with height fixed to 32 */
1087};
1088
1089static inline int
1090compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1091 int perm, struct console_font_op *op, struct vc_data *vc)
1092{
1093 int i;
1094
1095 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1096 return -EFAULT;
1097 if (!perm && op->op != KD_FONT_OP_GET)
1098 return -EPERM;
1099 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001100 i = con_font_op(vc, op);
1101 if (i)
1102 return i;
1103 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1104 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1105 return -EFAULT;
1106 return 0;
1107}
1108
1109struct compat_unimapdesc {
1110 unsigned short entry_ct;
1111 compat_caddr_t entries;
1112};
1113
1114static inline int
1115compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1116 int perm, struct vc_data *vc)
1117{
1118 struct compat_unimapdesc tmp;
1119 struct unipair __user *tmp_entries;
1120
1121 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1122 return -EFAULT;
1123 tmp_entries = compat_ptr(tmp.entries);
1124 if (tmp_entries)
1125 if (!access_ok(VERIFY_WRITE, tmp_entries,
1126 tmp.entry_ct*sizeof(struct unipair)))
1127 return -EFAULT;
1128 switch (cmd) {
1129 case PIO_UNIMAP:
1130 if (!perm)
1131 return -EPERM;
1132 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1133 case GIO_UNIMAP:
1134 if (!perm && fg_console != vc->vc_num)
1135 return -EPERM;
1136 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1137 }
1138 return 0;
1139}
1140
Alan Cox6caa76b2011-02-14 16:27:22 +00001141long vt_compat_ioctl(struct tty_struct *tty,
Arnd Bergmanne9216652009-08-06 15:09:28 +02001142 unsigned int cmd, unsigned long arg)
1143{
1144 struct vc_data *vc = tty->driver_data;
1145 struct console_font_op op; /* used in multiple places here */
Arnd Bergmanne9216652009-08-06 15:09:28 +02001146 unsigned int console;
1147 void __user *up = (void __user *)arg;
1148 int perm;
1149 int ret = 0;
1150
1151 console = vc->vc_num;
1152
Arnd Bergmannec79d602010-06-01 22:53:01 +02001153 tty_lock();
Arnd Bergmanne9216652009-08-06 15:09:28 +02001154
1155 if (!vc_cons_allocated(console)) { /* impossible? */
1156 ret = -ENOIOCTLCMD;
1157 goto out;
1158 }
1159
1160 /*
1161 * To have permissions to do most of the vt ioctls, we either have
1162 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1163 */
1164 perm = 0;
1165 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1166 perm = 1;
1167
Arnd Bergmanne9216652009-08-06 15:09:28 +02001168 switch (cmd) {
1169 /*
1170 * these need special handlers for incompatible data structures
1171 */
1172 case PIO_FONTX:
1173 case GIO_FONTX:
1174 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1175 break;
1176
1177 case KDFONTOP:
1178 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1179 break;
1180
1181 case PIO_UNIMAP:
1182 case GIO_UNIMAP:
Andreas Schwab4b1fe772009-09-28 20:10:02 +02001183 ret = compat_unimap_ioctl(cmd, up, perm, vc);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001184 break;
1185
1186 /*
1187 * all these treat 'arg' as an integer
1188 */
1189 case KIOCSOUND:
1190 case KDMKTONE:
1191#ifdef CONFIG_X86
1192 case KDADDIO:
1193 case KDDELIO:
1194#endif
1195 case KDSETMODE:
1196 case KDMAPDISP:
1197 case KDUNMAPDISP:
1198 case KDSKBMODE:
1199 case KDSKBMETA:
1200 case KDSKBLED:
1201 case KDSETLED:
1202 case KDSIGACCEPT:
1203 case VT_ACTIVATE:
1204 case VT_WAITACTIVE:
1205 case VT_RELDISP:
1206 case VT_DISALLOCATE:
1207 case VT_RESIZE:
1208 case VT_RESIZEX:
1209 goto fallback;
1210
1211 /*
1212 * the rest has a compatible data structure behind arg,
1213 * but we have to convert it to a proper 64 bit pointer.
1214 */
1215 default:
1216 arg = (unsigned long)compat_ptr(arg);
1217 goto fallback;
1218 }
1219out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001220 tty_unlock();
Arnd Bergmanne9216652009-08-06 15:09:28 +02001221 return ret;
1222
1223fallback:
Arnd Bergmannec79d602010-06-01 22:53:01 +02001224 tty_unlock();
Alan Cox6caa76b2011-02-14 16:27:22 +00001225 return vt_ioctl(tty, cmd, arg);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001226}
1227
1228
1229#endif /* CONFIG_COMPAT */
1230
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
Alan Coxd3b5cff2009-09-19 13:13:26 -07001233 * Performs the back end of a vt switch. Called under the console
1234 * semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236static void complete_change_console(struct vc_data *vc)
1237{
1238 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001239 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 last_console = fg_console;
1242
1243 /*
1244 * If we're switching, we could be going from KD_GRAPHICS to
1245 * KD_TEXT mode or vice versa, which means we need to blank or
1246 * unblank the screen later.
1247 */
1248 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1249 switch_screen(vc);
1250
1251 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001252 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * then the *blank_screen operation could occur while X, having
1254 * received acqsig, is waking up on another processor. This
1255 * condition can lead to overlapping accesses to the VGA range
1256 * and the framebuffer (causing system lockups).
1257 *
1258 * To account for this we duplicate this code below only if the
1259 * controlling process is gone and we've called reset_vc.
1260 */
1261 if (old_vc_mode != vc->vc_mode) {
1262 if (vc->vc_mode == KD_TEXT)
1263 do_unblank_screen(1);
1264 else
1265 do_blank_screen(1);
1266 }
1267
1268 /*
1269 * If this new console is under process control, send it a signal
1270 * telling it that it has acquired. Also check if it has died and
1271 * clean up (similar to logic employed in change_console())
1272 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001273 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001275 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * tell us if the process has gone or something else
1277 * is awry
1278 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001279 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * The controlling process has died, so we revert back to
1282 * normal operation. In this case, we'll also change back
1283 * to KD_TEXT mode. I'm not sure if this is strictly correct
1284 * but it saves the agony when the X server dies and the screen
1285 * remains blanked due to KD_GRAPHICS! It would be nice to do
1286 * this outside of VT_PROCESS but there is no single process
1287 * to account for and tracking tty count may be undesirable.
1288 */
1289 reset_vc(vc);
1290
1291 if (old_vc_mode != vc->vc_mode) {
1292 if (vc->vc_mode == KD_TEXT)
1293 do_unblank_screen(1);
1294 else
1295 do_blank_screen(1);
1296 }
1297 }
1298 }
1299
1300 /*
1301 * Wake anyone waiting for their VT to activate
1302 */
Alan Cox8b92e872009-09-19 13:13:24 -07001303 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return;
1305}
1306
1307/*
1308 * Performs the front-end of a vt switch
1309 */
1310void change_console(struct vc_data *new_vc)
1311{
1312 struct vc_data *vc;
1313
1314 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1315 return;
1316
1317 /*
1318 * If this vt is in process mode, then we need to handshake with
1319 * that process before switching. Essentially, we store where that
1320 * vt wants to switch to and wait for it to tell us when it's done
1321 * (via VT_RELDISP ioctl).
1322 *
1323 * We also check to see if the controlling process still exists.
1324 * If it doesn't, we reset this vt to auto mode and continue.
1325 * This is a cheap way to track process control. The worst thing
1326 * that can happen is: we send a signal to a process, it dies, and
1327 * the switch gets "lost" waiting for a response; hopefully, the
1328 * user will try again, we'll detect the process is gone (unless
1329 * the user waits just the right amount of time :-) and revert the
1330 * vt to auto control.
1331 */
1332 vc = vc_cons[fg_console].d;
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001333 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001335 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001337 * is awry.
1338 *
1339 * We need to set vt_newvt *before* sending the signal or we
1340 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001342 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001343 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001345 * It worked. Mark the vt to switch to and
1346 * return. The process needs to send us a
1347 * VT_RELDISP ioctl to complete the switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001349 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
1352 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001353 * The controlling process has died, so we revert back to
1354 * normal operation. In this case, we'll also change back
1355 * to KD_TEXT mode. I'm not sure if this is strictly correct
1356 * but it saves the agony when the X server dies and the screen
1357 * remains blanked due to KD_GRAPHICS! It would be nice to do
1358 * this outside of VT_PROCESS but there is no single process
1359 * to account for and tracking tty count may be undesirable.
1360 */
1361 reset_vc(vc);
1362
1363 /*
1364 * Fall through to normal (VT_AUTO) handling of the switch...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366 }
1367
1368 /*
1369 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1370 */
1371 if (vc->vc_mode == KD_GRAPHICS)
1372 return;
1373
1374 complete_change_console(new_vc);
1375}
Alan Cox8d233552009-09-19 13:13:25 -07001376
1377/* Perform a kernel triggered VT switch for suspend/resume */
1378
1379static int disable_vt_switch;
1380
1381int vt_move_to_console(unsigned int vt, int alloc)
1382{
1383 int prev;
1384
Torben Hohnac751ef2011-01-25 15:07:35 -08001385 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001386 /* Graphics mode - up to X */
1387 if (disable_vt_switch) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001388 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001389 return 0;
1390 }
1391 prev = fg_console;
1392
1393 if (alloc && vc_allocate(vt)) {
1394 /* we can't have a free VC for now. Too bad,
1395 * we don't want to mess the screen for now. */
Torben Hohnac751ef2011-01-25 15:07:35 -08001396 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001397 return -ENOSPC;
1398 }
1399
1400 if (set_console(vt)) {
1401 /*
1402 * We're unable to switch to the SUSPEND_CONSOLE.
1403 * Let the calling function know so it can decide
1404 * what to do.
1405 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001406 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001407 return -EIO;
1408 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001409 console_unlock();
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001410 tty_lock();
Jiri Slaby797938b2009-08-11 23:20:41 +02001411 if (vt_waitactive(vt + 1)) {
Alan Cox8d233552009-09-19 13:13:25 -07001412 pr_debug("Suspend: Can't switch VCs.");
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001413 tty_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001414 return -EINTR;
1415 }
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001416 tty_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001417 return prev;
1418}
1419
1420/*
1421 * Normally during a suspend, we allocate a new console and switch to it.
1422 * When we resume, we switch back to the original console. This switch
1423 * can be slow, so on systems where the framebuffer can handle restoration
1424 * of video registers anyways, there's little point in doing the console
1425 * switch. This function allows you to disable it by passing it '0'.
1426 */
1427void pm_set_vt_switch(int do_switch)
1428{
Torben Hohnac751ef2011-01-25 15:07:35 -08001429 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001430 disable_vt_switch = !do_switch;
Torben Hohnac751ef2011-01-25 15:07:35 -08001431 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001432}
1433EXPORT_SYMBOL(pm_set_vt_switch);