Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/char/keyboard.c |
| 3 | * ebcdic keycode functions for s390 console drivers |
| 4 | * |
| 5 | * S390 version |
| 6 | * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 7 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com), |
| 8 | */ |
| 9 | |
| 10 | #include <linux/config.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/sysrq.h> |
| 14 | |
| 15 | #include <linux/kbd_kern.h> |
| 16 | #include <linux/kbd_diacr.h> |
| 17 | #include <asm/uaccess.h> |
| 18 | |
| 19 | #include "keyboard.h" |
| 20 | |
| 21 | /* |
| 22 | * Handler Tables. |
| 23 | */ |
| 24 | #define K_HANDLERS\ |
| 25 | k_self, k_fn, k_spec, k_ignore,\ |
| 26 | k_dead, k_ignore, k_ignore, k_ignore,\ |
| 27 | k_ignore, k_ignore, k_ignore, k_ignore,\ |
| 28 | k_ignore, k_ignore, k_ignore, k_ignore |
| 29 | |
| 30 | typedef void (k_handler_fn)(struct kbd_data *, unsigned char); |
| 31 | static k_handler_fn K_HANDLERS; |
| 32 | static k_handler_fn *k_handler[16] = { K_HANDLERS }; |
| 33 | |
| 34 | /* maximum values each key_handler can handle */ |
| 35 | static const int kbd_max_vals[] = { |
| 36 | 255, ARRAY_SIZE(func_table) - 1, NR_FN_HANDLER - 1, 0, |
| 37 | NR_DEAD - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 38 | }; |
| 39 | static const int KBD_NR_TYPES = ARRAY_SIZE(kbd_max_vals); |
| 40 | |
| 41 | static unsigned char ret_diacr[NR_DEAD] = { |
| 42 | '`', '\'', '^', '~', '"', ',' |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Alloc/free of kbd_data structures. |
| 47 | */ |
| 48 | struct kbd_data * |
| 49 | kbd_alloc(void) { |
| 50 | struct kbd_data *kbd; |
| 51 | int i, len; |
| 52 | |
| 53 | kbd = kmalloc(sizeof(struct kbd_data), GFP_KERNEL); |
| 54 | if (!kbd) |
| 55 | goto out; |
| 56 | memset(kbd, 0, sizeof(struct kbd_data)); |
| 57 | kbd->key_maps = kmalloc(sizeof(key_maps), GFP_KERNEL); |
| 58 | if (!key_maps) |
| 59 | goto out_kbd; |
| 60 | memset(kbd->key_maps, 0, sizeof(key_maps)); |
| 61 | for (i = 0; i < ARRAY_SIZE(key_maps); i++) { |
| 62 | if (key_maps[i]) { |
| 63 | kbd->key_maps[i] = |
| 64 | kmalloc(sizeof(u_short)*NR_KEYS, GFP_KERNEL); |
| 65 | if (!kbd->key_maps[i]) |
| 66 | goto out_maps; |
| 67 | memcpy(kbd->key_maps[i], key_maps[i], |
| 68 | sizeof(u_short)*NR_KEYS); |
| 69 | } |
| 70 | } |
| 71 | kbd->func_table = kmalloc(sizeof(func_table), GFP_KERNEL); |
| 72 | if (!kbd->func_table) |
| 73 | goto out_maps; |
| 74 | memset(kbd->func_table, 0, sizeof(func_table)); |
| 75 | for (i = 0; i < ARRAY_SIZE(func_table); i++) { |
| 76 | if (func_table[i]) { |
| 77 | len = strlen(func_table[i]) + 1; |
| 78 | kbd->func_table[i] = kmalloc(len, GFP_KERNEL); |
| 79 | if (!kbd->func_table[i]) |
| 80 | goto out_func; |
| 81 | memcpy(kbd->func_table[i], func_table[i], len); |
| 82 | } |
| 83 | } |
| 84 | kbd->fn_handler = |
| 85 | kmalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL); |
| 86 | if (!kbd->fn_handler) |
| 87 | goto out_func; |
| 88 | memset(kbd->fn_handler, 0, sizeof(fn_handler_fn *) * NR_FN_HANDLER); |
| 89 | kbd->accent_table = |
| 90 | kmalloc(sizeof(struct kbdiacr)*MAX_DIACR, GFP_KERNEL); |
| 91 | if (!kbd->accent_table) |
| 92 | goto out_fn_handler; |
| 93 | memcpy(kbd->accent_table, accent_table, |
| 94 | sizeof(struct kbdiacr)*MAX_DIACR); |
| 95 | kbd->accent_table_size = accent_table_size; |
| 96 | return kbd; |
| 97 | |
| 98 | out_fn_handler: |
| 99 | kfree(kbd->fn_handler); |
| 100 | out_func: |
| 101 | for (i = 0; i < ARRAY_SIZE(func_table); i++) |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame^] | 102 | kfree(kbd->func_table[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | kfree(kbd->func_table); |
| 104 | out_maps: |
| 105 | for (i = 0; i < ARRAY_SIZE(key_maps); i++) |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame^] | 106 | kfree(kbd->key_maps[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | kfree(kbd->key_maps); |
| 108 | out_kbd: |
| 109 | kfree(kbd); |
| 110 | out: |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | kbd_free(struct kbd_data *kbd) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | kfree(kbd->accent_table); |
| 120 | kfree(kbd->fn_handler); |
| 121 | for (i = 0; i < ARRAY_SIZE(func_table); i++) |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame^] | 122 | kfree(kbd->func_table[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | kfree(kbd->func_table); |
| 124 | for (i = 0; i < ARRAY_SIZE(key_maps); i++) |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame^] | 125 | kfree(kbd->key_maps[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | kfree(kbd->key_maps); |
| 127 | kfree(kbd); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Generate ascii -> ebcdic translation table from kbd_data. |
| 132 | */ |
| 133 | void |
| 134 | kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc) |
| 135 | { |
| 136 | unsigned short *keymap, keysym; |
| 137 | int i, j, k; |
| 138 | |
| 139 | memset(ascebc, 0x40, 256); |
| 140 | for (i = 0; i < ARRAY_SIZE(key_maps); i++) { |
| 141 | keymap = kbd->key_maps[i]; |
| 142 | if (!keymap) |
| 143 | continue; |
| 144 | for (j = 0; j < NR_KEYS; j++) { |
| 145 | k = ((i & 1) << 7) + j; |
| 146 | keysym = keymap[j]; |
| 147 | if (KTYP(keysym) == (KT_LATIN | 0xf0) || |
| 148 | KTYP(keysym) == (KT_LETTER | 0xf0)) |
| 149 | ascebc[KVAL(keysym)] = k; |
| 150 | else if (KTYP(keysym) == (KT_DEAD | 0xf0)) |
| 151 | ascebc[ret_diacr[KVAL(keysym)]] = k; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Generate ebcdic -> ascii translation table from kbd_data. |
| 158 | */ |
| 159 | void |
| 160 | kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc) |
| 161 | { |
| 162 | unsigned short *keymap, keysym; |
| 163 | int i, j, k; |
| 164 | |
| 165 | memset(ebcasc, ' ', 256); |
| 166 | for (i = 0; i < ARRAY_SIZE(key_maps); i++) { |
| 167 | keymap = kbd->key_maps[i]; |
| 168 | if (!keymap) |
| 169 | continue; |
| 170 | for (j = 0; j < NR_KEYS; j++) { |
| 171 | keysym = keymap[j]; |
| 172 | k = ((i & 1) << 7) + j; |
| 173 | if (KTYP(keysym) == (KT_LATIN | 0xf0) || |
| 174 | KTYP(keysym) == (KT_LETTER | 0xf0)) |
| 175 | ebcasc[k] = KVAL(keysym); |
| 176 | else if (KTYP(keysym) == (KT_DEAD | 0xf0)) |
| 177 | ebcasc[k] = ret_diacr[KVAL(keysym)]; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * We have a combining character DIACR here, followed by the character CH. |
| 184 | * If the combination occurs in the table, return the corresponding value. |
| 185 | * Otherwise, if CH is a space or equals DIACR, return DIACR. |
| 186 | * Otherwise, conclude that DIACR was not combining after all, |
| 187 | * queue it and return CH. |
| 188 | */ |
| 189 | static unsigned char |
| 190 | handle_diacr(struct kbd_data *kbd, unsigned char ch) |
| 191 | { |
| 192 | int i, d; |
| 193 | |
| 194 | d = kbd->diacr; |
| 195 | kbd->diacr = 0; |
| 196 | |
| 197 | for (i = 0; i < kbd->accent_table_size; i++) { |
| 198 | if (kbd->accent_table[i].diacr == d && |
| 199 | kbd->accent_table[i].base == ch) |
| 200 | return kbd->accent_table[i].result; |
| 201 | } |
| 202 | |
| 203 | if (ch == ' ' || ch == d) |
| 204 | return d; |
| 205 | |
| 206 | kbd_put_queue(kbd->tty, d); |
| 207 | return ch; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Handle dead key. |
| 212 | */ |
| 213 | static void |
| 214 | k_dead(struct kbd_data *kbd, unsigned char value) |
| 215 | { |
| 216 | value = ret_diacr[value]; |
| 217 | kbd->diacr = (kbd->diacr ? handle_diacr(kbd, value) : value); |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Normal character handler. |
| 222 | */ |
| 223 | static void |
| 224 | k_self(struct kbd_data *kbd, unsigned char value) |
| 225 | { |
| 226 | if (kbd->diacr) |
| 227 | value = handle_diacr(kbd, value); |
| 228 | kbd_put_queue(kbd->tty, value); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Special key handlers |
| 233 | */ |
| 234 | static void |
| 235 | k_ignore(struct kbd_data *kbd, unsigned char value) |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Function key handler. |
| 241 | */ |
| 242 | static void |
| 243 | k_fn(struct kbd_data *kbd, unsigned char value) |
| 244 | { |
| 245 | if (kbd->func_table[value]) |
| 246 | kbd_puts_queue(kbd->tty, kbd->func_table[value]); |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | k_spec(struct kbd_data *kbd, unsigned char value) |
| 251 | { |
| 252 | if (value >= NR_FN_HANDLER) |
| 253 | return; |
| 254 | if (kbd->fn_handler[value]) |
| 255 | kbd->fn_handler[value](kbd); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Put utf8 character to tty flip buffer. |
| 260 | * UTF-8 is defined for words of up to 31 bits, |
| 261 | * but we need only 16 bits here |
| 262 | */ |
| 263 | static void |
| 264 | to_utf8(struct tty_struct *tty, ushort c) |
| 265 | { |
| 266 | if (c < 0x80) |
| 267 | /* 0******* */ |
| 268 | kbd_put_queue(tty, c); |
| 269 | else if (c < 0x800) { |
| 270 | /* 110***** 10****** */ |
| 271 | kbd_put_queue(tty, 0xc0 | (c >> 6)); |
| 272 | kbd_put_queue(tty, 0x80 | (c & 0x3f)); |
| 273 | } else { |
| 274 | /* 1110**** 10****** 10****** */ |
| 275 | kbd_put_queue(tty, 0xe0 | (c >> 12)); |
| 276 | kbd_put_queue(tty, 0x80 | ((c >> 6) & 0x3f)); |
| 277 | kbd_put_queue(tty, 0x80 | (c & 0x3f)); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Process keycode. |
| 283 | */ |
| 284 | void |
| 285 | kbd_keycode(struct kbd_data *kbd, unsigned int keycode) |
| 286 | { |
| 287 | unsigned short keysym; |
| 288 | unsigned char type, value; |
| 289 | |
| 290 | if (!kbd || !kbd->tty) |
| 291 | return; |
| 292 | |
| 293 | if (keycode >= 384) |
| 294 | keysym = kbd->key_maps[5][keycode - 384]; |
| 295 | else if (keycode >= 256) |
| 296 | keysym = kbd->key_maps[4][keycode - 256]; |
| 297 | else if (keycode >= 128) |
| 298 | keysym = kbd->key_maps[1][keycode - 128]; |
| 299 | else |
| 300 | keysym = kbd->key_maps[0][keycode]; |
| 301 | |
| 302 | type = KTYP(keysym); |
| 303 | if (type >= 0xf0) { |
| 304 | type -= 0xf0; |
| 305 | if (type == KT_LETTER) |
| 306 | type = KT_LATIN; |
| 307 | value = KVAL(keysym); |
| 308 | #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */ |
| 309 | if (kbd->sysrq) { |
| 310 | if (kbd->sysrq == K(KT_LATIN, '-')) { |
| 311 | kbd->sysrq = 0; |
| 312 | handle_sysrq(value, 0, kbd->tty); |
| 313 | return; |
| 314 | } |
| 315 | if (value == '-') { |
| 316 | kbd->sysrq = K(KT_LATIN, '-'); |
| 317 | return; |
| 318 | } |
| 319 | /* Incomplete sysrq sequence. */ |
| 320 | (*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq)); |
| 321 | kbd->sysrq = 0; |
| 322 | } else if ((type == KT_LATIN && value == '^') || |
| 323 | (type == KT_DEAD && ret_diacr[value] == '^')) { |
| 324 | kbd->sysrq = K(type, value); |
| 325 | return; |
| 326 | } |
| 327 | #endif |
| 328 | (*k_handler[type])(kbd, value); |
| 329 | } else |
| 330 | to_utf8(kbd->tty, keysym); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Ioctl stuff. |
| 335 | */ |
| 336 | static int |
| 337 | do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe, |
| 338 | int cmd, int perm) |
| 339 | { |
| 340 | struct kbentry tmp; |
| 341 | ushort *key_map, val, ov; |
| 342 | |
| 343 | if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry))) |
| 344 | return -EFAULT; |
| 345 | #if NR_KEYS < 256 |
| 346 | if (tmp.kb_index >= NR_KEYS) |
| 347 | return -EINVAL; |
| 348 | #endif |
| 349 | #if MAX_NR_KEYMAPS < 256 |
| 350 | if (tmp.kb_table >= MAX_NR_KEYMAPS) |
| 351 | return -EINVAL; |
| 352 | #endif |
| 353 | |
| 354 | switch (cmd) { |
| 355 | case KDGKBENT: |
| 356 | key_map = kbd->key_maps[tmp.kb_table]; |
| 357 | if (key_map) { |
| 358 | val = U(key_map[tmp.kb_index]); |
| 359 | if (KTYP(val) >= KBD_NR_TYPES) |
| 360 | val = K_HOLE; |
| 361 | } else |
| 362 | val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP); |
| 363 | return put_user(val, &user_kbe->kb_value); |
| 364 | case KDSKBENT: |
| 365 | if (!perm) |
| 366 | return -EPERM; |
| 367 | if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) { |
| 368 | /* disallocate map */ |
| 369 | key_map = kbd->key_maps[tmp.kb_table]; |
| 370 | if (key_map) { |
| 371 | kbd->key_maps[tmp.kb_table] = 0; |
| 372 | kfree(key_map); |
| 373 | } |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | if (KTYP(tmp.kb_value) >= KBD_NR_TYPES) |
| 378 | return -EINVAL; |
| 379 | if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)]) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | if (!(key_map = kbd->key_maps[tmp.kb_table])) { |
| 383 | int j; |
| 384 | |
| 385 | key_map = (ushort *) kmalloc(sizeof(plain_map), |
| 386 | GFP_KERNEL); |
| 387 | if (!key_map) |
| 388 | return -ENOMEM; |
| 389 | kbd->key_maps[tmp.kb_table] = key_map; |
| 390 | for (j = 0; j < NR_KEYS; j++) |
| 391 | key_map[j] = U(K_HOLE); |
| 392 | } |
| 393 | ov = U(key_map[tmp.kb_index]); |
| 394 | if (tmp.kb_value == ov) |
| 395 | break; /* nothing to do */ |
| 396 | /* |
| 397 | * Attention Key. |
| 398 | */ |
| 399 | if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) && |
| 400 | !capable(CAP_SYS_ADMIN)) |
| 401 | return -EPERM; |
| 402 | key_map[tmp.kb_index] = U(tmp.kb_value); |
| 403 | break; |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int |
| 409 | do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs, |
| 410 | int cmd, int perm) |
| 411 | { |
| 412 | unsigned char kb_func; |
| 413 | char *p; |
| 414 | int len; |
| 415 | |
| 416 | /* Get u_kbs->kb_func. */ |
| 417 | if (get_user(kb_func, &u_kbs->kb_func)) |
| 418 | return -EFAULT; |
| 419 | #if MAX_NR_FUNC < 256 |
| 420 | if (kb_func >= MAX_NR_FUNC) |
| 421 | return -EINVAL; |
| 422 | #endif |
| 423 | |
| 424 | switch (cmd) { |
| 425 | case KDGKBSENT: |
| 426 | p = kbd->func_table[kb_func]; |
| 427 | if (p) { |
| 428 | len = strlen(p); |
| 429 | if (len >= sizeof(u_kbs->kb_string)) |
| 430 | len = sizeof(u_kbs->kb_string) - 1; |
| 431 | if (copy_to_user(u_kbs->kb_string, p, len)) |
| 432 | return -EFAULT; |
| 433 | } else |
| 434 | len = 0; |
| 435 | if (put_user('\0', u_kbs->kb_string + len)) |
| 436 | return -EFAULT; |
| 437 | break; |
| 438 | case KDSKBSENT: |
| 439 | if (!perm) |
| 440 | return -EPERM; |
| 441 | len = strnlen_user(u_kbs->kb_string, |
| 442 | sizeof(u_kbs->kb_string) - 1); |
| 443 | p = kmalloc(len, GFP_KERNEL); |
| 444 | if (!p) |
| 445 | return -ENOMEM; |
| 446 | if (copy_from_user(p, u_kbs->kb_string, len)) { |
| 447 | kfree(p); |
| 448 | return -EFAULT; |
| 449 | } |
| 450 | p[len] = 0; |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame^] | 451 | kfree(kbd->func_table[kb_func]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | kbd->func_table[kb_func] = p; |
| 453 | break; |
| 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | int |
| 459 | kbd_ioctl(struct kbd_data *kbd, struct file *file, |
| 460 | unsigned int cmd, unsigned long arg) |
| 461 | { |
| 462 | struct kbdiacrs __user *a; |
| 463 | void __user *argp; |
| 464 | int ct, perm; |
| 465 | |
| 466 | argp = (void __user *)arg; |
| 467 | |
| 468 | /* |
| 469 | * To have permissions to do most of the vt ioctls, we either have |
| 470 | * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG. |
| 471 | */ |
| 472 | perm = current->signal->tty == kbd->tty || capable(CAP_SYS_TTY_CONFIG); |
| 473 | switch (cmd) { |
| 474 | case KDGKBTYPE: |
| 475 | return put_user(KB_101, (char __user *)argp); |
| 476 | case KDGKBENT: |
| 477 | case KDSKBENT: |
| 478 | return do_kdsk_ioctl(kbd, argp, cmd, perm); |
| 479 | case KDGKBSENT: |
| 480 | case KDSKBSENT: |
| 481 | return do_kdgkb_ioctl(kbd, argp, cmd, perm); |
| 482 | case KDGKBDIACR: |
| 483 | a = argp; |
| 484 | |
| 485 | if (put_user(kbd->accent_table_size, &a->kb_cnt)) |
| 486 | return -EFAULT; |
| 487 | ct = kbd->accent_table_size; |
| 488 | if (copy_to_user(a->kbdiacr, kbd->accent_table, |
| 489 | ct * sizeof(struct kbdiacr))) |
| 490 | return -EFAULT; |
| 491 | return 0; |
| 492 | case KDSKBDIACR: |
| 493 | a = argp; |
| 494 | if (!perm) |
| 495 | return -EPERM; |
| 496 | if (get_user(ct, &a->kb_cnt)) |
| 497 | return -EFAULT; |
| 498 | if (ct >= MAX_DIACR) |
| 499 | return -EINVAL; |
| 500 | kbd->accent_table_size = ct; |
| 501 | if (copy_from_user(kbd->accent_table, a->kbdiacr, |
| 502 | ct * sizeof(struct kbdiacr))) |
| 503 | return -EFAULT; |
| 504 | return 0; |
| 505 | default: |
| 506 | return -ENOIOCTLCMD; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | EXPORT_SYMBOL(kbd_ioctl); |
| 511 | EXPORT_SYMBOL(kbd_ascebc); |
| 512 | EXPORT_SYMBOL(kbd_free); |
| 513 | EXPORT_SYMBOL(kbd_alloc); |
| 514 | EXPORT_SYMBOL(kbd_keycode); |