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