1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Written for linux by Johan Myreen as a translation from 4 * the assembly version by Linus (with diacriticals added) 5 * 6 * Some additional features added by Christoph Niemann (ChN), March 1993 7 * 8 * Loadable keymaps by Risto Kankkunen, May 1993 9 * 10 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993 11 * Added decr/incr_console, dynamic keymaps, Unicode support, 12 * dynamic function/string keys, led setting, Sept 1994 13 * `Sticky' modifier keys, 951006. 14 * 15 * 11-11-96: SAK should now work in the raw mode (Martin Mares) 16 * 17 * Modified to provide 'generic' keyboard support by Hamish Macdonald 18 * Merge with the m68k keyboard driver and split-off of the PC low-level 19 * parts by Geert Uytterhoeven, May 1997 20 * 21 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares) 22 * 30-07-98: Dead keys redone, aeb@cwi.nl. 23 * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik) 24 */ 25 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 27 28 #include <linux/consolemap.h> 29 #include <linux/init.h> 30 #include <linux/input.h> 31 #include <linux/jiffies.h> 32 #include <linux/kbd_diacr.h> 33 #include <linux/kbd_kern.h> 34 #include <linux/leds.h> 35 #include <linux/mm.h> 36 #include <linux/module.h> 37 #include <linux/nospec.h> 38 #include <linux/notifier.h> 39 #include <linux/reboot.h> 40 #include <linux/sched/debug.h> 41 #include <linux/sched/signal.h> 42 #include <linux/slab.h> 43 #include <linux/spinlock.h> 44 #include <linux/string.h> 45 #include <linux/tty_flip.h> 46 #include <linux/tty.h> 47 #include <linux/uaccess.h> 48 #include <linux/vt_kern.h> 49 50 #include <asm/irq_regs.h> 51 52 /* 53 * Exported functions/variables 54 */ 55 56 #define KBD_DEFMODE (BIT(VC_REPEAT) | BIT(VC_META)) 57 58 #if defined(CONFIG_X86) || defined(CONFIG_PARISC) 59 #include <asm/kbdleds.h> 60 #else 61 static inline int kbd_defleds(void) 62 { 63 return 0; 64 } 65 #endif 66 67 #define KBD_DEFLOCK 0 68 69 /* 70 * Handler Tables. 71 */ 72 73 #define K_HANDLERS\ 74 k_self, k_fn, k_spec, k_pad,\ 75 k_dead, k_cons, k_cur, k_shift,\ 76 k_meta, k_ascii, k_lock, k_lowercase,\ 77 k_slock, k_dead2, k_brl, k_csi 78 79 typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value, 80 char up_flag); 81 static k_handler_fn K_HANDLERS; 82 static k_handler_fn *k_handler[16] = { K_HANDLERS }; 83 84 #define FN_HANDLERS\ 85 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\ 86 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\ 87 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\ 88 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\ 89 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num 90 91 typedef void (fn_handler_fn)(struct vc_data *vc); 92 static fn_handler_fn FN_HANDLERS; 93 static fn_handler_fn *fn_handler[] = { FN_HANDLERS }; 94 95 /* 96 * Variables exported for vt_ioctl.c 97 */ 98 99 struct vt_spawn_console vt_spawn_con = { 100 .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock), 101 .pid = NULL, 102 .sig = 0, 103 }; 104 105 106 /* 107 * Internal Data. 108 */ 109 110 static struct kbd_struct kbd_table[MAX_NR_CONSOLES]; 111 static struct kbd_struct *kbd = kbd_table; 112 113 /* maximum values each key_handler can handle */ 114 static const unsigned char max_vals[] = { 115 [ KT_LATIN ] = 255, 116 [ KT_FN ] = ARRAY_SIZE(func_table) - 1, 117 [ KT_SPEC ] = ARRAY_SIZE(fn_handler) - 1, 118 [ KT_PAD ] = NR_PAD - 1, 119 [ KT_DEAD ] = NR_DEAD - 1, 120 [ KT_CONS ] = 255, 121 [ KT_CUR ] = 3, 122 [ KT_SHIFT ] = NR_SHIFT - 1, 123 [ KT_META ] = 255, 124 [ KT_ASCII ] = NR_ASCII - 1, 125 [ KT_LOCK ] = NR_LOCK - 1, 126 [ KT_LETTER ] = 255, 127 [ KT_SLOCK ] = NR_LOCK - 1, 128 [ KT_DEAD2 ] = 255, 129 [ KT_BRL ] = NR_BRL - 1, 130 [ KT_CSI ] = 99, 131 }; 132 133 static const int NR_TYPES = ARRAY_SIZE(max_vals); 134 135 static void kbd_bh(struct tasklet_struct *unused); 136 static DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh); 137 138 static struct input_handler kbd_handler; 139 static DEFINE_SPINLOCK(kbd_event_lock); 140 static DEFINE_SPINLOCK(led_lock); 141 static DEFINE_SPINLOCK(func_buf_lock); /* guard 'func_buf' and friends */ 142 static DECLARE_BITMAP(key_down, KEY_CNT); /* keyboard key bitmap */ 143 static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */ 144 static bool dead_key_next; 145 146 /* Handles a number being assembled on the number pad */ 147 static bool npadch_active; 148 static unsigned int npadch_value; 149 150 static unsigned int diacr; 151 static bool rep; /* flag telling character repeat */ 152 153 static int shift_state = 0; 154 155 static unsigned int ledstate = -1U; /* undefined */ 156 static unsigned char ledioctl; 157 static bool vt_switch; 158 159 /* 160 * Notifier list for console keyboard events 161 */ 162 static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list); 163 164 int register_keyboard_notifier(struct notifier_block *nb) 165 { 166 return atomic_notifier_chain_register(&keyboard_notifier_list, nb); 167 } 168 EXPORT_SYMBOL_GPL(register_keyboard_notifier); 169 170 int unregister_keyboard_notifier(struct notifier_block *nb) 171 { 172 return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb); 173 } 174 EXPORT_SYMBOL_GPL(unregister_keyboard_notifier); 175 176 /* 177 * Translation of scancodes to keycodes. We set them on only the first 178 * keyboard in the list that accepts the scancode and keycode. 179 * Explanation for not choosing the first attached keyboard anymore: 180 * USB keyboards for example have two event devices: one for all "normal" 181 * keys and one for extra function keys (like "volume up", "make coffee", 182 * etc.). So this means that scancodes for the extra function keys won't 183 * be valid for the first event device, but will be for the second. 184 */ 185 186 struct getset_keycode_data { 187 struct input_keymap_entry ke; 188 int error; 189 }; 190 191 static int getkeycode_helper(struct input_handle *handle, void *data) 192 { 193 struct getset_keycode_data *d = data; 194 195 d->error = input_get_keycode(handle->dev, &d->ke); 196 197 return d->error == 0; /* stop as soon as we successfully get one */ 198 } 199 200 static int getkeycode(unsigned int scancode) 201 { 202 struct getset_keycode_data d = { 203 .ke = { 204 .flags = 0, 205 .len = sizeof(scancode), 206 .keycode = 0, 207 }, 208 .error = -ENODEV, 209 }; 210 211 memcpy(d.ke.scancode, &scancode, sizeof(scancode)); 212 213 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper); 214 215 return d.error ?: d.ke.keycode; 216 } 217 218 static int setkeycode_helper(struct input_handle *handle, void *data) 219 { 220 struct getset_keycode_data *d = data; 221 222 d->error = input_set_keycode(handle->dev, &d->ke); 223 224 return d->error == 0; /* stop as soon as we successfully set one */ 225 } 226 227 static int setkeycode(unsigned int scancode, unsigned int keycode) 228 { 229 struct getset_keycode_data d = { 230 .ke = { 231 .flags = 0, 232 .len = sizeof(scancode), 233 .keycode = keycode, 234 }, 235 .error = -ENODEV, 236 }; 237 238 memcpy(d.ke.scancode, &scancode, sizeof(scancode)); 239 240 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper); 241 242 return d.error; 243 } 244 245 /* 246 * Making beeps and bells. Note that we prefer beeps to bells, but when 247 * shutting the sound off we do both. 248 */ 249 250 static int kd_sound_helper(struct input_handle *handle, void *data) 251 { 252 unsigned int *hz = data; 253 struct input_dev *dev = handle->dev; 254 255 if (test_bit(EV_SND, dev->evbit)) { 256 if (test_bit(SND_TONE, dev->sndbit)) { 257 input_inject_event(handle, EV_SND, SND_TONE, *hz); 258 if (*hz) 259 return 0; 260 } 261 if (test_bit(SND_BELL, dev->sndbit)) 262 input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0); 263 } 264 265 return 0; 266 } 267 268 static void kd_nosound(struct timer_list *unused) 269 { 270 static unsigned int zero; 271 272 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper); 273 } 274 275 static DEFINE_TIMER(kd_mksound_timer, kd_nosound); 276 277 void kd_mksound(unsigned int hz, unsigned int ticks) 278 { 279 timer_delete_sync(&kd_mksound_timer); 280 281 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper); 282 283 if (hz && ticks) 284 mod_timer(&kd_mksound_timer, jiffies + ticks); 285 } 286 EXPORT_SYMBOL(kd_mksound); 287 288 /* 289 * Setting the keyboard rate. 290 */ 291 292 static int kbd_rate_helper(struct input_handle *handle, void *data) 293 { 294 struct input_dev *dev = handle->dev; 295 struct kbd_repeat *rpt = data; 296 297 if (test_bit(EV_REP, dev->evbit)) { 298 299 if (rpt[0].delay > 0) 300 input_inject_event(handle, 301 EV_REP, REP_DELAY, rpt[0].delay); 302 if (rpt[0].period > 0) 303 input_inject_event(handle, 304 EV_REP, REP_PERIOD, rpt[0].period); 305 306 rpt[1].delay = dev->rep[REP_DELAY]; 307 rpt[1].period = dev->rep[REP_PERIOD]; 308 } 309 310 return 0; 311 } 312 313 int kbd_rate(struct kbd_repeat *rpt) 314 { 315 struct kbd_repeat data[2] = { *rpt }; 316 317 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper); 318 *rpt = data[1]; /* Copy currently used settings */ 319 320 return 0; 321 } 322 323 /* 324 * Helper Functions. 325 */ 326 static void put_queue(struct vc_data *vc, int ch) 327 { 328 tty_insert_flip_char(&vc->port, ch, 0); 329 tty_flip_buffer_push(&vc->port); 330 } 331 332 static void puts_queue(struct vc_data *vc, const char *cp) 333 { 334 tty_insert_flip_string(&vc->port, cp, strlen(cp)); 335 tty_flip_buffer_push(&vc->port); 336 } 337 338 static void applkey(struct vc_data *vc, int key, char mode) 339 { 340 static char buf[] = { 0x1b, 'O', 0x00, 0x00 }; 341 342 buf[1] = (mode ? 'O' : '['); 343 buf[2] = key; 344 puts_queue(vc, buf); 345 } 346 347 /* 348 * Many other routines do put_queue, but I think either 349 * they produce ASCII, or they produce some user-assigned 350 * string, and in both cases we might assume that it is 351 * in utf-8 already. 352 */ 353 static void to_utf8(struct vc_data *vc, uint c) 354 { 355 if (c < 0x80) 356 /* 0******* */ 357 put_queue(vc, c); 358 else if (c < 0x800) { 359 /* 110***** 10****** */ 360 put_queue(vc, 0xc0 | (c >> 6)); 361 put_queue(vc, 0x80 | (c & 0x3f)); 362 } else if (c < 0x10000) { 363 if (c >= 0xD800 && c < 0xE000) 364 return; 365 if (c == 0xFFFF) 366 return; 367 /* 1110**** 10****** 10****** */ 368 put_queue(vc, 0xe0 | (c >> 12)); 369 put_queue(vc, 0x80 | ((c >> 6) & 0x3f)); 370 put_queue(vc, 0x80 | (c & 0x3f)); 371 } else if (c < 0x110000) { 372 /* 11110*** 10****** 10****** 10****** */ 373 put_queue(vc, 0xf0 | (c >> 18)); 374 put_queue(vc, 0x80 | ((c >> 12) & 0x3f)); 375 put_queue(vc, 0x80 | ((c >> 6) & 0x3f)); 376 put_queue(vc, 0x80 | (c & 0x3f)); 377 } 378 } 379 380 static void put_queue_utf8(struct vc_data *vc, u32 value) 381 { 382 if (kbd->kbdmode == VC_UNICODE) 383 to_utf8(vc, value); 384 else { 385 int c = conv_uni_to_8bit(value); 386 if (c != -1) 387 put_queue(vc, c); 388 } 389 } 390 391 /* FIXME: review locking for vt.c callers */ 392 static void set_leds(void) 393 { 394 tasklet_schedule(&keyboard_tasklet); 395 } 396 397 /* 398 * Called after returning from RAW mode or when changing consoles - recompute 399 * shift_down[] and shift_state from key_down[] maybe called when keymap is 400 * undefined, so that shiftkey release is seen. The caller must hold the 401 * kbd_event_lock. 402 */ 403 404 static void do_compute_shiftstate(void) 405 { 406 unsigned int k, sym, val; 407 408 shift_state = 0; 409 memset(shift_down, 0, sizeof(shift_down)); 410 411 for_each_set_bit(k, key_down, min(NR_KEYS, KEY_CNT)) { 412 sym = U(key_maps[0][k]); 413 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK) 414 continue; 415 416 val = KVAL(sym); 417 if (val == KVAL(K_CAPSSHIFT)) 418 val = KVAL(K_SHIFT); 419 420 shift_down[val]++; 421 shift_state |= BIT(val); 422 } 423 } 424 425 /* We still have to export this method to vt.c */ 426 void vt_set_leds_compute_shiftstate(void) 427 { 428 /* 429 * When VT is switched, the keyboard led needs to be set once. 430 * Ensure that after the switch is completed, the state of the 431 * keyboard LED is consistent with the state of the keyboard lock. 432 */ 433 vt_switch = true; 434 set_leds(); 435 436 guard(spinlock_irqsave)(&kbd_event_lock); 437 do_compute_shiftstate(); 438 } 439 440 /* 441 * We have a combining character DIACR here, followed by the character CH. 442 * If the combination occurs in the table, return the corresponding value. 443 * Otherwise, if CH is a space or equals DIACR, return DIACR. 444 * Otherwise, conclude that DIACR was not combining after all, 445 * queue it and return CH. 446 */ 447 static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch) 448 { 449 unsigned int d = diacr; 450 unsigned int i; 451 452 diacr = 0; 453 454 if ((d & ~0xff) == BRL_UC_ROW) { 455 if ((ch & ~0xff) == BRL_UC_ROW) 456 return d | ch; 457 } else { 458 for (i = 0; i < accent_table_size; i++) 459 if (accent_table[i].diacr == d && accent_table[i].base == ch) 460 return accent_table[i].result; 461 } 462 463 if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d) 464 return d; 465 466 put_queue_utf8(vc, d); 467 468 return ch; 469 } 470 471 /* 472 * Special function handlers 473 */ 474 static void fn_enter(struct vc_data *vc) 475 { 476 if (diacr) { 477 put_queue_utf8(vc, diacr); 478 diacr = 0; 479 } 480 481 put_queue(vc, '\r'); 482 if (vc_kbd_mode(kbd, VC_CRLF)) 483 put_queue(vc, '\n'); 484 } 485 486 static void fn_caps_toggle(struct vc_data *vc) 487 { 488 if (rep) 489 return; 490 491 chg_vc_kbd_led(kbd, VC_CAPSLOCK); 492 } 493 494 static void fn_caps_on(struct vc_data *vc) 495 { 496 if (rep) 497 return; 498 499 set_vc_kbd_led(kbd, VC_CAPSLOCK); 500 } 501 502 static void fn_show_ptregs(struct vc_data *vc) 503 { 504 struct pt_regs *regs = get_irq_regs(); 505 506 if (regs) 507 show_regs(regs); 508 } 509 510 static void fn_hold(struct vc_data *vc) 511 { 512 struct tty_struct *tty = vc->port.tty; 513 514 if (rep || !tty) 515 return; 516 517 /* 518 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty); 519 * these routines are also activated by ^S/^Q. 520 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.) 521 */ 522 if (tty->flow.stopped) 523 start_tty(tty); 524 else 525 stop_tty(tty); 526 } 527 528 static void fn_num(struct vc_data *vc) 529 { 530 if (vc_kbd_mode(kbd, VC_APPLIC)) 531 applkey(vc, 'P', 1); 532 else 533 fn_bare_num(vc); 534 } 535 536 /* 537 * Bind this to Shift-NumLock if you work in application keypad mode 538 * but want to be able to change the NumLock flag. 539 * Bind this to NumLock if you prefer that the NumLock key always 540 * changes the NumLock flag. 541 */ 542 static void fn_bare_num(struct vc_data *vc) 543 { 544 if (!rep) 545 chg_vc_kbd_led(kbd, VC_NUMLOCK); 546 } 547 548 static void fn_lastcons(struct vc_data *vc) 549 { 550 /* switch to the last used console, ChN */ 551 set_console(last_console); 552 } 553 554 static void fn_dec_console(struct vc_data *vc) 555 { 556 int i, cur = fg_console; 557 558 /* Currently switching? Queue this next switch relative to that. */ 559 if (want_console != -1) 560 cur = want_console; 561 562 for (i = cur - 1; i != cur; i--) { 563 if (i == -1) 564 i = MAX_NR_CONSOLES - 1; 565 if (vc_cons_allocated(i)) 566 break; 567 } 568 set_console(i); 569 } 570 571 static void fn_inc_console(struct vc_data *vc) 572 { 573 int i, cur = fg_console; 574 575 /* Currently switching? Queue this next switch relative to that. */ 576 if (want_console != -1) 577 cur = want_console; 578 579 for (i = cur+1; i != cur; i++) { 580 if (i == MAX_NR_CONSOLES) 581 i = 0; 582 if (vc_cons_allocated(i)) 583 break; 584 } 585 set_console(i); 586 } 587 588 static void fn_send_intr(struct vc_data *vc) 589 { 590 tty_insert_flip_char(&vc->port, 0, TTY_BREAK); 591 tty_flip_buffer_push(&vc->port); 592 } 593 594 static void fn_scroll_forw(struct vc_data *vc) 595 { 596 scrollfront(vc, 0); 597 } 598 599 static void fn_scroll_back(struct vc_data *vc) 600 { 601 scrollback(vc); 602 } 603 604 static void fn_show_mem(struct vc_data *vc) 605 { 606 show_mem(); 607 } 608 609 static void fn_show_state(struct vc_data *vc) 610 { 611 show_state(); 612 } 613 614 static void fn_boot_it(struct vc_data *vc) 615 { 616 ctrl_alt_del(); 617 } 618 619 static void fn_compose(struct vc_data *vc) 620 { 621 dead_key_next = true; 622 } 623 624 static void fn_spawn_con(struct vc_data *vc) 625 { 626 guard(spinlock)(&vt_spawn_con.lock); 627 if (vt_spawn_con.pid) 628 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) { 629 put_pid(vt_spawn_con.pid); 630 vt_spawn_con.pid = NULL; 631 } 632 } 633 634 static void fn_SAK(struct vc_data *vc) 635 { 636 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work; 637 schedule_work(SAK_work); 638 } 639 640 static void fn_null(struct vc_data *vc) 641 { 642 do_compute_shiftstate(); 643 } 644 645 /* 646 * Special key handlers 647 */ 648 static void k_spec(struct vc_data *vc, unsigned char value, char up_flag) 649 { 650 if (up_flag) 651 return; 652 if (value >= ARRAY_SIZE(fn_handler)) 653 return; 654 if ((kbd->kbdmode == VC_RAW || 655 kbd->kbdmode == VC_MEDIUMRAW || 656 kbd->kbdmode == VC_OFF) && 657 value != KVAL(K_SAK)) 658 return; /* SAK is allowed even in raw mode */ 659 fn_handler[value](vc); 660 } 661 662 static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag) 663 { 664 pr_err("k_lowercase was called - impossible\n"); 665 } 666 667 static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag) 668 { 669 if (up_flag) 670 return; /* no action, if this is a key release */ 671 672 if (diacr) 673 value = handle_diacr(vc, value); 674 675 if (dead_key_next) { 676 dead_key_next = false; 677 diacr = value; 678 return; 679 } 680 put_queue_utf8(vc, value); 681 } 682 683 /* 684 * Handle dead key. Note that we now may have several 685 * dead keys modifying the same character. Very useful 686 * for Vietnamese. 687 */ 688 static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag) 689 { 690 if (up_flag) 691 return; 692 693 diacr = (diacr ? handle_diacr(vc, value) : value); 694 } 695 696 static void k_self(struct vc_data *vc, unsigned char value, char up_flag) 697 { 698 k_unicode(vc, conv_8bit_to_uni(value), up_flag); 699 } 700 701 static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag) 702 { 703 k_deadunicode(vc, value, up_flag); 704 } 705 706 /* 707 * Obsolete - for backwards compatibility only 708 */ 709 static void k_dead(struct vc_data *vc, unsigned char value, char up_flag) 710 { 711 static const unsigned char ret_diacr[NR_DEAD] = { 712 '`', /* dead_grave */ 713 '\'', /* dead_acute */ 714 '^', /* dead_circumflex */ 715 '~', /* dead_tilda */ 716 '"', /* dead_diaeresis */ 717 ',', /* dead_cedilla */ 718 '_', /* dead_macron */ 719 'U', /* dead_breve */ 720 '.', /* dead_abovedot */ 721 '*', /* dead_abovering */ 722 '=', /* dead_doubleacute */ 723 'c', /* dead_caron */ 724 'k', /* dead_ogonek */ 725 'i', /* dead_iota */ 726 '#', /* dead_voiced_sound */ 727 'o', /* dead_semivoiced_sound */ 728 '!', /* dead_belowdot */ 729 '?', /* dead_hook */ 730 '+', /* dead_horn */ 731 '-', /* dead_stroke */ 732 ')', /* dead_abovecomma */ 733 '(', /* dead_abovereversedcomma */ 734 ':', /* dead_doublegrave */ 735 'n', /* dead_invertedbreve */ 736 ';', /* dead_belowcomma */ 737 '$', /* dead_currency */ 738 '@', /* dead_greek */ 739 }; 740 741 k_deadunicode(vc, ret_diacr[value], up_flag); 742 } 743 744 static void k_cons(struct vc_data *vc, unsigned char value, char up_flag) 745 { 746 if (up_flag) 747 return; 748 749 set_console(value); 750 } 751 752 static void k_fn(struct vc_data *vc, unsigned char value, char up_flag) 753 { 754 if (up_flag) 755 return; 756 757 if ((unsigned)value < ARRAY_SIZE(func_table)) { 758 guard(spinlock_irqsave)(&func_buf_lock); 759 if (func_table[value]) 760 puts_queue(vc, func_table[value]); 761 } else 762 pr_err("k_fn called with value=%d\n", value); 763 } 764 765 /* 766 * Compute xterm-style modifier parameter for CSI sequences. 767 * Returns 1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0) 768 */ 769 static int csi_modifier_param(void) 770 { 771 int mod = 1; 772 773 if (shift_state & (BIT(KG_SHIFT) | BIT(KG_SHIFTL) | BIT(KG_SHIFTR))) 774 mod += 1; 775 if (shift_state & (BIT(KG_ALT) | BIT(KG_ALTGR))) 776 mod += 2; 777 if (shift_state & (BIT(KG_CTRL) | BIT(KG_CTRLL) | BIT(KG_CTRLR))) 778 mod += 4; 779 return mod; 780 } 781 782 static void k_cur(struct vc_data *vc, unsigned char value, char up_flag) 783 { 784 static const char cur_chars[] = "BDCA"; 785 int mod; 786 787 if (up_flag) 788 return; 789 790 mod = csi_modifier_param(); 791 if (mod > 1) { 792 char buf[] = { 0x1b, '[', '1', ';', '0' + mod, cur_chars[value], 0x00 }; 793 794 puts_queue(vc, buf); 795 } else { 796 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE)); 797 } 798 } 799 800 static void k_pad(struct vc_data *vc, unsigned char value, char up_flag) 801 { 802 static const char pad_chars[] = "0123456789+-*/\015,.?()#"; 803 static const char app_map[] = "pqrstuvwxylSRQMnnmPQS"; 804 805 if (up_flag) 806 return; /* no action, if this is a key release */ 807 808 /* kludge... shift forces cursor/number keys */ 809 if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) { 810 applkey(vc, app_map[value], 1); 811 return; 812 } 813 814 if (!vc_kbd_led(kbd, VC_NUMLOCK)) { 815 816 switch (value) { 817 case KVAL(K_PCOMMA): 818 case KVAL(K_PDOT): 819 k_fn(vc, KVAL(K_REMOVE), 0); 820 return; 821 case KVAL(K_P0): 822 k_fn(vc, KVAL(K_INSERT), 0); 823 return; 824 case KVAL(K_P1): 825 k_fn(vc, KVAL(K_SELECT), 0); 826 return; 827 case KVAL(K_P2): 828 k_cur(vc, KVAL(K_DOWN), 0); 829 return; 830 case KVAL(K_P3): 831 k_fn(vc, KVAL(K_PGDN), 0); 832 return; 833 case KVAL(K_P4): 834 k_cur(vc, KVAL(K_LEFT), 0); 835 return; 836 case KVAL(K_P6): 837 k_cur(vc, KVAL(K_RIGHT), 0); 838 return; 839 case KVAL(K_P7): 840 k_fn(vc, KVAL(K_FIND), 0); 841 return; 842 case KVAL(K_P8): 843 k_cur(vc, KVAL(K_UP), 0); 844 return; 845 case KVAL(K_P9): 846 k_fn(vc, KVAL(K_PGUP), 0); 847 return; 848 case KVAL(K_P5): 849 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC)); 850 return; 851 } 852 } 853 854 put_queue(vc, pad_chars[value]); 855 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF)) 856 put_queue(vc, '\n'); 857 } 858 859 static void k_shift(struct vc_data *vc, unsigned char value, char up_flag) 860 { 861 int old_state = shift_state; 862 863 if (rep) 864 return; 865 /* 866 * Mimic typewriter: 867 * a CapsShift key acts like Shift but undoes CapsLock 868 */ 869 if (value == KVAL(K_CAPSSHIFT)) { 870 value = KVAL(K_SHIFT); 871 if (!up_flag) 872 clr_vc_kbd_led(kbd, VC_CAPSLOCK); 873 } 874 875 if (up_flag) { 876 /* 877 * handle the case that two shift or control 878 * keys are depressed simultaneously 879 */ 880 if (shift_down[value]) 881 shift_down[value]--; 882 } else 883 shift_down[value]++; 884 885 if (shift_down[value]) 886 shift_state |= BIT(value); 887 else 888 shift_state &= ~BIT(value); 889 890 /* kludge */ 891 if (up_flag && shift_state != old_state && npadch_active) { 892 if (kbd->kbdmode == VC_UNICODE) 893 to_utf8(vc, npadch_value); 894 else 895 put_queue(vc, npadch_value & 0xff); 896 npadch_active = false; 897 } 898 } 899 900 static void k_meta(struct vc_data *vc, unsigned char value, char up_flag) 901 { 902 if (up_flag) 903 return; 904 905 if (vc_kbd_mode(kbd, VC_META)) { 906 put_queue(vc, '\033'); 907 put_queue(vc, value); 908 } else 909 put_queue(vc, value | BIT(7)); 910 } 911 912 static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag) 913 { 914 unsigned int base; 915 916 if (up_flag) 917 return; 918 919 if (value < 10) { 920 /* decimal input of code, while Alt depressed */ 921 base = 10; 922 } else { 923 /* hexadecimal input of code, while AltGr depressed */ 924 value -= 10; 925 base = 16; 926 } 927 928 if (!npadch_active) { 929 npadch_value = 0; 930 npadch_active = true; 931 } 932 933 npadch_value = npadch_value * base + value; 934 } 935 936 static void k_lock(struct vc_data *vc, unsigned char value, char up_flag) 937 { 938 if (up_flag || rep) 939 return; 940 941 chg_vc_kbd_lock(kbd, value); 942 } 943 944 static void k_slock(struct vc_data *vc, unsigned char value, char up_flag) 945 { 946 k_shift(vc, value, up_flag); 947 if (up_flag || rep) 948 return; 949 950 chg_vc_kbd_slock(kbd, value); 951 /* try to make Alt, oops, AltGr and such work */ 952 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) { 953 kbd->slockstate = 0; 954 chg_vc_kbd_slock(kbd, value); 955 } 956 } 957 958 /* by default, 300ms interval for combination release */ 959 static unsigned brl_timeout = 300; 960 MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)"); 961 module_param(brl_timeout, uint, 0644); 962 963 static unsigned brl_nbchords = 1; 964 MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)"); 965 module_param(brl_nbchords, uint, 0644); 966 967 static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag) 968 { 969 static unsigned long chords; 970 static unsigned committed; 971 972 if (!brl_nbchords) 973 k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag); 974 else { 975 committed |= pattern; 976 chords++; 977 if (chords == brl_nbchords) { 978 k_unicode(vc, BRL_UC_ROW | committed, up_flag); 979 chords = 0; 980 committed = 0; 981 } 982 } 983 } 984 985 static void k_brl(struct vc_data *vc, unsigned char value, char up_flag) 986 { 987 static unsigned pressed, committing; 988 static unsigned long releasestart; 989 990 if (kbd->kbdmode != VC_UNICODE) { 991 if (!up_flag) 992 pr_warn("keyboard mode must be unicode for braille patterns\n"); 993 return; 994 } 995 996 if (!value) { 997 k_unicode(vc, BRL_UC_ROW, up_flag); 998 return; 999 } 1000 1001 if (value > 8) 1002 return; 1003 1004 if (!up_flag) { 1005 pressed |= BIT(value - 1); 1006 if (!brl_timeout) 1007 committing = pressed; 1008 } else if (brl_timeout) { 1009 if (!committing || 1010 time_after(jiffies, 1011 releasestart + msecs_to_jiffies(brl_timeout))) { 1012 committing = pressed; 1013 releasestart = jiffies; 1014 } 1015 pressed &= ~BIT(value - 1); 1016 if (!pressed && committing) { 1017 k_brlcommit(vc, committing, 0); 1018 committing = 0; 1019 } 1020 } else { 1021 if (committing) { 1022 k_brlcommit(vc, committing, 0); 1023 committing = 0; 1024 } 1025 pressed &= ~BIT(value - 1); 1026 } 1027 } 1028 1029 /* 1030 * Handle KT_CSI keysym type: generate CSI tilde sequences with modifier 1031 * support. The value encodes the CSI parameter number, producing sequences 1032 * like ESC [ <value> ~ or ESC [ <value> ; <mod> ~ when modifiers are held. 1033 */ 1034 static void k_csi(struct vc_data *vc, unsigned char value, char up_flag) 1035 { 1036 char buf[10]; 1037 int i = 0; 1038 int mod; 1039 1040 if (up_flag) 1041 return; 1042 1043 mod = csi_modifier_param(); 1044 1045 buf[i++] = 0x1b; 1046 buf[i++] = '['; 1047 if (value >= 10) 1048 buf[i++] = '0' + value / 10; 1049 buf[i++] = '0' + value % 10; 1050 if (mod > 1) { 1051 buf[i++] = ';'; 1052 buf[i++] = '0' + mod; 1053 } 1054 buf[i++] = '~'; 1055 buf[i] = 0x00; 1056 1057 puts_queue(vc, buf); 1058 } 1059 1060 #if IS_ENABLED(CONFIG_INPUT_LEDS) && IS_ENABLED(CONFIG_LEDS_TRIGGERS) 1061 1062 struct kbd_led_trigger { 1063 struct led_trigger trigger; 1064 unsigned int mask; 1065 }; 1066 1067 static int kbd_led_trigger_activate(struct led_classdev *cdev) 1068 { 1069 struct kbd_led_trigger *trigger = 1070 container_of(cdev->trigger, struct kbd_led_trigger, trigger); 1071 1072 tasklet_disable(&keyboard_tasklet); 1073 if (ledstate != -1U) 1074 led_set_brightness(cdev, ledstate & trigger->mask ? LED_FULL : LED_OFF); 1075 tasklet_enable(&keyboard_tasklet); 1076 1077 return 0; 1078 } 1079 1080 #define KBD_LED_TRIGGER(_led_bit, _name) { \ 1081 .trigger = { \ 1082 .name = _name, \ 1083 .activate = kbd_led_trigger_activate, \ 1084 }, \ 1085 .mask = BIT(_led_bit), \ 1086 } 1087 1088 #define KBD_LOCKSTATE_TRIGGER(_led_bit, _name) \ 1089 KBD_LED_TRIGGER((_led_bit) + 8, _name) 1090 1091 static struct kbd_led_trigger kbd_led_triggers[] = { 1092 KBD_LED_TRIGGER(VC_SCROLLOCK, "kbd-scrolllock"), 1093 KBD_LED_TRIGGER(VC_NUMLOCK, "kbd-numlock"), 1094 KBD_LED_TRIGGER(VC_CAPSLOCK, "kbd-capslock"), 1095 KBD_LED_TRIGGER(VC_KANALOCK, "kbd-kanalock"), 1096 1097 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLOCK, "kbd-shiftlock"), 1098 KBD_LOCKSTATE_TRIGGER(VC_ALTGRLOCK, "kbd-altgrlock"), 1099 KBD_LOCKSTATE_TRIGGER(VC_CTRLLOCK, "kbd-ctrllock"), 1100 KBD_LOCKSTATE_TRIGGER(VC_ALTLOCK, "kbd-altlock"), 1101 KBD_LOCKSTATE_TRIGGER(VC_SHIFTLLOCK, "kbd-shiftllock"), 1102 KBD_LOCKSTATE_TRIGGER(VC_SHIFTRLOCK, "kbd-shiftrlock"), 1103 KBD_LOCKSTATE_TRIGGER(VC_CTRLLLOCK, "kbd-ctrlllock"), 1104 KBD_LOCKSTATE_TRIGGER(VC_CTRLRLOCK, "kbd-ctrlrlock"), 1105 }; 1106 1107 static void kbd_propagate_led_state(unsigned int old_state, 1108 unsigned int new_state) 1109 { 1110 struct kbd_led_trigger *trigger; 1111 unsigned int changed = old_state ^ new_state; 1112 int i; 1113 1114 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) { 1115 trigger = &kbd_led_triggers[i]; 1116 1117 if (changed & trigger->mask) 1118 led_trigger_event(&trigger->trigger, 1119 new_state & trigger->mask ? 1120 LED_FULL : LED_OFF); 1121 } 1122 } 1123 1124 static int kbd_update_leds_helper(struct input_handle *handle, void *data) 1125 { 1126 unsigned int led_state = *(unsigned int *)data; 1127 1128 if (test_bit(EV_LED, handle->dev->evbit)) 1129 kbd_propagate_led_state(~led_state, led_state); 1130 1131 return 0; 1132 } 1133 1134 static void kbd_init_leds(void) 1135 { 1136 int error; 1137 int i; 1138 1139 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); i++) { 1140 error = led_trigger_register(&kbd_led_triggers[i].trigger); 1141 if (error) 1142 pr_err("error %d while registering trigger %s\n", 1143 error, kbd_led_triggers[i].trigger.name); 1144 } 1145 } 1146 1147 #else 1148 1149 static int kbd_update_leds_helper(struct input_handle *handle, void *data) 1150 { 1151 unsigned int leds = *(unsigned int *)data; 1152 1153 if (test_bit(EV_LED, handle->dev->evbit)) { 1154 input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & BIT(0))); 1155 input_inject_event(handle, EV_LED, LED_NUML, !!(leds & BIT(1))); 1156 input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & BIT(2))); 1157 input_inject_event(handle, EV_SYN, SYN_REPORT, 0); 1158 } 1159 1160 return 0; 1161 } 1162 1163 static void kbd_propagate_led_state(unsigned int old_state, 1164 unsigned int new_state) 1165 { 1166 input_handler_for_each_handle(&kbd_handler, &new_state, 1167 kbd_update_leds_helper); 1168 } 1169 1170 static void kbd_init_leds(void) 1171 { 1172 } 1173 1174 #endif 1175 1176 /* 1177 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock, 1178 * or (ii) whatever pattern of lights people want to show using KDSETLED, 1179 * or (iii) specified bits of specified words in kernel memory. 1180 */ 1181 static unsigned char getledstate(void) 1182 { 1183 return ledstate & 0xff; 1184 } 1185 1186 void setledstate(struct kbd_struct *kb, unsigned int led) 1187 { 1188 guard(spinlock_irqsave)(&led_lock); 1189 if (!(led & ~7)) { 1190 ledioctl = led; 1191 kb->ledmode = LED_SHOW_IOCTL; 1192 } else 1193 kb->ledmode = LED_SHOW_FLAGS; 1194 1195 set_leds(); 1196 } 1197 1198 static inline unsigned char getleds(void) 1199 { 1200 struct kbd_struct *kb = kbd_table + fg_console; 1201 1202 if (kb->ledmode == LED_SHOW_IOCTL) 1203 return ledioctl; 1204 1205 return kb->ledflagstate; 1206 } 1207 1208 /** 1209 * vt_get_leds - helper for braille console 1210 * @console: console to read 1211 * @flag: flag we want to check 1212 * 1213 * Check the status of a keyboard led flag and report it back 1214 */ 1215 int vt_get_leds(unsigned int console, int flag) 1216 { 1217 struct kbd_struct *kb = &kbd_table[console]; 1218 1219 guard(spinlock_irqsave)(&led_lock); 1220 return vc_kbd_led(kb, flag); 1221 } 1222 EXPORT_SYMBOL_GPL(vt_get_leds); 1223 1224 /** 1225 * vt_set_led_state - set LED state of a console 1226 * @console: console to set 1227 * @leds: LED bits 1228 * 1229 * Set the LEDs on a console. This is a wrapper for the VT layer 1230 * so that we can keep kbd knowledge internal 1231 */ 1232 void vt_set_led_state(unsigned int console, int leds) 1233 { 1234 struct kbd_struct *kb = &kbd_table[console]; 1235 setledstate(kb, leds); 1236 } 1237 1238 /** 1239 * vt_kbd_con_start - Keyboard side of console start 1240 * @console: console 1241 * 1242 * Handle console start. This is a wrapper for the VT layer 1243 * so that we can keep kbd knowledge internal 1244 * 1245 * FIXME: We eventually need to hold the kbd lock here to protect 1246 * the LED updating. We can't do it yet because fn_hold calls stop_tty 1247 * and start_tty under the kbd_event_lock, while normal tty paths 1248 * don't hold the lock. We probably need to split out an LED lock 1249 * but not during an -rc release! 1250 */ 1251 void vt_kbd_con_start(unsigned int console) 1252 { 1253 struct kbd_struct *kb = &kbd_table[console]; 1254 1255 guard(spinlock_irqsave)(&led_lock); 1256 clr_vc_kbd_led(kb, VC_SCROLLOCK); 1257 set_leds(); 1258 } 1259 1260 /** 1261 * vt_kbd_con_stop - Keyboard side of console stop 1262 * @console: console 1263 * 1264 * Handle console stop. This is a wrapper for the VT layer 1265 * so that we can keep kbd knowledge internal 1266 */ 1267 void vt_kbd_con_stop(unsigned int console) 1268 { 1269 struct kbd_struct *kb = &kbd_table[console]; 1270 1271 guard(spinlock_irqsave)(&led_lock); 1272 set_vc_kbd_led(kb, VC_SCROLLOCK); 1273 set_leds(); 1274 } 1275 1276 /* 1277 * This is the tasklet that updates LED state of LEDs using standard 1278 * keyboard triggers. The reason we use tasklet is that we need to 1279 * handle the scenario when keyboard handler is not registered yet 1280 * but we already getting updates from the VT to update led state. 1281 */ 1282 static void kbd_bh(struct tasklet_struct *unused) 1283 { 1284 unsigned int leds; 1285 1286 scoped_guard(spinlock_irqsave, &led_lock) { 1287 leds = getleds(); 1288 leds |= (unsigned int)kbd->lockstate << 8; 1289 } 1290 1291 if (vt_switch) { 1292 ledstate = ~leds; 1293 vt_switch = false; 1294 } 1295 1296 if (leds != ledstate) { 1297 kbd_propagate_led_state(ledstate, leds); 1298 ledstate = leds; 1299 } 1300 } 1301 1302 #if defined(CONFIG_X86) || defined(CONFIG_ALPHA) ||\ 1303 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\ 1304 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\ 1305 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) 1306 1307 static inline bool kbd_is_hw_raw(const struct input_dev *dev) 1308 { 1309 if (!test_bit(EV_MSC, dev->evbit) || !test_bit(MSC_RAW, dev->mscbit)) 1310 return false; 1311 1312 return dev->id.bustype == BUS_I8042 && 1313 dev->id.vendor == 0x0001 && dev->id.product == 0x0001; 1314 } 1315 1316 static const unsigned short x86_keycodes[256] = 1317 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1318 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1319 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 1320 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 1321 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1322 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92, 1323 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339, 1324 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349, 1325 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355, 1326 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361, 1327 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114, 1328 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116, 1329 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307, 1330 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330, 1331 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 }; 1332 1333 #ifdef CONFIG_SPARC 1334 static int sparc_l1_a_state; 1335 extern void sun_do_break(void); 1336 #endif 1337 1338 static int emulate_raw(struct vc_data *vc, unsigned int keycode, 1339 unsigned char up_flag) 1340 { 1341 int code; 1342 1343 switch (keycode) { 1344 1345 case KEY_PAUSE: 1346 put_queue(vc, 0xe1); 1347 put_queue(vc, 0x1d | up_flag); 1348 put_queue(vc, 0x45 | up_flag); 1349 break; 1350 1351 case KEY_HANGEUL: 1352 if (!up_flag) 1353 put_queue(vc, 0xf2); 1354 break; 1355 1356 case KEY_HANJA: 1357 if (!up_flag) 1358 put_queue(vc, 0xf1); 1359 break; 1360 1361 case KEY_SYSRQ: 1362 /* 1363 * Real AT keyboards (that's what we're trying 1364 * to emulate here) emit 0xe0 0x2a 0xe0 0x37 when 1365 * pressing PrtSc/SysRq alone, but simply 0x54 1366 * when pressing Alt+PrtSc/SysRq. 1367 */ 1368 if (test_bit(KEY_LEFTALT, key_down) || 1369 test_bit(KEY_RIGHTALT, key_down)) { 1370 put_queue(vc, 0x54 | up_flag); 1371 } else { 1372 put_queue(vc, 0xe0); 1373 put_queue(vc, 0x2a | up_flag); 1374 put_queue(vc, 0xe0); 1375 put_queue(vc, 0x37 | up_flag); 1376 } 1377 break; 1378 1379 default: 1380 if (keycode > 255) 1381 return -1; 1382 1383 code = x86_keycodes[keycode]; 1384 if (!code) 1385 return -1; 1386 1387 if (code & 0x100) 1388 put_queue(vc, 0xe0); 1389 put_queue(vc, (code & 0x7f) | up_flag); 1390 1391 break; 1392 } 1393 1394 return 0; 1395 } 1396 1397 #else 1398 1399 static inline bool kbd_is_hw_raw(const struct input_dev *dev) 1400 { 1401 return false; 1402 } 1403 1404 static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag) 1405 { 1406 if (keycode > 127) 1407 return -1; 1408 1409 put_queue(vc, keycode | up_flag); 1410 return 0; 1411 } 1412 #endif 1413 1414 static void kbd_rawcode(unsigned char data) 1415 { 1416 struct vc_data *vc = vc_cons[fg_console].d; 1417 1418 kbd = &kbd_table[vc->vc_num]; 1419 if (kbd->kbdmode == VC_RAW) 1420 put_queue(vc, data); 1421 } 1422 1423 static void kbd_keycode(unsigned int keycode, int down, bool hw_raw) 1424 { 1425 struct vc_data *vc = vc_cons[fg_console].d; 1426 unsigned short keysym, *key_map; 1427 unsigned char type; 1428 bool raw_mode; 1429 struct tty_struct *tty; 1430 int shift_final; 1431 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down }; 1432 int rc; 1433 1434 tty = vc->port.tty; 1435 1436 if (tty && (!tty->driver_data)) { 1437 /* No driver data? Strange. Okay we fix it then. */ 1438 tty->driver_data = vc; 1439 } 1440 1441 kbd = &kbd_table[vc->vc_num]; 1442 1443 #ifdef CONFIG_SPARC 1444 if (keycode == KEY_STOP) 1445 sparc_l1_a_state = down; 1446 #endif 1447 1448 rep = (down == 2); 1449 1450 raw_mode = (kbd->kbdmode == VC_RAW); 1451 if (raw_mode && !hw_raw) 1452 if (emulate_raw(vc, keycode, !down << 7)) 1453 if (keycode < BTN_MISC && printk_ratelimit()) 1454 pr_warn("can't emulate rawmode for keycode %d\n", 1455 keycode); 1456 1457 #ifdef CONFIG_SPARC 1458 if (keycode == KEY_A && sparc_l1_a_state) { 1459 sparc_l1_a_state = false; 1460 sun_do_break(); 1461 } 1462 #endif 1463 1464 if (kbd->kbdmode == VC_MEDIUMRAW) { 1465 /* 1466 * This is extended medium raw mode, with keys above 127 1467 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing 1468 * the 'up' flag if needed. 0 is reserved, so this shouldn't 1469 * interfere with anything else. The two bytes after 0 will 1470 * always have the up flag set not to interfere with older 1471 * applications. This allows for 16384 different keycodes, 1472 * which should be enough. 1473 */ 1474 if (keycode < 128) { 1475 put_queue(vc, keycode | (!down << 7)); 1476 } else { 1477 put_queue(vc, !down << 7); 1478 put_queue(vc, (keycode >> 7) | BIT(7)); 1479 put_queue(vc, keycode | BIT(7)); 1480 } 1481 raw_mode = true; 1482 } 1483 1484 assign_bit(keycode, key_down, down); 1485 1486 if (rep && 1487 (!vc_kbd_mode(kbd, VC_REPEAT) || 1488 (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) { 1489 /* 1490 * Don't repeat a key if the input buffers are not empty and the 1491 * characters get aren't echoed locally. This makes key repeat 1492 * usable with slow applications and under heavy loads. 1493 */ 1494 return; 1495 } 1496 1497 param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate; 1498 param.ledstate = kbd->ledflagstate; 1499 key_map = key_maps[shift_final]; 1500 1501 /* 1502 * Fall back to the plain map if modifiers are active, the modifier- 1503 * specific map is missing or has no entry, and the plain map has a 1504 * modifier-aware key type (KT_CUR or KT_CSI). These handlers encode 1505 * the modifier state into the emitted escape sequence. 1506 */ 1507 if (shift_final && keycode < NR_KEYS && 1508 (!key_map || key_map[keycode] == K_HOLE) && key_maps[0]) { 1509 unsigned short plain = key_maps[0][keycode]; 1510 unsigned char type = KTYP(plain); 1511 1512 if (type >= 0xf0 && (type - 0xf0 == KT_CUR || type - 0xf0 == KT_CSI)) 1513 key_map = key_maps[0]; 1514 } 1515 1516 rc = atomic_notifier_call_chain(&keyboard_notifier_list, 1517 KBD_KEYCODE, ¶m); 1518 if (rc == NOTIFY_STOP || !key_map) { 1519 atomic_notifier_call_chain(&keyboard_notifier_list, 1520 KBD_UNBOUND_KEYCODE, ¶m); 1521 do_compute_shiftstate(); 1522 kbd->slockstate = 0; 1523 return; 1524 } 1525 1526 if (keycode < NR_KEYS) 1527 keysym = key_map[keycode]; 1528 else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8) 1529 keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1)); 1530 else 1531 return; 1532 1533 type = KTYP(keysym); 1534 1535 if (type < 0xf0) { 1536 param.value = keysym; 1537 rc = atomic_notifier_call_chain(&keyboard_notifier_list, 1538 KBD_UNICODE, ¶m); 1539 if (rc != NOTIFY_STOP) 1540 if (down && !(raw_mode || kbd->kbdmode == VC_OFF)) 1541 k_unicode(vc, keysym, !down); 1542 return; 1543 } 1544 1545 type -= 0xf0; 1546 1547 if (type == KT_LETTER) { 1548 type = KT_LATIN; 1549 if (vc_kbd_led(kbd, VC_CAPSLOCK)) { 1550 key_map = key_maps[shift_final ^ BIT(KG_SHIFT)]; 1551 if (key_map) 1552 keysym = key_map[keycode]; 1553 } 1554 } 1555 1556 param.value = keysym; 1557 rc = atomic_notifier_call_chain(&keyboard_notifier_list, 1558 KBD_KEYSYM, ¶m); 1559 if (rc == NOTIFY_STOP) 1560 return; 1561 1562 if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT) 1563 return; 1564 1565 (*k_handler[type])(vc, KVAL(keysym), !down); 1566 1567 param.ledstate = kbd->ledflagstate; 1568 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, ¶m); 1569 1570 if (type != KT_SLOCK) 1571 kbd->slockstate = 0; 1572 } 1573 1574 static void kbd_event(struct input_handle *handle, unsigned int event_type, 1575 unsigned int event_code, int value) 1576 { 1577 /* We are called with interrupts disabled, just take the lock */ 1578 scoped_guard(spinlock, &kbd_event_lock) { 1579 if (event_type == EV_MSC && event_code == MSC_RAW && 1580 kbd_is_hw_raw(handle->dev)) 1581 kbd_rawcode(value); 1582 if (event_type == EV_KEY && event_code <= KEY_MAX) 1583 kbd_keycode(event_code, value, kbd_is_hw_raw(handle->dev)); 1584 } 1585 1586 tasklet_schedule(&keyboard_tasklet); 1587 do_poke_blanked_console = 1; 1588 schedule_console_callback(); 1589 } 1590 1591 static bool kbd_match(struct input_handler *handler, struct input_dev *dev) 1592 { 1593 if (test_bit(EV_SND, dev->evbit)) 1594 return true; 1595 1596 if (test_bit(EV_KEY, dev->evbit)) { 1597 if (find_next_bit(dev->keybit, BTN_MISC, KEY_RESERVED) < 1598 BTN_MISC) 1599 return true; 1600 if (find_next_bit(dev->keybit, KEY_BRL_DOT10 + 1, 1601 KEY_BRL_DOT1) <= KEY_BRL_DOT10) 1602 return true; 1603 } 1604 1605 return false; 1606 } 1607 1608 /* 1609 * When a keyboard (or other input device) is found, the kbd_connect 1610 * function is called. The function then looks at the device, and if it 1611 * likes it, it can open it and get events from it. In this (kbd_connect) 1612 * function, we should decide which VT to bind that keyboard to initially. 1613 */ 1614 static int kbd_connect(struct input_handler *handler, struct input_dev *dev, 1615 const struct input_device_id *id) 1616 { 1617 int error; 1618 1619 struct input_handle __free(kfree) *handle = kzalloc_obj(*handle); 1620 if (!handle) 1621 return -ENOMEM; 1622 1623 handle->dev = dev; 1624 handle->handler = handler; 1625 handle->name = "kbd"; 1626 1627 error = input_register_handle(handle); 1628 if (error) 1629 return error; 1630 1631 error = input_open_device(handle); 1632 if (error) 1633 goto err_unregister_handle; 1634 1635 retain_and_null_ptr(handle); 1636 1637 return 0; 1638 1639 err_unregister_handle: 1640 input_unregister_handle(handle); 1641 return error; 1642 } 1643 1644 static void kbd_disconnect(struct input_handle *handle) 1645 { 1646 input_close_device(handle); 1647 input_unregister_handle(handle); 1648 kfree(handle); 1649 } 1650 1651 /* 1652 * Start keyboard handler on the new keyboard by refreshing LED state to 1653 * match the rest of the system. 1654 */ 1655 static void kbd_start(struct input_handle *handle) 1656 { 1657 tasklet_disable(&keyboard_tasklet); 1658 1659 if (ledstate != -1U) 1660 kbd_update_leds_helper(handle, &ledstate); 1661 1662 tasklet_enable(&keyboard_tasklet); 1663 } 1664 1665 static const struct input_device_id kbd_ids[] = { 1666 { 1667 .flags = INPUT_DEVICE_ID_MATCH_EVBIT, 1668 .evbit = { BIT_MASK(EV_KEY) }, 1669 }, 1670 1671 { 1672 .flags = INPUT_DEVICE_ID_MATCH_EVBIT, 1673 .evbit = { BIT_MASK(EV_SND) }, 1674 }, 1675 1676 { }, /* Terminating entry */ 1677 }; 1678 1679 MODULE_DEVICE_TABLE(input, kbd_ids); 1680 1681 static struct input_handler kbd_handler = { 1682 .event = kbd_event, 1683 .match = kbd_match, 1684 .connect = kbd_connect, 1685 .disconnect = kbd_disconnect, 1686 .start = kbd_start, 1687 .name = "kbd", 1688 .id_table = kbd_ids, 1689 }; 1690 1691 int __init kbd_init(void) 1692 { 1693 int i; 1694 int error; 1695 1696 for (i = 0; i < MAX_NR_CONSOLES; i++) { 1697 kbd_table[i].ledflagstate = kbd_defleds(); 1698 kbd_table[i].default_ledflagstate = kbd_defleds(); 1699 kbd_table[i].ledmode = LED_SHOW_FLAGS; 1700 kbd_table[i].lockstate = KBD_DEFLOCK; 1701 kbd_table[i].slockstate = 0; 1702 kbd_table[i].modeflags = KBD_DEFMODE; 1703 kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; 1704 } 1705 1706 kbd_init_leds(); 1707 1708 error = input_register_handler(&kbd_handler); 1709 if (error) 1710 return error; 1711 1712 tasklet_enable(&keyboard_tasklet); 1713 tasklet_schedule(&keyboard_tasklet); 1714 1715 return 0; 1716 } 1717 1718 /* Ioctl support code */ 1719 1720 static int vt_do_kdgkbdiacr(void __user *udp) 1721 { 1722 struct kbdiacrs __user *a = udp; 1723 int i, asize; 1724 1725 struct kbdiacr __free(kfree) *dia = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacr), 1726 GFP_KERNEL); 1727 if (!dia) 1728 return -ENOMEM; 1729 1730 /* Lock the diacriticals table, make a copy and then 1731 copy it after we unlock */ 1732 scoped_guard(spinlock_irqsave, &kbd_event_lock) { 1733 asize = accent_table_size; 1734 for (i = 0; i < asize; i++) { 1735 dia[i].diacr = conv_uni_to_8bit(accent_table[i].diacr); 1736 dia[i].base = conv_uni_to_8bit(accent_table[i].base); 1737 dia[i].result = conv_uni_to_8bit(accent_table[i].result); 1738 } 1739 } 1740 1741 if (put_user(asize, &a->kb_cnt)) 1742 return -EFAULT; 1743 if (copy_to_user(a->kbdiacr, dia, asize * sizeof(struct kbdiacr))) 1744 return -EFAULT; 1745 return 0; 1746 } 1747 1748 static int vt_do_kdgkbdiacruc(void __user *udp) 1749 { 1750 struct kbdiacrsuc __user *a = udp; 1751 int asize; 1752 1753 void __free(kfree) *buf = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacruc), 1754 GFP_KERNEL); 1755 if (buf == NULL) 1756 return -ENOMEM; 1757 1758 /* Lock the diacriticals table, make a copy and then 1759 copy it after we unlock */ 1760 scoped_guard(spinlock_irqsave, &kbd_event_lock) { 1761 asize = accent_table_size; 1762 memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc)); 1763 } 1764 1765 if (put_user(asize, &a->kb_cnt)) 1766 return -EFAULT; 1767 if (copy_to_user(a->kbdiacruc, buf, asize * sizeof(struct kbdiacruc))) 1768 return -EFAULT; 1769 1770 return 0; 1771 } 1772 1773 static int vt_do_kdskbdiacr(void __user *udp, int perm) 1774 { 1775 struct kbdiacrs __user *a = udp; 1776 struct kbdiacr __free(kfree) *dia = NULL; 1777 unsigned int ct; 1778 int i; 1779 1780 if (!perm) 1781 return -EPERM; 1782 if (get_user(ct, &a->kb_cnt)) 1783 return -EFAULT; 1784 if (ct >= MAX_DIACR) 1785 return -EINVAL; 1786 1787 if (ct) { 1788 dia = memdup_array_user(a->kbdiacr, 1789 ct, sizeof(struct kbdiacr)); 1790 if (IS_ERR(dia)) 1791 return PTR_ERR(dia); 1792 } 1793 1794 guard(spinlock_irqsave)(&kbd_event_lock); 1795 accent_table_size = ct; 1796 for (i = 0; i < ct; i++) { 1797 accent_table[i].diacr = 1798 conv_8bit_to_uni(dia[i].diacr); 1799 accent_table[i].base = 1800 conv_8bit_to_uni(dia[i].base); 1801 accent_table[i].result = 1802 conv_8bit_to_uni(dia[i].result); 1803 } 1804 1805 return 0; 1806 } 1807 1808 static int vt_do_kdskbdiacruc(void __user *udp, int perm) 1809 { 1810 struct kbdiacrsuc __user *a = udp; 1811 unsigned int ct; 1812 void __free(kfree) *buf = NULL; 1813 1814 if (!perm) 1815 return -EPERM; 1816 1817 if (get_user(ct, &a->kb_cnt)) 1818 return -EFAULT; 1819 1820 if (ct >= MAX_DIACR) 1821 return -EINVAL; 1822 1823 if (ct) { 1824 buf = memdup_array_user(a->kbdiacruc, 1825 ct, sizeof(struct kbdiacruc)); 1826 if (IS_ERR(buf)) 1827 return PTR_ERR(buf); 1828 } 1829 guard(spinlock_irqsave)(&kbd_event_lock); 1830 if (ct) 1831 memcpy(accent_table, buf, 1832 ct * sizeof(struct kbdiacruc)); 1833 accent_table_size = ct; 1834 return 0; 1835 } 1836 1837 /** 1838 * vt_do_diacrit - diacritical table updates 1839 * @cmd: ioctl request 1840 * @udp: pointer to user data for ioctl 1841 * @perm: permissions check computed by caller 1842 * 1843 * Update the diacritical tables atomically and safely. Lock them 1844 * against simultaneous keypresses 1845 */ 1846 int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm) 1847 { 1848 switch (cmd) { 1849 case KDGKBDIACR: 1850 return vt_do_kdgkbdiacr(udp); 1851 case KDGKBDIACRUC: 1852 return vt_do_kdgkbdiacruc(udp); 1853 case KDSKBDIACR: 1854 return vt_do_kdskbdiacr(udp, perm); 1855 case KDSKBDIACRUC: 1856 return vt_do_kdskbdiacruc(udp, perm); 1857 } 1858 return 0; 1859 } 1860 1861 /** 1862 * vt_do_kdskbmode - set keyboard mode ioctl 1863 * @console: the console to use 1864 * @arg: the requested mode 1865 * 1866 * Update the keyboard mode bits while holding the correct locks. 1867 * Return 0 for success or an error code. 1868 */ 1869 int vt_do_kdskbmode(unsigned int console, unsigned int arg) 1870 { 1871 struct kbd_struct *kb = &kbd_table[console]; 1872 1873 guard(spinlock_irqsave)(&kbd_event_lock); 1874 switch(arg) { 1875 case K_RAW: 1876 kb->kbdmode = VC_RAW; 1877 return 0; 1878 case K_MEDIUMRAW: 1879 kb->kbdmode = VC_MEDIUMRAW; 1880 return 0; 1881 case K_XLATE: 1882 kb->kbdmode = VC_XLATE; 1883 do_compute_shiftstate(); 1884 return 0; 1885 case K_UNICODE: 1886 kb->kbdmode = VC_UNICODE; 1887 do_compute_shiftstate(); 1888 return 0; 1889 case K_OFF: 1890 kb->kbdmode = VC_OFF; 1891 return 0; 1892 default: 1893 return -EINVAL; 1894 } 1895 } 1896 1897 /** 1898 * vt_do_kdskbmeta - set keyboard meta state 1899 * @console: the console to use 1900 * @arg: the requested meta state 1901 * 1902 * Update the keyboard meta bits while holding the correct locks. 1903 * Return 0 for success or an error code. 1904 */ 1905 int vt_do_kdskbmeta(unsigned int console, unsigned int arg) 1906 { 1907 struct kbd_struct *kb = &kbd_table[console]; 1908 1909 guard(spinlock_irqsave)(&kbd_event_lock); 1910 switch(arg) { 1911 case K_METABIT: 1912 clr_vc_kbd_mode(kb, VC_META); 1913 return 0; 1914 case K_ESCPREFIX: 1915 set_vc_kbd_mode(kb, VC_META); 1916 return 0; 1917 default: 1918 return -EINVAL; 1919 } 1920 } 1921 1922 int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm) 1923 { 1924 struct kbkeycode tmp; 1925 int kc; 1926 1927 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) 1928 return -EFAULT; 1929 1930 switch (cmd) { 1931 case KDGETKEYCODE: 1932 kc = getkeycode(tmp.scancode); 1933 if (kc < 0) 1934 return kc; 1935 return put_user(kc, &user_kbkc->keycode); 1936 case KDSETKEYCODE: 1937 if (!perm) 1938 return -EPERM; 1939 return setkeycode(tmp.scancode, tmp.keycode); 1940 } 1941 1942 return 0; 1943 } 1944 1945 static unsigned short vt_kdgkbent(unsigned char kbdmode, unsigned char idx, 1946 unsigned char map) 1947 { 1948 unsigned short *key_map; 1949 1950 /* Ensure another thread doesn't free it under us */ 1951 guard(spinlock_irqsave)(&kbd_event_lock); 1952 key_map = key_maps[map]; 1953 if (key_map) { 1954 unsigned short val = U(key_map[idx]); 1955 if (kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) 1956 return K_HOLE; 1957 return val; 1958 } 1959 1960 return idx ? K_HOLE : K_NOSUCHMAP; 1961 } 1962 1963 static int vt_kdskbent(unsigned char kbdmode, unsigned char idx, 1964 unsigned char map, unsigned short val) 1965 { 1966 unsigned short *key_map, oldval; 1967 1968 if (!idx && val == K_NOSUCHMAP) { 1969 guard(spinlock_irqsave)(&kbd_event_lock); 1970 /* deallocate map */ 1971 key_map = key_maps[map]; 1972 if (map && key_map) { 1973 key_maps[map] = NULL; 1974 if (key_map[0] == U(K_ALLOCATED)) { 1975 kfree(key_map); 1976 keymap_count--; 1977 } 1978 } 1979 1980 return 0; 1981 } 1982 1983 if (KTYP(val) < NR_TYPES) { 1984 if (KVAL(val) > max_vals[KTYP(val)]) 1985 return -EINVAL; 1986 } else if (kbdmode != VC_UNICODE) 1987 return -EINVAL; 1988 1989 /* ++Geert: non-PC keyboards may generate keycode zero */ 1990 #if !defined(__mc68000__) && !defined(__powerpc__) 1991 /* assignment to entry 0 only tests validity of args */ 1992 if (!idx) 1993 return 0; 1994 #endif 1995 1996 unsigned short __free(kfree) *new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); 1997 if (!new_map) 1998 return -ENOMEM; 1999 2000 guard(spinlock_irqsave)(&kbd_event_lock); 2001 key_map = key_maps[map]; 2002 if (key_map == NULL) { 2003 int j; 2004 2005 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && !capable(CAP_SYS_RESOURCE)) 2006 return -EPERM; 2007 2008 key_map = key_maps[map] = no_free_ptr(new_map); 2009 key_map[0] = U(K_ALLOCATED); 2010 for (j = 1; j < NR_KEYS; j++) 2011 key_map[j] = U(K_HOLE); 2012 keymap_count++; 2013 } 2014 2015 oldval = U(key_map[idx]); 2016 if (val == oldval) 2017 return 0; 2018 2019 /* Attention Key */ 2020 if ((oldval == K_SAK || val == K_SAK) && !capable(CAP_SYS_ADMIN)) 2021 return -EPERM; 2022 2023 key_map[idx] = U(val); 2024 if (!map && (KTYP(oldval) == KT_SHIFT || KTYP(val) == KT_SHIFT)) 2025 do_compute_shiftstate(); 2026 2027 return 0; 2028 } 2029 2030 int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, 2031 unsigned int console) 2032 { 2033 struct kbd_struct *kb = &kbd_table[console]; 2034 struct kbentry kbe; 2035 2036 if (copy_from_user(&kbe, user_kbe, sizeof(struct kbentry))) 2037 return -EFAULT; 2038 2039 switch (cmd) { 2040 case KDGKBENT: 2041 return put_user(vt_kdgkbent(kb->kbdmode, kbe.kb_index, 2042 kbe.kb_table), 2043 &user_kbe->kb_value); 2044 case KDSKBENT: 2045 if (!perm || !capable(CAP_SYS_TTY_CONFIG)) 2046 return -EPERM; 2047 return vt_kdskbent(kb->kbdmode, kbe.kb_index, kbe.kb_table, 2048 kbe.kb_value); 2049 } 2050 return 0; 2051 } 2052 2053 static char *vt_kdskbsent(char *kbs, unsigned char cur) 2054 { 2055 static DECLARE_BITMAP(is_kmalloc, MAX_NR_FUNC); 2056 char *cur_f = func_table[cur]; 2057 2058 if (cur_f && strlen(cur_f) >= strlen(kbs)) { 2059 strcpy(cur_f, kbs); 2060 return kbs; 2061 } 2062 2063 func_table[cur] = kbs; 2064 2065 return __test_and_set_bit(cur, is_kmalloc) ? cur_f : NULL; 2066 } 2067 2068 int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) 2069 { 2070 unsigned char kb_func; 2071 2072 if (get_user(kb_func, &user_kdgkb->kb_func)) 2073 return -EFAULT; 2074 2075 kb_func = array_index_nospec(kb_func, MAX_NR_FUNC); 2076 2077 switch (cmd) { 2078 case KDGKBSENT: { 2079 /* size should have been a struct member */ 2080 ssize_t len = sizeof(user_kdgkb->kb_string); 2081 2082 char __free(kfree) *kbs = kmalloc(len, GFP_KERNEL); 2083 if (!kbs) 2084 return -ENOMEM; 2085 2086 scoped_guard(spinlock_irqsave, &func_buf_lock) 2087 len = strscpy(kbs, func_table[kb_func] ? : "", len); 2088 2089 if (len < 0) 2090 return -ENOSPC; 2091 2092 if (copy_to_user(user_kdgkb->kb_string, kbs, len + 1)) 2093 return -EFAULT; 2094 2095 return 0; 2096 } 2097 case KDSKBSENT: 2098 if (!perm || !capable(CAP_SYS_TTY_CONFIG)) 2099 return -EPERM; 2100 2101 char __free(kfree) *kbs = strndup_user(user_kdgkb->kb_string, 2102 sizeof(user_kdgkb->kb_string)); 2103 if (IS_ERR(kbs)) 2104 return PTR_ERR(kbs); 2105 2106 guard(spinlock_irqsave)(&func_buf_lock); 2107 kbs = vt_kdskbsent(kbs, kb_func); 2108 2109 return 0; 2110 } 2111 2112 return 0; 2113 } 2114 2115 int vt_do_kdskled(unsigned int console, int cmd, unsigned long arg, int perm) 2116 { 2117 struct kbd_struct *kb = &kbd_table[console]; 2118 unsigned char ucval; 2119 2120 switch(cmd) { 2121 /* the ioctls below read/set the flags usually shown in the leds */ 2122 /* don't use them - they will go away without warning */ 2123 case KDGKBLED: 2124 scoped_guard(spinlock_irqsave, &kbd_event_lock) 2125 ucval = kb->ledflagstate | (kb->default_ledflagstate << 4); 2126 return put_user(ucval, (char __user *)arg); 2127 2128 case KDSKBLED: 2129 if (!perm) 2130 return -EPERM; 2131 if (arg & ~0x77) 2132 return -EINVAL; 2133 scoped_guard(spinlock_irqsave, &led_lock) { 2134 kb->ledflagstate = (arg & 7); 2135 kb->default_ledflagstate = ((arg >> 4) & 7); 2136 set_leds(); 2137 } 2138 return 0; 2139 2140 /* the ioctls below only set the lights, not the functions */ 2141 /* for those, see KDGKBLED and KDSKBLED above */ 2142 case KDGETLED: 2143 ucval = getledstate(); 2144 return put_user(ucval, (char __user *)arg); 2145 2146 case KDSETLED: 2147 if (!perm) 2148 return -EPERM; 2149 setledstate(kb, arg); 2150 return 0; 2151 } 2152 return -ENOIOCTLCMD; 2153 } 2154 2155 int vt_do_kdgkbmode(unsigned int console) 2156 { 2157 struct kbd_struct *kb = &kbd_table[console]; 2158 /* This is a spot read so needs no locking */ 2159 switch (kb->kbdmode) { 2160 case VC_RAW: 2161 return K_RAW; 2162 case VC_MEDIUMRAW: 2163 return K_MEDIUMRAW; 2164 case VC_UNICODE: 2165 return K_UNICODE; 2166 case VC_OFF: 2167 return K_OFF; 2168 default: 2169 return K_XLATE; 2170 } 2171 } 2172 2173 /** 2174 * vt_do_kdgkbmeta - report meta status 2175 * @console: console to report 2176 * 2177 * Report the meta flag status of this console 2178 */ 2179 int vt_do_kdgkbmeta(unsigned int console) 2180 { 2181 struct kbd_struct *kb = &kbd_table[console]; 2182 /* Again a spot read so no locking */ 2183 return vc_kbd_mode(kb, VC_META) ? K_ESCPREFIX : K_METABIT; 2184 } 2185 2186 /** 2187 * vt_reset_unicode - reset the unicode status 2188 * @console: console being reset 2189 * 2190 * Restore the unicode console state to its default 2191 */ 2192 void vt_reset_unicode(unsigned int console) 2193 { 2194 guard(spinlock_irqsave)(&kbd_event_lock); 2195 kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; 2196 } 2197 2198 /** 2199 * vt_get_shift_state - shift bit state 2200 * 2201 * Report the shift bits from the keyboard state. We have to export 2202 * this to support some oddities in the vt layer. 2203 */ 2204 int vt_get_shift_state(void) 2205 { 2206 /* Don't lock as this is a transient report */ 2207 return shift_state; 2208 } 2209 2210 /** 2211 * vt_reset_keyboard - reset keyboard state 2212 * @console: console to reset 2213 * 2214 * Reset the keyboard bits for a console as part of a general console 2215 * reset event 2216 */ 2217 void vt_reset_keyboard(unsigned int console) 2218 { 2219 struct kbd_struct *kb = &kbd_table[console]; 2220 2221 guard(spinlock_irqsave)(&kbd_event_lock); 2222 set_vc_kbd_mode(kb, VC_REPEAT); 2223 clr_vc_kbd_mode(kb, VC_CKMODE); 2224 clr_vc_kbd_mode(kb, VC_APPLIC); 2225 clr_vc_kbd_mode(kb, VC_CRLF); 2226 kb->lockstate = 0; 2227 kb->slockstate = 0; 2228 guard(spinlock)(&led_lock); 2229 kb->ledmode = LED_SHOW_FLAGS; 2230 kb->ledflagstate = kb->default_ledflagstate; 2231 /* do not do set_leds here because this causes an endless tasklet loop 2232 when the keyboard hasn't been initialized yet */ 2233 } 2234 2235 /** 2236 * vt_get_kbd_mode_bit - read keyboard status bits 2237 * @console: console to read from 2238 * @bit: mode bit to read 2239 * 2240 * Report back a vt mode bit. We do this without locking so the 2241 * caller must be sure that there are no synchronization needs 2242 */ 2243 2244 int vt_get_kbd_mode_bit(unsigned int console, int bit) 2245 { 2246 struct kbd_struct *kb = &kbd_table[console]; 2247 return vc_kbd_mode(kb, bit); 2248 } 2249 2250 /** 2251 * vt_set_kbd_mode_bit - read keyboard status bits 2252 * @console: console to read from 2253 * @bit: mode bit to read 2254 * 2255 * Set a vt mode bit. We do this without locking so the 2256 * caller must be sure that there are no synchronization needs 2257 */ 2258 2259 void vt_set_kbd_mode_bit(unsigned int console, int bit) 2260 { 2261 struct kbd_struct *kb = &kbd_table[console]; 2262 2263 guard(spinlock_irqsave)(&kbd_event_lock); 2264 set_vc_kbd_mode(kb, bit); 2265 } 2266 2267 /** 2268 * vt_clr_kbd_mode_bit - read keyboard status bits 2269 * @console: console to read from 2270 * @bit: mode bit to read 2271 * 2272 * Report back a vt mode bit. We do this without locking so the 2273 * caller must be sure that there are no synchronization needs 2274 */ 2275 2276 void vt_clr_kbd_mode_bit(unsigned int console, int bit) 2277 { 2278 struct kbd_struct *kb = &kbd_table[console]; 2279 2280 guard(spinlock_irqsave)(&kbd_event_lock); 2281 clr_vc_kbd_mode(kb, bit); 2282 } 2283