| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/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 Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 26 | #include <linux/consolemap.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 27 | #include <linux/signal.h> | 
| Emmanuel Colbus | bcc8ca0 | 2005-06-28 20:44:49 -0700 | [diff] [blame] | 28 | #include <linux/timex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include <asm/io.h> | 
|  | 31 | #include <asm/uaccess.h> | 
|  | 32 |  | 
|  | 33 | #include <linux/kbd_kern.h> | 
|  | 34 | #include <linux/vt_kern.h> | 
|  | 35 | #include <linux/kbd_diacr.h> | 
|  | 36 | #include <linux/selection.h> | 
|  | 37 |  | 
| Andrew Johnson | b257bc0 | 2007-03-16 13:38:24 -0800 | [diff] [blame] | 38 | char vt_dont_switch; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | extern struct tty_driver *console_driver; | 
|  | 40 |  | 
|  | 41 | #define VT_IS_IN_USE(i)	(console_driver->ttys[i] && console_driver->ttys[i]->count) | 
|  | 42 | #define VT_BUSY(i)	(VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons) | 
|  | 43 |  | 
|  | 44 | /* | 
|  | 45 | * Console (vt and kd) routines, as defined by USL SVR4 manual, and by | 
|  | 46 | * experimentation and study of X386 SYSV handling. | 
|  | 47 | * | 
|  | 48 | * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and | 
|  | 49 | * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console, | 
|  | 50 | * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will | 
|  | 51 | * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to | 
|  | 52 | * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using | 
|  | 53 | * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing | 
|  | 54 | * to the current console is done by the main ioctl code. | 
|  | 55 | */ | 
|  | 56 |  | 
|  | 57 | #ifdef CONFIG_X86 | 
|  | 58 | #include <linux/syscalls.h> | 
|  | 59 | #endif | 
|  | 60 |  | 
|  | 61 | static void complete_change_console(struct vc_data *vc); | 
|  | 62 |  | 
|  | 63 | /* | 
|  | 64 | * these are the valid i/o ports we're allowed to change. they map all the | 
|  | 65 | * video ports | 
|  | 66 | */ | 
|  | 67 | #define GPFIRST 0x3b4 | 
|  | 68 | #define GPLAST 0x3df | 
|  | 69 | #define GPNUM (GPLAST - GPFIRST + 1) | 
|  | 70 |  | 
|  | 71 | #define i (tmp.kb_index) | 
|  | 72 | #define s (tmp.kb_table) | 
|  | 73 | #define v (tmp.kb_value) | 
|  | 74 | static inline int | 
|  | 75 | do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd) | 
|  | 76 | { | 
|  | 77 | struct kbentry tmp; | 
|  | 78 | ushort *key_map, val, ov; | 
|  | 79 |  | 
|  | 80 | if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) | 
|  | 81 | return -EFAULT; | 
|  | 82 |  | 
| Marcelo Tosatti | e3f17f0 | 2005-11-07 00:59:34 -0800 | [diff] [blame] | 83 | if (!capable(CAP_SYS_TTY_CONFIG)) | 
|  | 84 | perm = 0; | 
|  | 85 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | switch (cmd) { | 
|  | 87 | case KDGKBENT: | 
|  | 88 | key_map = key_maps[s]; | 
|  | 89 | if (key_map) { | 
|  | 90 | val = U(key_map[i]); | 
|  | 91 | if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) | 
|  | 92 | val = K_HOLE; | 
|  | 93 | } else | 
|  | 94 | val = (i ? K_HOLE : K_NOSUCHMAP); | 
|  | 95 | return put_user(val, &user_kbe->kb_value); | 
|  | 96 | case KDSKBENT: | 
|  | 97 | if (!perm) | 
|  | 98 | return -EPERM; | 
|  | 99 | if (!i && v == K_NOSUCHMAP) { | 
| Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 100 | /* deallocate map */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | key_map = key_maps[s]; | 
|  | 102 | if (s && key_map) { | 
|  | 103 | key_maps[s] = NULL; | 
|  | 104 | if (key_map[0] == U(K_ALLOCATED)) { | 
|  | 105 | kfree(key_map); | 
|  | 106 | keymap_count--; | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 | break; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | if (KTYP(v) < NR_TYPES) { | 
|  | 113 | if (KVAL(v) > max_vals[KTYP(v)]) | 
|  | 114 | return -EINVAL; | 
|  | 115 | } else | 
|  | 116 | if (kbd->kbdmode != VC_UNICODE) | 
|  | 117 | return -EINVAL; | 
|  | 118 |  | 
|  | 119 | /* ++Geert: non-PC keyboards may generate keycode zero */ | 
|  | 120 | #if !defined(__mc68000__) && !defined(__powerpc__) | 
|  | 121 | /* assignment to entry 0 only tests validity of args */ | 
|  | 122 | if (!i) | 
|  | 123 | break; | 
|  | 124 | #endif | 
|  | 125 |  | 
|  | 126 | if (!(key_map = key_maps[s])) { | 
|  | 127 | int j; | 
|  | 128 |  | 
|  | 129 | if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && | 
|  | 130 | !capable(CAP_SYS_RESOURCE)) | 
|  | 131 | return -EPERM; | 
|  | 132 |  | 
| Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 133 | key_map = kmalloc(sizeof(plain_map), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | GFP_KERNEL); | 
|  | 135 | if (!key_map) | 
|  | 136 | return -ENOMEM; | 
|  | 137 | key_maps[s] = key_map; | 
|  | 138 | key_map[0] = U(K_ALLOCATED); | 
|  | 139 | for (j = 1; j < NR_KEYS; j++) | 
|  | 140 | key_map[j] = U(K_HOLE); | 
|  | 141 | keymap_count++; | 
|  | 142 | } | 
|  | 143 | ov = U(key_map[i]); | 
|  | 144 | if (v == ov) | 
|  | 145 | break;	/* nothing to do */ | 
|  | 146 | /* | 
|  | 147 | * Attention Key. | 
|  | 148 | */ | 
|  | 149 | if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN)) | 
|  | 150 | return -EPERM; | 
|  | 151 | key_map[i] = U(v); | 
|  | 152 | if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT)) | 
|  | 153 | compute_shiftstate(); | 
|  | 154 | break; | 
|  | 155 | } | 
|  | 156 | return 0; | 
|  | 157 | } | 
|  | 158 | #undef i | 
|  | 159 | #undef s | 
|  | 160 | #undef v | 
|  | 161 |  | 
|  | 162 | static inline int | 
|  | 163 | do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm) | 
|  | 164 | { | 
|  | 165 | struct kbkeycode tmp; | 
|  | 166 | int kc = 0; | 
|  | 167 |  | 
|  | 168 | if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) | 
|  | 169 | return -EFAULT; | 
|  | 170 | switch (cmd) { | 
|  | 171 | case KDGETKEYCODE: | 
|  | 172 | kc = getkeycode(tmp.scancode); | 
|  | 173 | if (kc >= 0) | 
|  | 174 | kc = put_user(kc, &user_kbkc->keycode); | 
|  | 175 | break; | 
|  | 176 | case KDSETKEYCODE: | 
|  | 177 | if (!perm) | 
|  | 178 | return -EPERM; | 
|  | 179 | kc = setkeycode(tmp.scancode, tmp.keycode); | 
|  | 180 | break; | 
|  | 181 | } | 
|  | 182 | return kc; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | static inline int | 
|  | 186 | do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) | 
|  | 187 | { | 
|  | 188 | struct kbsentry *kbs; | 
|  | 189 | char *p; | 
|  | 190 | u_char *q; | 
|  | 191 | u_char __user *up; | 
|  | 192 | int sz; | 
|  | 193 | int delta; | 
|  | 194 | char *first_free, *fj, *fnw; | 
|  | 195 | int i, j, k; | 
|  | 196 | int ret; | 
|  | 197 |  | 
| Andrew Morton | 0b360ad | 2005-10-30 15:03:02 -0800 | [diff] [blame] | 198 | if (!capable(CAP_SYS_TTY_CONFIG)) | 
| Marcelo Tosatti | e3f17f0 | 2005-11-07 00:59:34 -0800 | [diff] [blame] | 199 | perm = 0; | 
| Andrew Morton | 0b360ad | 2005-10-30 15:03:02 -0800 | [diff] [blame] | 200 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | kbs = kmalloc(sizeof(*kbs), GFP_KERNEL); | 
|  | 202 | if (!kbs) { | 
|  | 203 | ret = -ENOMEM; | 
|  | 204 | goto reterr; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | /* we mostly copy too much here (512bytes), but who cares ;) */ | 
|  | 208 | if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) { | 
|  | 209 | ret = -EFAULT; | 
|  | 210 | goto reterr; | 
|  | 211 | } | 
|  | 212 | kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0'; | 
|  | 213 | i = kbs->kb_func; | 
|  | 214 |  | 
|  | 215 | switch (cmd) { | 
|  | 216 | case KDGKBSENT: | 
|  | 217 | sz = sizeof(kbs->kb_string) - 1; /* sz should have been | 
|  | 218 | a struct member */ | 
|  | 219 | up = user_kdgkb->kb_string; | 
|  | 220 | p = func_table[i]; | 
|  | 221 | if(p) | 
|  | 222 | for ( ; *p && sz; p++, sz--) | 
|  | 223 | if (put_user(*p, up++)) { | 
|  | 224 | ret = -EFAULT; | 
|  | 225 | goto reterr; | 
|  | 226 | } | 
|  | 227 | if (put_user('\0', up)) { | 
|  | 228 | ret = -EFAULT; | 
|  | 229 | goto reterr; | 
|  | 230 | } | 
|  | 231 | kfree(kbs); | 
|  | 232 | return ((p && *p) ? -EOVERFLOW : 0); | 
|  | 233 | case KDSKBSENT: | 
|  | 234 | if (!perm) { | 
|  | 235 | ret = -EPERM; | 
|  | 236 | goto reterr; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | q = func_table[i]; | 
|  | 240 | first_free = funcbufptr + (funcbufsize - funcbufleft); | 
|  | 241 | for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) | 
|  | 242 | ; | 
|  | 243 | if (j < MAX_NR_FUNC) | 
|  | 244 | fj = func_table[j]; | 
|  | 245 | else | 
|  | 246 | fj = first_free; | 
|  | 247 |  | 
|  | 248 | delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string); | 
|  | 249 | if (delta <= funcbufleft) { 	/* it fits in current buf */ | 
|  | 250 | if (j < MAX_NR_FUNC) { | 
|  | 251 | memmove(fj + delta, fj, first_free - fj); | 
|  | 252 | for (k = j; k < MAX_NR_FUNC; k++) | 
|  | 253 | if (func_table[k]) | 
|  | 254 | func_table[k] += delta; | 
|  | 255 | } | 
|  | 256 | if (!q) | 
|  | 257 | func_table[i] = fj; | 
|  | 258 | funcbufleft -= delta; | 
|  | 259 | } else {			/* allocate a larger buffer */ | 
|  | 260 | sz = 256; | 
|  | 261 | while (sz < funcbufsize - funcbufleft + delta) | 
|  | 262 | sz <<= 1; | 
| Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 263 | fnw = kmalloc(sz, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if(!fnw) { | 
|  | 265 | ret = -ENOMEM; | 
|  | 266 | goto reterr; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | if (!q) | 
|  | 270 | func_table[i] = fj; | 
|  | 271 | if (fj > funcbufptr) | 
|  | 272 | memmove(fnw, funcbufptr, fj - funcbufptr); | 
|  | 273 | for (k = 0; k < j; k++) | 
|  | 274 | if (func_table[k]) | 
|  | 275 | func_table[k] = fnw + (func_table[k] - funcbufptr); | 
|  | 276 |  | 
|  | 277 | if (first_free > fj) { | 
|  | 278 | memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj); | 
|  | 279 | for (k = j; k < MAX_NR_FUNC; k++) | 
|  | 280 | if (func_table[k]) | 
|  | 281 | func_table[k] = fnw + (func_table[k] - funcbufptr) + delta; | 
|  | 282 | } | 
|  | 283 | if (funcbufptr != func_buf) | 
|  | 284 | kfree(funcbufptr); | 
|  | 285 | funcbufptr = fnw; | 
|  | 286 | funcbufleft = funcbufleft - delta + sz - funcbufsize; | 
|  | 287 | funcbufsize = sz; | 
|  | 288 | } | 
|  | 289 | strcpy(func_table[i], kbs->kb_string); | 
|  | 290 | break; | 
|  | 291 | } | 
|  | 292 | ret = 0; | 
|  | 293 | reterr: | 
|  | 294 | kfree(kbs); | 
|  | 295 | return ret; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | static inline int | 
|  | 299 | do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op) | 
|  | 300 | { | 
|  | 301 | struct consolefontdesc cfdarg; | 
|  | 302 | int i; | 
|  | 303 |  | 
|  | 304 | if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) | 
|  | 305 | return -EFAULT; | 
|  | 306 |  | 
|  | 307 | switch (cmd) { | 
|  | 308 | case PIO_FONTX: | 
|  | 309 | if (!perm) | 
|  | 310 | return -EPERM; | 
|  | 311 | op->op = KD_FONT_OP_SET; | 
|  | 312 | op->flags = KD_FONT_FLAG_OLD; | 
|  | 313 | op->width = 8; | 
|  | 314 | op->height = cfdarg.charheight; | 
|  | 315 | op->charcount = cfdarg.charcount; | 
|  | 316 | op->data = cfdarg.chardata; | 
|  | 317 | return con_font_op(vc_cons[fg_console].d, op); | 
|  | 318 | case GIO_FONTX: { | 
|  | 319 | op->op = KD_FONT_OP_GET; | 
|  | 320 | op->flags = KD_FONT_FLAG_OLD; | 
|  | 321 | op->width = 8; | 
|  | 322 | op->height = cfdarg.charheight; | 
|  | 323 | op->charcount = cfdarg.charcount; | 
|  | 324 | op->data = cfdarg.chardata; | 
|  | 325 | i = con_font_op(vc_cons[fg_console].d, op); | 
|  | 326 | if (i) | 
|  | 327 | return i; | 
|  | 328 | cfdarg.charheight = op->height; | 
|  | 329 | cfdarg.charcount = op->charcount; | 
|  | 330 | if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc))) | 
|  | 331 | return -EFAULT; | 
|  | 332 | return 0; | 
|  | 333 | } | 
|  | 334 | } | 
|  | 335 | return -EINVAL; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | static inline int | 
|  | 339 | do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc) | 
|  | 340 | { | 
|  | 341 | struct unimapdesc tmp; | 
|  | 342 |  | 
|  | 343 | if (copy_from_user(&tmp, user_ud, sizeof tmp)) | 
|  | 344 | return -EFAULT; | 
|  | 345 | if (tmp.entries) | 
|  | 346 | if (!access_ok(VERIFY_WRITE, tmp.entries, | 
|  | 347 | tmp.entry_ct*sizeof(struct unipair))) | 
|  | 348 | return -EFAULT; | 
|  | 349 | switch (cmd) { | 
|  | 350 | case PIO_UNIMAP: | 
|  | 351 | if (!perm) | 
|  | 352 | return -EPERM; | 
|  | 353 | return con_set_unimap(vc, tmp.entry_ct, tmp.entries); | 
|  | 354 | case GIO_UNIMAP: | 
|  | 355 | if (!perm && fg_console != vc->vc_num) | 
|  | 356 | return -EPERM; | 
|  | 357 | return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries); | 
|  | 358 | } | 
|  | 359 | return 0; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | /* | 
|  | 363 | * We handle the console-specific ioctl's here.  We allow the | 
|  | 364 | * capability to modify any console, not just the fg_console. | 
|  | 365 | */ | 
|  | 366 | int vt_ioctl(struct tty_struct *tty, struct file * file, | 
|  | 367 | unsigned int cmd, unsigned long arg) | 
|  | 368 | { | 
|  | 369 | struct vc_data *vc = (struct vc_data *)tty->driver_data; | 
|  | 370 | struct console_font_op op;	/* used in multiple places here */ | 
|  | 371 | struct kbd_struct * kbd; | 
|  | 372 | unsigned int console; | 
|  | 373 | unsigned char ucval; | 
|  | 374 | void __user *up = (void __user *)arg; | 
|  | 375 | int i, perm; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 376 | int ret = 0; | 
|  | 377 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | console = vc->vc_num; | 
|  | 379 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 380 | lock_kernel(); | 
|  | 381 |  | 
|  | 382 | if (!vc_cons_allocated(console)) { 	/* impossible? */ | 
|  | 383 | ret = -ENOIOCTLCMD; | 
|  | 384 | goto out; | 
|  | 385 | } | 
|  | 386 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 |  | 
|  | 388 | /* | 
|  | 389 | * To have permissions to do most of the vt ioctls, we either have | 
|  | 390 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. | 
|  | 391 | */ | 
|  | 392 | perm = 0; | 
|  | 393 | if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG)) | 
|  | 394 | perm = 1; | 
|  | 395 |  | 
|  | 396 | kbd = kbd_table + console; | 
|  | 397 | switch (cmd) { | 
|  | 398 | case KIOCSOUND: | 
|  | 399 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 400 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (arg) | 
| Emmanuel Colbus | bcc8ca0 | 2005-06-28 20:44:49 -0700 | [diff] [blame] | 402 | arg = CLOCK_TICK_RATE / arg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | kd_mksound(arg, 0); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 404 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 |  | 
|  | 406 | case KDMKTONE: | 
|  | 407 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 408 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { | 
|  | 410 | unsigned int ticks, count; | 
|  | 411 |  | 
|  | 412 | /* | 
|  | 413 | * Generate the tone for the appropriate number of ticks. | 
|  | 414 | * If the time is zero, turn off sound ourselves. | 
|  | 415 | */ | 
|  | 416 | ticks = HZ * ((arg >> 16) & 0xffff) / 1000; | 
|  | 417 | count = ticks ? (arg & 0xffff) : 0; | 
|  | 418 | if (count) | 
| Emmanuel Colbus | bcc8ca0 | 2005-06-28 20:44:49 -0700 | [diff] [blame] | 419 | count = CLOCK_TICK_RATE / count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | kd_mksound(count, ticks); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 421 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } | 
|  | 423 |  | 
|  | 424 | case KDGKBTYPE: | 
|  | 425 | /* | 
|  | 426 | * this is naive. | 
|  | 427 | */ | 
|  | 428 | ucval = KB_101; | 
|  | 429 | goto setchar; | 
|  | 430 |  | 
|  | 431 | /* | 
|  | 432 | * These cannot be implemented on any machine that implements | 
|  | 433 | * ioperm() in user level (such as Alpha PCs) or not at all. | 
|  | 434 | * | 
|  | 435 | * XXX: you should never use these, just call ioperm directly.. | 
|  | 436 | */ | 
|  | 437 | #ifdef CONFIG_X86 | 
|  | 438 | case KDADDIO: | 
|  | 439 | case KDDELIO: | 
|  | 440 | /* | 
|  | 441 | * KDADDIO and KDDELIO may be able to add ports beyond what | 
|  | 442 | * we reject here, but to be safe... | 
|  | 443 | */ | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 444 | if (arg < GPFIRST || arg > GPLAST) { | 
|  | 445 | ret = -EINVAL; | 
|  | 446 | break; | 
|  | 447 | } | 
|  | 448 | ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0; | 
|  | 449 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 |  | 
|  | 451 | case KDENABIO: | 
|  | 452 | case KDDISABIO: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 453 | ret = sys_ioperm(GPFIRST, GPNUM, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | (cmd == KDENABIO)) ? -ENXIO : 0; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 455 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | #endif | 
|  | 457 |  | 
|  | 458 | /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */ | 
|  | 459 |  | 
|  | 460 | case KDKBDREP: | 
|  | 461 | { | 
|  | 462 | struct kbd_repeat kbrep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 |  | 
|  | 464 | if (!capable(CAP_SYS_TTY_CONFIG)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 465 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 467 | if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) { | 
|  | 468 | ret =  -EFAULT; | 
|  | 469 | break; | 
|  | 470 | } | 
|  | 471 | ret = kbd_rate(&kbrep); | 
|  | 472 | if (ret) | 
|  | 473 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat))) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 475 | ret = -EFAULT; | 
|  | 476 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } | 
|  | 478 |  | 
|  | 479 | case KDSETMODE: | 
|  | 480 | /* | 
|  | 481 | * currently, setting the mode from KD_TEXT to KD_GRAPHICS | 
|  | 482 | * doesn't do a whole lot. i'm not sure if it should do any | 
|  | 483 | * restoration of modes or what... | 
|  | 484 | * | 
|  | 485 | * XXX It should at least call into the driver, fbdev's definitely | 
|  | 486 | * need to restore their engine state. --BenH | 
|  | 487 | */ | 
|  | 488 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 489 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | switch (arg) { | 
|  | 491 | case KD_GRAPHICS: | 
|  | 492 | break; | 
|  | 493 | case KD_TEXT0: | 
|  | 494 | case KD_TEXT1: | 
|  | 495 | arg = KD_TEXT; | 
|  | 496 | case KD_TEXT: | 
|  | 497 | break; | 
|  | 498 | default: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 499 | ret = -EINVAL; | 
|  | 500 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } | 
|  | 502 | if (vc->vc_mode == (unsigned char) arg) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 503 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | vc->vc_mode = (unsigned char) arg; | 
|  | 505 | if (console != fg_console) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 506 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | /* | 
|  | 508 | * explicitly blank/unblank the screen if switching modes | 
|  | 509 | */ | 
|  | 510 | acquire_console_sem(); | 
|  | 511 | if (arg == KD_TEXT) | 
|  | 512 | do_unblank_screen(1); | 
|  | 513 | else | 
|  | 514 | do_blank_screen(1); | 
|  | 515 | release_console_sem(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 516 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 |  | 
|  | 518 | case KDGETMODE: | 
|  | 519 | ucval = vc->vc_mode; | 
|  | 520 | goto setint; | 
|  | 521 |  | 
|  | 522 | case KDMAPDISP: | 
|  | 523 | case KDUNMAPDISP: | 
|  | 524 | /* | 
|  | 525 | * these work like a combination of mmap and KDENABIO. | 
|  | 526 | * this could be easily finished. | 
|  | 527 | */ | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 528 | ret = -EINVAL; | 
|  | 529 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 |  | 
|  | 531 | case KDSKBMODE: | 
|  | 532 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 533 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | switch(arg) { | 
|  | 535 | case K_RAW: | 
|  | 536 | kbd->kbdmode = VC_RAW; | 
|  | 537 | break; | 
|  | 538 | case K_MEDIUMRAW: | 
|  | 539 | kbd->kbdmode = VC_MEDIUMRAW; | 
|  | 540 | break; | 
|  | 541 | case K_XLATE: | 
|  | 542 | kbd->kbdmode = VC_XLATE; | 
|  | 543 | compute_shiftstate(); | 
|  | 544 | break; | 
|  | 545 | case K_UNICODE: | 
|  | 546 | kbd->kbdmode = VC_UNICODE; | 
|  | 547 | compute_shiftstate(); | 
|  | 548 | break; | 
|  | 549 | default: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 550 | ret = -EINVAL; | 
|  | 551 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } | 
|  | 553 | tty_ldisc_flush(tty); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 554 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 |  | 
|  | 556 | case KDGKBMODE: | 
|  | 557 | ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW : | 
|  | 558 | (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW : | 
|  | 559 | (kbd->kbdmode == VC_UNICODE) ? K_UNICODE : | 
|  | 560 | K_XLATE); | 
|  | 561 | goto setint; | 
|  | 562 |  | 
|  | 563 | /* this could be folded into KDSKBMODE, but for compatibility | 
|  | 564 | reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */ | 
|  | 565 | case KDSKBMETA: | 
|  | 566 | switch(arg) { | 
|  | 567 | case K_METABIT: | 
|  | 568 | clr_vc_kbd_mode(kbd, VC_META); | 
|  | 569 | break; | 
|  | 570 | case K_ESCPREFIX: | 
|  | 571 | set_vc_kbd_mode(kbd, VC_META); | 
|  | 572 | break; | 
|  | 573 | default: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 574 | ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 576 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 |  | 
|  | 578 | case KDGKBMETA: | 
|  | 579 | ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT); | 
|  | 580 | setint: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 581 | ret = put_user(ucval, (int __user *)arg); | 
|  | 582 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 |  | 
|  | 584 | case KDGETKEYCODE: | 
|  | 585 | case KDSETKEYCODE: | 
|  | 586 | if(!capable(CAP_SYS_TTY_CONFIG)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 587 | perm = 0; | 
|  | 588 | ret = do_kbkeycode_ioctl(cmd, up, perm); | 
|  | 589 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 |  | 
|  | 591 | case KDGKBENT: | 
|  | 592 | case KDSKBENT: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 593 | ret = do_kdsk_ioctl(cmd, up, perm, kbd); | 
|  | 594 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 |  | 
|  | 596 | case KDGKBSENT: | 
|  | 597 | case KDSKBSENT: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 598 | ret = do_kdgkb_ioctl(cmd, up, perm); | 
|  | 599 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 |  | 
|  | 601 | case KDGKBDIACR: | 
|  | 602 | { | 
|  | 603 | struct kbdiacrs __user *a = up; | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 604 | struct kbdiacr diacr; | 
|  | 605 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 607 | if (put_user(accent_table_size, &a->kb_cnt)) { | 
|  | 608 | ret = -EFAULT; | 
|  | 609 | break; | 
|  | 610 | } | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 611 | for (i = 0; i < accent_table_size; i++) { | 
|  | 612 | diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr); | 
|  | 613 | diacr.base = conv_uni_to_8bit(accent_table[i].base); | 
|  | 614 | diacr.result = conv_uni_to_8bit(accent_table[i].result); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 615 | if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) { | 
|  | 616 | ret = -EFAULT; | 
|  | 617 | break; | 
|  | 618 | } | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 619 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 620 | break; | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 621 | } | 
|  | 622 | case KDGKBDIACRUC: | 
|  | 623 | { | 
|  | 624 | struct kbdiacrsuc __user *a = up; | 
|  | 625 |  | 
|  | 626 | if (put_user(accent_table_size, &a->kb_cnt)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 627 | ret = -EFAULT; | 
|  | 628 | else if (copy_to_user(a->kbdiacruc, accent_table, | 
|  | 629 | accent_table_size*sizeof(struct kbdiacruc))) | 
|  | 630 | ret = -EFAULT; | 
|  | 631 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } | 
|  | 633 |  | 
|  | 634 | case KDSKBDIACR: | 
|  | 635 | { | 
|  | 636 | struct kbdiacrs __user *a = up; | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 637 | struct kbdiacr diacr; | 
|  | 638 | unsigned int ct; | 
|  | 639 | int i; | 
|  | 640 |  | 
|  | 641 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 642 | goto eperm; | 
|  | 643 | if (get_user(ct,&a->kb_cnt)) { | 
|  | 644 | ret = -EFAULT; | 
|  | 645 | break; | 
|  | 646 | } | 
|  | 647 | if (ct >= MAX_DIACR) { | 
|  | 648 | ret = -EINVAL; | 
|  | 649 | break; | 
|  | 650 | } | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 651 | accent_table_size = ct; | 
|  | 652 | for (i = 0; i < ct; i++) { | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 653 | if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) { | 
|  | 654 | ret = -EFAULT; | 
|  | 655 | break; | 
|  | 656 | } | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 657 | accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr); | 
|  | 658 | accent_table[i].base = conv_8bit_to_uni(diacr.base); | 
|  | 659 | accent_table[i].result = conv_8bit_to_uni(diacr.result); | 
|  | 660 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 661 | break; | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 662 | } | 
|  | 663 |  | 
|  | 664 | case KDSKBDIACRUC: | 
|  | 665 | { | 
|  | 666 | struct kbdiacrsuc __user *a = up; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | unsigned int ct; | 
|  | 668 |  | 
|  | 669 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 670 | goto eperm; | 
|  | 671 | if (get_user(ct,&a->kb_cnt)) { | 
|  | 672 | ret = -EFAULT; | 
|  | 673 | break; | 
|  | 674 | } | 
|  | 675 | if (ct >= MAX_DIACR) { | 
|  | 676 | ret = -EINVAL; | 
|  | 677 | break; | 
|  | 678 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | accent_table_size = ct; | 
| Samuel Thibault | 04c7197 | 2007-10-16 23:27:04 -0700 | [diff] [blame] | 680 | if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc))) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 681 | ret = -EFAULT; | 
|  | 682 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } | 
|  | 684 |  | 
|  | 685 | /* the ioctls below read/set the flags usually shown in the leds */ | 
|  | 686 | /* don't use them - they will go away without warning */ | 
|  | 687 | case KDGKBLED: | 
|  | 688 | ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4); | 
|  | 689 | goto setchar; | 
|  | 690 |  | 
|  | 691 | case KDSKBLED: | 
|  | 692 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 693 | goto eperm; | 
|  | 694 | if (arg & ~0x77) { | 
|  | 695 | ret = -EINVAL; | 
|  | 696 | break; | 
|  | 697 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | kbd->ledflagstate = (arg & 7); | 
|  | 699 | kbd->default_ledflagstate = ((arg >> 4) & 7); | 
|  | 700 | set_leds(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 701 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 |  | 
|  | 703 | /* the ioctls below only set the lights, not the functions */ | 
|  | 704 | /* for those, see KDGKBLED and KDSKBLED above */ | 
|  | 705 | case KDGETLED: | 
|  | 706 | ucval = getledstate(); | 
|  | 707 | setchar: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 708 | ret = put_user(ucval, (char __user *)arg); | 
|  | 709 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 |  | 
|  | 711 | case KDSETLED: | 
|  | 712 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 713 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | setledstate(kbd, arg); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 715 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 |  | 
|  | 717 | /* | 
|  | 718 | * A process can indicate its willingness to accept signals | 
|  | 719 | * generated by pressing an appropriate key combination. | 
|  | 720 | * Thus, one can have a daemon that e.g. spawns a new console | 
|  | 721 | * upon a keypress and then changes to it. | 
|  | 722 | * See also the kbrequest field of inittab(5). | 
|  | 723 | */ | 
|  | 724 | case KDSIGACCEPT: | 
|  | 725 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (!perm || !capable(CAP_KILL)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 727 | goto eperm; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 728 | if (!valid_signal(arg) || arg < 1 || arg == SIGKILL) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 729 | ret = -EINVAL; | 
|  | 730 | else { | 
|  | 731 | spin_lock_irq(&vt_spawn_con.lock); | 
|  | 732 | put_pid(vt_spawn_con.pid); | 
|  | 733 | vt_spawn_con.pid = get_pid(task_pid(current)); | 
|  | 734 | vt_spawn_con.sig = arg; | 
|  | 735 | spin_unlock_irq(&vt_spawn_con.lock); | 
|  | 736 | } | 
|  | 737 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } | 
|  | 739 |  | 
|  | 740 | case VT_SETMODE: | 
|  | 741 | { | 
|  | 742 | struct vt_mode tmp; | 
|  | 743 |  | 
|  | 744 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 745 | goto eperm; | 
|  | 746 | if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) { | 
|  | 747 | ret = -EFAULT; | 
|  | 748 | goto out; | 
|  | 749 | } | 
|  | 750 | if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) { | 
|  | 751 | ret = -EINVAL; | 
|  | 752 | goto out; | 
|  | 753 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | acquire_console_sem(); | 
|  | 755 | vc->vt_mode = tmp; | 
|  | 756 | /* the frsig is ignored, so we set it to 0 */ | 
|  | 757 | vc->vt_mode.frsig = 0; | 
| Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 758 | put_pid(vc->vt_pid); | 
|  | 759 | vc->vt_pid = get_pid(task_pid(current)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | /* no switch is required -- saw@shade.msu.ru */ | 
|  | 761 | vc->vt_newvt = -1; | 
|  | 762 | release_console_sem(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 763 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } | 
|  | 765 |  | 
|  | 766 | case VT_GETMODE: | 
|  | 767 | { | 
|  | 768 | struct vt_mode tmp; | 
|  | 769 | int rc; | 
|  | 770 |  | 
|  | 771 | acquire_console_sem(); | 
|  | 772 | memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); | 
|  | 773 | release_console_sem(); | 
|  | 774 |  | 
|  | 775 | rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 776 | if (rc) | 
|  | 777 | ret = -EFAULT; | 
|  | 778 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } | 
|  | 780 |  | 
|  | 781 | /* | 
|  | 782 | * Returns global vt state. Note that VT 0 is always open, since | 
|  | 783 | * it's an alias for the current VT, and people can't use it here. | 
|  | 784 | * We cannot return state for more than 16 VTs, since v_state is short. | 
|  | 785 | */ | 
|  | 786 | case VT_GETSTATE: | 
|  | 787 | { | 
|  | 788 | struct vt_stat __user *vtstat = up; | 
|  | 789 | unsigned short state, mask; | 
|  | 790 |  | 
|  | 791 | if (put_user(fg_console + 1, &vtstat->v_active)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 792 | ret = -EFAULT; | 
|  | 793 | else { | 
|  | 794 | state = 1;	/* /dev/tty0 is always open */ | 
|  | 795 | for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; | 
|  | 796 | ++i, mask <<= 1) | 
|  | 797 | if (VT_IS_IN_USE(i)) | 
|  | 798 | state |= mask; | 
|  | 799 | ret = put_user(state, &vtstat->v_state); | 
|  | 800 | } | 
|  | 801 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
|  | 804 | /* | 
|  | 805 | * Returns the first available (non-opened) console. | 
|  | 806 | */ | 
|  | 807 | case VT_OPENQRY: | 
|  | 808 | for (i = 0; i < MAX_NR_CONSOLES; ++i) | 
|  | 809 | if (! VT_IS_IN_USE(i)) | 
|  | 810 | break; | 
|  | 811 | ucval = i < MAX_NR_CONSOLES ? (i+1) : -1; | 
|  | 812 | goto setint; | 
|  | 813 |  | 
|  | 814 | /* | 
|  | 815 | * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num, | 
|  | 816 | * with num >= 1 (switches to vt 0, our console, are not allowed, just | 
|  | 817 | * to preserve sanity). | 
|  | 818 | */ | 
|  | 819 | case VT_ACTIVATE: | 
|  | 820 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 821 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (arg == 0 || arg > MAX_NR_CONSOLES) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 823 | ret =  -ENXIO; | 
|  | 824 | else { | 
|  | 825 | arg--; | 
|  | 826 | acquire_console_sem(); | 
|  | 827 | ret = vc_allocate(arg); | 
|  | 828 | release_console_sem(); | 
|  | 829 | if (ret) | 
|  | 830 | break; | 
|  | 831 | set_console(arg); | 
|  | 832 | } | 
|  | 833 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 |  | 
|  | 835 | /* | 
|  | 836 | * wait until the specified VT has been activated | 
|  | 837 | */ | 
|  | 838 | case VT_WAITACTIVE: | 
|  | 839 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 840 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | if (arg == 0 || arg > MAX_NR_CONSOLES) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 842 | ret = -ENXIO; | 
|  | 843 | else | 
|  | 844 | ret = vt_waitactive(arg - 1); | 
|  | 845 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 |  | 
|  | 847 | /* | 
|  | 848 | * If a vt is under process control, the kernel will not switch to it | 
|  | 849 | * immediately, but postpone the operation until the process calls this | 
|  | 850 | * ioctl, allowing the switch to complete. | 
|  | 851 | * | 
|  | 852 | * According to the X sources this is the behavior: | 
|  | 853 | *	0:	pending switch-from not OK | 
|  | 854 | *	1:	pending switch-from OK | 
|  | 855 | *	2:	completed switch-to OK | 
|  | 856 | */ | 
|  | 857 | case VT_RELDISP: | 
|  | 858 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 859 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 861 | if (vc->vt_mode.mode != VT_PROCESS) { | 
|  | 862 | ret = -EINVAL; | 
|  | 863 | break; | 
|  | 864 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | /* | 
|  | 866 | * Switching-from response | 
|  | 867 | */ | 
| Samuel Ortiz | 8792f96 | 2007-10-01 01:20:12 -0700 | [diff] [blame] | 868 | acquire_console_sem(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | if (vc->vt_newvt >= 0) { | 
|  | 870 | if (arg == 0) | 
|  | 871 | /* | 
|  | 872 | * Switch disallowed, so forget we were trying | 
|  | 873 | * to do it. | 
|  | 874 | */ | 
|  | 875 | vc->vt_newvt = -1; | 
|  | 876 |  | 
|  | 877 | else { | 
|  | 878 | /* | 
|  | 879 | * The current vt has been released, so | 
|  | 880 | * complete the switch. | 
|  | 881 | */ | 
|  | 882 | int newvt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | newvt = vc->vt_newvt; | 
|  | 884 | vc->vt_newvt = -1; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 885 | ret = vc_allocate(newvt); | 
|  | 886 | if (ret) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | release_console_sem(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 888 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } | 
|  | 890 | /* | 
|  | 891 | * When we actually do the console switch, | 
|  | 892 | * make sure we are atomic with respect to | 
|  | 893 | * other console switches.. | 
|  | 894 | */ | 
|  | 895 | complete_change_console(vc_cons[newvt].d); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 897 | } else { | 
|  | 898 | /* | 
|  | 899 | * Switched-to response | 
|  | 900 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /* | 
|  | 902 | * If it's just an ACK, ignore it | 
|  | 903 | */ | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 904 | if (arg != VT_ACKACQ) | 
|  | 905 | ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } | 
| Samuel Ortiz | 8792f96 | 2007-10-01 01:20:12 -0700 | [diff] [blame] | 907 | release_console_sem(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 908 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 |  | 
|  | 910 | /* | 
|  | 911 | * Disallocate memory associated to VT (but leave VT1) | 
|  | 912 | */ | 
|  | 913 | case VT_DISALLOCATE: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 914 | if (arg > MAX_NR_CONSOLES) { | 
|  | 915 | ret = -ENXIO; | 
|  | 916 | break; | 
|  | 917 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | if (arg == 0) { | 
| Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 919 | /* deallocate all unused consoles, but leave 0 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | acquire_console_sem(); | 
|  | 921 | for (i=1; i<MAX_NR_CONSOLES; i++) | 
|  | 922 | if (! VT_BUSY(i)) | 
| Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 923 | vc_deallocate(i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | release_console_sem(); | 
|  | 925 | } else { | 
| Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 926 | /* deallocate a single console, if possible */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | arg--; | 
|  | 928 | if (VT_BUSY(arg)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 929 | ret = -EBUSY; | 
|  | 930 | else if (arg) {			      /* leave 0 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | acquire_console_sem(); | 
| Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 932 | vc_deallocate(arg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | release_console_sem(); | 
|  | 934 | } | 
|  | 935 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 936 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 |  | 
|  | 938 | case VT_RESIZE: | 
|  | 939 | { | 
|  | 940 | struct vt_sizes __user *vtsizes = up; | 
| Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 941 | struct vc_data *vc; | 
|  | 942 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | ushort ll,cc; | 
|  | 944 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 945 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if (get_user(ll, &vtsizes->v_rows) || | 
|  | 947 | get_user(cc, &vtsizes->v_cols)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 948 | ret = -EFAULT; | 
|  | 949 | else { | 
| Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 950 | acquire_console_sem(); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 951 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | 
|  | 952 | vc = vc_cons[i].d; | 
| Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 953 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 954 | if (vc) { | 
|  | 955 | vc->vc_resize_user = 1; | 
| Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 956 | vc_resize(vc_cons[i].d, cc, ll); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 957 | } | 
| Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 958 | } | 
| Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 959 | release_console_sem(); | 
| Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 960 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 961 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } | 
|  | 963 |  | 
|  | 964 | case VT_RESIZEX: | 
|  | 965 | { | 
|  | 966 | struct vt_consize __user *vtconsize = up; | 
|  | 967 | ushort ll,cc,vlin,clin,vcol,ccol; | 
|  | 968 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 969 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | if (!access_ok(VERIFY_READ, vtconsize, | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 971 | sizeof(struct vt_consize))) { | 
|  | 972 | ret = -EFAULT; | 
|  | 973 | break; | 
|  | 974 | } | 
|  | 975 | /* FIXME: Should check the copies properly */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | __get_user(ll, &vtconsize->v_rows); | 
|  | 977 | __get_user(cc, &vtconsize->v_cols); | 
|  | 978 | __get_user(vlin, &vtconsize->v_vlin); | 
|  | 979 | __get_user(clin, &vtconsize->v_clin); | 
|  | 980 | __get_user(vcol, &vtconsize->v_vcol); | 
|  | 981 | __get_user(ccol, &vtconsize->v_ccol); | 
|  | 982 | vlin = vlin ? vlin : vc->vc_scan_lines; | 
|  | 983 | if (clin) { | 
|  | 984 | if (ll) { | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 985 | if (ll != vlin/clin) { | 
|  | 986 | /* Parameters don't add up */ | 
|  | 987 | ret = -EINVAL; | 
|  | 988 | break; | 
|  | 989 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } else | 
|  | 991 | ll = vlin/clin; | 
|  | 992 | } | 
|  | 993 | if (vcol && ccol) { | 
|  | 994 | if (cc) { | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 995 | if (cc != vcol/ccol) { | 
|  | 996 | ret = -EINVAL; | 
|  | 997 | break; | 
|  | 998 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } else | 
|  | 1000 | cc = vcol/ccol; | 
|  | 1001 | } | 
|  | 1002 |  | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1003 | if (clin > 32) { | 
|  | 1004 | ret =  -EINVAL; | 
|  | 1005 | break; | 
|  | 1006 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 |  | 
|  | 1008 | for (i = 0; i < MAX_NR_CONSOLES; i++) { | 
|  | 1009 | if (!vc_cons[i].d) | 
|  | 1010 | continue; | 
|  | 1011 | acquire_console_sem(); | 
|  | 1012 | if (vlin) | 
|  | 1013 | vc_cons[i].d->vc_scan_lines = vlin; | 
|  | 1014 | if (clin) | 
|  | 1015 | vc_cons[i].d->vc_font.height = clin; | 
| Antonino A. Daplas | e400b6e | 2007-10-16 01:29:35 -0700 | [diff] [blame] | 1016 | vc_cons[i].d->vc_resize_user = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | vc_resize(vc_cons[i].d, cc, ll); | 
|  | 1018 | release_console_sem(); | 
|  | 1019 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1020 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } | 
|  | 1022 |  | 
|  | 1023 | case PIO_FONT: { | 
|  | 1024 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1025 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | op.op = KD_FONT_OP_SET; | 
|  | 1027 | op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC;	/* Compatibility */ | 
|  | 1028 | op.width = 8; | 
|  | 1029 | op.height = 0; | 
|  | 1030 | op.charcount = 256; | 
|  | 1031 | op.data = up; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1032 | ret = con_font_op(vc_cons[fg_console].d, &op); | 
|  | 1033 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } | 
|  | 1035 |  | 
|  | 1036 | case GIO_FONT: { | 
|  | 1037 | op.op = KD_FONT_OP_GET; | 
|  | 1038 | op.flags = KD_FONT_FLAG_OLD; | 
|  | 1039 | op.width = 8; | 
|  | 1040 | op.height = 32; | 
|  | 1041 | op.charcount = 256; | 
|  | 1042 | op.data = up; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1043 | ret = con_font_op(vc_cons[fg_console].d, &op); | 
|  | 1044 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | case PIO_CMAP: | 
|  | 1048 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1049 | ret = -EPERM; | 
|  | 1050 | else | 
|  | 1051 | ret = con_set_cmap(up); | 
|  | 1052 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 |  | 
|  | 1054 | case GIO_CMAP: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1055 | ret = con_get_cmap(up); | 
|  | 1056 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 |  | 
|  | 1058 | case PIO_FONTX: | 
|  | 1059 | case GIO_FONTX: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1060 | ret = do_fontx_ioctl(cmd, up, perm, &op); | 
|  | 1061 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 |  | 
|  | 1063 | case PIO_FONTRESET: | 
|  | 1064 | { | 
|  | 1065 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1066 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 |  | 
|  | 1068 | #ifdef BROKEN_GRAPHICS_PROGRAMS | 
|  | 1069 | /* With BROKEN_GRAPHICS_PROGRAMS defined, the default | 
|  | 1070 | font is not saved. */ | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1071 | ret = -ENOSYS; | 
|  | 1072 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | #else | 
|  | 1074 | { | 
|  | 1075 | op.op = KD_FONT_OP_SET_DEFAULT; | 
|  | 1076 | op.data = NULL; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1077 | ret = con_font_op(vc_cons[fg_console].d, &op); | 
|  | 1078 | if (ret) | 
|  | 1079 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | con_set_default_unimap(vc_cons[fg_console].d); | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1081 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } | 
|  | 1083 | #endif | 
|  | 1084 | } | 
|  | 1085 |  | 
|  | 1086 | case KDFONTOP: { | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1087 | if (copy_from_user(&op, up, sizeof(op))) { | 
|  | 1088 | ret = -EFAULT; | 
|  | 1089 | break; | 
|  | 1090 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (!perm && op.op != KD_FONT_OP_GET) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1092 | goto eperm; | 
|  | 1093 | ret = con_font_op(vc, &op); | 
|  | 1094 | if (ret) | 
|  | 1095 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (copy_to_user(up, &op, sizeof(op))) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1097 | ret = -EFAULT; | 
|  | 1098 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | case PIO_SCRNMAP: | 
|  | 1102 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1103 | ret = -EPERM; | 
|  | 1104 | else | 
|  | 1105 | ret = con_set_trans_old(up); | 
|  | 1106 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 |  | 
|  | 1108 | case GIO_SCRNMAP: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1109 | ret = con_get_trans_old(up); | 
|  | 1110 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 |  | 
|  | 1112 | case PIO_UNISCRNMAP: | 
|  | 1113 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1114 | ret = -EPERM; | 
|  | 1115 | else | 
|  | 1116 | ret = con_set_trans_new(up); | 
|  | 1117 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 |  | 
|  | 1119 | case GIO_UNISCRNMAP: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1120 | ret = con_get_trans_new(up); | 
|  | 1121 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 |  | 
|  | 1123 | case PIO_UNIMAPCLR: | 
|  | 1124 | { struct unimapinit ui; | 
|  | 1125 | if (!perm) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1126 | goto eperm; | 
|  | 1127 | ret = copy_from_user(&ui, up, sizeof(struct unimapinit)); | 
|  | 1128 | if (!ret) | 
|  | 1129 | con_clear_unimap(vc, &ui); | 
|  | 1130 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | case PIO_UNIMAP: | 
|  | 1134 | case GIO_UNIMAP: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1135 | ret = do_unimap_ioctl(cmd, up, perm, vc); | 
|  | 1136 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 |  | 
|  | 1138 | case VT_LOCKSWITCH: | 
|  | 1139 | if (!capable(CAP_SYS_TTY_CONFIG)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1140 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | vt_dont_switch = 1; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1142 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | case VT_UNLOCKSWITCH: | 
|  | 1144 | if (!capable(CAP_SYS_TTY_CONFIG)) | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1145 | goto eperm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | vt_dont_switch = 0; | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1147 | break; | 
| Samuel Thibault | 533475d | 2006-08-27 01:23:39 -0700 | [diff] [blame] | 1148 | case VT_GETHIFONTMASK: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1149 | ret = put_user(vc->vc_hi_font_mask, | 
|  | 1150 | (unsigned short __user *)arg); | 
|  | 1151 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | default: | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1153 | ret = -ENOIOCTLCMD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } | 
| Alan Cox | 9cc3c22 | 2008-04-30 00:53:26 -0700 | [diff] [blame] | 1155 | out: | 
|  | 1156 | unlock_kernel(); | 
|  | 1157 | return ret; | 
|  | 1158 | eperm: | 
|  | 1159 | ret = -EPERM; | 
|  | 1160 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } | 
|  | 1162 |  | 
|  | 1163 | /* | 
|  | 1164 | * Sometimes we want to wait until a particular VT has been activated. We | 
|  | 1165 | * do it in a very simple manner. Everybody waits on a single queue and | 
|  | 1166 | * get woken up at once. Those that are satisfied go on with their business, | 
|  | 1167 | * while those not ready go back to sleep. Seems overkill to add a wait | 
|  | 1168 | * to each vt just for this - usually this does nothing! | 
|  | 1169 | */ | 
|  | 1170 | static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue); | 
|  | 1171 |  | 
|  | 1172 | /* | 
|  | 1173 | * Sleeps until a vt is activated, or the task is interrupted. Returns | 
| Linus Torvalds | 70cb979 | 2007-10-07 16:02:55 -0700 | [diff] [blame] | 1174 | * 0 if activation, -EINTR if interrupted by a signal handler. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | */ | 
|  | 1176 | int vt_waitactive(int vt) | 
|  | 1177 | { | 
|  | 1178 | int retval; | 
|  | 1179 | DECLARE_WAITQUEUE(wait, current); | 
|  | 1180 |  | 
|  | 1181 | add_wait_queue(&vt_activate_queue, &wait); | 
|  | 1182 | for (;;) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | retval = 0; | 
| Michal Januszewski | f991519 | 2007-04-01 23:49:51 -0700 | [diff] [blame] | 1184 |  | 
|  | 1185 | /* | 
|  | 1186 | * Synchronize with redraw_screen(). By acquiring the console | 
|  | 1187 | * semaphore we make sure that the console switch is completed | 
|  | 1188 | * before we return. If we didn't wait for the semaphore, we | 
|  | 1189 | * could return at a point where fg_console has already been | 
|  | 1190 | * updated, but the console switch hasn't been completed. | 
|  | 1191 | */ | 
|  | 1192 | acquire_console_sem(); | 
|  | 1193 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 1194 | if (vt == fg_console) { | 
|  | 1195 | release_console_sem(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | break; | 
| Michal Januszewski | f991519 | 2007-04-01 23:49:51 -0700 | [diff] [blame] | 1197 | } | 
|  | 1198 | release_console_sem(); | 
| Linus Torvalds | 70cb979 | 2007-10-07 16:02:55 -0700 | [diff] [blame] | 1199 | retval = -ERESTARTNOHAND; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if (signal_pending(current)) | 
|  | 1201 | break; | 
|  | 1202 | schedule(); | 
|  | 1203 | } | 
|  | 1204 | remove_wait_queue(&vt_activate_queue, &wait); | 
| Milind Arun Choudhary | cc0a8fb | 2007-05-08 00:30:52 -0700 | [diff] [blame] | 1205 | __set_current_state(TASK_RUNNING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | return retval; | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | #define vt_wake_waitactive() wake_up(&vt_activate_queue) | 
|  | 1210 |  | 
|  | 1211 | void reset_vc(struct vc_data *vc) | 
|  | 1212 | { | 
|  | 1213 | vc->vc_mode = KD_TEXT; | 
| Bill Nottingham | 2e8ecb9 | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1214 | kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | vc->vt_mode.mode = VT_AUTO; | 
|  | 1216 | vc->vt_mode.waitv = 0; | 
|  | 1217 | vc->vt_mode.relsig = 0; | 
|  | 1218 | vc->vt_mode.acqsig = 0; | 
|  | 1219 | vc->vt_mode.frsig = 0; | 
| Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1220 | put_pid(vc->vt_pid); | 
|  | 1221 | vc->vt_pid = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | vc->vt_newvt = -1; | 
|  | 1223 | if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */ | 
|  | 1224 | reset_palette(vc); | 
|  | 1225 | } | 
|  | 1226 |  | 
| Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 1227 | void vc_SAK(struct work_struct *work) | 
|  | 1228 | { | 
|  | 1229 | struct vc *vc_con = | 
|  | 1230 | container_of(work, struct vc, SAK_work); | 
|  | 1231 | struct vc_data *vc; | 
|  | 1232 | struct tty_struct *tty; | 
|  | 1233 |  | 
|  | 1234 | acquire_console_sem(); | 
|  | 1235 | vc = vc_con->d; | 
|  | 1236 | if (vc) { | 
|  | 1237 | tty = vc->vc_tty; | 
|  | 1238 | /* | 
|  | 1239 | * SAK should also work in all raw modes and reset | 
|  | 1240 | * them properly. | 
|  | 1241 | */ | 
|  | 1242 | if (tty) | 
|  | 1243 | __do_SAK(tty); | 
|  | 1244 | reset_vc(vc); | 
|  | 1245 | } | 
|  | 1246 | release_console_sem(); | 
|  | 1247 | } | 
|  | 1248 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | /* | 
|  | 1250 | * Performs the back end of a vt switch | 
|  | 1251 | */ | 
|  | 1252 | static void complete_change_console(struct vc_data *vc) | 
|  | 1253 | { | 
|  | 1254 | unsigned char old_vc_mode; | 
|  | 1255 |  | 
|  | 1256 | last_console = fg_console; | 
|  | 1257 |  | 
|  | 1258 | /* | 
|  | 1259 | * If we're switching, we could be going from KD_GRAPHICS to | 
|  | 1260 | * KD_TEXT mode or vice versa, which means we need to blank or | 
|  | 1261 | * unblank the screen later. | 
|  | 1262 | */ | 
|  | 1263 | old_vc_mode = vc_cons[fg_console].d->vc_mode; | 
|  | 1264 | switch_screen(vc); | 
|  | 1265 |  | 
|  | 1266 | /* | 
| Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1267 | * This can't appear below a successful kill_pid().  If it did, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | * then the *blank_screen operation could occur while X, having | 
|  | 1269 | * received acqsig, is waking up on another processor.  This | 
|  | 1270 | * condition can lead to overlapping accesses to the VGA range | 
|  | 1271 | * and the framebuffer (causing system lockups). | 
|  | 1272 | * | 
|  | 1273 | * To account for this we duplicate this code below only if the | 
|  | 1274 | * controlling process is gone and we've called reset_vc. | 
|  | 1275 | */ | 
|  | 1276 | if (old_vc_mode != vc->vc_mode) { | 
|  | 1277 | if (vc->vc_mode == KD_TEXT) | 
|  | 1278 | do_unblank_screen(1); | 
|  | 1279 | else | 
|  | 1280 | do_blank_screen(1); | 
|  | 1281 | } | 
|  | 1282 |  | 
|  | 1283 | /* | 
|  | 1284 | * If this new console is under process control, send it a signal | 
|  | 1285 | * telling it that it has acquired. Also check if it has died and | 
|  | 1286 | * clean up (similar to logic employed in change_console()) | 
|  | 1287 | */ | 
|  | 1288 | if (vc->vt_mode.mode == VT_PROCESS) { | 
|  | 1289 | /* | 
| Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1290 | * Send the signal as privileged - kill_pid() will | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | * tell us if the process has gone or something else | 
|  | 1292 | * is awry | 
|  | 1293 | */ | 
| Eric W. Biederman | bde0d2c | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1294 | if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | /* | 
|  | 1296 | * The controlling process has died, so we revert back to | 
|  | 1297 | * normal operation. In this case, we'll also change back | 
|  | 1298 | * to KD_TEXT mode. I'm not sure if this is strictly correct | 
|  | 1299 | * but it saves the agony when the X server dies and the screen | 
|  | 1300 | * remains blanked due to KD_GRAPHICS! It would be nice to do | 
|  | 1301 | * this outside of VT_PROCESS but there is no single process | 
|  | 1302 | * to account for and tracking tty count may be undesirable. | 
|  | 1303 | */ | 
|  | 1304 | reset_vc(vc); | 
|  | 1305 |  | 
|  | 1306 | if (old_vc_mode != vc->vc_mode) { | 
|  | 1307 | if (vc->vc_mode == KD_TEXT) | 
|  | 1308 | do_unblank_screen(1); | 
|  | 1309 | else | 
|  | 1310 | do_blank_screen(1); | 
|  | 1311 | } | 
|  | 1312 | } | 
|  | 1313 | } | 
|  | 1314 |  | 
|  | 1315 | /* | 
|  | 1316 | * Wake anyone waiting for their VT to activate | 
|  | 1317 | */ | 
|  | 1318 | vt_wake_waitactive(); | 
|  | 1319 | return; | 
|  | 1320 | } | 
|  | 1321 |  | 
|  | 1322 | /* | 
|  | 1323 | * Performs the front-end of a vt switch | 
|  | 1324 | */ | 
|  | 1325 | void change_console(struct vc_data *new_vc) | 
|  | 1326 | { | 
|  | 1327 | struct vc_data *vc; | 
|  | 1328 |  | 
|  | 1329 | if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch) | 
|  | 1330 | return; | 
|  | 1331 |  | 
|  | 1332 | /* | 
|  | 1333 | * If this vt is in process mode, then we need to handshake with | 
|  | 1334 | * that process before switching. Essentially, we store where that | 
|  | 1335 | * vt wants to switch to and wait for it to tell us when it's done | 
|  | 1336 | * (via VT_RELDISP ioctl). | 
|  | 1337 | * | 
|  | 1338 | * We also check to see if the controlling process still exists. | 
|  | 1339 | * If it doesn't, we reset this vt to auto mode and continue. | 
|  | 1340 | * This is a cheap way to track process control. The worst thing | 
|  | 1341 | * that can happen is: we send a signal to a process, it dies, and | 
|  | 1342 | * the switch gets "lost" waiting for a response; hopefully, the | 
|  | 1343 | * user will try again, we'll detect the process is gone (unless | 
|  | 1344 | * the user waits just the right amount of time :-) and revert the | 
|  | 1345 | * vt to auto control. | 
|  | 1346 | */ | 
|  | 1347 | vc = vc_cons[fg_console].d; | 
|  | 1348 | if (vc->vt_mode.mode == VT_PROCESS) { | 
|  | 1349 | /* | 
| Eric W. Biederman | 3dfcaf16 | 2006-12-13 00:34:05 -0800 | [diff] [blame] | 1350 | * Send the signal as privileged - kill_pid() will | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | * tell us if the process has gone or something else | 
| Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1352 | * is awry. | 
|  | 1353 | * | 
|  | 1354 | * We need to set vt_newvt *before* sending the signal or we | 
|  | 1355 | * have a race. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | */ | 
| Jan Lübbe | a64314e | 2007-09-29 18:47:51 +0200 | [diff] [blame] | 1357 | vc->vt_newvt = new_vc->vc_num; | 
| Eric W. Biederman | bde0d2c | 2006-10-02 02:17:14 -0700 | [diff] [blame] | 1358 | if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | /* | 
|  | 1360 | * It worked. Mark the vt to switch to and | 
|  | 1361 | * return. The process needs to send us a | 
|  | 1362 | * VT_RELDISP ioctl to complete the switch. | 
|  | 1363 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | return; | 
|  | 1365 | } | 
|  | 1366 |  | 
|  | 1367 | /* | 
|  | 1368 | * The controlling process has died, so we revert back to | 
|  | 1369 | * normal operation. In this case, we'll also change back | 
|  | 1370 | * to KD_TEXT mode. I'm not sure if this is strictly correct | 
|  | 1371 | * but it saves the agony when the X server dies and the screen | 
|  | 1372 | * remains blanked due to KD_GRAPHICS! It would be nice to do | 
|  | 1373 | * this outside of VT_PROCESS but there is no single process | 
|  | 1374 | * to account for and tracking tty count may be undesirable. | 
|  | 1375 | */ | 
|  | 1376 | reset_vc(vc); | 
|  | 1377 |  | 
|  | 1378 | /* | 
|  | 1379 | * Fall through to normal (VT_AUTO) handling of the switch... | 
|  | 1380 | */ | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | /* | 
|  | 1384 | * Ignore all switches in KD_GRAPHICS+VT_AUTO mode | 
|  | 1385 | */ | 
|  | 1386 | if (vc->vc_mode == KD_GRAPHICS) | 
|  | 1387 | return; | 
|  | 1388 |  | 
|  | 1389 | complete_change_console(new_vc); | 
|  | 1390 | } |