9e9a901442a35cd4b16ae7b78b766884beba8b72
[deliverable/linux.git] / drivers / tty / vt / vt_ioctl.c
1 /*
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
13 #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/compat.h>
20 #include <linux/module.h>
21 #include <linux/kd.h>
22 #include <linux/vt.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/major.h>
26 #include <linux/fs.h>
27 #include <linux/console.h>
28 #include <linux/consolemap.h>
29 #include <linux/signal.h>
30 #include <linux/smp_lock.h>
31 #include <linux/timex.h>
32
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/kbd_kern.h>
37 #include <linux/vt_kern.h>
38 #include <linux/kbd_diacr.h>
39 #include <linux/selection.h>
40
41 char vt_dont_switch;
42 extern struct tty_driver *console_driver;
43
44 #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
45 #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
46
47 /*
48 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
49 * experimentation and study of X386 SYSV handling.
50 *
51 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
52 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
53 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
54 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
55 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
56 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
57 * to the current console is done by the main ioctl code.
58 */
59
60 #ifdef CONFIG_X86
61 #include <linux/syscalls.h>
62 #endif
63
64 static void complete_change_console(struct vc_data *vc);
65
66 /*
67 * User space VT_EVENT handlers
68 */
69
70 struct vt_event_wait {
71 struct list_head list;
72 struct vt_event event;
73 int done;
74 };
75
76 static LIST_HEAD(vt_events);
77 static DEFINE_SPINLOCK(vt_event_lock);
78 static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
79
80 /**
81 * vt_event_post
82 * @event: the event that occurred
83 * @old: old console
84 * @new: new console
85 *
86 * Post an VT event to interested VT handlers
87 */
88
89 void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
90 {
91 struct list_head *pos, *head;
92 unsigned long flags;
93 int wake = 0;
94
95 spin_lock_irqsave(&vt_event_lock, flags);
96 head = &vt_events;
97
98 list_for_each(pos, head) {
99 struct vt_event_wait *ve = list_entry(pos,
100 struct vt_event_wait, list);
101 if (!(ve->event.event & event))
102 continue;
103 ve->event.event = event;
104 /* kernel view is consoles 0..n-1, user space view is
105 console 1..n with 0 meaning current, so we must bias */
106 ve->event.oldev = old + 1;
107 ve->event.newev = new + 1;
108 wake = 1;
109 ve->done = 1;
110 }
111 spin_unlock_irqrestore(&vt_event_lock, flags);
112 if (wake)
113 wake_up_interruptible(&vt_event_waitqueue);
114 }
115
116 /**
117 * vt_event_wait - wait for an event
118 * @vw: our event
119 *
120 * Waits for an event to occur which completes our vt_event_wait
121 * structure. On return the structure has wv->done set to 1 for success
122 * or 0 if some event such as a signal ended the wait.
123 */
124
125 static void vt_event_wait(struct vt_event_wait *vw)
126 {
127 unsigned long flags;
128 /* Prepare the event */
129 INIT_LIST_HEAD(&vw->list);
130 vw->done = 0;
131 /* Queue our event */
132 spin_lock_irqsave(&vt_event_lock, flags);
133 list_add(&vw->list, &vt_events);
134 spin_unlock_irqrestore(&vt_event_lock, flags);
135 /* Wait for it to pass */
136 wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
137 /* Dequeue it */
138 spin_lock_irqsave(&vt_event_lock, flags);
139 list_del(&vw->list);
140 spin_unlock_irqrestore(&vt_event_lock, flags);
141 }
142
143 /**
144 * vt_event_wait_ioctl - event ioctl handler
145 * @arg: argument to ioctl
146 *
147 * Implement the VT_WAITEVENT ioctl using the VT event interface
148 */
149
150 static int vt_event_wait_ioctl(struct vt_event __user *event)
151 {
152 struct vt_event_wait vw;
153
154 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
155 return -EFAULT;
156 /* Highest supported event for now */
157 if (vw.event.event & ~VT_MAX_EVENT)
158 return -EINVAL;
159
160 vt_event_wait(&vw);
161 /* If it occurred report it */
162 if (vw.done) {
163 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
164 return -EFAULT;
165 return 0;
166 }
167 return -EINTR;
168 }
169
170 /**
171 * vt_waitactive - active console wait
172 * @event: event code
173 * @n: new console
174 *
175 * Helper for event waits. Used to implement the legacy
176 * event waiting ioctls in terms of events
177 */
178
179 int vt_waitactive(int n)
180 {
181 struct vt_event_wait vw;
182 do {
183 if (n == fg_console + 1)
184 break;
185 vw.event.event = VT_EVENT_SWITCH;
186 vt_event_wait(&vw);
187 if (vw.done == 0)
188 return -EINTR;
189 } while (vw.event.newev != n);
190 return 0;
191 }
192
193 /*
194 * these are the valid i/o ports we're allowed to change. they map all the
195 * video ports
196 */
197 #define GPFIRST 0x3b4
198 #define GPLAST 0x3df
199 #define GPNUM (GPLAST - GPFIRST + 1)
200
201 #define i (tmp.kb_index)
202 #define s (tmp.kb_table)
203 #define v (tmp.kb_value)
204 static inline int
205 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
206 {
207 struct kbentry tmp;
208 ushort *key_map, val, ov;
209
210 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
211 return -EFAULT;
212
213 if (!capable(CAP_SYS_TTY_CONFIG))
214 perm = 0;
215
216 switch (cmd) {
217 case KDGKBENT:
218 key_map = key_maps[s];
219 if (key_map) {
220 val = U(key_map[i]);
221 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
222 val = K_HOLE;
223 } else
224 val = (i ? K_HOLE : K_NOSUCHMAP);
225 return put_user(val, &user_kbe->kb_value);
226 case KDSKBENT:
227 if (!perm)
228 return -EPERM;
229 if (!i && v == K_NOSUCHMAP) {
230 /* deallocate map */
231 key_map = key_maps[s];
232 if (s && key_map) {
233 key_maps[s] = NULL;
234 if (key_map[0] == U(K_ALLOCATED)) {
235 kfree(key_map);
236 keymap_count--;
237 }
238 }
239 break;
240 }
241
242 if (KTYP(v) < NR_TYPES) {
243 if (KVAL(v) > max_vals[KTYP(v)])
244 return -EINVAL;
245 } else
246 if (kbd->kbdmode != VC_UNICODE)
247 return -EINVAL;
248
249 /* ++Geert: non-PC keyboards may generate keycode zero */
250 #if !defined(__mc68000__) && !defined(__powerpc__)
251 /* assignment to entry 0 only tests validity of args */
252 if (!i)
253 break;
254 #endif
255
256 if (!(key_map = key_maps[s])) {
257 int j;
258
259 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
260 !capable(CAP_SYS_RESOURCE))
261 return -EPERM;
262
263 key_map = kmalloc(sizeof(plain_map),
264 GFP_KERNEL);
265 if (!key_map)
266 return -ENOMEM;
267 key_maps[s] = key_map;
268 key_map[0] = U(K_ALLOCATED);
269 for (j = 1; j < NR_KEYS; j++)
270 key_map[j] = U(K_HOLE);
271 keymap_count++;
272 }
273 ov = U(key_map[i]);
274 if (v == ov)
275 break; /* nothing to do */
276 /*
277 * Attention Key.
278 */
279 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
280 return -EPERM;
281 key_map[i] = U(v);
282 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
283 compute_shiftstate();
284 break;
285 }
286 return 0;
287 }
288 #undef i
289 #undef s
290 #undef v
291
292 static inline int
293 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
294 {
295 struct kbkeycode tmp;
296 int kc = 0;
297
298 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
299 return -EFAULT;
300 switch (cmd) {
301 case KDGETKEYCODE:
302 kc = getkeycode(tmp.scancode);
303 if (kc >= 0)
304 kc = put_user(kc, &user_kbkc->keycode);
305 break;
306 case KDSETKEYCODE:
307 if (!perm)
308 return -EPERM;
309 kc = setkeycode(tmp.scancode, tmp.keycode);
310 break;
311 }
312 return kc;
313 }
314
315 static inline int
316 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
317 {
318 struct kbsentry *kbs;
319 char *p;
320 u_char *q;
321 u_char __user *up;
322 int sz;
323 int delta;
324 char *first_free, *fj, *fnw;
325 int i, j, k;
326 int ret;
327
328 if (!capable(CAP_SYS_TTY_CONFIG))
329 perm = 0;
330
331 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
332 if (!kbs) {
333 ret = -ENOMEM;
334 goto reterr;
335 }
336
337 /* we mostly copy too much here (512bytes), but who cares ;) */
338 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
339 ret = -EFAULT;
340 goto reterr;
341 }
342 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
343 i = kbs->kb_func;
344
345 switch (cmd) {
346 case KDGKBSENT:
347 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
348 a struct member */
349 up = user_kdgkb->kb_string;
350 p = func_table[i];
351 if(p)
352 for ( ; *p && sz; p++, sz--)
353 if (put_user(*p, up++)) {
354 ret = -EFAULT;
355 goto reterr;
356 }
357 if (put_user('\0', up)) {
358 ret = -EFAULT;
359 goto reterr;
360 }
361 kfree(kbs);
362 return ((p && *p) ? -EOVERFLOW : 0);
363 case KDSKBSENT:
364 if (!perm) {
365 ret = -EPERM;
366 goto reterr;
367 }
368
369 q = func_table[i];
370 first_free = funcbufptr + (funcbufsize - funcbufleft);
371 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
372 ;
373 if (j < MAX_NR_FUNC)
374 fj = func_table[j];
375 else
376 fj = first_free;
377
378 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
379 if (delta <= funcbufleft) { /* it fits in current buf */
380 if (j < MAX_NR_FUNC) {
381 memmove(fj + delta, fj, first_free - fj);
382 for (k = j; k < MAX_NR_FUNC; k++)
383 if (func_table[k])
384 func_table[k] += delta;
385 }
386 if (!q)
387 func_table[i] = fj;
388 funcbufleft -= delta;
389 } else { /* allocate a larger buffer */
390 sz = 256;
391 while (sz < funcbufsize - funcbufleft + delta)
392 sz <<= 1;
393 fnw = kmalloc(sz, GFP_KERNEL);
394 if(!fnw) {
395 ret = -ENOMEM;
396 goto reterr;
397 }
398
399 if (!q)
400 func_table[i] = fj;
401 if (fj > funcbufptr)
402 memmove(fnw, funcbufptr, fj - funcbufptr);
403 for (k = 0; k < j; k++)
404 if (func_table[k])
405 func_table[k] = fnw + (func_table[k] - funcbufptr);
406
407 if (first_free > fj) {
408 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
409 for (k = j; k < MAX_NR_FUNC; k++)
410 if (func_table[k])
411 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
412 }
413 if (funcbufptr != func_buf)
414 kfree(funcbufptr);
415 funcbufptr = fnw;
416 funcbufleft = funcbufleft - delta + sz - funcbufsize;
417 funcbufsize = sz;
418 }
419 strcpy(func_table[i], kbs->kb_string);
420 break;
421 }
422 ret = 0;
423 reterr:
424 kfree(kbs);
425 return ret;
426 }
427
428 static inline int
429 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
430 {
431 struct consolefontdesc cfdarg;
432 int i;
433
434 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
435 return -EFAULT;
436
437 switch (cmd) {
438 case PIO_FONTX:
439 if (!perm)
440 return -EPERM;
441 op->op = KD_FONT_OP_SET;
442 op->flags = KD_FONT_FLAG_OLD;
443 op->width = 8;
444 op->height = cfdarg.charheight;
445 op->charcount = cfdarg.charcount;
446 op->data = cfdarg.chardata;
447 return con_font_op(vc_cons[fg_console].d, op);
448 case GIO_FONTX: {
449 op->op = KD_FONT_OP_GET;
450 op->flags = KD_FONT_FLAG_OLD;
451 op->width = 8;
452 op->height = cfdarg.charheight;
453 op->charcount = cfdarg.charcount;
454 op->data = cfdarg.chardata;
455 i = con_font_op(vc_cons[fg_console].d, op);
456 if (i)
457 return i;
458 cfdarg.charheight = op->height;
459 cfdarg.charcount = op->charcount;
460 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
461 return -EFAULT;
462 return 0;
463 }
464 }
465 return -EINVAL;
466 }
467
468 static inline int
469 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
470 {
471 struct unimapdesc tmp;
472
473 if (copy_from_user(&tmp, user_ud, sizeof tmp))
474 return -EFAULT;
475 if (tmp.entries)
476 if (!access_ok(VERIFY_WRITE, tmp.entries,
477 tmp.entry_ct*sizeof(struct unipair)))
478 return -EFAULT;
479 switch (cmd) {
480 case PIO_UNIMAP:
481 if (!perm)
482 return -EPERM;
483 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
484 case GIO_UNIMAP:
485 if (!perm && fg_console != vc->vc_num)
486 return -EPERM;
487 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
488 }
489 return 0;
490 }
491
492
493
494 /*
495 * We handle the console-specific ioctl's here. We allow the
496 * capability to modify any console, not just the fg_console.
497 */
498 int vt_ioctl(struct tty_struct *tty, struct file * file,
499 unsigned int cmd, unsigned long arg)
500 {
501 struct vc_data *vc = tty->driver_data;
502 struct console_font_op op; /* used in multiple places here */
503 struct kbd_struct * kbd;
504 unsigned int console;
505 unsigned char ucval;
506 unsigned int uival;
507 void __user *up = (void __user *)arg;
508 int i, perm;
509 int ret = 0;
510
511 console = vc->vc_num;
512
513 tty_lock();
514
515 if (!vc_cons_allocated(console)) { /* impossible? */
516 ret = -ENOIOCTLCMD;
517 goto out;
518 }
519
520
521 /*
522 * To have permissions to do most of the vt ioctls, we either have
523 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
524 */
525 perm = 0;
526 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
527 perm = 1;
528
529 kbd = kbd_table + console;
530 switch (cmd) {
531 case TIOCLINUX:
532 ret = tioclinux(tty, arg);
533 break;
534 case KIOCSOUND:
535 if (!perm)
536 goto eperm;
537 /*
538 * The use of PIT_TICK_RATE is historic, it used to be
539 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
540 * and 2.6.36, which was a minor but unfortunate ABI
541 * change.
542 */
543 if (arg)
544 arg = PIT_TICK_RATE / arg;
545 kd_mksound(arg, 0);
546 break;
547
548 case KDMKTONE:
549 if (!perm)
550 goto eperm;
551 {
552 unsigned int ticks, count;
553
554 /*
555 * Generate the tone for the appropriate number of ticks.
556 * If the time is zero, turn off sound ourselves.
557 */
558 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
559 count = ticks ? (arg & 0xffff) : 0;
560 if (count)
561 count = PIT_TICK_RATE / count;
562 kd_mksound(count, ticks);
563 break;
564 }
565
566 case KDGKBTYPE:
567 /*
568 * this is naive.
569 */
570 ucval = KB_101;
571 goto setchar;
572
573 /*
574 * These cannot be implemented on any machine that implements
575 * ioperm() in user level (such as Alpha PCs) or not at all.
576 *
577 * XXX: you should never use these, just call ioperm directly..
578 */
579 #ifdef CONFIG_X86
580 case KDADDIO:
581 case KDDELIO:
582 /*
583 * KDADDIO and KDDELIO may be able to add ports beyond what
584 * we reject here, but to be safe...
585 */
586 if (arg < GPFIRST || arg > GPLAST) {
587 ret = -EINVAL;
588 break;
589 }
590 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
591 break;
592
593 case KDENABIO:
594 case KDDISABIO:
595 ret = sys_ioperm(GPFIRST, GPNUM,
596 (cmd == KDENABIO)) ? -ENXIO : 0;
597 break;
598 #endif
599
600 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
601
602 case KDKBDREP:
603 {
604 struct kbd_repeat kbrep;
605
606 if (!capable(CAP_SYS_TTY_CONFIG))
607 goto eperm;
608
609 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
610 ret = -EFAULT;
611 break;
612 }
613 ret = kbd_rate(&kbrep);
614 if (ret)
615 break;
616 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
617 ret = -EFAULT;
618 break;
619 }
620
621 case KDSETMODE:
622 /*
623 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
624 * doesn't do a whole lot. i'm not sure if it should do any
625 * restoration of modes or what...
626 *
627 * XXX It should at least call into the driver, fbdev's definitely
628 * need to restore their engine state. --BenH
629 */
630 if (!perm)
631 goto eperm;
632 switch (arg) {
633 case KD_GRAPHICS:
634 break;
635 case KD_TEXT0:
636 case KD_TEXT1:
637 arg = KD_TEXT;
638 case KD_TEXT:
639 break;
640 default:
641 ret = -EINVAL;
642 goto out;
643 }
644 if (vc->vc_mode == (unsigned char) arg)
645 break;
646 vc->vc_mode = (unsigned char) arg;
647 if (console != fg_console)
648 break;
649 /*
650 * explicitly blank/unblank the screen if switching modes
651 */
652 console_lock();
653 if (arg == KD_TEXT)
654 do_unblank_screen(1);
655 else
656 do_blank_screen(1);
657 console_unlock();
658 break;
659
660 case KDGETMODE:
661 uival = vc->vc_mode;
662 goto setint;
663
664 case KDMAPDISP:
665 case KDUNMAPDISP:
666 /*
667 * these work like a combination of mmap and KDENABIO.
668 * this could be easily finished.
669 */
670 ret = -EINVAL;
671 break;
672
673 case KDSKBMODE:
674 if (!perm)
675 goto eperm;
676 switch(arg) {
677 case K_RAW:
678 kbd->kbdmode = VC_RAW;
679 break;
680 case K_MEDIUMRAW:
681 kbd->kbdmode = VC_MEDIUMRAW;
682 break;
683 case K_XLATE:
684 kbd->kbdmode = VC_XLATE;
685 compute_shiftstate();
686 break;
687 case K_UNICODE:
688 kbd->kbdmode = VC_UNICODE;
689 compute_shiftstate();
690 break;
691 case K_OFF:
692 kbd->kbdmode = VC_OFF;
693 break;
694 default:
695 ret = -EINVAL;
696 goto out;
697 }
698 tty_ldisc_flush(tty);
699 break;
700
701 case KDGKBMODE:
702 uival = ((kbd->kbdmode == VC_RAW) ? K_RAW :
703 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
704 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
705 K_XLATE);
706 goto setint;
707
708 /* this could be folded into KDSKBMODE, but for compatibility
709 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
710 case KDSKBMETA:
711 switch(arg) {
712 case K_METABIT:
713 clr_vc_kbd_mode(kbd, VC_META);
714 break;
715 case K_ESCPREFIX:
716 set_vc_kbd_mode(kbd, VC_META);
717 break;
718 default:
719 ret = -EINVAL;
720 }
721 break;
722
723 case KDGKBMETA:
724 uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
725 setint:
726 ret = put_user(uival, (int __user *)arg);
727 break;
728
729 case KDGETKEYCODE:
730 case KDSETKEYCODE:
731 if(!capable(CAP_SYS_TTY_CONFIG))
732 perm = 0;
733 ret = do_kbkeycode_ioctl(cmd, up, perm);
734 break;
735
736 case KDGKBENT:
737 case KDSKBENT:
738 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
739 break;
740
741 case KDGKBSENT:
742 case KDSKBSENT:
743 ret = do_kdgkb_ioctl(cmd, up, perm);
744 break;
745
746 case KDGKBDIACR:
747 {
748 struct kbdiacrs __user *a = up;
749 struct kbdiacr diacr;
750 int i;
751
752 if (put_user(accent_table_size, &a->kb_cnt)) {
753 ret = -EFAULT;
754 break;
755 }
756 for (i = 0; i < accent_table_size; i++) {
757 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
758 diacr.base = conv_uni_to_8bit(accent_table[i].base);
759 diacr.result = conv_uni_to_8bit(accent_table[i].result);
760 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
761 ret = -EFAULT;
762 break;
763 }
764 }
765 break;
766 }
767 case KDGKBDIACRUC:
768 {
769 struct kbdiacrsuc __user *a = up;
770
771 if (put_user(accent_table_size, &a->kb_cnt))
772 ret = -EFAULT;
773 else if (copy_to_user(a->kbdiacruc, accent_table,
774 accent_table_size*sizeof(struct kbdiacruc)))
775 ret = -EFAULT;
776 break;
777 }
778
779 case KDSKBDIACR:
780 {
781 struct kbdiacrs __user *a = up;
782 struct kbdiacr diacr;
783 unsigned int ct;
784 int i;
785
786 if (!perm)
787 goto eperm;
788 if (get_user(ct,&a->kb_cnt)) {
789 ret = -EFAULT;
790 break;
791 }
792 if (ct >= MAX_DIACR) {
793 ret = -EINVAL;
794 break;
795 }
796 accent_table_size = ct;
797 for (i = 0; i < ct; i++) {
798 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
799 ret = -EFAULT;
800 break;
801 }
802 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
803 accent_table[i].base = conv_8bit_to_uni(diacr.base);
804 accent_table[i].result = conv_8bit_to_uni(diacr.result);
805 }
806 break;
807 }
808
809 case KDSKBDIACRUC:
810 {
811 struct kbdiacrsuc __user *a = up;
812 unsigned int ct;
813
814 if (!perm)
815 goto eperm;
816 if (get_user(ct,&a->kb_cnt)) {
817 ret = -EFAULT;
818 break;
819 }
820 if (ct >= MAX_DIACR) {
821 ret = -EINVAL;
822 break;
823 }
824 accent_table_size = ct;
825 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
826 ret = -EFAULT;
827 break;
828 }
829
830 /* the ioctls below read/set the flags usually shown in the leds */
831 /* don't use them - they will go away without warning */
832 case KDGKBLED:
833 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
834 goto setchar;
835
836 case KDSKBLED:
837 if (!perm)
838 goto eperm;
839 if (arg & ~0x77) {
840 ret = -EINVAL;
841 break;
842 }
843 kbd->ledflagstate = (arg & 7);
844 kbd->default_ledflagstate = ((arg >> 4) & 7);
845 set_leds();
846 break;
847
848 /* the ioctls below only set the lights, not the functions */
849 /* for those, see KDGKBLED and KDSKBLED above */
850 case KDGETLED:
851 ucval = getledstate();
852 setchar:
853 ret = put_user(ucval, (char __user *)arg);
854 break;
855
856 case KDSETLED:
857 if (!perm)
858 goto eperm;
859 setledstate(kbd, arg);
860 break;
861
862 /*
863 * A process can indicate its willingness to accept signals
864 * generated by pressing an appropriate key combination.
865 * Thus, one can have a daemon that e.g. spawns a new console
866 * upon a keypress and then changes to it.
867 * See also the kbrequest field of inittab(5).
868 */
869 case KDSIGACCEPT:
870 {
871 if (!perm || !capable(CAP_KILL))
872 goto eperm;
873 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
874 ret = -EINVAL;
875 else {
876 spin_lock_irq(&vt_spawn_con.lock);
877 put_pid(vt_spawn_con.pid);
878 vt_spawn_con.pid = get_pid(task_pid(current));
879 vt_spawn_con.sig = arg;
880 spin_unlock_irq(&vt_spawn_con.lock);
881 }
882 break;
883 }
884
885 case VT_SETMODE:
886 {
887 struct vt_mode tmp;
888
889 if (!perm)
890 goto eperm;
891 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
892 ret = -EFAULT;
893 goto out;
894 }
895 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
896 ret = -EINVAL;
897 goto out;
898 }
899 console_lock();
900 vc->vt_mode = tmp;
901 /* the frsig is ignored, so we set it to 0 */
902 vc->vt_mode.frsig = 0;
903 put_pid(vc->vt_pid);
904 vc->vt_pid = get_pid(task_pid(current));
905 /* no switch is required -- saw@shade.msu.ru */
906 vc->vt_newvt = -1;
907 console_unlock();
908 break;
909 }
910
911 case VT_GETMODE:
912 {
913 struct vt_mode tmp;
914 int rc;
915
916 console_lock();
917 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
918 console_unlock();
919
920 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
921 if (rc)
922 ret = -EFAULT;
923 break;
924 }
925
926 /*
927 * Returns global vt state. Note that VT 0 is always open, since
928 * it's an alias for the current VT, and people can't use it here.
929 * We cannot return state for more than 16 VTs, since v_state is short.
930 */
931 case VT_GETSTATE:
932 {
933 struct vt_stat __user *vtstat = up;
934 unsigned short state, mask;
935
936 if (put_user(fg_console + 1, &vtstat->v_active))
937 ret = -EFAULT;
938 else {
939 state = 1; /* /dev/tty0 is always open */
940 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
941 ++i, mask <<= 1)
942 if (VT_IS_IN_USE(i))
943 state |= mask;
944 ret = put_user(state, &vtstat->v_state);
945 }
946 break;
947 }
948
949 /*
950 * Returns the first available (non-opened) console.
951 */
952 case VT_OPENQRY:
953 for (i = 0; i < MAX_NR_CONSOLES; ++i)
954 if (! VT_IS_IN_USE(i))
955 break;
956 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
957 goto setint;
958
959 /*
960 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
961 * with num >= 1 (switches to vt 0, our console, are not allowed, just
962 * to preserve sanity).
963 */
964 case VT_ACTIVATE:
965 if (!perm)
966 goto eperm;
967 if (arg == 0 || arg > MAX_NR_CONSOLES)
968 ret = -ENXIO;
969 else {
970 arg--;
971 console_lock();
972 ret = vc_allocate(arg);
973 console_unlock();
974 if (ret)
975 break;
976 set_console(arg);
977 }
978 break;
979
980 case VT_SETACTIVATE:
981 {
982 struct vt_setactivate vsa;
983
984 if (!perm)
985 goto eperm;
986
987 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
988 sizeof(struct vt_setactivate))) {
989 ret = -EFAULT;
990 goto out;
991 }
992 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
993 ret = -ENXIO;
994 else {
995 vsa.console--;
996 console_lock();
997 ret = vc_allocate(vsa.console);
998 if (ret == 0) {
999 struct vc_data *nvc;
1000 /* This is safe providing we don't drop the
1001 console sem between vc_allocate and
1002 finishing referencing nvc */
1003 nvc = vc_cons[vsa.console].d;
1004 nvc->vt_mode = vsa.mode;
1005 nvc->vt_mode.frsig = 0;
1006 put_pid(nvc->vt_pid);
1007 nvc->vt_pid = get_pid(task_pid(current));
1008 }
1009 console_unlock();
1010 if (ret)
1011 break;
1012 /* Commence switch and lock */
1013 set_console(vsa.console);
1014 }
1015 break;
1016 }
1017
1018 /*
1019 * wait until the specified VT has been activated
1020 */
1021 case VT_WAITACTIVE:
1022 if (!perm)
1023 goto eperm;
1024 if (arg == 0 || arg > MAX_NR_CONSOLES)
1025 ret = -ENXIO;
1026 else
1027 ret = vt_waitactive(arg);
1028 break;
1029
1030 /*
1031 * If a vt is under process control, the kernel will not switch to it
1032 * immediately, but postpone the operation until the process calls this
1033 * ioctl, allowing the switch to complete.
1034 *
1035 * According to the X sources this is the behavior:
1036 * 0: pending switch-from not OK
1037 * 1: pending switch-from OK
1038 * 2: completed switch-to OK
1039 */
1040 case VT_RELDISP:
1041 if (!perm)
1042 goto eperm;
1043
1044 if (vc->vt_mode.mode != VT_PROCESS) {
1045 ret = -EINVAL;
1046 break;
1047 }
1048 /*
1049 * Switching-from response
1050 */
1051 console_lock();
1052 if (vc->vt_newvt >= 0) {
1053 if (arg == 0)
1054 /*
1055 * Switch disallowed, so forget we were trying
1056 * to do it.
1057 */
1058 vc->vt_newvt = -1;
1059
1060 else {
1061 /*
1062 * The current vt has been released, so
1063 * complete the switch.
1064 */
1065 int newvt;
1066 newvt = vc->vt_newvt;
1067 vc->vt_newvt = -1;
1068 ret = vc_allocate(newvt);
1069 if (ret) {
1070 console_unlock();
1071 break;
1072 }
1073 /*
1074 * When we actually do the console switch,
1075 * make sure we are atomic with respect to
1076 * other console switches..
1077 */
1078 complete_change_console(vc_cons[newvt].d);
1079 }
1080 } else {
1081 /*
1082 * Switched-to response
1083 */
1084 /*
1085 * If it's just an ACK, ignore it
1086 */
1087 if (arg != VT_ACKACQ)
1088 ret = -EINVAL;
1089 }
1090 console_unlock();
1091 break;
1092
1093 /*
1094 * Disallocate memory associated to VT (but leave VT1)
1095 */
1096 case VT_DISALLOCATE:
1097 if (arg > MAX_NR_CONSOLES) {
1098 ret = -ENXIO;
1099 break;
1100 }
1101 if (arg == 0) {
1102 /* deallocate all unused consoles, but leave 0 */
1103 console_lock();
1104 for (i=1; i<MAX_NR_CONSOLES; i++)
1105 if (! VT_BUSY(i))
1106 vc_deallocate(i);
1107 console_unlock();
1108 } else {
1109 /* deallocate a single console, if possible */
1110 arg--;
1111 if (VT_BUSY(arg))
1112 ret = -EBUSY;
1113 else if (arg) { /* leave 0 */
1114 console_lock();
1115 vc_deallocate(arg);
1116 console_unlock();
1117 }
1118 }
1119 break;
1120
1121 case VT_RESIZE:
1122 {
1123 struct vt_sizes __user *vtsizes = up;
1124 struct vc_data *vc;
1125
1126 ushort ll,cc;
1127 if (!perm)
1128 goto eperm;
1129 if (get_user(ll, &vtsizes->v_rows) ||
1130 get_user(cc, &vtsizes->v_cols))
1131 ret = -EFAULT;
1132 else {
1133 console_lock();
1134 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1135 vc = vc_cons[i].d;
1136
1137 if (vc) {
1138 vc->vc_resize_user = 1;
1139 vc_resize(vc_cons[i].d, cc, ll);
1140 }
1141 }
1142 console_unlock();
1143 }
1144 break;
1145 }
1146
1147 case VT_RESIZEX:
1148 {
1149 struct vt_consize __user *vtconsize = up;
1150 ushort ll,cc,vlin,clin,vcol,ccol;
1151 if (!perm)
1152 goto eperm;
1153 if (!access_ok(VERIFY_READ, vtconsize,
1154 sizeof(struct vt_consize))) {
1155 ret = -EFAULT;
1156 break;
1157 }
1158 /* FIXME: Should check the copies properly */
1159 __get_user(ll, &vtconsize->v_rows);
1160 __get_user(cc, &vtconsize->v_cols);
1161 __get_user(vlin, &vtconsize->v_vlin);
1162 __get_user(clin, &vtconsize->v_clin);
1163 __get_user(vcol, &vtconsize->v_vcol);
1164 __get_user(ccol, &vtconsize->v_ccol);
1165 vlin = vlin ? vlin : vc->vc_scan_lines;
1166 if (clin) {
1167 if (ll) {
1168 if (ll != vlin/clin) {
1169 /* Parameters don't add up */
1170 ret = -EINVAL;
1171 break;
1172 }
1173 } else
1174 ll = vlin/clin;
1175 }
1176 if (vcol && ccol) {
1177 if (cc) {
1178 if (cc != vcol/ccol) {
1179 ret = -EINVAL;
1180 break;
1181 }
1182 } else
1183 cc = vcol/ccol;
1184 }
1185
1186 if (clin > 32) {
1187 ret = -EINVAL;
1188 break;
1189 }
1190
1191 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1192 if (!vc_cons[i].d)
1193 continue;
1194 console_lock();
1195 if (vlin)
1196 vc_cons[i].d->vc_scan_lines = vlin;
1197 if (clin)
1198 vc_cons[i].d->vc_font.height = clin;
1199 vc_cons[i].d->vc_resize_user = 1;
1200 vc_resize(vc_cons[i].d, cc, ll);
1201 console_unlock();
1202 }
1203 break;
1204 }
1205
1206 case PIO_FONT: {
1207 if (!perm)
1208 goto eperm;
1209 op.op = KD_FONT_OP_SET;
1210 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1211 op.width = 8;
1212 op.height = 0;
1213 op.charcount = 256;
1214 op.data = up;
1215 ret = con_font_op(vc_cons[fg_console].d, &op);
1216 break;
1217 }
1218
1219 case GIO_FONT: {
1220 op.op = KD_FONT_OP_GET;
1221 op.flags = KD_FONT_FLAG_OLD;
1222 op.width = 8;
1223 op.height = 32;
1224 op.charcount = 256;
1225 op.data = up;
1226 ret = con_font_op(vc_cons[fg_console].d, &op);
1227 break;
1228 }
1229
1230 case PIO_CMAP:
1231 if (!perm)
1232 ret = -EPERM;
1233 else
1234 ret = con_set_cmap(up);
1235 break;
1236
1237 case GIO_CMAP:
1238 ret = con_get_cmap(up);
1239 break;
1240
1241 case PIO_FONTX:
1242 case GIO_FONTX:
1243 ret = do_fontx_ioctl(cmd, up, perm, &op);
1244 break;
1245
1246 case PIO_FONTRESET:
1247 {
1248 if (!perm)
1249 goto eperm;
1250
1251 #ifdef BROKEN_GRAPHICS_PROGRAMS
1252 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1253 font is not saved. */
1254 ret = -ENOSYS;
1255 break;
1256 #else
1257 {
1258 op.op = KD_FONT_OP_SET_DEFAULT;
1259 op.data = NULL;
1260 ret = con_font_op(vc_cons[fg_console].d, &op);
1261 if (ret)
1262 break;
1263 con_set_default_unimap(vc_cons[fg_console].d);
1264 break;
1265 }
1266 #endif
1267 }
1268
1269 case KDFONTOP: {
1270 if (copy_from_user(&op, up, sizeof(op))) {
1271 ret = -EFAULT;
1272 break;
1273 }
1274 if (!perm && op.op != KD_FONT_OP_GET)
1275 goto eperm;
1276 ret = con_font_op(vc, &op);
1277 if (ret)
1278 break;
1279 if (copy_to_user(up, &op, sizeof(op)))
1280 ret = -EFAULT;
1281 break;
1282 }
1283
1284 case PIO_SCRNMAP:
1285 if (!perm)
1286 ret = -EPERM;
1287 else
1288 ret = con_set_trans_old(up);
1289 break;
1290
1291 case GIO_SCRNMAP:
1292 ret = con_get_trans_old(up);
1293 break;
1294
1295 case PIO_UNISCRNMAP:
1296 if (!perm)
1297 ret = -EPERM;
1298 else
1299 ret = con_set_trans_new(up);
1300 break;
1301
1302 case GIO_UNISCRNMAP:
1303 ret = con_get_trans_new(up);
1304 break;
1305
1306 case PIO_UNIMAPCLR:
1307 { struct unimapinit ui;
1308 if (!perm)
1309 goto eperm;
1310 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1311 if (ret)
1312 ret = -EFAULT;
1313 else
1314 con_clear_unimap(vc, &ui);
1315 break;
1316 }
1317
1318 case PIO_UNIMAP:
1319 case GIO_UNIMAP:
1320 ret = do_unimap_ioctl(cmd, up, perm, vc);
1321 break;
1322
1323 case VT_LOCKSWITCH:
1324 if (!capable(CAP_SYS_TTY_CONFIG))
1325 goto eperm;
1326 vt_dont_switch = 1;
1327 break;
1328 case VT_UNLOCKSWITCH:
1329 if (!capable(CAP_SYS_TTY_CONFIG))
1330 goto eperm;
1331 vt_dont_switch = 0;
1332 break;
1333 case VT_GETHIFONTMASK:
1334 ret = put_user(vc->vc_hi_font_mask,
1335 (unsigned short __user *)arg);
1336 break;
1337 case VT_WAITEVENT:
1338 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1339 break;
1340 default:
1341 ret = -ENOIOCTLCMD;
1342 }
1343 out:
1344 tty_unlock();
1345 return ret;
1346 eperm:
1347 ret = -EPERM;
1348 goto out;
1349 }
1350
1351 void reset_vc(struct vc_data *vc)
1352 {
1353 vc->vc_mode = KD_TEXT;
1354 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1355 vc->vt_mode.mode = VT_AUTO;
1356 vc->vt_mode.waitv = 0;
1357 vc->vt_mode.relsig = 0;
1358 vc->vt_mode.acqsig = 0;
1359 vc->vt_mode.frsig = 0;
1360 put_pid(vc->vt_pid);
1361 vc->vt_pid = NULL;
1362 vc->vt_newvt = -1;
1363 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1364 reset_palette(vc);
1365 }
1366
1367 void vc_SAK(struct work_struct *work)
1368 {
1369 struct vc *vc_con =
1370 container_of(work, struct vc, SAK_work);
1371 struct vc_data *vc;
1372 struct tty_struct *tty;
1373
1374 console_lock();
1375 vc = vc_con->d;
1376 if (vc) {
1377 tty = vc->port.tty;
1378 /*
1379 * SAK should also work in all raw modes and reset
1380 * them properly.
1381 */
1382 if (tty)
1383 __do_SAK(tty);
1384 reset_vc(vc);
1385 }
1386 console_unlock();
1387 }
1388
1389 #ifdef CONFIG_COMPAT
1390
1391 struct compat_consolefontdesc {
1392 unsigned short charcount; /* characters in font (256 or 512) */
1393 unsigned short charheight; /* scan lines per character (1-32) */
1394 compat_caddr_t chardata; /* font data in expanded form */
1395 };
1396
1397 static inline int
1398 compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1399 int perm, struct console_font_op *op)
1400 {
1401 struct compat_consolefontdesc cfdarg;
1402 int i;
1403
1404 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1405 return -EFAULT;
1406
1407 switch (cmd) {
1408 case PIO_FONTX:
1409 if (!perm)
1410 return -EPERM;
1411 op->op = KD_FONT_OP_SET;
1412 op->flags = KD_FONT_FLAG_OLD;
1413 op->width = 8;
1414 op->height = cfdarg.charheight;
1415 op->charcount = cfdarg.charcount;
1416 op->data = compat_ptr(cfdarg.chardata);
1417 return con_font_op(vc_cons[fg_console].d, op);
1418 case GIO_FONTX:
1419 op->op = KD_FONT_OP_GET;
1420 op->flags = KD_FONT_FLAG_OLD;
1421 op->width = 8;
1422 op->height = cfdarg.charheight;
1423 op->charcount = cfdarg.charcount;
1424 op->data = compat_ptr(cfdarg.chardata);
1425 i = con_font_op(vc_cons[fg_console].d, op);
1426 if (i)
1427 return i;
1428 cfdarg.charheight = op->height;
1429 cfdarg.charcount = op->charcount;
1430 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1431 return -EFAULT;
1432 return 0;
1433 }
1434 return -EINVAL;
1435 }
1436
1437 struct compat_console_font_op {
1438 compat_uint_t op; /* operation code KD_FONT_OP_* */
1439 compat_uint_t flags; /* KD_FONT_FLAG_* */
1440 compat_uint_t width, height; /* font size */
1441 compat_uint_t charcount;
1442 compat_caddr_t data; /* font data with height fixed to 32 */
1443 };
1444
1445 static inline int
1446 compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1447 int perm, struct console_font_op *op, struct vc_data *vc)
1448 {
1449 int i;
1450
1451 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1452 return -EFAULT;
1453 if (!perm && op->op != KD_FONT_OP_GET)
1454 return -EPERM;
1455 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1456 op->flags |= KD_FONT_FLAG_OLD;
1457 i = con_font_op(vc, op);
1458 if (i)
1459 return i;
1460 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1461 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1462 return -EFAULT;
1463 return 0;
1464 }
1465
1466 struct compat_unimapdesc {
1467 unsigned short entry_ct;
1468 compat_caddr_t entries;
1469 };
1470
1471 static inline int
1472 compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1473 int perm, struct vc_data *vc)
1474 {
1475 struct compat_unimapdesc tmp;
1476 struct unipair __user *tmp_entries;
1477
1478 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1479 return -EFAULT;
1480 tmp_entries = compat_ptr(tmp.entries);
1481 if (tmp_entries)
1482 if (!access_ok(VERIFY_WRITE, tmp_entries,
1483 tmp.entry_ct*sizeof(struct unipair)))
1484 return -EFAULT;
1485 switch (cmd) {
1486 case PIO_UNIMAP:
1487 if (!perm)
1488 return -EPERM;
1489 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1490 case GIO_UNIMAP:
1491 if (!perm && fg_console != vc->vc_num)
1492 return -EPERM;
1493 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1494 }
1495 return 0;
1496 }
1497
1498 long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
1499 unsigned int cmd, unsigned long arg)
1500 {
1501 struct vc_data *vc = tty->driver_data;
1502 struct console_font_op op; /* used in multiple places here */
1503 struct kbd_struct *kbd;
1504 unsigned int console;
1505 void __user *up = (void __user *)arg;
1506 int perm;
1507 int ret = 0;
1508
1509 console = vc->vc_num;
1510
1511 tty_lock();
1512
1513 if (!vc_cons_allocated(console)) { /* impossible? */
1514 ret = -ENOIOCTLCMD;
1515 goto out;
1516 }
1517
1518 /*
1519 * To have permissions to do most of the vt ioctls, we either have
1520 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1521 */
1522 perm = 0;
1523 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1524 perm = 1;
1525
1526 kbd = kbd_table + console;
1527 switch (cmd) {
1528 /*
1529 * these need special handlers for incompatible data structures
1530 */
1531 case PIO_FONTX:
1532 case GIO_FONTX:
1533 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1534 break;
1535
1536 case KDFONTOP:
1537 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1538 break;
1539
1540 case PIO_UNIMAP:
1541 case GIO_UNIMAP:
1542 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1543 break;
1544
1545 /*
1546 * all these treat 'arg' as an integer
1547 */
1548 case KIOCSOUND:
1549 case KDMKTONE:
1550 #ifdef CONFIG_X86
1551 case KDADDIO:
1552 case KDDELIO:
1553 #endif
1554 case KDSETMODE:
1555 case KDMAPDISP:
1556 case KDUNMAPDISP:
1557 case KDSKBMODE:
1558 case KDSKBMETA:
1559 case KDSKBLED:
1560 case KDSETLED:
1561 case KDSIGACCEPT:
1562 case VT_ACTIVATE:
1563 case VT_WAITACTIVE:
1564 case VT_RELDISP:
1565 case VT_DISALLOCATE:
1566 case VT_RESIZE:
1567 case VT_RESIZEX:
1568 goto fallback;
1569
1570 /*
1571 * the rest has a compatible data structure behind arg,
1572 * but we have to convert it to a proper 64 bit pointer.
1573 */
1574 default:
1575 arg = (unsigned long)compat_ptr(arg);
1576 goto fallback;
1577 }
1578 out:
1579 tty_unlock();
1580 return ret;
1581
1582 fallback:
1583 tty_unlock();
1584 return vt_ioctl(tty, file, cmd, arg);
1585 }
1586
1587
1588 #endif /* CONFIG_COMPAT */
1589
1590
1591 /*
1592 * Performs the back end of a vt switch. Called under the console
1593 * semaphore.
1594 */
1595 static void complete_change_console(struct vc_data *vc)
1596 {
1597 unsigned char old_vc_mode;
1598 int old = fg_console;
1599
1600 last_console = fg_console;
1601
1602 /*
1603 * If we're switching, we could be going from KD_GRAPHICS to
1604 * KD_TEXT mode or vice versa, which means we need to blank or
1605 * unblank the screen later.
1606 */
1607 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1608 switch_screen(vc);
1609
1610 /*
1611 * This can't appear below a successful kill_pid(). If it did,
1612 * then the *blank_screen operation could occur while X, having
1613 * received acqsig, is waking up on another processor. This
1614 * condition can lead to overlapping accesses to the VGA range
1615 * and the framebuffer (causing system lockups).
1616 *
1617 * To account for this we duplicate this code below only if the
1618 * controlling process is gone and we've called reset_vc.
1619 */
1620 if (old_vc_mode != vc->vc_mode) {
1621 if (vc->vc_mode == KD_TEXT)
1622 do_unblank_screen(1);
1623 else
1624 do_blank_screen(1);
1625 }
1626
1627 /*
1628 * If this new console is under process control, send it a signal
1629 * telling it that it has acquired. Also check if it has died and
1630 * clean up (similar to logic employed in change_console())
1631 */
1632 if (vc->vt_mode.mode == VT_PROCESS) {
1633 /*
1634 * Send the signal as privileged - kill_pid() will
1635 * tell us if the process has gone or something else
1636 * is awry
1637 */
1638 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1639 /*
1640 * The controlling process has died, so we revert back to
1641 * normal operation. In this case, we'll also change back
1642 * to KD_TEXT mode. I'm not sure if this is strictly correct
1643 * but it saves the agony when the X server dies and the screen
1644 * remains blanked due to KD_GRAPHICS! It would be nice to do
1645 * this outside of VT_PROCESS but there is no single process
1646 * to account for and tracking tty count may be undesirable.
1647 */
1648 reset_vc(vc);
1649
1650 if (old_vc_mode != vc->vc_mode) {
1651 if (vc->vc_mode == KD_TEXT)
1652 do_unblank_screen(1);
1653 else
1654 do_blank_screen(1);
1655 }
1656 }
1657 }
1658
1659 /*
1660 * Wake anyone waiting for their VT to activate
1661 */
1662 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1663 return;
1664 }
1665
1666 /*
1667 * Performs the front-end of a vt switch
1668 */
1669 void change_console(struct vc_data *new_vc)
1670 {
1671 struct vc_data *vc;
1672
1673 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1674 return;
1675
1676 /*
1677 * If this vt is in process mode, then we need to handshake with
1678 * that process before switching. Essentially, we store where that
1679 * vt wants to switch to and wait for it to tell us when it's done
1680 * (via VT_RELDISP ioctl).
1681 *
1682 * We also check to see if the controlling process still exists.
1683 * If it doesn't, we reset this vt to auto mode and continue.
1684 * This is a cheap way to track process control. The worst thing
1685 * that can happen is: we send a signal to a process, it dies, and
1686 * the switch gets "lost" waiting for a response; hopefully, the
1687 * user will try again, we'll detect the process is gone (unless
1688 * the user waits just the right amount of time :-) and revert the
1689 * vt to auto control.
1690 */
1691 vc = vc_cons[fg_console].d;
1692 if (vc->vt_mode.mode == VT_PROCESS) {
1693 /*
1694 * Send the signal as privileged - kill_pid() will
1695 * tell us if the process has gone or something else
1696 * is awry.
1697 *
1698 * We need to set vt_newvt *before* sending the signal or we
1699 * have a race.
1700 */
1701 vc->vt_newvt = new_vc->vc_num;
1702 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1703 /*
1704 * It worked. Mark the vt to switch to and
1705 * return. The process needs to send us a
1706 * VT_RELDISP ioctl to complete the switch.
1707 */
1708 return;
1709 }
1710
1711 /*
1712 * The controlling process has died, so we revert back to
1713 * normal operation. In this case, we'll also change back
1714 * to KD_TEXT mode. I'm not sure if this is strictly correct
1715 * but it saves the agony when the X server dies and the screen
1716 * remains blanked due to KD_GRAPHICS! It would be nice to do
1717 * this outside of VT_PROCESS but there is no single process
1718 * to account for and tracking tty count may be undesirable.
1719 */
1720 reset_vc(vc);
1721
1722 /*
1723 * Fall through to normal (VT_AUTO) handling of the switch...
1724 */
1725 }
1726
1727 /*
1728 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1729 */
1730 if (vc->vc_mode == KD_GRAPHICS)
1731 return;
1732
1733 complete_change_console(new_vc);
1734 }
1735
1736 /* Perform a kernel triggered VT switch for suspend/resume */
1737
1738 static int disable_vt_switch;
1739
1740 int vt_move_to_console(unsigned int vt, int alloc)
1741 {
1742 int prev;
1743
1744 console_lock();
1745 /* Graphics mode - up to X */
1746 if (disable_vt_switch) {
1747 console_unlock();
1748 return 0;
1749 }
1750 prev = fg_console;
1751
1752 if (alloc && vc_allocate(vt)) {
1753 /* we can't have a free VC for now. Too bad,
1754 * we don't want to mess the screen for now. */
1755 console_unlock();
1756 return -ENOSPC;
1757 }
1758
1759 if (set_console(vt)) {
1760 /*
1761 * We're unable to switch to the SUSPEND_CONSOLE.
1762 * Let the calling function know so it can decide
1763 * what to do.
1764 */
1765 console_unlock();
1766 return -EIO;
1767 }
1768 console_unlock();
1769 tty_lock();
1770 if (vt_waitactive(vt + 1)) {
1771 pr_debug("Suspend: Can't switch VCs.");
1772 tty_unlock();
1773 return -EINTR;
1774 }
1775 tty_unlock();
1776 return prev;
1777 }
1778
1779 /*
1780 * Normally during a suspend, we allocate a new console and switch to it.
1781 * When we resume, we switch back to the original console. This switch
1782 * can be slow, so on systems where the framebuffer can handle restoration
1783 * of video registers anyways, there's little point in doing the console
1784 * switch. This function allows you to disable it by passing it '0'.
1785 */
1786 void pm_set_vt_switch(int do_switch)
1787 {
1788 console_lock();
1789 disable_vt_switch = !do_switch;
1790 console_unlock();
1791 }
1792 EXPORT_SYMBOL(pm_set_vt_switch);
This page took 0.113427 seconds and 4 git commands to generate.