blob: d29fbd44bdca0e20e94b46cf51f1aaa5cd806a59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vt_ioctl.c
3 *
4 * Copyright (C) 1992 obz under the linux copyright
5 *
6 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
7 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
8 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
9 * Some code moved for less code duplication - Andi Kleen - Mar 1997
10 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/tty.h>
17#include <linux/timer.h>
18#include <linux/kernel.h>
Alan Cox8d233552009-09-19 13:13:25 -070019#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kd.h>
21#include <linux/vt.h>
22#include <linux/string.h>
23#include <linux/slab.h>
24#include <linux/major.h>
25#include <linux/fs.h>
26#include <linux/console.h>
Samuel Thibault04c71972007-10-16 23:27:04 -070027#include <linux/consolemap.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070028#include <linux/signal.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040029#include <linux/smp_lock.h>
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -070030#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/uaccess.h>
34
35#include <linux/kbd_kern.h>
36#include <linux/vt_kern.h>
37#include <linux/kbd_diacr.h>
38#include <linux/selection.h>
39
Andrew Johnsonb257bc02007-03-16 13:38:24 -080040char vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041extern struct tty_driver *console_driver;
42
43#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
44#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
45
46/*
47 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
48 * experimentation and study of X386 SYSV handling.
49 *
50 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
51 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
52 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
53 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
54 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
55 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
56 * to the current console is done by the main ioctl code.
57 */
58
59#ifdef CONFIG_X86
60#include <linux/syscalls.h>
61#endif
62
63static void complete_change_console(struct vc_data *vc);
64
65/*
Alan Cox8b92e872009-09-19 13:13:24 -070066 * User space VT_EVENT handlers
67 */
68
69struct vt_event_wait {
70 struct list_head list;
71 struct vt_event event;
72 int done;
73};
74
75static LIST_HEAD(vt_events);
76static DEFINE_SPINLOCK(vt_event_lock);
77static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
78
79/**
80 * vt_event_post
81 * @event: the event that occurred
82 * @old: old console
83 * @new: new console
84 *
85 * Post an VT event to interested VT handlers
86 */
87
88void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
89{
90 struct list_head *pos, *head;
91 unsigned long flags;
92 int wake = 0;
93
94 spin_lock_irqsave(&vt_event_lock, flags);
95 head = &vt_events;
96
97 list_for_each(pos, head) {
98 struct vt_event_wait *ve = list_entry(pos,
99 struct vt_event_wait, list);
100 if (!(ve->event.event & event))
101 continue;
102 ve->event.event = event;
103 /* kernel view is consoles 0..n-1, user space view is
104 console 1..n with 0 meaning current, so we must bias */
105 ve->event.old = old + 1;
106 ve->event.new = new + 1;
107 wake = 1;
108 ve->done = 1;
109 }
110 spin_unlock_irqrestore(&vt_event_lock, flags);
111 if (wake)
112 wake_up_interruptible(&vt_event_waitqueue);
113}
114
115/**
116 * vt_event_wait - wait for an event
117 * @vw: our event
118 *
119 * Waits for an event to occur which completes our vt_event_wait
120 * structure. On return the structure has wv->done set to 1 for success
121 * or 0 if some event such as a signal ended the wait.
122 */
123
124static void vt_event_wait(struct vt_event_wait *vw)
125{
126 unsigned long flags;
127 /* Prepare the event */
128 INIT_LIST_HEAD(&vw->list);
129 vw->done = 0;
130 /* Queue our event */
131 spin_lock_irqsave(&vt_event_lock, flags);
132 list_add(&vw->list, &vt_events);
133 spin_unlock_irqrestore(&vt_event_lock, flags);
134 /* Wait for it to pass */
135 wait_event_interruptible(vt_event_waitqueue, vw->done);
136 /* Dequeue it */
137 spin_lock_irqsave(&vt_event_lock, flags);
138 list_del(&vw->list);
139 spin_unlock_irqrestore(&vt_event_lock, flags);
140}
141
142/**
143 * vt_event_wait_ioctl - event ioctl handler
144 * @arg: argument to ioctl
145 *
146 * Implement the VT_WAITEVENT ioctl using the VT event interface
147 */
148
149static int vt_event_wait_ioctl(struct vt_event __user *event)
150{
151 struct vt_event_wait vw;
152
153 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
154 return -EFAULT;
155 /* Highest supported event for now */
156 if (vw.event.event & ~VT_MAX_EVENT)
157 return -EINVAL;
158
159 vt_event_wait(&vw);
160 /* If it occurred report it */
161 if (vw.done) {
162 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
163 return -EFAULT;
164 return 0;
165 }
166 return -EINTR;
167}
168
169/**
170 * vt_waitactive - active console wait
171 * @event: event code
172 * @n: new console
173 *
174 * Helper for event waits. Used to implement the legacy
175 * event waiting ioctls in terms of events
176 */
177
178int vt_waitactive(int n)
179{
180 struct vt_event_wait vw;
181 do {
182 if (n == fg_console + 1)
183 break;
184 vw.event.event = VT_EVENT_SWITCH;
185 vt_event_wait(&vw);
186 if (vw.done == 0)
187 return -EINTR;
188 } while (vw.event.new != n);
189 return 0;
190}
191
192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * these are the valid i/o ports we're allowed to change. they map all the
194 * video ports
195 */
196#define GPFIRST 0x3b4
197#define GPLAST 0x3df
198#define GPNUM (GPLAST - GPFIRST + 1)
199
200#define i (tmp.kb_index)
201#define s (tmp.kb_table)
202#define v (tmp.kb_value)
203static inline int
204do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
205{
206 struct kbentry tmp;
207 ushort *key_map, val, ov;
208
209 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
210 return -EFAULT;
211
Marcelo Tosattie3f17f02005-11-07 00:59:34 -0800212 if (!capable(CAP_SYS_TTY_CONFIG))
213 perm = 0;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 switch (cmd) {
216 case KDGKBENT:
217 key_map = key_maps[s];
218 if (key_map) {
219 val = U(key_map[i]);
220 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
221 val = K_HOLE;
222 } else
223 val = (i ? K_HOLE : K_NOSUCHMAP);
224 return put_user(val, &user_kbe->kb_value);
225 case KDSKBENT:
226 if (!perm)
227 return -EPERM;
228 if (!i && v == K_NOSUCHMAP) {
Alan Coxca9bda02006-09-29 02:00:03 -0700229 /* deallocate map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 key_map = key_maps[s];
231 if (s && key_map) {
232 key_maps[s] = NULL;
233 if (key_map[0] == U(K_ALLOCATED)) {
234 kfree(key_map);
235 keymap_count--;
236 }
237 }
238 break;
239 }
240
241 if (KTYP(v) < NR_TYPES) {
242 if (KVAL(v) > max_vals[KTYP(v)])
243 return -EINVAL;
244 } else
245 if (kbd->kbdmode != VC_UNICODE)
246 return -EINVAL;
247
248 /* ++Geert: non-PC keyboards may generate keycode zero */
249#if !defined(__mc68000__) && !defined(__powerpc__)
250 /* assignment to entry 0 only tests validity of args */
251 if (!i)
252 break;
253#endif
254
255 if (!(key_map = key_maps[s])) {
256 int j;
257
258 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
259 !capable(CAP_SYS_RESOURCE))
260 return -EPERM;
261
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800262 key_map = kmalloc(sizeof(plain_map),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 GFP_KERNEL);
264 if (!key_map)
265 return -ENOMEM;
266 key_maps[s] = key_map;
267 key_map[0] = U(K_ALLOCATED);
268 for (j = 1; j < NR_KEYS; j++)
269 key_map[j] = U(K_HOLE);
270 keymap_count++;
271 }
272 ov = U(key_map[i]);
273 if (v == ov)
274 break; /* nothing to do */
275 /*
276 * Attention Key.
277 */
278 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
279 return -EPERM;
280 key_map[i] = U(v);
281 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
282 compute_shiftstate();
283 break;
284 }
285 return 0;
286}
287#undef i
288#undef s
289#undef v
290
291static inline int
292do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
293{
294 struct kbkeycode tmp;
295 int kc = 0;
296
297 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
298 return -EFAULT;
299 switch (cmd) {
300 case KDGETKEYCODE:
301 kc = getkeycode(tmp.scancode);
302 if (kc >= 0)
303 kc = put_user(kc, &user_kbkc->keycode);
304 break;
305 case KDSETKEYCODE:
306 if (!perm)
307 return -EPERM;
308 kc = setkeycode(tmp.scancode, tmp.keycode);
309 break;
310 }
311 return kc;
312}
313
314static inline int
315do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
316{
317 struct kbsentry *kbs;
318 char *p;
319 u_char *q;
320 u_char __user *up;
321 int sz;
322 int delta;
323 char *first_free, *fj, *fnw;
324 int i, j, k;
325 int ret;
326
Andrew Morton0b360ad2005-10-30 15:03:02 -0800327 if (!capable(CAP_SYS_TTY_CONFIG))
Marcelo Tosattie3f17f02005-11-07 00:59:34 -0800328 perm = 0;
Andrew Morton0b360ad2005-10-30 15:03:02 -0800329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
331 if (!kbs) {
332 ret = -ENOMEM;
333 goto reterr;
334 }
335
336 /* we mostly copy too much here (512bytes), but who cares ;) */
337 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
338 ret = -EFAULT;
339 goto reterr;
340 }
341 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
342 i = kbs->kb_func;
343
344 switch (cmd) {
345 case KDGKBSENT:
346 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
347 a struct member */
348 up = user_kdgkb->kb_string;
349 p = func_table[i];
350 if(p)
351 for ( ; *p && sz; p++, sz--)
352 if (put_user(*p, up++)) {
353 ret = -EFAULT;
354 goto reterr;
355 }
356 if (put_user('\0', up)) {
357 ret = -EFAULT;
358 goto reterr;
359 }
360 kfree(kbs);
361 return ((p && *p) ? -EOVERFLOW : 0);
362 case KDSKBSENT:
363 if (!perm) {
364 ret = -EPERM;
365 goto reterr;
366 }
367
368 q = func_table[i];
369 first_free = funcbufptr + (funcbufsize - funcbufleft);
370 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
371 ;
372 if (j < MAX_NR_FUNC)
373 fj = func_table[j];
374 else
375 fj = first_free;
376
377 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
378 if (delta <= funcbufleft) { /* it fits in current buf */
379 if (j < MAX_NR_FUNC) {
380 memmove(fj + delta, fj, first_free - fj);
381 for (k = j; k < MAX_NR_FUNC; k++)
382 if (func_table[k])
383 func_table[k] += delta;
384 }
385 if (!q)
386 func_table[i] = fj;
387 funcbufleft -= delta;
388 } else { /* allocate a larger buffer */
389 sz = 256;
390 while (sz < funcbufsize - funcbufleft + delta)
391 sz <<= 1;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800392 fnw = kmalloc(sz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if(!fnw) {
394 ret = -ENOMEM;
395 goto reterr;
396 }
397
398 if (!q)
399 func_table[i] = fj;
400 if (fj > funcbufptr)
401 memmove(fnw, funcbufptr, fj - funcbufptr);
402 for (k = 0; k < j; k++)
403 if (func_table[k])
404 func_table[k] = fnw + (func_table[k] - funcbufptr);
405
406 if (first_free > fj) {
407 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
408 for (k = j; k < MAX_NR_FUNC; k++)
409 if (func_table[k])
410 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
411 }
412 if (funcbufptr != func_buf)
413 kfree(funcbufptr);
414 funcbufptr = fnw;
415 funcbufleft = funcbufleft - delta + sz - funcbufsize;
416 funcbufsize = sz;
417 }
418 strcpy(func_table[i], kbs->kb_string);
419 break;
420 }
421 ret = 0;
422reterr:
423 kfree(kbs);
424 return ret;
425}
426
427static inline int
428do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
429{
430 struct consolefontdesc cfdarg;
431 int i;
432
433 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
434 return -EFAULT;
435
436 switch (cmd) {
437 case PIO_FONTX:
438 if (!perm)
439 return -EPERM;
440 op->op = KD_FONT_OP_SET;
441 op->flags = KD_FONT_FLAG_OLD;
442 op->width = 8;
443 op->height = cfdarg.charheight;
444 op->charcount = cfdarg.charcount;
445 op->data = cfdarg.chardata;
446 return con_font_op(vc_cons[fg_console].d, op);
447 case GIO_FONTX: {
448 op->op = KD_FONT_OP_GET;
449 op->flags = KD_FONT_FLAG_OLD;
450 op->width = 8;
451 op->height = cfdarg.charheight;
452 op->charcount = cfdarg.charcount;
453 op->data = cfdarg.chardata;
454 i = con_font_op(vc_cons[fg_console].d, op);
455 if (i)
456 return i;
457 cfdarg.charheight = op->height;
458 cfdarg.charcount = op->charcount;
459 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
460 return -EFAULT;
461 return 0;
462 }
463 }
464 return -EINVAL;
465}
466
467static inline int
468do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
469{
470 struct unimapdesc tmp;
471
472 if (copy_from_user(&tmp, user_ud, sizeof tmp))
473 return -EFAULT;
474 if (tmp.entries)
475 if (!access_ok(VERIFY_WRITE, tmp.entries,
476 tmp.entry_ct*sizeof(struct unipair)))
477 return -EFAULT;
478 switch (cmd) {
479 case PIO_UNIMAP:
480 if (!perm)
481 return -EPERM;
482 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
483 case GIO_UNIMAP:
484 if (!perm && fg_console != vc->vc_num)
485 return -EPERM;
486 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
487 }
488 return 0;
489}
490
Alan Cox8b92e872009-09-19 13:13:24 -0700491
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * We handle the console-specific ioctl's here. We allow the
495 * capability to modify any console, not just the fg_console.
496 */
497int vt_ioctl(struct tty_struct *tty, struct file * file,
498 unsigned int cmd, unsigned long arg)
499{
Alan Coxc9f19e92009-01-02 13:47:26 +0000500 struct vc_data *vc = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct console_font_op op; /* used in multiple places here */
502 struct kbd_struct * kbd;
503 unsigned int console;
504 unsigned char ucval;
505 void __user *up = (void __user *)arg;
506 int i, perm;
Alan Cox9cc3c222008-04-30 00:53:26 -0700507 int ret = 0;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 console = vc->vc_num;
510
Alan Cox9cc3c222008-04-30 00:53:26 -0700511 lock_kernel();
512
513 if (!vc_cons_allocated(console)) { /* impossible? */
514 ret = -ENOIOCTLCMD;
515 goto out;
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /*
520 * To have permissions to do most of the vt ioctls, we either have
521 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
522 */
523 perm = 0;
524 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
525 perm = 1;
526
527 kbd = kbd_table + console;
528 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100529 case TIOCLINUX:
Jiri Slabya1159022009-06-22 18:42:18 +0100530 ret = tioclinux(tty, arg);
531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 case KIOCSOUND:
533 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700534 goto eperm;
Alan Coxfab89222009-05-06 17:17:26 +0100535 /* FIXME: This is an old broken API but we need to keep it
536 supported and somehow separate the historic advertised
537 tick rate from any real one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (arg)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700539 arg = CLOCK_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 case KDMKTONE:
544 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700545 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 {
547 unsigned int ticks, count;
548
549 /*
550 * Generate the tone for the appropriate number of ticks.
551 * If the time is zero, turn off sound ourselves.
552 */
553 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
554 count = ticks ? (arg & 0xffff) : 0;
Alan Coxfab89222009-05-06 17:17:26 +0100555 /* FIXME: This is an old broken API but we need to keep it
556 supported and somehow separate the historic advertised
557 tick rate from any real one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (count)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700559 count = CLOCK_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 case KDGKBTYPE:
565 /*
566 * this is naive.
567 */
568 ucval = KB_101;
569 goto setchar;
570
571 /*
572 * These cannot be implemented on any machine that implements
573 * ioperm() in user level (such as Alpha PCs) or not at all.
574 *
575 * XXX: you should never use these, just call ioperm directly..
576 */
577#ifdef CONFIG_X86
578 case KDADDIO:
579 case KDDELIO:
580 /*
581 * KDADDIO and KDDELIO may be able to add ports beyond what
582 * we reject here, but to be safe...
583 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700584 if (arg < GPFIRST || arg > GPLAST) {
585 ret = -EINVAL;
586 break;
587 }
588 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 case KDENABIO:
592 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700593 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
597
598 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
599
600 case KDKBDREP:
601 {
602 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700605 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Alan Cox9cc3c222008-04-30 00:53:26 -0700607 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
608 ret = -EFAULT;
609 break;
610 }
611 ret = kbd_rate(&kbrep);
612 if (ret)
613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700615 ret = -EFAULT;
616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 case KDSETMODE:
620 /*
621 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
622 * doesn't do a whole lot. i'm not sure if it should do any
623 * restoration of modes or what...
624 *
625 * XXX It should at least call into the driver, fbdev's definitely
626 * need to restore their engine state. --BenH
627 */
628 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700629 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 switch (arg) {
631 case KD_GRAPHICS:
632 break;
633 case KD_TEXT0:
634 case KD_TEXT1:
635 arg = KD_TEXT;
636 case KD_TEXT:
637 break;
638 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700639 ret = -EINVAL;
640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700643 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 vc->vc_mode = (unsigned char) arg;
645 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * explicitly blank/unblank the screen if switching modes
649 */
650 acquire_console_sem();
651 if (arg == KD_TEXT)
652 do_unblank_screen(1);
653 else
654 do_blank_screen(1);
655 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -0700656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 case KDGETMODE:
659 ucval = vc->vc_mode;
660 goto setint;
661
662 case KDMAPDISP:
663 case KDUNMAPDISP:
664 /*
665 * these work like a combination of mmap and KDENABIO.
666 * this could be easily finished.
667 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700668 ret = -EINVAL;
669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 case KDSKBMODE:
672 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700673 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 switch(arg) {
675 case K_RAW:
676 kbd->kbdmode = VC_RAW;
677 break;
678 case K_MEDIUMRAW:
679 kbd->kbdmode = VC_MEDIUMRAW;
680 break;
681 case K_XLATE:
682 kbd->kbdmode = VC_XLATE;
683 compute_shiftstate();
684 break;
685 case K_UNICODE:
686 kbd->kbdmode = VC_UNICODE;
687 compute_shiftstate();
688 break;
689 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700690 ret = -EINVAL;
691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 case KDGKBMODE:
697 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
698 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
699 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
700 K_XLATE);
701 goto setint;
702
703 /* this could be folded into KDSKBMODE, but for compatibility
704 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
705 case KDSKBMETA:
706 switch(arg) {
707 case K_METABIT:
708 clr_vc_kbd_mode(kbd, VC_META);
709 break;
710 case K_ESCPREFIX:
711 set_vc_kbd_mode(kbd, VC_META);
712 break;
713 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700714 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 case KDGKBMETA:
719 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
720 setint:
Alan Cox9cc3c222008-04-30 00:53:26 -0700721 ret = put_user(ucval, (int __user *)arg);
722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 case KDGETKEYCODE:
725 case KDSETKEYCODE:
726 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700727 perm = 0;
728 ret = do_kbkeycode_ioctl(cmd, up, perm);
729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 case KDGKBENT:
732 case KDSKBENT:
Alan Cox9cc3c222008-04-30 00:53:26 -0700733 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 case KDGKBSENT:
737 case KDSKBSENT:
Alan Cox9cc3c222008-04-30 00:53:26 -0700738 ret = do_kdgkb_ioctl(cmd, up, perm);
739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 case KDGKBDIACR:
742 {
743 struct kbdiacrs __user *a = up;
Samuel Thibault04c71972007-10-16 23:27:04 -0700744 struct kbdiacr diacr;
745 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Alan Cox9cc3c222008-04-30 00:53:26 -0700747 if (put_user(accent_table_size, &a->kb_cnt)) {
748 ret = -EFAULT;
749 break;
750 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700751 for (i = 0; i < accent_table_size; i++) {
752 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
753 diacr.base = conv_uni_to_8bit(accent_table[i].base);
754 diacr.result = conv_uni_to_8bit(accent_table[i].result);
Alan Cox9cc3c222008-04-30 00:53:26 -0700755 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
756 ret = -EFAULT;
757 break;
758 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700759 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700760 break;
Samuel Thibault04c71972007-10-16 23:27:04 -0700761 }
762 case KDGKBDIACRUC:
763 {
764 struct kbdiacrsuc __user *a = up;
765
766 if (put_user(accent_table_size, &a->kb_cnt))
Alan Cox9cc3c222008-04-30 00:53:26 -0700767 ret = -EFAULT;
768 else if (copy_to_user(a->kbdiacruc, accent_table,
769 accent_table_size*sizeof(struct kbdiacruc)))
770 ret = -EFAULT;
771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 case KDSKBDIACR:
775 {
776 struct kbdiacrs __user *a = up;
Samuel Thibault04c71972007-10-16 23:27:04 -0700777 struct kbdiacr diacr;
778 unsigned int ct;
779 int i;
780
781 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700782 goto eperm;
783 if (get_user(ct,&a->kb_cnt)) {
784 ret = -EFAULT;
785 break;
786 }
787 if (ct >= MAX_DIACR) {
788 ret = -EINVAL;
789 break;
790 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700791 accent_table_size = ct;
792 for (i = 0; i < ct; i++) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700793 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
794 ret = -EFAULT;
795 break;
796 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700797 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
798 accent_table[i].base = conv_8bit_to_uni(diacr.base);
799 accent_table[i].result = conv_8bit_to_uni(diacr.result);
800 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700801 break;
Samuel Thibault04c71972007-10-16 23:27:04 -0700802 }
803
804 case KDSKBDIACRUC:
805 {
806 struct kbdiacrsuc __user *a = up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned int ct;
808
809 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700810 goto eperm;
811 if (get_user(ct,&a->kb_cnt)) {
812 ret = -EFAULT;
813 break;
814 }
815 if (ct >= MAX_DIACR) {
816 ret = -EINVAL;
817 break;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 accent_table_size = ct;
Samuel Thibault04c71972007-10-16 23:27:04 -0700820 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700821 ret = -EFAULT;
822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 /* the ioctls below read/set the flags usually shown in the leds */
826 /* don't use them - they will go away without warning */
827 case KDGKBLED:
828 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
829 goto setchar;
830
831 case KDSKBLED:
832 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700833 goto eperm;
834 if (arg & ~0x77) {
835 ret = -EINVAL;
836 break;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kbd->ledflagstate = (arg & 7);
839 kbd->default_ledflagstate = ((arg >> 4) & 7);
840 set_leds();
Alan Cox9cc3c222008-04-30 00:53:26 -0700841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* the ioctls below only set the lights, not the functions */
844 /* for those, see KDGKBLED and KDSKBLED above */
845 case KDGETLED:
846 ucval = getledstate();
847 setchar:
Alan Cox9cc3c222008-04-30 00:53:26 -0700848 ret = put_user(ucval, (char __user *)arg);
849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 case KDSETLED:
852 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700853 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 setledstate(kbd, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /*
858 * A process can indicate its willingness to accept signals
859 * generated by pressing an appropriate key combination.
860 * Thus, one can have a daemon that e.g. spawns a new console
861 * upon a keypress and then changes to it.
862 * See also the kbrequest field of inittab(5).
863 */
864 case KDSIGACCEPT:
865 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!perm || !capable(CAP_KILL))
Alan Cox9cc3c222008-04-30 00:53:26 -0700867 goto eperm;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700868 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700869 ret = -EINVAL;
870 else {
871 spin_lock_irq(&vt_spawn_con.lock);
872 put_pid(vt_spawn_con.pid);
873 vt_spawn_con.pid = get_pid(task_pid(current));
874 vt_spawn_con.sig = arg;
875 spin_unlock_irq(&vt_spawn_con.lock);
876 }
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 case VT_SETMODE:
881 {
882 struct vt_mode tmp;
883
884 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700885 goto eperm;
886 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
887 ret = -EFAULT;
888 goto out;
889 }
890 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
891 ret = -EINVAL;
892 goto out;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 acquire_console_sem();
895 vc->vt_mode = tmp;
896 /* the frsig is ignored, so we set it to 0 */
897 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800898 put_pid(vc->vt_pid);
899 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* no switch is required -- saw@shade.msu.ru */
901 vc->vt_newvt = -1;
902 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -0700903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 case VT_GETMODE:
907 {
908 struct vt_mode tmp;
909 int rc;
910
911 acquire_console_sem();
912 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
913 release_console_sem();
914
915 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700916 if (rc)
917 ret = -EFAULT;
918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 /*
922 * Returns global vt state. Note that VT 0 is always open, since
923 * it's an alias for the current VT, and people can't use it here.
924 * We cannot return state for more than 16 VTs, since v_state is short.
925 */
926 case VT_GETSTATE:
927 {
928 struct vt_stat __user *vtstat = up;
929 unsigned short state, mask;
930
931 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700932 ret = -EFAULT;
933 else {
934 state = 1; /* /dev/tty0 is always open */
935 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
936 ++i, mask <<= 1)
937 if (VT_IS_IN_USE(i))
938 state |= mask;
939 ret = put_user(state, &vtstat->v_state);
940 }
941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944 /*
945 * Returns the first available (non-opened) console.
946 */
947 case VT_OPENQRY:
948 for (i = 0; i < MAX_NR_CONSOLES; ++i)
949 if (! VT_IS_IN_USE(i))
950 break;
951 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
952 goto setint;
953
954 /*
955 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
956 * with num >= 1 (switches to vt 0, our console, are not allowed, just
957 * to preserve sanity).
958 */
959 case VT_ACTIVATE:
960 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700961 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700963 ret = -ENXIO;
964 else {
965 arg--;
966 acquire_console_sem();
967 ret = vc_allocate(arg);
968 release_console_sem();
969 if (ret)
970 break;
971 set_console(arg);
972 }
973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /*
976 * wait until the specified VT has been activated
977 */
978 case VT_WAITACTIVE:
979 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700980 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700982 ret = -ENXIO;
983 else
Alan Cox8b92e872009-09-19 13:13:24 -0700984 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /*
988 * If a vt is under process control, the kernel will not switch to it
989 * immediately, but postpone the operation until the process calls this
990 * ioctl, allowing the switch to complete.
991 *
992 * According to the X sources this is the behavior:
993 * 0: pending switch-from not OK
994 * 1: pending switch-from OK
995 * 2: completed switch-to OK
996 */
997 case VT_RELDISP:
998 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700999 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Alan Cox9cc3c222008-04-30 00:53:26 -07001001 if (vc->vt_mode.mode != VT_PROCESS) {
1002 ret = -EINVAL;
1003 break;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * Switching-from response
1007 */
Samuel Ortiz8792f962007-10-01 01:20:12 -07001008 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (vc->vt_newvt >= 0) {
1010 if (arg == 0)
1011 /*
1012 * Switch disallowed, so forget we were trying
1013 * to do it.
1014 */
1015 vc->vt_newvt = -1;
1016
1017 else {
1018 /*
1019 * The current vt has been released, so
1020 * complete the switch.
1021 */
1022 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 newvt = vc->vt_newvt;
1024 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001025 ret = vc_allocate(newvt);
1026 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 /*
1031 * When we actually do the console switch,
1032 * make sure we are atomic with respect to
1033 * other console switches..
1034 */
1035 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001037 } else {
1038 /*
1039 * Switched-to response
1040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * If it's just an ACK, ignore it
1043 */
Alan Cox9cc3c222008-04-30 00:53:26 -07001044 if (arg != VT_ACKACQ)
1045 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Samuel Ortiz8792f962007-10-01 01:20:12 -07001047 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * Disallocate memory associated to VT (but leave VT1)
1052 */
1053 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -07001054 if (arg > MAX_NR_CONSOLES) {
1055 ret = -ENXIO;
1056 break;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -07001059 /* deallocate all unused consoles, but leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 acquire_console_sem();
1061 for (i=1; i<MAX_NR_CONSOLES; i++)
1062 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -07001063 vc_deallocate(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 release_console_sem();
1065 } else {
Alan Coxca9bda02006-09-29 02:00:03 -07001066 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 arg--;
1068 if (VT_BUSY(arg))
Alan Cox9cc3c222008-04-30 00:53:26 -07001069 ret = -EBUSY;
1070 else if (arg) { /* leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 acquire_console_sem();
Alan Coxca9bda02006-09-29 02:00:03 -07001072 vc_deallocate(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 release_console_sem();
1074 }
1075 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 case VT_RESIZE:
1079 {
1080 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001081 struct vc_data *vc;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ushort ll,cc;
1084 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001085 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (get_user(ll, &vtsizes->v_rows) ||
1087 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -07001088 ret = -EFAULT;
1089 else {
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001090 acquire_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001091 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1092 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001093
Alan Cox9cc3c222008-04-30 00:53:26 -07001094 if (vc) {
1095 vc->vc_resize_user = 1;
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001096 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -07001097 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001098 }
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001099 release_console_sem();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001100 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
1104 case VT_RESIZEX:
1105 {
1106 struct vt_consize __user *vtconsize = up;
1107 ushort ll,cc,vlin,clin,vcol,ccol;
1108 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001109 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox9cc3c222008-04-30 00:53:26 -07001111 sizeof(struct vt_consize))) {
1112 ret = -EFAULT;
1113 break;
1114 }
1115 /* FIXME: Should check the copies properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 __get_user(ll, &vtconsize->v_rows);
1117 __get_user(cc, &vtconsize->v_cols);
1118 __get_user(vlin, &vtconsize->v_vlin);
1119 __get_user(clin, &vtconsize->v_clin);
1120 __get_user(vcol, &vtconsize->v_vcol);
1121 __get_user(ccol, &vtconsize->v_ccol);
1122 vlin = vlin ? vlin : vc->vc_scan_lines;
1123 if (clin) {
1124 if (ll) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001125 if (ll != vlin/clin) {
1126 /* Parameters don't add up */
1127 ret = -EINVAL;
1128 break;
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else
1131 ll = vlin/clin;
1132 }
1133 if (vcol && ccol) {
1134 if (cc) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001135 if (cc != vcol/ccol) {
1136 ret = -EINVAL;
1137 break;
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 } else
1140 cc = vcol/ccol;
1141 }
1142
Alan Cox9cc3c222008-04-30 00:53:26 -07001143 if (clin > 32) {
1144 ret = -EINVAL;
1145 break;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1149 if (!vc_cons[i].d)
1150 continue;
1151 acquire_console_sem();
1152 if (vlin)
1153 vc_cons[i].d->vc_scan_lines = vlin;
1154 if (clin)
1155 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001156 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 vc_resize(vc_cons[i].d, cc, ll);
1158 release_console_sem();
1159 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 case PIO_FONT: {
1164 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001165 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 op.op = KD_FONT_OP_SET;
1167 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1168 op.width = 8;
1169 op.height = 0;
1170 op.charcount = 256;
1171 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001172 ret = con_font_op(vc_cons[fg_console].d, &op);
1173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175
1176 case GIO_FONT: {
1177 op.op = KD_FONT_OP_GET;
1178 op.flags = KD_FONT_FLAG_OLD;
1179 op.width = 8;
1180 op.height = 32;
1181 op.charcount = 256;
1182 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001183 ret = con_font_op(vc_cons[fg_console].d, &op);
1184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 case PIO_CMAP:
1188 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001189 ret = -EPERM;
1190 else
1191 ret = con_set_cmap(up);
1192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001195 ret = con_get_cmap(up);
1196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 case PIO_FONTX:
1199 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -07001200 ret = do_fontx_ioctl(cmd, up, perm, &op);
1201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 case PIO_FONTRESET:
1204 {
1205 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001206 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208#ifdef BROKEN_GRAPHICS_PROGRAMS
1209 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1210 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -07001211 ret = -ENOSYS;
1212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#else
1214 {
1215 op.op = KD_FONT_OP_SET_DEFAULT;
1216 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -07001217 ret = con_font_op(vc_cons[fg_console].d, &op);
1218 if (ret)
1219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox9cc3c222008-04-30 00:53:26 -07001221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223#endif
1224 }
1225
1226 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -07001227 if (copy_from_user(&op, up, sizeof(op))) {
1228 ret = -EFAULT;
1229 break;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox9cc3c222008-04-30 00:53:26 -07001232 goto eperm;
1233 ret = con_font_op(vc, &op);
1234 if (ret)
1235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -07001237 ret = -EFAULT;
1238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 case PIO_SCRNMAP:
1242 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001243 ret = -EPERM;
1244 else
1245 ret = con_set_trans_old(up);
1246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001249 ret = con_get_trans_old(up);
1250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 case PIO_UNISCRNMAP:
1253 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001254 ret = -EPERM;
1255 else
1256 ret = con_set_trans_new(up);
1257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001260 ret = con_get_trans_new(up);
1261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 case PIO_UNIMAPCLR:
1264 { struct unimapinit ui;
1265 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001266 goto eperm;
1267 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1268 if (!ret)
1269 con_clear_unimap(vc, &ui);
1270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 case PIO_UNIMAP:
1274 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001275 ret = do_unimap_ioctl(cmd, up, perm, vc);
1276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 case VT_LOCKSWITCH:
1279 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001280 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case VT_UNLOCKSWITCH:
1284 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001285 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -07001287 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001288 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001289 ret = put_user(vc->vc_hi_font_mask,
1290 (unsigned short __user *)arg);
1291 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001292 case VT_WAITEVENT:
1293 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001296 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001298out:
1299 unlock_kernel();
1300 return ret;
1301eperm:
1302 ret = -EPERM;
1303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306void reset_vc(struct vc_data *vc)
1307{
1308 vc->vc_mode = KD_TEXT;
Bill Nottingham2e8ecb92007-10-16 23:29:38 -07001309 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 vc->vt_mode.mode = VT_AUTO;
1311 vc->vt_mode.waitv = 0;
1312 vc->vt_mode.relsig = 0;
1313 vc->vt_mode.acqsig = 0;
1314 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001315 put_pid(vc->vt_pid);
1316 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 vc->vt_newvt = -1;
1318 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1319 reset_palette(vc);
1320}
1321
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001322void vc_SAK(struct work_struct *work)
1323{
1324 struct vc *vc_con =
1325 container_of(work, struct vc, SAK_work);
1326 struct vc_data *vc;
1327 struct tty_struct *tty;
1328
1329 acquire_console_sem();
1330 vc = vc_con->d;
1331 if (vc) {
1332 tty = vc->vc_tty;
1333 /*
1334 * SAK should also work in all raw modes and reset
1335 * them properly.
1336 */
1337 if (tty)
1338 __do_SAK(tty);
1339 reset_vc(vc);
1340 }
1341 release_console_sem();
1342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/*
1345 * Performs the back end of a vt switch
1346 */
1347static void complete_change_console(struct vc_data *vc)
1348{
1349 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001350 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 last_console = fg_console;
1353
1354 /*
1355 * If we're switching, we could be going from KD_GRAPHICS to
1356 * KD_TEXT mode or vice versa, which means we need to blank or
1357 * unblank the screen later.
1358 */
1359 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1360 switch_screen(vc);
1361
1362 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001363 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * then the *blank_screen operation could occur while X, having
1365 * received acqsig, is waking up on another processor. This
1366 * condition can lead to overlapping accesses to the VGA range
1367 * and the framebuffer (causing system lockups).
1368 *
1369 * To account for this we duplicate this code below only if the
1370 * controlling process is gone and we've called reset_vc.
1371 */
1372 if (old_vc_mode != vc->vc_mode) {
1373 if (vc->vc_mode == KD_TEXT)
1374 do_unblank_screen(1);
1375 else
1376 do_blank_screen(1);
1377 }
1378
1379 /*
1380 * If this new console is under process control, send it a signal
1381 * telling it that it has acquired. Also check if it has died and
1382 * clean up (similar to logic employed in change_console())
1383 */
1384 if (vc->vt_mode.mode == VT_PROCESS) {
1385 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001386 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * tell us if the process has gone or something else
1388 * is awry
1389 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001390 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * The controlling process has died, so we revert back to
1393 * normal operation. In this case, we'll also change back
1394 * to KD_TEXT mode. I'm not sure if this is strictly correct
1395 * but it saves the agony when the X server dies and the screen
1396 * remains blanked due to KD_GRAPHICS! It would be nice to do
1397 * this outside of VT_PROCESS but there is no single process
1398 * to account for and tracking tty count may be undesirable.
1399 */
1400 reset_vc(vc);
1401
1402 if (old_vc_mode != vc->vc_mode) {
1403 if (vc->vc_mode == KD_TEXT)
1404 do_unblank_screen(1);
1405 else
1406 do_blank_screen(1);
1407 }
1408 }
1409 }
1410
1411 /*
1412 * Wake anyone waiting for their VT to activate
1413 */
Alan Cox8b92e872009-09-19 13:13:24 -07001414 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return;
1416}
1417
1418/*
1419 * Performs the front-end of a vt switch
1420 */
1421void change_console(struct vc_data *new_vc)
1422{
1423 struct vc_data *vc;
1424
1425 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1426 return;
1427
1428 /*
1429 * If this vt is in process mode, then we need to handshake with
1430 * that process before switching. Essentially, we store where that
1431 * vt wants to switch to and wait for it to tell us when it's done
1432 * (via VT_RELDISP ioctl).
1433 *
1434 * We also check to see if the controlling process still exists.
1435 * If it doesn't, we reset this vt to auto mode and continue.
1436 * This is a cheap way to track process control. The worst thing
1437 * that can happen is: we send a signal to a process, it dies, and
1438 * the switch gets "lost" waiting for a response; hopefully, the
1439 * user will try again, we'll detect the process is gone (unless
1440 * the user waits just the right amount of time :-) and revert the
1441 * vt to auto control.
1442 */
1443 vc = vc_cons[fg_console].d;
1444 if (vc->vt_mode.mode == VT_PROCESS) {
1445 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001446 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001448 * is awry.
1449 *
1450 * We need to set vt_newvt *before* sending the signal or we
1451 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001453 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001454 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * It worked. Mark the vt to switch to and
1457 * return. The process needs to send us a
1458 * VT_RELDISP ioctl to complete the switch.
1459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return;
1461 }
1462
1463 /*
1464 * The controlling process has died, so we revert back to
1465 * normal operation. In this case, we'll also change back
1466 * to KD_TEXT mode. I'm not sure if this is strictly correct
1467 * but it saves the agony when the X server dies and the screen
1468 * remains blanked due to KD_GRAPHICS! It would be nice to do
1469 * this outside of VT_PROCESS but there is no single process
1470 * to account for and tracking tty count may be undesirable.
1471 */
1472 reset_vc(vc);
1473
1474 /*
1475 * Fall through to normal (VT_AUTO) handling of the switch...
1476 */
1477 }
1478
1479 /*
1480 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1481 */
1482 if (vc->vc_mode == KD_GRAPHICS)
1483 return;
1484
1485 complete_change_console(new_vc);
1486}
Alan Cox8d233552009-09-19 13:13:25 -07001487
1488/* Perform a kernel triggered VT switch for suspend/resume */
1489
1490static int disable_vt_switch;
1491
1492int vt_move_to_console(unsigned int vt, int alloc)
1493{
1494 int prev;
1495
1496 acquire_console_sem();
1497 /* Graphics mode - up to X */
1498 if (disable_vt_switch) {
1499 release_console_sem();
1500 return 0;
1501 }
1502 prev = fg_console;
1503
1504 if (alloc && vc_allocate(vt)) {
1505 /* we can't have a free VC for now. Too bad,
1506 * we don't want to mess the screen for now. */
1507 release_console_sem();
1508 return -ENOSPC;
1509 }
1510
1511 if (set_console(vt)) {
1512 /*
1513 * We're unable to switch to the SUSPEND_CONSOLE.
1514 * Let the calling function know so it can decide
1515 * what to do.
1516 */
1517 release_console_sem();
1518 return -EIO;
1519 }
1520 release_console_sem();
1521 if (vt_waitactive(vt)) {
1522 pr_debug("Suspend: Can't switch VCs.");
1523 return -EINTR;
1524 }
1525 return prev;
1526}
1527
1528/*
1529 * Normally during a suspend, we allocate a new console and switch to it.
1530 * When we resume, we switch back to the original console. This switch
1531 * can be slow, so on systems where the framebuffer can handle restoration
1532 * of video registers anyways, there's little point in doing the console
1533 * switch. This function allows you to disable it by passing it '0'.
1534 */
1535void pm_set_vt_switch(int do_switch)
1536{
1537 acquire_console_sem();
1538 disable_vt_switch = !do_switch;
1539 release_console_sem();
1540}
1541EXPORT_SYMBOL(pm_set_vt_switch);