blob: 35ad94e65d0dde5362f9680207ea8e7492eb53a9 [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>
19#include <linux/kd.h>
20#include <linux/vt.h>
21#include <linux/string.h>
22#include <linux/slab.h>
23#include <linux/major.h>
24#include <linux/fs.h>
25#include <linux/console.h>
Samuel Thibault04c71972007-10-16 23:27:04 -070026#include <linux/consolemap.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070027#include <linux/signal.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040028#include <linux/smp_lock.h>
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -070029#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/io.h>
32#include <asm/uaccess.h>
33
34#include <linux/kbd_kern.h>
35#include <linux/vt_kern.h>
36#include <linux/kbd_diacr.h>
37#include <linux/selection.h>
38
Andrew Johnsonb257bc02007-03-16 13:38:24 -080039char vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040extern struct tty_driver *console_driver;
41
42#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
43#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
44
45/*
46 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
47 * experimentation and study of X386 SYSV handling.
48 *
49 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
50 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
51 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
52 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
53 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
54 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
55 * to the current console is done by the main ioctl code.
56 */
57
58#ifdef CONFIG_X86
59#include <linux/syscalls.h>
60#endif
61
62static void complete_change_console(struct vc_data *vc);
63
64/*
Alan Cox8b92e872009-09-19 13:13:24 -070065 * User space VT_EVENT handlers
66 */
67
68struct vt_event_wait {
69 struct list_head list;
70 struct vt_event event;
71 int done;
72};
73
74static LIST_HEAD(vt_events);
75static DEFINE_SPINLOCK(vt_event_lock);
76static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
77
78/**
79 * vt_event_post
80 * @event: the event that occurred
81 * @old: old console
82 * @new: new console
83 *
84 * Post an VT event to interested VT handlers
85 */
86
87void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
88{
89 struct list_head *pos, *head;
90 unsigned long flags;
91 int wake = 0;
92
93 spin_lock_irqsave(&vt_event_lock, flags);
94 head = &vt_events;
95
96 list_for_each(pos, head) {
97 struct vt_event_wait *ve = list_entry(pos,
98 struct vt_event_wait, list);
99 if (!(ve->event.event & event))
100 continue;
101 ve->event.event = event;
102 /* kernel view is consoles 0..n-1, user space view is
103 console 1..n with 0 meaning current, so we must bias */
104 ve->event.old = old + 1;
105 ve->event.new = new + 1;
106 wake = 1;
107 ve->done = 1;
108 }
109 spin_unlock_irqrestore(&vt_event_lock, flags);
110 if (wake)
111 wake_up_interruptible(&vt_event_waitqueue);
112}
113
114/**
115 * vt_event_wait - wait for an event
116 * @vw: our event
117 *
118 * Waits for an event to occur which completes our vt_event_wait
119 * structure. On return the structure has wv->done set to 1 for success
120 * or 0 if some event such as a signal ended the wait.
121 */
122
123static void vt_event_wait(struct vt_event_wait *vw)
124{
125 unsigned long flags;
126 /* Prepare the event */
127 INIT_LIST_HEAD(&vw->list);
128 vw->done = 0;
129 /* Queue our event */
130 spin_lock_irqsave(&vt_event_lock, flags);
131 list_add(&vw->list, &vt_events);
132 spin_unlock_irqrestore(&vt_event_lock, flags);
133 /* Wait for it to pass */
134 wait_event_interruptible(vt_event_waitqueue, vw->done);
135 /* Dequeue it */
136 spin_lock_irqsave(&vt_event_lock, flags);
137 list_del(&vw->list);
138 spin_unlock_irqrestore(&vt_event_lock, flags);
139}
140
141/**
142 * vt_event_wait_ioctl - event ioctl handler
143 * @arg: argument to ioctl
144 *
145 * Implement the VT_WAITEVENT ioctl using the VT event interface
146 */
147
148static int vt_event_wait_ioctl(struct vt_event __user *event)
149{
150 struct vt_event_wait vw;
151
152 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
153 return -EFAULT;
154 /* Highest supported event for now */
155 if (vw.event.event & ~VT_MAX_EVENT)
156 return -EINVAL;
157
158 vt_event_wait(&vw);
159 /* If it occurred report it */
160 if (vw.done) {
161 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
162 return -EFAULT;
163 return 0;
164 }
165 return -EINTR;
166}
167
168/**
169 * vt_waitactive - active console wait
170 * @event: event code
171 * @n: new console
172 *
173 * Helper for event waits. Used to implement the legacy
174 * event waiting ioctls in terms of events
175 */
176
177int vt_waitactive(int n)
178{
179 struct vt_event_wait vw;
180 do {
181 if (n == fg_console + 1)
182 break;
183 vw.event.event = VT_EVENT_SWITCH;
184 vt_event_wait(&vw);
185 if (vw.done == 0)
186 return -EINTR;
187 } while (vw.event.new != n);
188 return 0;
189}
190
191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * these are the valid i/o ports we're allowed to change. they map all the
193 * video ports
194 */
195#define GPFIRST 0x3b4
196#define GPLAST 0x3df
197#define GPNUM (GPLAST - GPFIRST + 1)
198
199#define i (tmp.kb_index)
200#define s (tmp.kb_table)
201#define v (tmp.kb_value)
202static inline int
203do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
204{
205 struct kbentry tmp;
206 ushort *key_map, val, ov;
207
208 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
209 return -EFAULT;
210
Marcelo Tosattie3f17f02005-11-07 00:59:34 -0800211 if (!capable(CAP_SYS_TTY_CONFIG))
212 perm = 0;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 switch (cmd) {
215 case KDGKBENT:
216 key_map = key_maps[s];
217 if (key_map) {
218 val = U(key_map[i]);
219 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
220 val = K_HOLE;
221 } else
222 val = (i ? K_HOLE : K_NOSUCHMAP);
223 return put_user(val, &user_kbe->kb_value);
224 case KDSKBENT:
225 if (!perm)
226 return -EPERM;
227 if (!i && v == K_NOSUCHMAP) {
Alan Coxca9bda02006-09-29 02:00:03 -0700228 /* deallocate map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 key_map = key_maps[s];
230 if (s && key_map) {
231 key_maps[s] = NULL;
232 if (key_map[0] == U(K_ALLOCATED)) {
233 kfree(key_map);
234 keymap_count--;
235 }
236 }
237 break;
238 }
239
240 if (KTYP(v) < NR_TYPES) {
241 if (KVAL(v) > max_vals[KTYP(v)])
242 return -EINVAL;
243 } else
244 if (kbd->kbdmode != VC_UNICODE)
245 return -EINVAL;
246
247 /* ++Geert: non-PC keyboards may generate keycode zero */
248#if !defined(__mc68000__) && !defined(__powerpc__)
249 /* assignment to entry 0 only tests validity of args */
250 if (!i)
251 break;
252#endif
253
254 if (!(key_map = key_maps[s])) {
255 int j;
256
257 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
258 !capable(CAP_SYS_RESOURCE))
259 return -EPERM;
260
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800261 key_map = kmalloc(sizeof(plain_map),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 GFP_KERNEL);
263 if (!key_map)
264 return -ENOMEM;
265 key_maps[s] = key_map;
266 key_map[0] = U(K_ALLOCATED);
267 for (j = 1; j < NR_KEYS; j++)
268 key_map[j] = U(K_HOLE);
269 keymap_count++;
270 }
271 ov = U(key_map[i]);
272 if (v == ov)
273 break; /* nothing to do */
274 /*
275 * Attention Key.
276 */
277 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
278 return -EPERM;
279 key_map[i] = U(v);
280 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
281 compute_shiftstate();
282 break;
283 }
284 return 0;
285}
286#undef i
287#undef s
288#undef v
289
290static inline int
291do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
292{
293 struct kbkeycode tmp;
294 int kc = 0;
295
296 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
297 return -EFAULT;
298 switch (cmd) {
299 case KDGETKEYCODE:
300 kc = getkeycode(tmp.scancode);
301 if (kc >= 0)
302 kc = put_user(kc, &user_kbkc->keycode);
303 break;
304 case KDSETKEYCODE:
305 if (!perm)
306 return -EPERM;
307 kc = setkeycode(tmp.scancode, tmp.keycode);
308 break;
309 }
310 return kc;
311}
312
313static inline int
314do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
315{
316 struct kbsentry *kbs;
317 char *p;
318 u_char *q;
319 u_char __user *up;
320 int sz;
321 int delta;
322 char *first_free, *fj, *fnw;
323 int i, j, k;
324 int ret;
325
Andrew Morton0b360ad2005-10-30 15:03:02 -0800326 if (!capable(CAP_SYS_TTY_CONFIG))
Marcelo Tosattie3f17f02005-11-07 00:59:34 -0800327 perm = 0;
Andrew Morton0b360ad2005-10-30 15:03:02 -0800328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
330 if (!kbs) {
331 ret = -ENOMEM;
332 goto reterr;
333 }
334
335 /* we mostly copy too much here (512bytes), but who cares ;) */
336 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
337 ret = -EFAULT;
338 goto reterr;
339 }
340 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
341 i = kbs->kb_func;
342
343 switch (cmd) {
344 case KDGKBSENT:
345 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
346 a struct member */
347 up = user_kdgkb->kb_string;
348 p = func_table[i];
349 if(p)
350 for ( ; *p && sz; p++, sz--)
351 if (put_user(*p, up++)) {
352 ret = -EFAULT;
353 goto reterr;
354 }
355 if (put_user('\0', up)) {
356 ret = -EFAULT;
357 goto reterr;
358 }
359 kfree(kbs);
360 return ((p && *p) ? -EOVERFLOW : 0);
361 case KDSKBSENT:
362 if (!perm) {
363 ret = -EPERM;
364 goto reterr;
365 }
366
367 q = func_table[i];
368 first_free = funcbufptr + (funcbufsize - funcbufleft);
369 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
370 ;
371 if (j < MAX_NR_FUNC)
372 fj = func_table[j];
373 else
374 fj = first_free;
375
376 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
377 if (delta <= funcbufleft) { /* it fits in current buf */
378 if (j < MAX_NR_FUNC) {
379 memmove(fj + delta, fj, first_free - fj);
380 for (k = j; k < MAX_NR_FUNC; k++)
381 if (func_table[k])
382 func_table[k] += delta;
383 }
384 if (!q)
385 func_table[i] = fj;
386 funcbufleft -= delta;
387 } else { /* allocate a larger buffer */
388 sz = 256;
389 while (sz < funcbufsize - funcbufleft + delta)
390 sz <<= 1;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800391 fnw = kmalloc(sz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if(!fnw) {
393 ret = -ENOMEM;
394 goto reterr;
395 }
396
397 if (!q)
398 func_table[i] = fj;
399 if (fj > funcbufptr)
400 memmove(fnw, funcbufptr, fj - funcbufptr);
401 for (k = 0; k < j; k++)
402 if (func_table[k])
403 func_table[k] = fnw + (func_table[k] - funcbufptr);
404
405 if (first_free > fj) {
406 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
407 for (k = j; k < MAX_NR_FUNC; k++)
408 if (func_table[k])
409 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
410 }
411 if (funcbufptr != func_buf)
412 kfree(funcbufptr);
413 funcbufptr = fnw;
414 funcbufleft = funcbufleft - delta + sz - funcbufsize;
415 funcbufsize = sz;
416 }
417 strcpy(func_table[i], kbs->kb_string);
418 break;
419 }
420 ret = 0;
421reterr:
422 kfree(kbs);
423 return ret;
424}
425
426static inline int
427do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
428{
429 struct consolefontdesc cfdarg;
430 int i;
431
432 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
433 return -EFAULT;
434
435 switch (cmd) {
436 case PIO_FONTX:
437 if (!perm)
438 return -EPERM;
439 op->op = KD_FONT_OP_SET;
440 op->flags = KD_FONT_FLAG_OLD;
441 op->width = 8;
442 op->height = cfdarg.charheight;
443 op->charcount = cfdarg.charcount;
444 op->data = cfdarg.chardata;
445 return con_font_op(vc_cons[fg_console].d, op);
446 case GIO_FONTX: {
447 op->op = KD_FONT_OP_GET;
448 op->flags = KD_FONT_FLAG_OLD;
449 op->width = 8;
450 op->height = cfdarg.charheight;
451 op->charcount = cfdarg.charcount;
452 op->data = cfdarg.chardata;
453 i = con_font_op(vc_cons[fg_console].d, op);
454 if (i)
455 return i;
456 cfdarg.charheight = op->height;
457 cfdarg.charcount = op->charcount;
458 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
459 return -EFAULT;
460 return 0;
461 }
462 }
463 return -EINVAL;
464}
465
466static inline int
467do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
468{
469 struct unimapdesc tmp;
470
471 if (copy_from_user(&tmp, user_ud, sizeof tmp))
472 return -EFAULT;
473 if (tmp.entries)
474 if (!access_ok(VERIFY_WRITE, tmp.entries,
475 tmp.entry_ct*sizeof(struct unipair)))
476 return -EFAULT;
477 switch (cmd) {
478 case PIO_UNIMAP:
479 if (!perm)
480 return -EPERM;
481 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
482 case GIO_UNIMAP:
483 if (!perm && fg_console != vc->vc_num)
484 return -EPERM;
485 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
486 }
487 return 0;
488}
489
Alan Cox8b92e872009-09-19 13:13:24 -0700490
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * We handle the console-specific ioctl's here. We allow the
494 * capability to modify any console, not just the fg_console.
495 */
496int vt_ioctl(struct tty_struct *tty, struct file * file,
497 unsigned int cmd, unsigned long arg)
498{
Alan Coxc9f19e92009-01-02 13:47:26 +0000499 struct vc_data *vc = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct console_font_op op; /* used in multiple places here */
501 struct kbd_struct * kbd;
502 unsigned int console;
503 unsigned char ucval;
504 void __user *up = (void __user *)arg;
505 int i, perm;
Alan Cox9cc3c222008-04-30 00:53:26 -0700506 int ret = 0;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 console = vc->vc_num;
509
Alan Cox9cc3c222008-04-30 00:53:26 -0700510 lock_kernel();
511
512 if (!vc_cons_allocated(console)) { /* impossible? */
513 ret = -ENOIOCTLCMD;
514 goto out;
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * To have permissions to do most of the vt ioctls, we either have
520 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
521 */
522 perm = 0;
523 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
524 perm = 1;
525
526 kbd = kbd_table + console;
527 switch (cmd) {
Alan Coxe6885102008-10-13 10:36:40 +0100528 case TIOCLINUX:
Jiri Slabya1159022009-06-22 18:42:18 +0100529 ret = tioclinux(tty, arg);
530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 case KIOCSOUND:
532 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700533 goto eperm;
Alan Coxfab89222009-05-06 17:17:26 +0100534 /* FIXME: This is an old broken API but we need to keep it
535 supported and somehow separate the historic advertised
536 tick rate from any real one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (arg)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700538 arg = CLOCK_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 kd_mksound(arg, 0);
Alan Cox9cc3c222008-04-30 00:53:26 -0700540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 case KDMKTONE:
543 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700544 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 {
546 unsigned int ticks, count;
547
548 /*
549 * Generate the tone for the appropriate number of ticks.
550 * If the time is zero, turn off sound ourselves.
551 */
552 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
553 count = ticks ? (arg & 0xffff) : 0;
Alan Coxfab89222009-05-06 17:17:26 +0100554 /* FIXME: This is an old broken API but we need to keep it
555 supported and somehow separate the historic advertised
556 tick rate from any real one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (count)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700558 count = CLOCK_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 kd_mksound(count, ticks);
Alan Cox9cc3c222008-04-30 00:53:26 -0700560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563 case KDGKBTYPE:
564 /*
565 * this is naive.
566 */
567 ucval = KB_101;
568 goto setchar;
569
570 /*
571 * These cannot be implemented on any machine that implements
572 * ioperm() in user level (such as Alpha PCs) or not at all.
573 *
574 * XXX: you should never use these, just call ioperm directly..
575 */
576#ifdef CONFIG_X86
577 case KDADDIO:
578 case KDDELIO:
579 /*
580 * KDADDIO and KDDELIO may be able to add ports beyond what
581 * we reject here, but to be safe...
582 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700583 if (arg < GPFIRST || arg > GPLAST) {
584 ret = -EINVAL;
585 break;
586 }
587 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 case KDENABIO:
591 case KDDISABIO:
Alan Cox9cc3c222008-04-30 00:53:26 -0700592 ret = sys_ioperm(GPFIRST, GPNUM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 (cmd == KDENABIO)) ? -ENXIO : 0;
Alan Cox9cc3c222008-04-30 00:53:26 -0700594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#endif
596
597 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
598
599 case KDKBDREP:
600 {
601 struct kbd_repeat kbrep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700604 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Alan Cox9cc3c222008-04-30 00:53:26 -0700606 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
607 ret = -EFAULT;
608 break;
609 }
610 ret = kbd_rate(&kbrep);
611 if (ret)
612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700614 ret = -EFAULT;
615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 case KDSETMODE:
619 /*
620 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
621 * doesn't do a whole lot. i'm not sure if it should do any
622 * restoration of modes or what...
623 *
624 * XXX It should at least call into the driver, fbdev's definitely
625 * need to restore their engine state. --BenH
626 */
627 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700628 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 switch (arg) {
630 case KD_GRAPHICS:
631 break;
632 case KD_TEXT0:
633 case KD_TEXT1:
634 arg = KD_TEXT;
635 case KD_TEXT:
636 break;
637 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700638 ret = -EINVAL;
639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 if (vc->vc_mode == (unsigned char) arg)
Alan Cox9cc3c222008-04-30 00:53:26 -0700642 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 vc->vc_mode = (unsigned char) arg;
644 if (console != fg_console)
Alan Cox9cc3c222008-04-30 00:53:26 -0700645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /*
647 * explicitly blank/unblank the screen if switching modes
648 */
649 acquire_console_sem();
650 if (arg == KD_TEXT)
651 do_unblank_screen(1);
652 else
653 do_blank_screen(1);
654 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -0700655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 case KDGETMODE:
658 ucval = vc->vc_mode;
659 goto setint;
660
661 case KDMAPDISP:
662 case KDUNMAPDISP:
663 /*
664 * these work like a combination of mmap and KDENABIO.
665 * this could be easily finished.
666 */
Alan Cox9cc3c222008-04-30 00:53:26 -0700667 ret = -EINVAL;
668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 case KDSKBMODE:
671 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700672 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 switch(arg) {
674 case K_RAW:
675 kbd->kbdmode = VC_RAW;
676 break;
677 case K_MEDIUMRAW:
678 kbd->kbdmode = VC_MEDIUMRAW;
679 break;
680 case K_XLATE:
681 kbd->kbdmode = VC_XLATE;
682 compute_shiftstate();
683 break;
684 case K_UNICODE:
685 kbd->kbdmode = VC_UNICODE;
686 compute_shiftstate();
687 break;
688 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700689 ret = -EINVAL;
690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 tty_ldisc_flush(tty);
Alan Cox9cc3c222008-04-30 00:53:26 -0700693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 case KDGKBMODE:
696 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
697 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
698 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
699 K_XLATE);
700 goto setint;
701
702 /* this could be folded into KDSKBMODE, but for compatibility
703 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
704 case KDSKBMETA:
705 switch(arg) {
706 case K_METABIT:
707 clr_vc_kbd_mode(kbd, VC_META);
708 break;
709 case K_ESCPREFIX:
710 set_vc_kbd_mode(kbd, VC_META);
711 break;
712 default:
Alan Cox9cc3c222008-04-30 00:53:26 -0700713 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 case KDGKBMETA:
718 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
719 setint:
Alan Cox9cc3c222008-04-30 00:53:26 -0700720 ret = put_user(ucval, (int __user *)arg);
721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 case KDGETKEYCODE:
724 case KDSETKEYCODE:
725 if(!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -0700726 perm = 0;
727 ret = do_kbkeycode_ioctl(cmd, up, perm);
728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 case KDGKBENT:
731 case KDSKBENT:
Alan Cox9cc3c222008-04-30 00:53:26 -0700732 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 case KDGKBSENT:
736 case KDSKBSENT:
Alan Cox9cc3c222008-04-30 00:53:26 -0700737 ret = do_kdgkb_ioctl(cmd, up, perm);
738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 case KDGKBDIACR:
741 {
742 struct kbdiacrs __user *a = up;
Samuel Thibault04c71972007-10-16 23:27:04 -0700743 struct kbdiacr diacr;
744 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alan Cox9cc3c222008-04-30 00:53:26 -0700746 if (put_user(accent_table_size, &a->kb_cnt)) {
747 ret = -EFAULT;
748 break;
749 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700750 for (i = 0; i < accent_table_size; i++) {
751 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
752 diacr.base = conv_uni_to_8bit(accent_table[i].base);
753 diacr.result = conv_uni_to_8bit(accent_table[i].result);
Alan Cox9cc3c222008-04-30 00:53:26 -0700754 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
755 ret = -EFAULT;
756 break;
757 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700758 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700759 break;
Samuel Thibault04c71972007-10-16 23:27:04 -0700760 }
761 case KDGKBDIACRUC:
762 {
763 struct kbdiacrsuc __user *a = up;
764
765 if (put_user(accent_table_size, &a->kb_cnt))
Alan Cox9cc3c222008-04-30 00:53:26 -0700766 ret = -EFAULT;
767 else if (copy_to_user(a->kbdiacruc, accent_table,
768 accent_table_size*sizeof(struct kbdiacruc)))
769 ret = -EFAULT;
770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 case KDSKBDIACR:
774 {
775 struct kbdiacrs __user *a = up;
Samuel Thibault04c71972007-10-16 23:27:04 -0700776 struct kbdiacr diacr;
777 unsigned int ct;
778 int i;
779
780 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700781 goto eperm;
782 if (get_user(ct,&a->kb_cnt)) {
783 ret = -EFAULT;
784 break;
785 }
786 if (ct >= MAX_DIACR) {
787 ret = -EINVAL;
788 break;
789 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700790 accent_table_size = ct;
791 for (i = 0; i < ct; i++) {
Alan Cox9cc3c222008-04-30 00:53:26 -0700792 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
793 ret = -EFAULT;
794 break;
795 }
Samuel Thibault04c71972007-10-16 23:27:04 -0700796 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
797 accent_table[i].base = conv_8bit_to_uni(diacr.base);
798 accent_table[i].result = conv_8bit_to_uni(diacr.result);
799 }
Alan Cox9cc3c222008-04-30 00:53:26 -0700800 break;
Samuel Thibault04c71972007-10-16 23:27:04 -0700801 }
802
803 case KDSKBDIACRUC:
804 {
805 struct kbdiacrsuc __user *a = up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 unsigned int ct;
807
808 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700809 goto eperm;
810 if (get_user(ct,&a->kb_cnt)) {
811 ret = -EFAULT;
812 break;
813 }
814 if (ct >= MAX_DIACR) {
815 ret = -EINVAL;
816 break;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 accent_table_size = ct;
Samuel Thibault04c71972007-10-16 23:27:04 -0700819 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
Alan Cox9cc3c222008-04-30 00:53:26 -0700820 ret = -EFAULT;
821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 /* the ioctls below read/set the flags usually shown in the leds */
825 /* don't use them - they will go away without warning */
826 case KDGKBLED:
827 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
828 goto setchar;
829
830 case KDSKBLED:
831 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700832 goto eperm;
833 if (arg & ~0x77) {
834 ret = -EINVAL;
835 break;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 kbd->ledflagstate = (arg & 7);
838 kbd->default_ledflagstate = ((arg >> 4) & 7);
839 set_leds();
Alan Cox9cc3c222008-04-30 00:53:26 -0700840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* the ioctls below only set the lights, not the functions */
843 /* for those, see KDGKBLED and KDSKBLED above */
844 case KDGETLED:
845 ucval = getledstate();
846 setchar:
Alan Cox9cc3c222008-04-30 00:53:26 -0700847 ret = put_user(ucval, (char __user *)arg);
848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 case KDSETLED:
851 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700852 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 setledstate(kbd, arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /*
857 * A process can indicate its willingness to accept signals
858 * generated by pressing an appropriate key combination.
859 * Thus, one can have a daemon that e.g. spawns a new console
860 * upon a keypress and then changes to it.
861 * See also the kbrequest field of inittab(5).
862 */
863 case KDSIGACCEPT:
864 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!perm || !capable(CAP_KILL))
Alan Cox9cc3c222008-04-30 00:53:26 -0700866 goto eperm;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700867 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Alan Cox9cc3c222008-04-30 00:53:26 -0700868 ret = -EINVAL;
869 else {
870 spin_lock_irq(&vt_spawn_con.lock);
871 put_pid(vt_spawn_con.pid);
872 vt_spawn_con.pid = get_pid(task_pid(current));
873 vt_spawn_con.sig = arg;
874 spin_unlock_irq(&vt_spawn_con.lock);
875 }
876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 case VT_SETMODE:
880 {
881 struct vt_mode tmp;
882
883 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700884 goto eperm;
885 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
886 ret = -EFAULT;
887 goto out;
888 }
889 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
890 ret = -EINVAL;
891 goto out;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 acquire_console_sem();
894 vc->vt_mode = tmp;
895 /* the frsig is ignored, so we set it to 0 */
896 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800897 put_pid(vc->vt_pid);
898 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* no switch is required -- saw@shade.msu.ru */
900 vc->vt_newvt = -1;
901 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -0700902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 case VT_GETMODE:
906 {
907 struct vt_mode tmp;
908 int rc;
909
910 acquire_console_sem();
911 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
912 release_console_sem();
913
914 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
Alan Cox9cc3c222008-04-30 00:53:26 -0700915 if (rc)
916 ret = -EFAULT;
917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
920 /*
921 * Returns global vt state. Note that VT 0 is always open, since
922 * it's an alias for the current VT, and people can't use it here.
923 * We cannot return state for more than 16 VTs, since v_state is short.
924 */
925 case VT_GETSTATE:
926 {
927 struct vt_stat __user *vtstat = up;
928 unsigned short state, mask;
929
930 if (put_user(fg_console + 1, &vtstat->v_active))
Alan Cox9cc3c222008-04-30 00:53:26 -0700931 ret = -EFAULT;
932 else {
933 state = 1; /* /dev/tty0 is always open */
934 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
935 ++i, mask <<= 1)
936 if (VT_IS_IN_USE(i))
937 state |= mask;
938 ret = put_user(state, &vtstat->v_state);
939 }
940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 /*
944 * Returns the first available (non-opened) console.
945 */
946 case VT_OPENQRY:
947 for (i = 0; i < MAX_NR_CONSOLES; ++i)
948 if (! VT_IS_IN_USE(i))
949 break;
950 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
951 goto setint;
952
953 /*
954 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
955 * with num >= 1 (switches to vt 0, our console, are not allowed, just
956 * to preserve sanity).
957 */
958 case VT_ACTIVATE:
959 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700960 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700962 ret = -ENXIO;
963 else {
964 arg--;
965 acquire_console_sem();
966 ret = vc_allocate(arg);
967 release_console_sem();
968 if (ret)
969 break;
970 set_console(arg);
971 }
972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /*
975 * wait until the specified VT has been activated
976 */
977 case VT_WAITACTIVE:
978 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700979 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (arg == 0 || arg > MAX_NR_CONSOLES)
Alan Cox9cc3c222008-04-30 00:53:26 -0700981 ret = -ENXIO;
982 else
Alan Cox8b92e872009-09-19 13:13:24 -0700983 ret = vt_waitactive(arg);
Alan Cox9cc3c222008-04-30 00:53:26 -0700984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /*
987 * If a vt is under process control, the kernel will not switch to it
988 * immediately, but postpone the operation until the process calls this
989 * ioctl, allowing the switch to complete.
990 *
991 * According to the X sources this is the behavior:
992 * 0: pending switch-from not OK
993 * 1: pending switch-from OK
994 * 2: completed switch-to OK
995 */
996 case VT_RELDISP:
997 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -0700998 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Alan Cox9cc3c222008-04-30 00:53:26 -07001000 if (vc->vt_mode.mode != VT_PROCESS) {
1001 ret = -EINVAL;
1002 break;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Switching-from response
1006 */
Samuel Ortiz8792f962007-10-01 01:20:12 -07001007 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (vc->vt_newvt >= 0) {
1009 if (arg == 0)
1010 /*
1011 * Switch disallowed, so forget we were trying
1012 * to do it.
1013 */
1014 vc->vt_newvt = -1;
1015
1016 else {
1017 /*
1018 * The current vt has been released, so
1019 * complete the switch.
1020 */
1021 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 newvt = vc->vt_newvt;
1023 vc->vt_newvt = -1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001024 ret = vc_allocate(newvt);
1025 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 /*
1030 * When we actually do the console switch,
1031 * make sure we are atomic with respect to
1032 * other console switches..
1033 */
1034 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001036 } else {
1037 /*
1038 * Switched-to response
1039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * If it's just an ACK, ignore it
1042 */
Alan Cox9cc3c222008-04-30 00:53:26 -07001043 if (arg != VT_ACKACQ)
1044 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Samuel Ortiz8792f962007-10-01 01:20:12 -07001046 release_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /*
1050 * Disallocate memory associated to VT (but leave VT1)
1051 */
1052 case VT_DISALLOCATE:
Alan Cox9cc3c222008-04-30 00:53:26 -07001053 if (arg > MAX_NR_CONSOLES) {
1054 ret = -ENXIO;
1055 break;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -07001058 /* deallocate all unused consoles, but leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 acquire_console_sem();
1060 for (i=1; i<MAX_NR_CONSOLES; i++)
1061 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -07001062 vc_deallocate(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 release_console_sem();
1064 } else {
Alan Coxca9bda02006-09-29 02:00:03 -07001065 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 arg--;
1067 if (VT_BUSY(arg))
Alan Cox9cc3c222008-04-30 00:53:26 -07001068 ret = -EBUSY;
1069 else if (arg) { /* leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 acquire_console_sem();
Alan Coxca9bda02006-09-29 02:00:03 -07001071 vc_deallocate(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 release_console_sem();
1073 }
1074 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 case VT_RESIZE:
1078 {
1079 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001080 struct vc_data *vc;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ushort ll,cc;
1083 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001084 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (get_user(ll, &vtsizes->v_rows) ||
1086 get_user(cc, &vtsizes->v_cols))
Alan Cox9cc3c222008-04-30 00:53:26 -07001087 ret = -EFAULT;
1088 else {
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001089 acquire_console_sem();
Alan Cox9cc3c222008-04-30 00:53:26 -07001090 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1091 vc = vc_cons[i].d;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001092
Alan Cox9cc3c222008-04-30 00:53:26 -07001093 if (vc) {
1094 vc->vc_resize_user = 1;
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001095 vc_resize(vc_cons[i].d, cc, ll);
Alan Cox9cc3c222008-04-30 00:53:26 -07001096 }
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001097 }
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001098 release_console_sem();
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001099 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 case VT_RESIZEX:
1104 {
1105 struct vt_consize __user *vtconsize = up;
1106 ushort ll,cc,vlin,clin,vcol,ccol;
1107 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001108 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!access_ok(VERIFY_READ, vtconsize,
Alan Cox9cc3c222008-04-30 00:53:26 -07001110 sizeof(struct vt_consize))) {
1111 ret = -EFAULT;
1112 break;
1113 }
1114 /* FIXME: Should check the copies properly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 __get_user(ll, &vtconsize->v_rows);
1116 __get_user(cc, &vtconsize->v_cols);
1117 __get_user(vlin, &vtconsize->v_vlin);
1118 __get_user(clin, &vtconsize->v_clin);
1119 __get_user(vcol, &vtconsize->v_vcol);
1120 __get_user(ccol, &vtconsize->v_ccol);
1121 vlin = vlin ? vlin : vc->vc_scan_lines;
1122 if (clin) {
1123 if (ll) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001124 if (ll != vlin/clin) {
1125 /* Parameters don't add up */
1126 ret = -EINVAL;
1127 break;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 } else
1130 ll = vlin/clin;
1131 }
1132 if (vcol && ccol) {
1133 if (cc) {
Alan Cox9cc3c222008-04-30 00:53:26 -07001134 if (cc != vcol/ccol) {
1135 ret = -EINVAL;
1136 break;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } else
1139 cc = vcol/ccol;
1140 }
1141
Alan Cox9cc3c222008-04-30 00:53:26 -07001142 if (clin > 32) {
1143 ret = -EINVAL;
1144 break;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1148 if (!vc_cons[i].d)
1149 continue;
1150 acquire_console_sem();
1151 if (vlin)
1152 vc_cons[i].d->vc_scan_lines = vlin;
1153 if (clin)
1154 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001155 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 vc_resize(vc_cons[i].d, cc, ll);
1157 release_console_sem();
1158 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001159 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161
1162 case PIO_FONT: {
1163 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001164 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 op.op = KD_FONT_OP_SET;
1166 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1167 op.width = 8;
1168 op.height = 0;
1169 op.charcount = 256;
1170 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001171 ret = con_font_op(vc_cons[fg_console].d, &op);
1172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
1175 case GIO_FONT: {
1176 op.op = KD_FONT_OP_GET;
1177 op.flags = KD_FONT_FLAG_OLD;
1178 op.width = 8;
1179 op.height = 32;
1180 op.charcount = 256;
1181 op.data = up;
Alan Cox9cc3c222008-04-30 00:53:26 -07001182 ret = con_font_op(vc_cons[fg_console].d, &op);
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 case PIO_CMAP:
1187 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001188 ret = -EPERM;
1189 else
1190 ret = con_set_cmap(up);
1191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 case GIO_CMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001194 ret = con_get_cmap(up);
1195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 case PIO_FONTX:
1198 case GIO_FONTX:
Alan Cox9cc3c222008-04-30 00:53:26 -07001199 ret = do_fontx_ioctl(cmd, up, perm, &op);
1200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 case PIO_FONTRESET:
1203 {
1204 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001205 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207#ifdef BROKEN_GRAPHICS_PROGRAMS
1208 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1209 font is not saved. */
Alan Cox9cc3c222008-04-30 00:53:26 -07001210 ret = -ENOSYS;
1211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212#else
1213 {
1214 op.op = KD_FONT_OP_SET_DEFAULT;
1215 op.data = NULL;
Alan Cox9cc3c222008-04-30 00:53:26 -07001216 ret = con_font_op(vc_cons[fg_console].d, &op);
1217 if (ret)
1218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 con_set_default_unimap(vc_cons[fg_console].d);
Alan Cox9cc3c222008-04-30 00:53:26 -07001220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222#endif
1223 }
1224
1225 case KDFONTOP: {
Alan Cox9cc3c222008-04-30 00:53:26 -07001226 if (copy_from_user(&op, up, sizeof(op))) {
1227 ret = -EFAULT;
1228 break;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!perm && op.op != KD_FONT_OP_GET)
Alan Cox9cc3c222008-04-30 00:53:26 -07001231 goto eperm;
1232 ret = con_font_op(vc, &op);
1233 if (ret)
1234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (copy_to_user(up, &op, sizeof(op)))
Alan Cox9cc3c222008-04-30 00:53:26 -07001236 ret = -EFAULT;
1237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 case PIO_SCRNMAP:
1241 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001242 ret = -EPERM;
1243 else
1244 ret = con_set_trans_old(up);
1245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 case GIO_SCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001248 ret = con_get_trans_old(up);
1249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 case PIO_UNISCRNMAP:
1252 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001253 ret = -EPERM;
1254 else
1255 ret = con_set_trans_new(up);
1256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 case GIO_UNISCRNMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001259 ret = con_get_trans_new(up);
1260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 case PIO_UNIMAPCLR:
1263 { struct unimapinit ui;
1264 if (!perm)
Alan Cox9cc3c222008-04-30 00:53:26 -07001265 goto eperm;
1266 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1267 if (!ret)
1268 con_clear_unimap(vc, &ui);
1269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
1272 case PIO_UNIMAP:
1273 case GIO_UNIMAP:
Alan Cox9cc3c222008-04-30 00:53:26 -07001274 ret = do_unimap_ioctl(cmd, up, perm, vc);
1275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 case VT_LOCKSWITCH:
1278 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001279 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 vt_dont_switch = 1;
Alan Cox9cc3c222008-04-30 00:53:26 -07001281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 case VT_UNLOCKSWITCH:
1283 if (!capable(CAP_SYS_TTY_CONFIG))
Alan Cox9cc3c222008-04-30 00:53:26 -07001284 goto eperm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 vt_dont_switch = 0;
Alan Cox9cc3c222008-04-30 00:53:26 -07001286 break;
Samuel Thibault533475d2006-08-27 01:23:39 -07001287 case VT_GETHIFONTMASK:
Alan Cox9cc3c222008-04-30 00:53:26 -07001288 ret = put_user(vc->vc_hi_font_mask,
1289 (unsigned short __user *)arg);
1290 break;
Alan Cox8b92e872009-09-19 13:13:24 -07001291 case VT_WAITEVENT:
1292 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 default:
Alan Cox9cc3c222008-04-30 00:53:26 -07001295 ret = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Alan Cox9cc3c222008-04-30 00:53:26 -07001297out:
1298 unlock_kernel();
1299 return ret;
1300eperm:
1301 ret = -EPERM;
1302 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305void reset_vc(struct vc_data *vc)
1306{
1307 vc->vc_mode = KD_TEXT;
Bill Nottingham2e8ecb92007-10-16 23:29:38 -07001308 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 vc->vt_mode.mode = VT_AUTO;
1310 vc->vt_mode.waitv = 0;
1311 vc->vt_mode.relsig = 0;
1312 vc->vt_mode.acqsig = 0;
1313 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001314 put_pid(vc->vt_pid);
1315 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 vc->vt_newvt = -1;
1317 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1318 reset_palette(vc);
1319}
1320
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001321void vc_SAK(struct work_struct *work)
1322{
1323 struct vc *vc_con =
1324 container_of(work, struct vc, SAK_work);
1325 struct vc_data *vc;
1326 struct tty_struct *tty;
1327
1328 acquire_console_sem();
1329 vc = vc_con->d;
1330 if (vc) {
1331 tty = vc->vc_tty;
1332 /*
1333 * SAK should also work in all raw modes and reset
1334 * them properly.
1335 */
1336 if (tty)
1337 __do_SAK(tty);
1338 reset_vc(vc);
1339 }
1340 release_console_sem();
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * Performs the back end of a vt switch
1345 */
1346static void complete_change_console(struct vc_data *vc)
1347{
1348 unsigned char old_vc_mode;
Alan Cox8b92e872009-09-19 13:13:24 -07001349 int old = fg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 last_console = fg_console;
1352
1353 /*
1354 * If we're switching, we could be going from KD_GRAPHICS to
1355 * KD_TEXT mode or vice versa, which means we need to blank or
1356 * unblank the screen later.
1357 */
1358 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1359 switch_screen(vc);
1360
1361 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001362 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * then the *blank_screen operation could occur while X, having
1364 * received acqsig, is waking up on another processor. This
1365 * condition can lead to overlapping accesses to the VGA range
1366 * and the framebuffer (causing system lockups).
1367 *
1368 * To account for this we duplicate this code below only if the
1369 * controlling process is gone and we've called reset_vc.
1370 */
1371 if (old_vc_mode != vc->vc_mode) {
1372 if (vc->vc_mode == KD_TEXT)
1373 do_unblank_screen(1);
1374 else
1375 do_blank_screen(1);
1376 }
1377
1378 /*
1379 * If this new console is under process control, send it a signal
1380 * telling it that it has acquired. Also check if it has died and
1381 * clean up (similar to logic employed in change_console())
1382 */
1383 if (vc->vt_mode.mode == VT_PROCESS) {
1384 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001385 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * tell us if the process has gone or something else
1387 * is awry
1388 */
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001389 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * The controlling process has died, so we revert back to
1392 * normal operation. In this case, we'll also change back
1393 * to KD_TEXT mode. I'm not sure if this is strictly correct
1394 * but it saves the agony when the X server dies and the screen
1395 * remains blanked due to KD_GRAPHICS! It would be nice to do
1396 * this outside of VT_PROCESS but there is no single process
1397 * to account for and tracking tty count may be undesirable.
1398 */
1399 reset_vc(vc);
1400
1401 if (old_vc_mode != vc->vc_mode) {
1402 if (vc->vc_mode == KD_TEXT)
1403 do_unblank_screen(1);
1404 else
1405 do_blank_screen(1);
1406 }
1407 }
1408 }
1409
1410 /*
1411 * Wake anyone waiting for their VT to activate
1412 */
Alan Cox8b92e872009-09-19 13:13:24 -07001413 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return;
1415}
1416
1417/*
1418 * Performs the front-end of a vt switch
1419 */
1420void change_console(struct vc_data *new_vc)
1421{
1422 struct vc_data *vc;
1423
1424 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1425 return;
1426
1427 /*
1428 * If this vt is in process mode, then we need to handshake with
1429 * that process before switching. Essentially, we store where that
1430 * vt wants to switch to and wait for it to tell us when it's done
1431 * (via VT_RELDISP ioctl).
1432 *
1433 * We also check to see if the controlling process still exists.
1434 * If it doesn't, we reset this vt to auto mode and continue.
1435 * This is a cheap way to track process control. The worst thing
1436 * that can happen is: we send a signal to a process, it dies, and
1437 * the switch gets "lost" waiting for a response; hopefully, the
1438 * user will try again, we'll detect the process is gone (unless
1439 * the user waits just the right amount of time :-) and revert the
1440 * vt to auto control.
1441 */
1442 vc = vc_cons[fg_console].d;
1443 if (vc->vt_mode.mode == VT_PROCESS) {
1444 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001445 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001447 * is awry.
1448 *
1449 * We need to set vt_newvt *before* sending the signal or we
1450 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001452 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001453 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /*
1455 * It worked. Mark the vt to switch to and
1456 * return. The process needs to send us a
1457 * VT_RELDISP ioctl to complete the switch.
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return;
1460 }
1461
1462 /*
1463 * The controlling process has died, so we revert back to
1464 * normal operation. In this case, we'll also change back
1465 * to KD_TEXT mode. I'm not sure if this is strictly correct
1466 * but it saves the agony when the X server dies and the screen
1467 * remains blanked due to KD_GRAPHICS! It would be nice to do
1468 * this outside of VT_PROCESS but there is no single process
1469 * to account for and tracking tty count may be undesirable.
1470 */
1471 reset_vc(vc);
1472
1473 /*
1474 * Fall through to normal (VT_AUTO) handling of the switch...
1475 */
1476 }
1477
1478 /*
1479 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1480 */
1481 if (vc->vc_mode == KD_GRAPHICS)
1482 return;
1483
1484 complete_change_console(new_vc);
1485}