blob: e05094d76344e0297d99db3d861217d351a03504 [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
Alan Cox9cc3c222008-04-30 00:53:26 -0700284
285 if (!vc_cons_allocated(console)) { /* impossible? */
286 ret = -ENOIOCTLCMD;
287 goto out;
288 }
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /*
292 * To have permissions to do most of the vt ioctls, we either have
293 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
294 */
295 perm = 0;
296 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
297 perm = 1;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100300 case TIOCLINUX:
Alan Cox4001d7b2012-03-02 14:59:20 +0000301 tty_lock();
Jiri Slabya1159022009-06-22 18:42:18 +0100302 ret = tioclinux(tty, arg);
Alan Cox4001d7b2012-03-02 14:59:20 +0000303 tty_unlock();
Jiri Slabya1159022009-06-22 18:42:18 +0100304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case KIOCSOUND:
306 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000307 return -EPERM;
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700308 /*
309 * The use of PIT_TICK_RATE is historic, it used to be
310 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
311 * and 2.6.36, which was a minor but unfortunate ABI
Alan Cox4001d7b2012-03-02 14:59:20 +0000312 * change. kd_mksound is locked by the input layer.
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (arg)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700315 arg = PIT_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 case KDMKTONE:
320 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000321 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 {
323 unsigned int ticks, count;
324
325 /*
326 * Generate the tone for the appropriate number of ticks.
327 * If the time is zero, turn off sound ourselves.
328 */
329 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
330 count = ticks ? (arg & 0xffff) : 0;
331 if (count)
Arnd Bergmann2c4e9672010-08-28 20:35:17 -0700332 count = PIT_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 case KDGKBTYPE:
338 /*
Alan Cox4001d7b2012-03-02 14:59:20 +0000339 * this is naïve.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341 ucval = KB_101;
Alan Cox079c9532012-02-28 14:49:23 +0000342 ret = put_user(ucval, (char __user *)arg);
343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /*
346 * These cannot be implemented on any machine that implements
347 * ioperm() in user level (such as Alpha PCs) or not at all.
348 *
349 * XXX: you should never use these, just call ioperm directly..
350 */
351#ifdef CONFIG_X86
352 case KDADDIO:
353 case KDDELIO:
354 /*
355 * KDADDIO and KDDELIO may be able to add ports beyond what
356 * we reject here, but to be safe...
Alan Cox4001d7b2012-03-02 14:59:20 +0000357 *
358 * These are locked internally via sys_ioperm
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700360 if (arg < GPFIRST || arg > GPLAST) {
361 ret = -EINVAL;
362 break;
363 }
364 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 case KDENABIO:
368 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700369 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#endif
373
374 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
375
376 case KDKBDREP:
377 {
378 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +0000381 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Alan Cox9cc3c222008-04-30 00:53:26 -0700383 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
384 ret = -EFAULT;
385 break;
386 }
387 ret = kbd_rate(&kbrep);
388 if (ret)
389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700391 ret = -EFAULT;
392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 case KDSETMODE:
396 /*
397 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
398 * doesn't do a whole lot. i'm not sure if it should do any
399 * restoration of modes or what...
400 *
401 * XXX It should at least call into the driver, fbdev's definitely
402 * need to restore their engine state. --BenH
403 */
404 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000405 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 switch (arg) {
407 case KD_GRAPHICS:
408 break;
409 case KD_TEXT0:
410 case KD_TEXT1:
411 arg = KD_TEXT;
412 case KD_TEXT:
413 break;
414 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700415 ret = -EINVAL;
416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Alan Cox4001d7b2012-03-02 14:59:20 +0000418 /* FIXME: this needs the console lock extending */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 vc->vc_mode = (unsigned char) arg;
422 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * explicitly blank/unblank the screen if switching modes
426 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800427 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (arg == KD_TEXT)
429 do_unblank_screen(1);
430 else
431 do_blank_screen(1);
Torben Hohnac751ef2011-01-25 15:07:35 -0800432 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 case KDGETMODE:
Graham Gower1e0ad282010-10-27 15:33:00 -0700436 uival = vc->vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto setint;
438
439 case KDMAPDISP:
440 case KDUNMAPDISP:
441 /*
442 * these work like a combination of mmap and KDENABIO.
443 * this could be easily finished.
444 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700445 ret = -EINVAL;
446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 case KDSKBMODE:
449 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000450 return -EPERM;
Alan Cox079c9532012-02-28 14:49:23 +0000451 ret = vt_do_kdskbmode(console, arg);
452 if (ret == 0)
453 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 case KDGKBMODE:
Alan Cox079c9532012-02-28 14:49:23 +0000457 uival = vt_do_kdgkbmode(console);
458 ret = put_user(uival, (int __user *)arg);
459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* this could be folded into KDSKBMODE, but for compatibility
462 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
463 case KDSKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000464 ret = vt_do_kdskbmeta(console, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 case KDGKBMETA:
Alan Cox079c9532012-02-28 14:49:23 +0000468 /* FIXME: should review whether this is worth locking */
469 uival = vt_do_kdgkbmeta(console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 setint:
Graham Gower1e0ad282010-10-27 15:33:00 -0700471 ret = put_user(uival, (int __user *)arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 case KDGETKEYCODE:
475 case KDSETKEYCODE:
476 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700477 perm = 0;
Alan Cox079c9532012-02-28 14:49:23 +0000478 ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 case KDGKBENT:
482 case KDSKBENT:
Alan Cox079c9532012-02-28 14:49:23 +0000483 ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
Alan Cox9cc3c222008-04-30 00:53:26 -0700484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 case KDGKBSENT:
487 case KDSKBSENT:
Alan Cox079c9532012-02-28 14:49:23 +0000488 ret = vt_do_kdgkb_ioctl(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alan Cox247ff8e2012-02-24 12:47:11 +0000491 /* Diacritical processing. Handled in keyboard.c as it has
492 to operate on the keyboard locks and structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case KDGKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700494 case KDGKBDIACRUC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case KDSKBDIACR:
Samuel Thibault04c71972007-10-16 23:27:04 -0700496 case KDSKBDIACRUC:
Alan Cox247ff8e2012-02-24 12:47:11 +0000497 ret = vt_do_diacrit(cmd, up, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* the ioctls below read/set the flags usually shown in the leds */
501 /* don't use them - they will go away without warning */
502 case KDGKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case KDSKBLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 case KDGETLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case KDSETLED:
Alan Cox079c9532012-02-28 14:49:23 +0000506 ret = vt_do_kdskled(console, cmd, arg, perm);
Alan Cox9cc3c222008-04-30 00:53:26 -0700507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /*
510 * A process can indicate its willingness to accept signals
511 * generated by pressing an appropriate key combination.
512 * Thus, one can have a daemon that e.g. spawns a new console
513 * upon a keypress and then changes to it.
514 * See also the kbrequest field of inittab(5).
515 */
516 case KDSIGACCEPT:
517 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!perm || !capable(CAP_KILL))
Alan Cox4001d7b2012-03-02 14:59:20 +0000519 return -EPERM;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700520 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700521 ret = -EINVAL;
522 else {
523 spin_lock_irq(&vt_spawn_con.lock);
524 put_pid(vt_spawn_con.pid);
525 vt_spawn_con.pid = get_pid(task_pid(current));
526 vt_spawn_con.sig = arg;
527 spin_unlock_irq(&vt_spawn_con.lock);
528 }
529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 case VT_SETMODE:
533 {
534 struct vt_mode tmp;
535
536 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000537 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700538 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
539 ret = -EFAULT;
540 goto out;
541 }
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -0700542 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700543 ret = -EINVAL;
544 goto out;
545 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800546 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 vc->vt_mode = tmp;
548 /* the frsig is ignored, so we set it to 0 */
549 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800550 put_pid(vc->vt_pid);
551 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* no switch is required -- saw@shade.msu.ru */
553 vc->vt_newvt = -1;
Torben Hohnac751ef2011-01-25 15:07:35 -0800554 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 case VT_GETMODE:
559 {
560 struct vt_mode tmp;
561 int rc;
562
Torben Hohnac751ef2011-01-25 15:07:35 -0800563 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
Torben Hohnac751ef2011-01-25 15:07:35 -0800565 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700568 if (rc)
569 ret = -EFAULT;
570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 /*
574 * Returns global vt state. Note that VT 0 is always open, since
575 * it's an alias for the current VT, and people can't use it here.
576 * We cannot return state for more than 16 VTs, since v_state is short.
577 */
578 case VT_GETSTATE:
579 {
580 struct vt_stat __user *vtstat = up;
581 unsigned short state, mask;
582
Alan Cox4001d7b2012-03-02 14:59:20 +0000583 /* Review: FIXME: Console lock ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700585 ret = -EFAULT;
586 else {
587 state = 1; /* /dev/tty0 is always open */
588 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
589 ++i, mask <<= 1)
590 if (VT_IS_IN_USE(i))
591 state |= mask;
592 ret = put_user(state, &vtstat->v_state);
593 }
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 /*
598 * Returns the first available (non-opened) console.
599 */
600 case VT_OPENQRY:
Alan Cox4001d7b2012-03-02 14:59:20 +0000601 /* FIXME: locking ? - but then this is a stupid API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 for (i = 0; i < MAX_NR_CONSOLES; ++i)
603 if (! VT_IS_IN_USE(i))
604 break;
Graham Gower1e0ad282010-10-27 15:33:00 -0700605 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto setint;
607
608 /*
609 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
610 * with num >= 1 (switches to vt 0, our console, are not allowed, just
611 * to preserve sanity).
612 */
613 case VT_ACTIVATE:
614 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000615 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700617 ret = -ENXIO;
618 else {
619 arg--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800620 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700621 ret = vc_allocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800622 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700623 if (ret)
624 break;
625 set_console(arg);
626 }
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Alan Coxd3b5cff2009-09-19 13:13:26 -0700629 case VT_SETACTIVATE:
630 {
631 struct vt_setactivate vsa;
632
633 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000634 return -EPERM;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700635
636 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
Jiri Slabya09efb02009-10-01 15:43:59 -0700637 sizeof(struct vt_setactivate))) {
638 ret = -EFAULT;
639 goto out;
640 }
Alan Coxd3b5cff2009-09-19 13:13:26 -0700641 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
642 ret = -ENXIO;
643 else {
644 vsa.console--;
Torben Hohnac751ef2011-01-25 15:07:35 -0800645 console_lock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700646 ret = vc_allocate(vsa.console);
647 if (ret == 0) {
648 struct vc_data *nvc;
649 /* This is safe providing we don't drop the
650 console sem between vc_allocate and
651 finishing referencing nvc */
652 nvc = vc_cons[vsa.console].d;
653 nvc->vt_mode = vsa.mode;
654 nvc->vt_mode.frsig = 0;
655 put_pid(nvc->vt_pid);
656 nvc->vt_pid = get_pid(task_pid(current));
657 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800658 console_unlock();
Alan Coxd3b5cff2009-09-19 13:13:26 -0700659 if (ret)
660 break;
661 /* Commence switch and lock */
Alan Cox4001d7b2012-03-02 14:59:20 +0000662 /* Review set_console locks */
Jiri Olsad6378372011-02-11 15:39:28 +0100663 set_console(vsa.console);
Alan Coxd3b5cff2009-09-19 13:13:26 -0700664 }
Jiri Olsad6378372011-02-11 15:39:28 +0100665 break;
Alan Coxd3b5cff2009-09-19 13:13:26 -0700666 }
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * wait until the specified VT has been activated
670 */
671 case VT_WAITACTIVE:
672 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000673 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700675 ret = -ENXIO;
Alan Cox4001d7b2012-03-02 14:59:20 +0000676 else {
677 tty_lock();
Alan Cox8b92e872009-09-19 13:13:24 -0700678 ret = vt_waitactive(arg);
Alan Cox4001d7b2012-03-02 14:59:20 +0000679 tty_unlock();
680 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /*
684 * If a vt is under process control, the kernel will not switch to it
685 * immediately, but postpone the operation until the process calls this
686 * ioctl, allowing the switch to complete.
687 *
688 * According to the X sources this is the behavior:
689 * 0: pending switch-from not OK
690 * 1: pending switch-from OK
691 * 2: completed switch-to OK
692 */
693 case VT_RELDISP:
694 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000695 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Alan Cox4001d7b2012-03-02 14:59:20 +0000697 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700698 if (vc->vt_mode.mode != VT_PROCESS) {
Alan Cox4001d7b2012-03-02 14:59:20 +0000699 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700700 ret = -EINVAL;
701 break;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
704 * Switching-from response
705 */
706 if (vc->vt_newvt >= 0) {
707 if (arg == 0)
708 /*
709 * Switch disallowed, so forget we were trying
710 * to do it.
711 */
712 vc->vt_newvt = -1;
713
714 else {
715 /*
716 * The current vt has been released, so
717 * complete the switch.
718 */
719 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 newvt = vc->vt_newvt;
721 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700722 ret = vc_allocate(newvt);
723 if (ret) {
Torben Hohnac751ef2011-01-25 15:07:35 -0800724 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 /*
728 * When we actually do the console switch,
729 * make sure we are atomic with respect to
730 * other console switches..
731 */
732 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700734 } else {
735 /*
736 * Switched-to response
737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * If it's just an ACK, ignore it
740 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700741 if (arg != VT_ACKACQ)
742 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800744 console_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
748 * Disallocate memory associated to VT (but leave VT1)
749 */
750 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -0700751 if (arg > MAX_NR_CONSOLES) {
752 ret = -ENXIO;
753 break;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -0700756 /* deallocate all unused consoles, but leave 0 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800757 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 for (i=1; i<MAX_NR_CONSOLES; i++)
759 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -0700760 vc_deallocate(i);
Torben Hohnac751ef2011-01-25 15:07:35 -0800761 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
Alan Coxca9bda02006-09-29 02:00:03 -0700763 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 arg--;
765 if (VT_BUSY(arg))
Alan Cox9cc3c222008-04-30 00:53:26 -0700766 ret = -EBUSY;
767 else if (arg) { /* leave 0 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800768 console_lock();
Alan Coxca9bda02006-09-29 02:00:03 -0700769 vc_deallocate(arg);
Torben Hohnac751ef2011-01-25 15:07:35 -0800770 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 case VT_RESIZE:
776 {
777 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700778 struct vc_data *vc;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ushort ll,cc;
781 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000782 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (get_user(ll, &vtsizes->v_rows) ||
784 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -0700785 ret = -EFAULT;
786 else {
Torben Hohnac751ef2011-01-25 15:07:35 -0800787 console_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700788 for (i = 0; i < MAX_NR_CONSOLES; i++) {
789 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700790
Alan Cox9cc3c222008-04-30 00:53:26 -0700791 if (vc) {
792 vc->vc_resize_user = 1;
Alan Cox4001d7b2012-03-02 14:59:20 +0000793 /* FIXME: review v tty lock */
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100794 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -0700795 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700796 }
Torben Hohnac751ef2011-01-25 15:07:35 -0800797 console_unlock();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700798 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 case VT_RESIZEX:
803 {
804 struct vt_consize __user *vtconsize = up;
805 ushort ll,cc,vlin,clin,vcol,ccol;
806 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000807 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox9cc3c222008-04-30 00:53:26 -0700809 sizeof(struct vt_consize))) {
810 ret = -EFAULT;
811 break;
812 }
813 /* FIXME: Should check the copies properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 __get_user(ll, &vtconsize->v_rows);
815 __get_user(cc, &vtconsize->v_cols);
816 __get_user(vlin, &vtconsize->v_vlin);
817 __get_user(clin, &vtconsize->v_clin);
818 __get_user(vcol, &vtconsize->v_vcol);
819 __get_user(ccol, &vtconsize->v_ccol);
820 vlin = vlin ? vlin : vc->vc_scan_lines;
821 if (clin) {
822 if (ll) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700823 if (ll != vlin/clin) {
824 /* Parameters don't add up */
825 ret = -EINVAL;
826 break;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 } else
829 ll = vlin/clin;
830 }
831 if (vcol && ccol) {
832 if (cc) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700833 if (cc != vcol/ccol) {
834 ret = -EINVAL;
835 break;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else
838 cc = vcol/ccol;
839 }
840
Alan Cox9cc3c222008-04-30 00:53:26 -0700841 if (clin > 32) {
842 ret = -EINVAL;
843 break;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 for (i = 0; i < MAX_NR_CONSOLES; i++) {
847 if (!vc_cons[i].d)
848 continue;
Torben Hohnac751ef2011-01-25 15:07:35 -0800849 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (vlin)
851 vc_cons[i].d->vc_scan_lines = vlin;
852 if (clin)
853 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700854 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 vc_resize(vc_cons[i].d, cc, ll);
Torben Hohnac751ef2011-01-25 15:07:35 -0800856 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 case PIO_FONT: {
862 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000863 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 op.op = KD_FONT_OP_SET;
865 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
866 op.width = 8;
867 op.height = 0;
868 op.charcount = 256;
869 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700870 ret = con_font_op(vc_cons[fg_console].d, &op);
871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
874 case GIO_FONT: {
875 op.op = KD_FONT_OP_GET;
876 op.flags = KD_FONT_FLAG_OLD;
877 op.width = 8;
878 op.height = 32;
879 op.charcount = 256;
880 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -0700881 ret = con_font_op(vc_cons[fg_console].d, &op);
882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
885 case PIO_CMAP:
886 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700887 ret = -EPERM;
888 else
889 ret = con_set_cmap(up);
890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -0700893 ret = con_get_cmap(up);
894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 case PIO_FONTX:
897 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -0700898 ret = do_fontx_ioctl(cmd, up, perm, &op);
899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 case PIO_FONTRESET:
902 {
903 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000904 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906#ifdef BROKEN_GRAPHICS_PROGRAMS
907 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
908 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -0700909 ret = -ENOSYS;
910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#else
912 {
913 op.op = KD_FONT_OP_SET_DEFAULT;
914 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -0700915 ret = con_font_op(vc_cons[fg_console].d, &op);
916 if (ret)
917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox9cc3c222008-04-30 00:53:26 -0700919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921#endif
922 }
923
924 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -0700925 if (copy_from_user(&op, up, sizeof(op))) {
926 ret = -EFAULT;
927 break;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox4001d7b2012-03-02 14:59:20 +0000930 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700931 ret = con_font_op(vc, &op);
932 if (ret)
933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700935 ret = -EFAULT;
936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 case PIO_SCRNMAP:
940 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700941 ret = -EPERM;
Alan Cox4001d7b2012-03-02 14:59:20 +0000942 else {
943 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700944 ret = con_set_trans_old(up);
Alan Cox4001d7b2012-03-02 14:59:20 +0000945 tty_unlock();
946 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 case GIO_SCRNMAP:
Alan Cox4001d7b2012-03-02 14:59:20 +0000950 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700951 ret = con_get_trans_old(up);
Alan Cox4001d7b2012-03-02 14:59:20 +0000952 tty_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 case PIO_UNISCRNMAP:
956 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700957 ret = -EPERM;
Alan Cox4001d7b2012-03-02 14:59:20 +0000958 else {
959 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700960 ret = con_set_trans_new(up);
Alan Cox4001d7b2012-03-02 14:59:20 +0000961 tty_unlock();
962 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 case GIO_UNISCRNMAP:
Alan Cox4001d7b2012-03-02 14:59:20 +0000966 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700967 ret = con_get_trans_new(up);
Alan Cox4001d7b2012-03-02 14:59:20 +0000968 tty_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 case PIO_UNIMAPCLR:
972 { struct unimapinit ui;
973 if (!perm)
Alan Cox4001d7b2012-03-02 14:59:20 +0000974 return -EPERM;
Alan Cox9cc3c222008-04-30 00:53:26 -0700975 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
Dan Carpenter3fde85d2010-06-04 12:20:46 +0200976 if (ret)
977 ret = -EFAULT;
Alan Cox4001d7b2012-03-02 14:59:20 +0000978 else {
979 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700980 con_clear_unimap(vc, &ui);
Alan Cox4001d7b2012-03-02 14:59:20 +0000981 tty_unlock();
982 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 case PIO_UNIMAP:
987 case GIO_UNIMAP:
Alan Cox4001d7b2012-03-02 14:59:20 +0000988 tty_lock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700989 ret = do_unimap_ioctl(cmd, up, perm, vc);
Alan Cox4001d7b2012-03-02 14:59:20 +0000990 tty_unlock();
Alan Cox9cc3c222008-04-30 00:53:26 -0700991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 case VT_LOCKSWITCH:
994 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +0000995 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -0700997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 case VT_UNLOCKSWITCH:
999 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox4001d7b2012-03-02 14:59:20 +00001000 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -07001002 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001003 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001004 ret = put_user(vc->vc_hi_font_mask,
1005 (unsigned short __user *)arg);
1006 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001007 case VT_WAITEVENT:
1008 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001011 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001013out:
Alan Cox9cc3c222008-04-30 00:53:26 -07001014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017void reset_vc(struct vc_data *vc)
1018{
1019 vc->vc_mode = KD_TEXT;
Alan Cox079c9532012-02-28 14:49:23 +00001020 vt_reset_unicode(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 vc->vt_mode.mode = VT_AUTO;
1022 vc->vt_mode.waitv = 0;
1023 vc->vt_mode.relsig = 0;
1024 vc->vt_mode.acqsig = 0;
1025 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001026 put_pid(vc->vt_pid);
1027 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 vc->vt_newvt = -1;
1029 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1030 reset_palette(vc);
1031}
1032
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001033void vc_SAK(struct work_struct *work)
1034{
1035 struct vc *vc_con =
1036 container_of(work, struct vc, SAK_work);
1037 struct vc_data *vc;
1038 struct tty_struct *tty;
1039
Torben Hohnac751ef2011-01-25 15:07:35 -08001040 console_lock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001041 vc = vc_con->d;
1042 if (vc) {
Alan Cox079c9532012-02-28 14:49:23 +00001043 /* FIXME: review tty ref counting */
Alan Cox8ce73262010-06-01 22:52:56 +02001044 tty = vc->port.tty;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001045 /*
1046 * SAK should also work in all raw modes and reset
1047 * them properly.
1048 */
1049 if (tty)
1050 __do_SAK(tty);
1051 reset_vc(vc);
1052 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001053 console_unlock();
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001054}
1055
Arnd Bergmanne9216652009-08-06 15:09:28 +02001056#ifdef CONFIG_COMPAT
1057
1058struct compat_consolefontdesc {
1059 unsigned short charcount; /* characters in font (256 or 512) */
1060 unsigned short charheight; /* scan lines per character (1-32) */
1061 compat_caddr_t chardata; /* font data in expanded form */
1062};
1063
1064static inline int
1065compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1066 int perm, struct console_font_op *op)
1067{
1068 struct compat_consolefontdesc cfdarg;
1069 int i;
1070
1071 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1072 return -EFAULT;
1073
1074 switch (cmd) {
1075 case PIO_FONTX:
1076 if (!perm)
1077 return -EPERM;
1078 op->op = KD_FONT_OP_SET;
1079 op->flags = KD_FONT_FLAG_OLD;
1080 op->width = 8;
1081 op->height = cfdarg.charheight;
1082 op->charcount = cfdarg.charcount;
1083 op->data = compat_ptr(cfdarg.chardata);
1084 return con_font_op(vc_cons[fg_console].d, op);
1085 case GIO_FONTX:
1086 op->op = KD_FONT_OP_GET;
1087 op->flags = KD_FONT_FLAG_OLD;
1088 op->width = 8;
1089 op->height = cfdarg.charheight;
1090 op->charcount = cfdarg.charcount;
1091 op->data = compat_ptr(cfdarg.chardata);
1092 i = con_font_op(vc_cons[fg_console].d, op);
1093 if (i)
1094 return i;
1095 cfdarg.charheight = op->height;
1096 cfdarg.charcount = op->charcount;
1097 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1098 return -EFAULT;
1099 return 0;
1100 }
1101 return -EINVAL;
1102}
1103
1104struct compat_console_font_op {
1105 compat_uint_t op; /* operation code KD_FONT_OP_* */
1106 compat_uint_t flags; /* KD_FONT_FLAG_* */
1107 compat_uint_t width, height; /* font size */
1108 compat_uint_t charcount;
1109 compat_caddr_t data; /* font data with height fixed to 32 */
1110};
1111
1112static inline int
1113compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1114 int perm, struct console_font_op *op, struct vc_data *vc)
1115{
1116 int i;
1117
1118 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1119 return -EFAULT;
1120 if (!perm && op->op != KD_FONT_OP_GET)
1121 return -EPERM;
1122 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001123 i = con_font_op(vc, op);
1124 if (i)
1125 return i;
1126 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1127 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1128 return -EFAULT;
1129 return 0;
1130}
1131
1132struct compat_unimapdesc {
1133 unsigned short entry_ct;
1134 compat_caddr_t entries;
1135};
1136
1137static inline int
1138compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1139 int perm, struct vc_data *vc)
1140{
1141 struct compat_unimapdesc tmp;
1142 struct unipair __user *tmp_entries;
1143
1144 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1145 return -EFAULT;
1146 tmp_entries = compat_ptr(tmp.entries);
1147 if (tmp_entries)
1148 if (!access_ok(VERIFY_WRITE, tmp_entries,
1149 tmp.entry_ct*sizeof(struct unipair)))
1150 return -EFAULT;
1151 switch (cmd) {
1152 case PIO_UNIMAP:
1153 if (!perm)
1154 return -EPERM;
1155 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1156 case GIO_UNIMAP:
1157 if (!perm && fg_console != vc->vc_num)
1158 return -EPERM;
1159 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1160 }
1161 return 0;
1162}
1163
Alan Cox6caa76b2011-02-14 16:27:22 +00001164long vt_compat_ioctl(struct tty_struct *tty,
Arnd Bergmanne9216652009-08-06 15:09:28 +02001165 unsigned int cmd, unsigned long arg)
1166{
1167 struct vc_data *vc = tty->driver_data;
1168 struct console_font_op op; /* used in multiple places here */
Arnd Bergmanne9216652009-08-06 15:09:28 +02001169 unsigned int console;
1170 void __user *up = (void __user *)arg;
1171 int perm;
1172 int ret = 0;
1173
1174 console = vc->vc_num;
1175
Arnd Bergmanne9216652009-08-06 15:09:28 +02001176 if (!vc_cons_allocated(console)) { /* impossible? */
1177 ret = -ENOIOCTLCMD;
1178 goto out;
1179 }
1180
1181 /*
1182 * To have permissions to do most of the vt ioctls, we either have
1183 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1184 */
1185 perm = 0;
1186 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1187 perm = 1;
1188
Arnd Bergmanne9216652009-08-06 15:09:28 +02001189 switch (cmd) {
1190 /*
1191 * these need special handlers for incompatible data structures
1192 */
1193 case PIO_FONTX:
1194 case GIO_FONTX:
1195 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1196 break;
1197
1198 case KDFONTOP:
1199 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1200 break;
1201
1202 case PIO_UNIMAP:
1203 case GIO_UNIMAP:
Alan Cox4001d7b2012-03-02 14:59:20 +00001204 tty_lock();
Andreas Schwab4b1fe772009-09-28 20:10:02 +02001205 ret = compat_unimap_ioctl(cmd, up, perm, vc);
Alan Cox4001d7b2012-03-02 14:59:20 +00001206 tty_unlock();
Arnd Bergmanne9216652009-08-06 15:09:28 +02001207 break;
1208
1209 /*
1210 * all these treat 'arg' as an integer
1211 */
1212 case KIOCSOUND:
1213 case KDMKTONE:
1214#ifdef CONFIG_X86
1215 case KDADDIO:
1216 case KDDELIO:
1217#endif
1218 case KDSETMODE:
1219 case KDMAPDISP:
1220 case KDUNMAPDISP:
1221 case KDSKBMODE:
1222 case KDSKBMETA:
1223 case KDSKBLED:
1224 case KDSETLED:
1225 case KDSIGACCEPT:
1226 case VT_ACTIVATE:
1227 case VT_WAITACTIVE:
1228 case VT_RELDISP:
1229 case VT_DISALLOCATE:
1230 case VT_RESIZE:
1231 case VT_RESIZEX:
1232 goto fallback;
1233
1234 /*
1235 * the rest has a compatible data structure behind arg,
1236 * but we have to convert it to a proper 64 bit pointer.
1237 */
1238 default:
1239 arg = (unsigned long)compat_ptr(arg);
1240 goto fallback;
1241 }
1242out:
Arnd Bergmanne9216652009-08-06 15:09:28 +02001243 return ret;
1244
1245fallback:
Alan Cox6caa76b2011-02-14 16:27:22 +00001246 return vt_ioctl(tty, cmd, arg);
Arnd Bergmanne9216652009-08-06 15:09:28 +02001247}
1248
1249
1250#endif /* CONFIG_COMPAT */
1251
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/*
Alan Coxd3b5cff2009-09-19 13:13:26 -07001254 * Performs the back end of a vt switch. Called under the console
1255 * semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257static void complete_change_console(struct vc_data *vc)
1258{
1259 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001260 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 last_console = fg_console;
1263
1264 /*
1265 * If we're switching, we could be going from KD_GRAPHICS to
1266 * KD_TEXT mode or vice versa, which means we need to blank or
1267 * unblank the screen later.
1268 */
1269 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1270 switch_screen(vc);
1271
1272 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001273 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * then the *blank_screen operation could occur while X, having
1275 * received acqsig, is waking up on another processor. This
1276 * condition can lead to overlapping accesses to the VGA range
1277 * and the framebuffer (causing system lockups).
1278 *
1279 * To account for this we duplicate this code below only if the
1280 * controlling process is gone and we've called reset_vc.
1281 */
1282 if (old_vc_mode != vc->vc_mode) {
1283 if (vc->vc_mode == KD_TEXT)
1284 do_unblank_screen(1);
1285 else
1286 do_blank_screen(1);
1287 }
1288
1289 /*
1290 * If this new console is under process control, send it a signal
1291 * telling it that it has acquired. Also check if it has died and
1292 * clean up (similar to logic employed in change_console())
1293 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001294 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001296 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 * tell us if the process has gone or something else
1298 * is awry
1299 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001300 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * The controlling process has died, so we revert back to
1303 * normal operation. In this case, we'll also change back
1304 * to KD_TEXT mode. I'm not sure if this is strictly correct
1305 * but it saves the agony when the X server dies and the screen
1306 * remains blanked due to KD_GRAPHICS! It would be nice to do
1307 * this outside of VT_PROCESS but there is no single process
1308 * to account for and tracking tty count may be undesirable.
1309 */
1310 reset_vc(vc);
1311
1312 if (old_vc_mode != vc->vc_mode) {
1313 if (vc->vc_mode == KD_TEXT)
1314 do_unblank_screen(1);
1315 else
1316 do_blank_screen(1);
1317 }
1318 }
1319 }
1320
1321 /*
1322 * Wake anyone waiting for their VT to activate
1323 */
Alan Cox8b92e872009-09-19 13:13:24 -07001324 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return;
1326}
1327
1328/*
1329 * Performs the front-end of a vt switch
1330 */
1331void change_console(struct vc_data *new_vc)
1332{
1333 struct vc_data *vc;
1334
1335 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1336 return;
1337
1338 /*
1339 * If this vt is in process mode, then we need to handshake with
1340 * that process before switching. Essentially, we store where that
1341 * vt wants to switch to and wait for it to tell us when it's done
1342 * (via VT_RELDISP ioctl).
1343 *
1344 * We also check to see if the controlling process still exists.
1345 * If it doesn't, we reset this vt to auto mode and continue.
1346 * This is a cheap way to track process control. The worst thing
1347 * that can happen is: we send a signal to a process, it dies, and
1348 * the switch gets "lost" waiting for a response; hopefully, the
1349 * user will try again, we'll detect the process is gone (unless
1350 * the user waits just the right amount of time :-) and revert the
1351 * vt to auto control.
1352 */
1353 vc = vc_cons[fg_console].d;
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001354 if (vc->vt_mode.mode == VT_PROCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001356 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001358 * is awry.
1359 *
1360 * We need to set vt_newvt *before* sending the signal or we
1361 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001363 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001364 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001366 * It worked. Mark the vt to switch to and
1367 * return. The process needs to send us a
1368 * VT_RELDISP ioctl to complete the switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
1373 /*
Greg Kroah-Hartman87a6aca2010-03-15 17:14:15 -07001374 * The controlling process has died, so we revert back to
1375 * normal operation. In this case, we'll also change back
1376 * to KD_TEXT mode. I'm not sure if this is strictly correct
1377 * but it saves the agony when the X server dies and the screen
1378 * remains blanked due to KD_GRAPHICS! It would be nice to do
1379 * this outside of VT_PROCESS but there is no single process
1380 * to account for and tracking tty count may be undesirable.
1381 */
1382 reset_vc(vc);
1383
1384 /*
1385 * Fall through to normal (VT_AUTO) handling of the switch...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
1387 }
1388
1389 /*
1390 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1391 */
1392 if (vc->vc_mode == KD_GRAPHICS)
1393 return;
1394
1395 complete_change_console(new_vc);
1396}
Alan Cox8d233552009-09-19 13:13:25 -07001397
1398/* Perform a kernel triggered VT switch for suspend/resume */
1399
1400static int disable_vt_switch;
1401
1402int vt_move_to_console(unsigned int vt, int alloc)
1403{
1404 int prev;
1405
Torben Hohnac751ef2011-01-25 15:07:35 -08001406 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001407 /* Graphics mode - up to X */
1408 if (disable_vt_switch) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001409 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001410 return 0;
1411 }
1412 prev = fg_console;
1413
1414 if (alloc && vc_allocate(vt)) {
1415 /* we can't have a free VC for now. Too bad,
1416 * we don't want to mess the screen for now. */
Torben Hohnac751ef2011-01-25 15:07:35 -08001417 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001418 return -ENOSPC;
1419 }
1420
1421 if (set_console(vt)) {
1422 /*
1423 * We're unable to switch to the SUSPEND_CONSOLE.
1424 * Let the calling function know so it can decide
1425 * what to do.
1426 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001427 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001428 return -EIO;
1429 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001430 console_unlock();
Alan Cox4001d7b2012-03-02 14:59:20 +00001431 /* Review: I don't see why we need tty_lock here FIXME */
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001432 tty_lock();
Jiri Slaby797938b2009-08-11 23:20:41 +02001433 if (vt_waitactive(vt + 1)) {
Alan Cox8d233552009-09-19 13:13:25 -07001434 pr_debug("Suspend: Can't switch VCs.");
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001435 tty_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001436 return -EINTR;
1437 }
Arnd Bergmannbe1bc282010-06-01 22:53:05 +02001438 tty_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001439 return prev;
1440}
1441
1442/*
1443 * Normally during a suspend, we allocate a new console and switch to it.
1444 * When we resume, we switch back to the original console. This switch
1445 * can be slow, so on systems where the framebuffer can handle restoration
1446 * of video registers anyways, there's little point in doing the console
1447 * switch. This function allows you to disable it by passing it '0'.
1448 */
1449void pm_set_vt_switch(int do_switch)
1450{
Torben Hohnac751ef2011-01-25 15:07:35 -08001451 console_lock();
Alan Cox8d233552009-09-19 13:13:25 -07001452 disable_vt_switch = !do_switch;
Torben Hohnac751ef2011-01-25 15:07:35 -08001453 console_unlock();
Alan Cox8d233552009-09-19 13:13:25 -07001454}
1455EXPORT_SYMBOL(pm_set_vt_switch);