blob: 0fceb8f4d6f22622b5d8f70db707b99667eece2c [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
Alan Coxd3b5cff2009-09-19 13:13:26 -0700975 case VT_SETACTIVATE:
976 {
977 struct vt_setactivate vsa;
978
979 if (!perm)
980 goto eperm;
981
982 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
983 sizeof(struct vt_setactivate)))
984 return -EFAULT;
985 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
986 ret = -ENXIO;
987 else {
988 vsa.console--;
989 acquire_console_sem();
990 ret = vc_allocate(vsa.console);
991 if (ret == 0) {
992 struct vc_data *nvc;
993 /* This is safe providing we don't drop the
994 console sem between vc_allocate and
995 finishing referencing nvc */
996 nvc = vc_cons[vsa.console].d;
997 nvc->vt_mode = vsa.mode;
998 nvc->vt_mode.frsig = 0;
999 put_pid(nvc->vt_pid);
1000 nvc->vt_pid = get_pid(task_pid(current));
1001 }
1002 release_console_sem();
1003 if (ret)
1004 break;
1005 /* Commence switch and lock */
1006 set_console(arg);
1007 }
1008 }
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
1011 * wait until the specified VT has been activated
1012 */
1013 case VT_WAITACTIVE:
1014 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001015 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -07001017 ret = -ENXIO;
1018 else
Alan Cox8b92e872009-09-19 13:13:24 -07001019 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -07001020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /*
1023 * If a vt is under process control, the kernel will not switch to it
1024 * immediately, but postpone the operation until the process calls this
1025 * ioctl, allowing the switch to complete.
1026 *
1027 * According to the X sources this is the behavior:
1028 * 0: pending switch-from not OK
1029 * 1: pending switch-from OK
1030 * 2: completed switch-to OK
1031 */
1032 case VT_RELDISP:
1033 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001034 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Alan Cox9cc3c222008-04-30 00:53:26 -07001036 if (vc->vt_mode.mode != VT_PROCESS) {
1037 ret = -EINVAL;
1038 break;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * Switching-from response
1042 */
Samuel Ortiz8792f962007-10-01 01:20:12 -07001043 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (vc->vt_newvt >= 0) {
1045 if (arg == 0)
1046 /*
1047 * Switch disallowed, so forget we were trying
1048 * to do it.
1049 */
1050 vc->vt_newvt = -1;
1051
1052 else {
1053 /*
1054 * The current vt has been released, so
1055 * complete the switch.
1056 */
1057 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 newvt = vc->vt_newvt;
1059 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001060 ret = vc_allocate(newvt);
1061 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 /*
1066 * When we actually do the console switch,
1067 * make sure we are atomic with respect to
1068 * other console switches..
1069 */
1070 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001072 } else {
1073 /*
1074 * Switched-to response
1075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * If it's just an ACK, ignore it
1078 */
Alan Cox9cc3c222008-04-30 00:53:26 -07001079 if (arg != VT_ACKACQ)
1080 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Samuel Ortiz8792f962007-10-01 01:20:12 -07001082 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /*
1086 * Disallocate memory associated to VT (but leave VT1)
1087 */
1088 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -07001089 if (arg > MAX_NR_CONSOLES) {
1090 ret = -ENXIO;
1091 break;
1092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -07001094 /* deallocate all unused consoles, but leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 acquire_console_sem();
1096 for (i=1; i<MAX_NR_CONSOLES; i++)
1097 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -07001098 vc_deallocate(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 release_console_sem();
1100 } else {
Alan Coxca9bda02006-09-29 02:00:03 -07001101 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 arg--;
1103 if (VT_BUSY(arg))
Alan Cox9cc3c222008-04-30 00:53:26 -07001104 ret = -EBUSY;
1105 else if (arg) { /* leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 acquire_console_sem();
Alan Coxca9bda02006-09-29 02:00:03 -07001107 vc_deallocate(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 release_console_sem();
1109 }
1110 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 case VT_RESIZE:
1114 {
1115 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001116 struct vc_data *vc;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ushort ll,cc;
1119 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001120 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (get_user(ll, &vtsizes->v_rows) ||
1122 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -07001123 ret = -EFAULT;
1124 else {
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001125 acquire_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001126 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1127 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001128
Alan Cox9cc3c222008-04-30 00:53:26 -07001129 if (vc) {
1130 vc->vc_resize_user = 1;
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001131 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -07001132 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001133 }
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001134 release_console_sem();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001135 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
1139 case VT_RESIZEX:
1140 {
1141 struct vt_consize __user *vtconsize = up;
1142 ushort ll,cc,vlin,clin,vcol,ccol;
1143 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001144 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox9cc3c222008-04-30 00:53:26 -07001146 sizeof(struct vt_consize))) {
1147 ret = -EFAULT;
1148 break;
1149 }
1150 /* FIXME: Should check the copies properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 __get_user(ll, &vtconsize->v_rows);
1152 __get_user(cc, &vtconsize->v_cols);
1153 __get_user(vlin, &vtconsize->v_vlin);
1154 __get_user(clin, &vtconsize->v_clin);
1155 __get_user(vcol, &vtconsize->v_vcol);
1156 __get_user(ccol, &vtconsize->v_ccol);
1157 vlin = vlin ? vlin : vc->vc_scan_lines;
1158 if (clin) {
1159 if (ll) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001160 if (ll != vlin/clin) {
1161 /* Parameters don't add up */
1162 ret = -EINVAL;
1163 break;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else
1166 ll = vlin/clin;
1167 }
1168 if (vcol && ccol) {
1169 if (cc) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001170 if (cc != vcol/ccol) {
1171 ret = -EINVAL;
1172 break;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 } else
1175 cc = vcol/ccol;
1176 }
1177
Alan Cox9cc3c222008-04-30 00:53:26 -07001178 if (clin > 32) {
1179 ret = -EINVAL;
1180 break;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1184 if (!vc_cons[i].d)
1185 continue;
1186 acquire_console_sem();
1187 if (vlin)
1188 vc_cons[i].d->vc_scan_lines = vlin;
1189 if (clin)
1190 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001191 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 vc_resize(vc_cons[i].d, cc, ll);
1193 release_console_sem();
1194 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
1198 case PIO_FONT: {
1199 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001200 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 op.op = KD_FONT_OP_SET;
1202 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1203 op.width = 8;
1204 op.height = 0;
1205 op.charcount = 256;
1206 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001207 ret = con_font_op(vc_cons[fg_console].d, &op);
1208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
1211 case GIO_FONT: {
1212 op.op = KD_FONT_OP_GET;
1213 op.flags = KD_FONT_FLAG_OLD;
1214 op.width = 8;
1215 op.height = 32;
1216 op.charcount = 256;
1217 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001218 ret = con_font_op(vc_cons[fg_console].d, &op);
1219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
1222 case PIO_CMAP:
1223 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001224 ret = -EPERM;
1225 else
1226 ret = con_set_cmap(up);
1227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001230 ret = con_get_cmap(up);
1231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 case PIO_FONTX:
1234 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -07001235 ret = do_fontx_ioctl(cmd, up, perm, &op);
1236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 case PIO_FONTRESET:
1239 {
1240 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001241 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243#ifdef BROKEN_GRAPHICS_PROGRAMS
1244 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1245 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -07001246 ret = -ENOSYS;
1247 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248#else
1249 {
1250 op.op = KD_FONT_OP_SET_DEFAULT;
1251 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -07001252 ret = con_font_op(vc_cons[fg_console].d, &op);
1253 if (ret)
1254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox9cc3c222008-04-30 00:53:26 -07001256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258#endif
1259 }
1260
1261 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -07001262 if (copy_from_user(&op, up, sizeof(op))) {
1263 ret = -EFAULT;
1264 break;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox9cc3c222008-04-30 00:53:26 -07001267 goto eperm;
1268 ret = con_font_op(vc, &op);
1269 if (ret)
1270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -07001272 ret = -EFAULT;
1273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276 case PIO_SCRNMAP:
1277 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001278 ret = -EPERM;
1279 else
1280 ret = con_set_trans_old(up);
1281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001284 ret = con_get_trans_old(up);
1285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 case PIO_UNISCRNMAP:
1288 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001289 ret = -EPERM;
1290 else
1291 ret = con_set_trans_new(up);
1292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001295 ret = con_get_trans_new(up);
1296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 case PIO_UNIMAPCLR:
1299 { struct unimapinit ui;
1300 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001301 goto eperm;
1302 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1303 if (!ret)
1304 con_clear_unimap(vc, &ui);
1305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 case PIO_UNIMAP:
1309 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001310 ret = do_unimap_ioctl(cmd, up, perm, vc);
1311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 case VT_LOCKSWITCH:
1314 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001315 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case VT_UNLOCKSWITCH:
1319 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001320 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -07001322 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001323 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001324 ret = put_user(vc->vc_hi_font_mask,
1325 (unsigned short __user *)arg);
1326 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001327 case VT_WAITEVENT:
1328 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001331 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001333out:
1334 unlock_kernel();
1335 return ret;
1336eperm:
1337 ret = -EPERM;
1338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341void reset_vc(struct vc_data *vc)
1342{
1343 vc->vc_mode = KD_TEXT;
Bill Nottingham2e8ecb92007-10-16 23:29:38 -07001344 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 vc->vt_mode.mode = VT_AUTO;
1346 vc->vt_mode.waitv = 0;
1347 vc->vt_mode.relsig = 0;
1348 vc->vt_mode.acqsig = 0;
1349 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001350 put_pid(vc->vt_pid);
1351 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 vc->vt_newvt = -1;
1353 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1354 reset_palette(vc);
1355}
1356
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001357void vc_SAK(struct work_struct *work)
1358{
1359 struct vc *vc_con =
1360 container_of(work, struct vc, SAK_work);
1361 struct vc_data *vc;
1362 struct tty_struct *tty;
1363
1364 acquire_console_sem();
1365 vc = vc_con->d;
1366 if (vc) {
1367 tty = vc->vc_tty;
1368 /*
1369 * SAK should also work in all raw modes and reset
1370 * them properly.
1371 */
1372 if (tty)
1373 __do_SAK(tty);
1374 reset_vc(vc);
1375 }
1376 release_console_sem();
1377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/*
Alan Coxd3b5cff2009-09-19 13:13:26 -07001380 * Performs the back end of a vt switch. Called under the console
1381 * semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
1383static void complete_change_console(struct vc_data *vc)
1384{
1385 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001386 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 last_console = fg_console;
1389
1390 /*
1391 * If we're switching, we could be going from KD_GRAPHICS to
1392 * KD_TEXT mode or vice versa, which means we need to blank or
1393 * unblank the screen later.
1394 */
1395 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1396 switch_screen(vc);
1397
1398 /*
Eric W. Biederman3dfcaf12006-12-13 00:34:05 -08001399 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * then the *blank_screen operation could occur while X, having
1401 * received acqsig, is waking up on another processor. This
1402 * condition can lead to overlapping accesses to the VGA range
1403 * and the framebuffer (causing system lockups).
1404 *
1405 * To account for this we duplicate this code below only if the
1406 * controlling process is gone and we've called reset_vc.
1407 */
1408 if (old_vc_mode != vc->vc_mode) {
1409 if (vc->vc_mode == KD_TEXT)
1410 do_unblank_screen(1);
1411 else
1412 do_blank_screen(1);
1413 }
1414
1415 /*
1416 * If this new console is under process control, send it a signal
1417 * telling it that it has acquired. Also check if it has died and
1418 * clean up (similar to logic employed in change_console())
1419 */
1420 if (vc->vt_mode.mode == VT_PROCESS) {
1421 /*
Eric W. Biederman3dfcaf12006-12-13 00:34:05 -08001422 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * tell us if the process has gone or something else
1424 * is awry
1425 */
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001426 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * The controlling process has died, so we revert back to
1429 * normal operation. In this case, we'll also change back
1430 * to KD_TEXT mode. I'm not sure if this is strictly correct
1431 * but it saves the agony when the X server dies and the screen
1432 * remains blanked due to KD_GRAPHICS! It would be nice to do
1433 * this outside of VT_PROCESS but there is no single process
1434 * to account for and tracking tty count may be undesirable.
1435 */
1436 reset_vc(vc);
1437
1438 if (old_vc_mode != vc->vc_mode) {
1439 if (vc->vc_mode == KD_TEXT)
1440 do_unblank_screen(1);
1441 else
1442 do_blank_screen(1);
1443 }
1444 }
1445 }
1446
1447 /*
1448 * Wake anyone waiting for their VT to activate
1449 */
Alan Cox8b92e872009-09-19 13:13:24 -07001450 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return;
1452}
1453
1454/*
1455 * Performs the front-end of a vt switch
1456 */
1457void change_console(struct vc_data *new_vc)
1458{
1459 struct vc_data *vc;
1460
1461 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1462 return;
1463
1464 /*
1465 * If this vt is in process mode, then we need to handshake with
1466 * that process before switching. Essentially, we store where that
1467 * vt wants to switch to and wait for it to tell us when it's done
1468 * (via VT_RELDISP ioctl).
1469 *
1470 * We also check to see if the controlling process still exists.
1471 * If it doesn't, we reset this vt to auto mode and continue.
1472 * This is a cheap way to track process control. The worst thing
1473 * that can happen is: we send a signal to a process, it dies, and
1474 * the switch gets "lost" waiting for a response; hopefully, the
1475 * user will try again, we'll detect the process is gone (unless
1476 * the user waits just the right amount of time :-) and revert the
1477 * vt to auto control.
1478 */
1479 vc = vc_cons[fg_console].d;
1480 if (vc->vt_mode.mode == VT_PROCESS) {
1481 /*
Eric W. Biederman3dfcaf12006-12-13 00:34:05 -08001482 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001484 * is awry.
1485 *
1486 * We need to set vt_newvt *before* sending the signal or we
1487 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001489 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001490 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /*
1492 * It worked. Mark the vt to switch to and
1493 * return. The process needs to send us a
1494 * VT_RELDISP ioctl to complete the switch.
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return;
1497 }
1498
1499 /*
1500 * The controlling process has died, so we revert back to
1501 * normal operation. In this case, we'll also change back
1502 * to KD_TEXT mode. I'm not sure if this is strictly correct
1503 * but it saves the agony when the X server dies and the screen
1504 * remains blanked due to KD_GRAPHICS! It would be nice to do
1505 * this outside of VT_PROCESS but there is no single process
1506 * to account for and tracking tty count may be undesirable.
1507 */
1508 reset_vc(vc);
1509
1510 /*
1511 * Fall through to normal (VT_AUTO) handling of the switch...
1512 */
1513 }
1514
1515 /*
1516 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1517 */
1518 if (vc->vc_mode == KD_GRAPHICS)
1519 return;
1520
1521 complete_change_console(new_vc);
1522}
Alan Cox8d233552009-09-19 13:13:25 -07001523
1524/* Perform a kernel triggered VT switch for suspend/resume */
1525
1526static int disable_vt_switch;
1527
1528int vt_move_to_console(unsigned int vt, int alloc)
1529{
1530 int prev;
1531
1532 acquire_console_sem();
1533 /* Graphics mode - up to X */
1534 if (disable_vt_switch) {
1535 release_console_sem();
1536 return 0;
1537 }
1538 prev = fg_console;
1539
1540 if (alloc && vc_allocate(vt)) {
1541 /* we can't have a free VC for now. Too bad,
1542 * we don't want to mess the screen for now. */
1543 release_console_sem();
1544 return -ENOSPC;
1545 }
1546
1547 if (set_console(vt)) {
1548 /*
1549 * We're unable to switch to the SUSPEND_CONSOLE.
1550 * Let the calling function know so it can decide
1551 * what to do.
1552 */
1553 release_console_sem();
1554 return -EIO;
1555 }
1556 release_console_sem();
1557 if (vt_waitactive(vt)) {
1558 pr_debug("Suspend: Can't switch VCs.");
1559 return -EINTR;
1560 }
1561 return prev;
1562}
1563
1564/*
1565 * Normally during a suspend, we allocate a new console and switch to it.
1566 * When we resume, we switch back to the original console. This switch
1567 * can be slow, so on systems where the framebuffer can handle restoration
1568 * of video registers anyways, there's little point in doing the console
1569 * switch. This function allows you to disable it by passing it '0'.
1570 */
1571void pm_set_vt_switch(int do_switch)
1572{
1573 acquire_console_sem();
1574 disable_vt_switch = !do_switch;
1575 release_console_sem();
1576}
1577EXPORT_SYMBOL(pm_set_vt_switch);