Import 2.3.13pre7
[davej-history.git] / drivers / char / keyboard.c
blob58888cfadabb7e431a09e42e53c1cb3e380290c2
1 /*
2 * linux/drivers/char/keyboard.c
4 * Written for linux by Johan Myreen as a translation from
5 * the assembly version by Linus (with diacriticals added)
7 * Some additional features added by Christoph Niemann (ChN), March 1993
9 * Loadable keymaps by Risto Kankkunen, May 1993
11 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
12 * Added decr/incr_console, dynamic keymaps, Unicode support,
13 * dynamic function/string keys, led setting, Sept 1994
14 * `Sticky' modifier keys, 951006.
16 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
18 * Modified to provide 'generic' keyboard support by Hamish Macdonald
19 * Merge with the m68k keyboard driver and split-off of the PC low-level
20 * parts by Geert Uytterhoeven, May 1997
22 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
23 * 30-07-98: Dead keys redone, aeb@cwi.nl.
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/random.h>
34 #include <linux/init.h>
36 #include <asm/keyboard.h>
37 #include <asm/bitops.h>
39 #include <linux/kbd_kern.h>
40 #include <linux/kbd_diacr.h>
41 #include <linux/vt_kern.h>
42 #include <linux/kbd_ll.h>
43 #include <linux/sysrq.h>
45 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
47 #ifndef KBD_DEFMODE
48 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
49 #endif
51 #ifndef KBD_DEFLEDS
53 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
54 * is on. This seems a good reason to start with NumLock off.
56 #define KBD_DEFLEDS 0
57 #endif
59 #ifndef KBD_DEFLOCK
60 #define KBD_DEFLOCK 0
61 #endif
63 EXPORT_SYMBOL(handle_scancode);
65 extern void ctrl_alt_del(void);
67 DECLARE_WAIT_QUEUE_HEAD(keypress_wait);
68 struct console;
70 int keyboard_wait_for_keypress(struct console *co)
72 sleep_on(&keypress_wait);
73 return 0;
77 * global state includes the following, and various static variables
78 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
79 * (last_console is now a global variable)
82 /* shift state counters.. */
83 static unsigned char k_down[NR_SHIFT] = {0, };
84 /* keyboard key bitmap */
85 static unsigned long key_down[256/BITS_PER_LONG] = { 0, };
87 static int dead_key_next = 0;
88 /*
89 * In order to retrieve the shift_state (for the mouse server), either
90 * the variable must be global, or a new procedure must be created to
91 * return the value. I chose the former way.
93 int shift_state = 0;
94 static int npadch = -1; /* -1 or number assembled on pad */
95 static unsigned char diacr = 0;
96 static char rep = 0; /* flag telling character repeat */
97 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
98 static struct tty_struct **ttytab;
99 static struct kbd_struct * kbd = kbd_table;
100 static struct tty_struct * tty = NULL;
102 void compute_shiftstate(void);
104 typedef void (*k_hand)(unsigned char value, char up_flag);
105 typedef void (k_handfn)(unsigned char value, char up_flag);
107 static k_handfn
108 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
109 do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
110 do_ignore;
112 static k_hand key_handler[16] = {
113 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
114 do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
115 do_ignore, do_ignore
118 /* Key types processed even in raw modes */
120 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
122 typedef void (*void_fnp)(void);
123 typedef void (void_fn)(void);
125 static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle,
126 num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose,
127 SAK, decr_console, incr_console, spawn_console, bare_num;
129 static void_fnp spec_fn_table[] = {
130 do_null, enter, show_ptregs, show_mem,
131 show_state, send_intr, lastcons, caps_toggle,
132 num, hold, scroll_forw, scroll_back,
133 boot_it, caps_on, compose, SAK,
134 decr_console, incr_console, spawn_console, bare_num
137 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
139 /* maximum values each key_handler can handle */
140 const int max_vals[] = {
141 255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1,
142 NR_DEAD - 1, 255, 3, NR_SHIFT - 1,
143 255, NR_ASCII - 1, NR_LOCK - 1, 255,
144 NR_LOCK - 1, 255
147 const int NR_TYPES = SIZE(max_vals);
149 /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */
150 void put_queue(int);
151 static unsigned char handle_diacr(unsigned char);
153 /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
154 struct pt_regs * kbd_pt_regs;
156 #ifdef CONFIG_MAGIC_SYSRQ
157 static int sysrq_pressed;
158 int sysrq_enabled = 1;
159 #endif
162 * Many other routines do put_queue, but I think either
163 * they produce ASCII, or they produce some user-assigned
164 * string, and in both cases we might assume that it is
165 * in utf-8 already.
167 void to_utf8(ushort c) {
168 if (c < 0x80)
169 put_queue(c); /* 0******* */
170 else if (c < 0x800) {
171 put_queue(0xc0 | (c >> 6)); /* 110***** 10****** */
172 put_queue(0x80 | (c & 0x3f));
173 } else {
174 put_queue(0xe0 | (c >> 12)); /* 1110**** 10****** 10****** */
175 put_queue(0x80 | ((c >> 6) & 0x3f));
176 put_queue(0x80 | (c & 0x3f));
178 /* UTF-8 is defined for words of up to 31 bits,
179 but we need only 16 bits here */
183 * Translation of escaped scancodes to keycodes.
184 * This is now user-settable (for machines were it makes sense).
187 int setkeycode(unsigned int scancode, unsigned int keycode)
189 return kbd_setkeycode(scancode, keycode);
192 int getkeycode(unsigned int scancode)
194 return kbd_getkeycode(scancode);
197 void handle_scancode(unsigned char scancode, int down)
199 unsigned char keycode;
200 char up_flag = down ? 0 : 0200;
201 char raw_mode;
203 do_poke_blanked_console = 1;
204 mark_bh(CONSOLE_BH);
205 add_keyboard_randomness(scancode | up_flag);
207 tty = ttytab? ttytab[fg_console]: NULL;
208 if (tty && (!tty->driver_data)) {
210 * We touch the tty structure via the the ttytab array
211 * without knowing whether or not tty is open, which
212 * is inherently dangerous. We currently rely on that
213 * fact that console_open sets tty->driver_data when
214 * it opens it, and clears it when it closes it.
216 tty = NULL;
218 kbd = kbd_table + fg_console;
219 if ((raw_mode = (kbd->kbdmode == VC_RAW))) {
220 put_queue(scancode | up_flag);
221 /* we do not return yet, because we want to maintain
222 the key_down array, so that we have the correct
223 values when finishing RAW mode or when changing VT's */
227 * Convert scancode to keycode
229 if (!kbd_translate(scancode, &keycode, raw_mode))
230 return;
233 * At this point the variable `keycode' contains the keycode.
234 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
235 * We keep track of the up/down status of the key, and
236 * return the keycode if in MEDIUMRAW mode.
239 if (up_flag) {
240 rep = 0;
241 if(!test_and_clear_bit(keycode, key_down))
242 up_flag = kbd_unexpected_up(keycode);
243 } else
244 rep = test_and_set_bit(keycode, key_down);
246 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
247 if (keycode == SYSRQ_KEY) {
248 sysrq_pressed = !up_flag;
249 return;
250 } else if (sysrq_pressed) {
251 if (!up_flag && sysrq_enabled) {
252 handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty);
253 return;
256 #endif
258 if (kbd->kbdmode == VC_MEDIUMRAW) {
259 /* soon keycodes will require more than one byte */
260 put_queue(keycode + up_flag);
261 raw_mode = 1; /* Most key classes will be ignored */
265 * Small change in philosophy: earlier we defined repetition by
266 * rep = keycode == prev_keycode;
267 * prev_keycode = keycode;
268 * but now by the fact that the depressed key was down already.
269 * Does this ever make a difference? Yes.
273 * Repeat a key only if the input buffers are empty or the
274 * characters get echoed locally. This makes key repeat usable
275 * with slow applications and under heavy loads.
277 if (!rep ||
278 (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
279 (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
280 u_short keysym;
281 u_char type;
283 /* the XOR below used to be an OR */
284 int shift_final = (shift_state | kbd->slockstate) ^
285 kbd->lockstate;
286 ushort *key_map = key_maps[shift_final];
288 if (key_map != NULL) {
289 keysym = key_map[keycode];
290 type = KTYP(keysym);
292 if (type >= 0xf0) {
293 type -= 0xf0;
294 if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type)))
295 return;
296 if (type == KT_LETTER) {
297 type = KT_LATIN;
298 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
299 key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
300 if (key_map)
301 keysym = key_map[keycode];
304 (*key_handler[type])(keysym & 0xff, up_flag);
305 if (type != KT_SLOCK)
306 kbd->slockstate = 0;
307 } else {
308 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
309 if (!up_flag && !raw_mode)
310 to_utf8(keysym);
312 } else {
313 /* maybe beep? */
314 /* we have at least to update shift_state */
315 #if 1 /* how? two almost equivalent choices follow */
316 compute_shiftstate();
317 kbd->slockstate = 0; /* play it safe */
318 #else
319 keysym = U(plain_map[keycode]);
320 type = KTYP(keysym);
321 if (type == KT_SHIFT)
322 (*key_handler[type])(keysym & 0xff, up_flag);
323 #endif
329 void put_queue(int ch)
331 wake_up(&keypress_wait);
332 if (tty) {
333 tty_insert_flip_char(tty, ch, 0);
334 con_schedule_flip(tty);
338 static void puts_queue(char *cp)
340 wake_up(&keypress_wait);
341 if (!tty)
342 return;
344 while (*cp) {
345 tty_insert_flip_char(tty, *cp, 0);
346 cp++;
348 con_schedule_flip(tty);
351 static void applkey(int key, char mode)
353 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
355 buf[1] = (mode ? 'O' : '[');
356 buf[2] = key;
357 puts_queue(buf);
360 static void enter(void)
362 if (diacr) {
363 put_queue(diacr);
364 diacr = 0;
366 put_queue(13);
367 if (vc_kbd_mode(kbd,VC_CRLF))
368 put_queue(10);
371 static void caps_toggle(void)
373 if (rep)
374 return;
375 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
378 static void caps_on(void)
380 if (rep)
381 return;
382 set_vc_kbd_led(kbd, VC_CAPSLOCK);
385 static void show_ptregs(void)
387 if (kbd_pt_regs)
388 show_regs(kbd_pt_regs);
391 static void hold(void)
393 if (rep || !tty)
394 return;
397 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
398 * these routines are also activated by ^S/^Q.
399 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
401 if (tty->stopped)
402 start_tty(tty);
403 else
404 stop_tty(tty);
407 static void num(void)
409 if (vc_kbd_mode(kbd,VC_APPLIC))
410 applkey('P', 1);
411 else
412 bare_num();
416 * Bind this to Shift-NumLock if you work in application keypad mode
417 * but want to be able to change the NumLock flag.
418 * Bind this to NumLock if you prefer that the NumLock key always
419 * changes the NumLock flag.
421 static void bare_num(void)
423 if (!rep)
424 chg_vc_kbd_led(kbd,VC_NUMLOCK);
427 static void lastcons(void)
429 /* switch to the last used console, ChN */
430 set_console(last_console);
433 static void decr_console(void)
435 int i;
437 for (i = fg_console-1; i != fg_console; i--) {
438 if (i == -1)
439 i = MAX_NR_CONSOLES-1;
440 if (vc_cons_allocated(i))
441 break;
443 set_console(i);
446 static void incr_console(void)
448 int i;
450 for (i = fg_console+1; i != fg_console; i++) {
451 if (i == MAX_NR_CONSOLES)
452 i = 0;
453 if (vc_cons_allocated(i))
454 break;
456 set_console(i);
459 static void send_intr(void)
461 if (!tty)
462 return;
463 tty_insert_flip_char(tty, 0, TTY_BREAK);
464 con_schedule_flip(tty);
467 static void scroll_forw(void)
469 scrollfront(0);
472 static void scroll_back(void)
474 scrollback(0);
477 static void boot_it(void)
479 ctrl_alt_del();
482 static void compose(void)
484 dead_key_next = 1;
487 int spawnpid, spawnsig;
489 static void spawn_console(void)
491 if (spawnpid)
492 if(kill_proc(spawnpid, spawnsig, 1))
493 spawnpid = 0;
496 static void SAK(void)
499 * SAK should also work in all raw modes and reset
500 * them properly.
503 do_SAK(tty);
504 reset_vc(fg_console);
505 #if 0
506 do_unblank_screen(); /* not in interrupt routine? */
507 #endif
510 static void do_ignore(unsigned char value, char up_flag)
514 static void do_null()
516 compute_shiftstate();
519 static void do_spec(unsigned char value, char up_flag)
521 if (up_flag)
522 return;
523 if (value >= SIZE(spec_fn_table))
524 return;
525 if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
526 !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
527 return;
528 spec_fn_table[value]();
531 static void do_lowercase(unsigned char value, char up_flag)
533 printk(KERN_ERR "keyboard.c: do_lowercase was called - impossible\n");
536 static void do_self(unsigned char value, char up_flag)
538 if (up_flag)
539 return; /* no action, if this is a key release */
541 if (diacr)
542 value = handle_diacr(value);
544 if (dead_key_next) {
545 dead_key_next = 0;
546 diacr = value;
547 return;
550 put_queue(value);
553 #define A_GRAVE '`'
554 #define A_ACUTE '\''
555 #define A_CFLEX '^'
556 #define A_TILDE '~'
557 #define A_DIAER '"'
558 #define A_CEDIL ','
559 static unsigned char ret_diacr[NR_DEAD] =
560 {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
562 /* Obsolete - for backwards compatibility only */
563 static void do_dead(unsigned char value, char up_flag)
565 value = ret_diacr[value];
566 do_dead2(value,up_flag);
570 * Handle dead key. Note that we now may have several
571 * dead keys modifying the same character. Very useful
572 * for Vietnamese.
574 static void do_dead2(unsigned char value, char up_flag)
576 if (up_flag)
577 return;
579 diacr = (diacr ? handle_diacr(value) : value);
584 * We have a combining character DIACR here, followed by the character CH.
585 * If the combination occurs in the table, return the corresponding value.
586 * Otherwise, if CH is a space or equals DIACR, return DIACR.
587 * Otherwise, conclude that DIACR was not combining after all,
588 * queue it and return CH.
590 unsigned char handle_diacr(unsigned char ch)
592 int d = diacr;
593 int i;
595 diacr = 0;
597 for (i = 0; i < accent_table_size; i++) {
598 if (accent_table[i].diacr == d && accent_table[i].base == ch)
599 return accent_table[i].result;
602 if (ch == ' ' || ch == d)
603 return d;
605 put_queue(d);
606 return ch;
609 static void do_cons(unsigned char value, char up_flag)
611 if (up_flag)
612 return;
613 set_console(value);
616 static void do_fn(unsigned char value, char up_flag)
618 if (up_flag)
619 return;
620 if (value < SIZE(func_table)) {
621 if (func_table[value])
622 puts_queue(func_table[value]);
623 } else
624 printk(KERN_ERR "do_fn called with value=%d\n", value);
627 static void do_pad(unsigned char value, char up_flag)
629 static const char *pad_chars = "0123456789+-*/\015,.?()";
630 static const char *app_map = "pqrstuvwxylSRQMnnmPQ";
632 if (up_flag)
633 return; /* no action, if this is a key release */
635 /* kludge... shift forces cursor/number keys */
636 if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
637 applkey(app_map[value], 1);
638 return;
641 if (!vc_kbd_led(kbd,VC_NUMLOCK))
642 switch (value) {
643 case KVAL(K_PCOMMA):
644 case KVAL(K_PDOT):
645 do_fn(KVAL(K_REMOVE), 0);
646 return;
647 case KVAL(K_P0):
648 do_fn(KVAL(K_INSERT), 0);
649 return;
650 case KVAL(K_P1):
651 do_fn(KVAL(K_SELECT), 0);
652 return;
653 case KVAL(K_P2):
654 do_cur(KVAL(K_DOWN), 0);
655 return;
656 case KVAL(K_P3):
657 do_fn(KVAL(K_PGDN), 0);
658 return;
659 case KVAL(K_P4):
660 do_cur(KVAL(K_LEFT), 0);
661 return;
662 case KVAL(K_P6):
663 do_cur(KVAL(K_RIGHT), 0);
664 return;
665 case KVAL(K_P7):
666 do_fn(KVAL(K_FIND), 0);
667 return;
668 case KVAL(K_P8):
669 do_cur(KVAL(K_UP), 0);
670 return;
671 case KVAL(K_P9):
672 do_fn(KVAL(K_PGUP), 0);
673 return;
674 case KVAL(K_P5):
675 applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
676 return;
679 put_queue(pad_chars[value]);
680 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
681 put_queue(10);
684 static void do_cur(unsigned char value, char up_flag)
686 static const char *cur_chars = "BDCA";
687 if (up_flag)
688 return;
690 applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
693 static void do_shift(unsigned char value, char up_flag)
695 int old_state = shift_state;
697 if (rep)
698 return;
700 /* Mimic typewriter:
701 a CapsShift key acts like Shift but undoes CapsLock */
702 if (value == KVAL(K_CAPSSHIFT)) {
703 value = KVAL(K_SHIFT);
704 if (!up_flag)
705 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
708 if (up_flag) {
709 /* handle the case that two shift or control
710 keys are depressed simultaneously */
711 if (k_down[value])
712 k_down[value]--;
713 } else
714 k_down[value]++;
716 if (k_down[value])
717 shift_state |= (1 << value);
718 else
719 shift_state &= ~ (1 << value);
721 /* kludge */
722 if (up_flag && shift_state != old_state && npadch != -1) {
723 if (kbd->kbdmode == VC_UNICODE)
724 to_utf8(npadch & 0xffff);
725 else
726 put_queue(npadch & 0xff);
727 npadch = -1;
731 /* called after returning from RAW mode or when changing consoles -
732 recompute k_down[] and shift_state from key_down[] */
733 /* maybe called when keymap is undefined, so that shiftkey release is seen */
734 void compute_shiftstate(void)
736 int i, j, k, sym, val;
738 shift_state = 0;
739 for(i=0; i < SIZE(k_down); i++)
740 k_down[i] = 0;
742 for(i=0; i < SIZE(key_down); i++)
743 if(key_down[i]) { /* skip this word if not a single bit on */
744 k = i*BITS_PER_LONG;
745 for(j=0; j<BITS_PER_LONG; j++,k++)
746 if(test_bit(k, key_down)) {
747 sym = U(plain_map[k]);
748 if(KTYP(sym) == KT_SHIFT || KTYP(sym) == KT_SLOCK) {
749 val = KVAL(sym);
750 if (val == KVAL(K_CAPSSHIFT))
751 val = KVAL(K_SHIFT);
752 k_down[val]++;
753 shift_state |= (1<<val);
759 static void do_meta(unsigned char value, char up_flag)
761 if (up_flag)
762 return;
764 if (vc_kbd_mode(kbd, VC_META)) {
765 put_queue('\033');
766 put_queue(value);
767 } else
768 put_queue(value | 0x80);
771 static void do_ascii(unsigned char value, char up_flag)
773 int base;
775 if (up_flag)
776 return;
778 if (value < 10) /* decimal input of code, while Alt depressed */
779 base = 10;
780 else { /* hexadecimal input of code, while AltGr depressed */
781 value -= 10;
782 base = 16;
785 if (npadch == -1)
786 npadch = value;
787 else
788 npadch = npadch * base + value;
791 static void do_lock(unsigned char value, char up_flag)
793 if (up_flag || rep)
794 return;
795 chg_vc_kbd_lock(kbd, value);
798 static void do_slock(unsigned char value, char up_flag)
800 do_shift(value,up_flag);
801 if (up_flag || rep)
802 return;
803 chg_vc_kbd_slock(kbd, value);
804 /* try to make Alt, oops, AltGr and such work */
805 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
806 kbd->slockstate = 0;
807 chg_vc_kbd_slock(kbd, value);
812 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
813 * or (ii) whatever pattern of lights people want to show using KDSETLED,
814 * or (iii) specified bits of specified words in kernel memory.
817 static unsigned char ledstate = 0xff; /* undefined */
818 static unsigned char ledioctl;
820 unsigned char getledstate(void) {
821 return ledstate;
824 void setledstate(struct kbd_struct *kbd, unsigned int led) {
825 if (!(led & ~7)) {
826 ledioctl = led;
827 kbd->ledmode = LED_SHOW_IOCTL;
828 } else
829 kbd->ledmode = LED_SHOW_FLAGS;
830 set_leds();
833 static struct ledptr {
834 unsigned int *addr;
835 unsigned int mask;
836 unsigned char valid:1;
837 } ledptrs[3];
839 void register_leds(int console, unsigned int led,
840 unsigned int *addr, unsigned int mask) {
841 struct kbd_struct *kbd = kbd_table + console;
842 if (led < 3) {
843 ledptrs[led].addr = addr;
844 ledptrs[led].mask = mask;
845 ledptrs[led].valid = 1;
846 kbd->ledmode = LED_SHOW_MEM;
847 } else
848 kbd->ledmode = LED_SHOW_FLAGS;
851 static inline unsigned char getleds(void){
852 struct kbd_struct *kbd = kbd_table + fg_console;
853 unsigned char leds;
855 if (kbd->ledmode == LED_SHOW_IOCTL)
856 return ledioctl;
857 leds = kbd->ledflagstate;
858 if (kbd->ledmode == LED_SHOW_MEM) {
859 if (ledptrs[0].valid) {
860 if (*ledptrs[0].addr & ledptrs[0].mask)
861 leds |= 1;
862 else
863 leds &= ~1;
865 if (ledptrs[1].valid) {
866 if (*ledptrs[1].addr & ledptrs[1].mask)
867 leds |= 2;
868 else
869 leds &= ~2;
871 if (ledptrs[2].valid) {
872 if (*ledptrs[2].addr & ledptrs[2].mask)
873 leds |= 4;
874 else
875 leds &= ~4;
878 return leds;
882 * This routine is the bottom half of the keyboard interrupt
883 * routine, and runs with all interrupts enabled. It does
884 * console changing, led setting and copy_to_cooked, which can
885 * take a reasonably long time.
887 * Aside from timing (which isn't really that important for
888 * keyboard interrupts as they happen often), using the software
889 * interrupt routines for this thing allows us to easily mask
890 * this when we don't want any of the above to happen. Not yet
891 * used, but this allows for easy and efficient race-condition
892 * prevention later on.
894 static void kbd_bh(void)
896 unsigned char leds = getleds();
898 if (leds != ledstate) {
899 ledstate = leds;
900 kbd_leds(leds);
904 __initfunc(int kbd_init(void))
906 int i;
907 struct kbd_struct kbd0;
908 extern struct tty_driver console_driver;
910 kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
911 kbd0.ledmode = LED_SHOW_FLAGS;
912 kbd0.lockstate = KBD_DEFLOCK;
913 kbd0.slockstate = 0;
914 kbd0.modeflags = KBD_DEFMODE;
915 kbd0.kbdmode = VC_XLATE;
917 for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
918 kbd_table[i] = kbd0;
920 ttytab = console_driver.table;
922 kbd_init_hw();
923 init_bh(KEYBOARD_BH, kbd_bh);
924 mark_bh(KEYBOARD_BH);
925 return 0;