Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / drivers / char / keyboard.c
blobf889378a3935ca72451beaada8175a219542d8a5
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>
44 #include <linux/pm.h>
46 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
48 #ifndef KBD_DEFMODE
49 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
50 #endif
52 #ifndef KBD_DEFLEDS
54 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
55 * is on. This seems a good reason to start with NumLock off.
57 #define KBD_DEFLEDS 0
58 #endif
60 #ifndef KBD_DEFLOCK
61 #define KBD_DEFLOCK 0
62 #endif
64 void (*kbd_ledfunc)(unsigned int led) = NULL;
65 EXPORT_SYMBOL(handle_scancode);
66 EXPORT_SYMBOL(kbd_ledfunc);
68 extern void ctrl_alt_del(void);
70 DECLARE_WAIT_QUEUE_HEAD(keypress_wait);
71 struct console;
73 int keyboard_wait_for_keypress(struct console *co)
75 sleep_on(&keypress_wait);
76 return 0;
80 * global state includes the following, and various static variables
81 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
82 * (last_console is now a global variable)
85 /* shift state counters.. */
86 static unsigned char k_down[NR_SHIFT] = {0, };
87 /* keyboard key bitmap */
88 static unsigned long key_down[256/BITS_PER_LONG] = { 0, };
90 static int dead_key_next = 0;
91 /*
92 * In order to retrieve the shift_state (for the mouse server), either
93 * the variable must be global, or a new procedure must be created to
94 * return the value. I chose the former way.
96 int shift_state = 0;
97 static int npadch = -1; /* -1 or number assembled on pad */
98 static unsigned char diacr = 0;
99 static char rep = 0; /* flag telling character repeat */
100 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
101 static struct tty_struct **ttytab;
102 static struct kbd_struct * kbd = kbd_table;
103 static struct tty_struct * tty = NULL;
105 void compute_shiftstate(void);
107 typedef void (*k_hand)(unsigned char value, char up_flag);
108 typedef void (k_handfn)(unsigned char value, char up_flag);
110 static k_handfn
111 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
112 do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
113 do_ignore;
115 static k_hand key_handler[16] = {
116 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
117 do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
118 do_ignore, do_ignore
121 /* Key types processed even in raw modes */
123 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
125 typedef void (*void_fnp)(void);
126 typedef void (void_fn)(void);
128 static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle,
129 num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose,
130 SAK, decr_console, incr_console, spawn_console, bare_num;
132 static void_fnp spec_fn_table[] = {
133 do_null, enter, show_ptregs, show_mem,
134 show_state, send_intr, lastcons, caps_toggle,
135 num, hold, scroll_forw, scroll_back,
136 boot_it, caps_on, compose, SAK,
137 decr_console, incr_console, spawn_console, bare_num
140 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
142 /* maximum values each key_handler can handle */
143 const int max_vals[] = {
144 255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1,
145 NR_DEAD - 1, 255, 3, NR_SHIFT - 1,
146 255, NR_ASCII - 1, NR_LOCK - 1, 255,
147 NR_LOCK - 1, 255
150 const int NR_TYPES = SIZE(max_vals);
152 /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */
153 void put_queue(int);
154 static unsigned char handle_diacr(unsigned char);
156 /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
157 struct pt_regs * kbd_pt_regs;
159 #ifdef CONFIG_MAGIC_SYSRQ
160 static int sysrq_pressed;
161 #endif
163 static struct pm_dev *pm_kbd = NULL;
166 * Many other routines do put_queue, but I think either
167 * they produce ASCII, or they produce some user-assigned
168 * string, and in both cases we might assume that it is
169 * in utf-8 already.
171 void to_utf8(ushort c) {
172 if (c < 0x80)
173 put_queue(c); /* 0******* */
174 else if (c < 0x800) {
175 put_queue(0xc0 | (c >> 6)); /* 110***** 10****** */
176 put_queue(0x80 | (c & 0x3f));
177 } else {
178 put_queue(0xe0 | (c >> 12)); /* 1110**** 10****** 10****** */
179 put_queue(0x80 | ((c >> 6) & 0x3f));
180 put_queue(0x80 | (c & 0x3f));
182 /* UTF-8 is defined for words of up to 31 bits,
183 but we need only 16 bits here */
187 * Translation of escaped scancodes to keycodes.
188 * This is now user-settable (for machines were it makes sense).
191 int setkeycode(unsigned int scancode, unsigned int keycode)
193 return kbd_setkeycode(scancode, keycode);
196 int getkeycode(unsigned int scancode)
198 return kbd_getkeycode(scancode);
201 void handle_scancode(unsigned char scancode, int down)
203 unsigned char keycode;
204 char up_flag = down ? 0 : 0200;
205 char raw_mode;
207 pm_access(pm_kbd);
209 do_poke_blanked_console = 1;
210 tasklet_schedule(&console_tasklet);
211 add_keyboard_randomness(scancode | up_flag);
213 tty = ttytab? ttytab[fg_console]: NULL;
214 if (tty && (!tty->driver_data)) {
216 * We touch the tty structure via the the ttytab array
217 * without knowing whether or not tty is open, which
218 * is inherently dangerous. We currently rely on that
219 * fact that console_open sets tty->driver_data when
220 * it opens it, and clears it when it closes it.
222 tty = NULL;
224 kbd = kbd_table + fg_console;
225 if ((raw_mode = (kbd->kbdmode == VC_RAW))) {
226 put_queue(scancode | up_flag);
227 /* we do not return yet, because we want to maintain
228 the key_down array, so that we have the correct
229 values when finishing RAW mode or when changing VT's */
233 * Convert scancode to keycode
235 if (!kbd_translate(scancode, &keycode, raw_mode))
236 return;
239 * At this point the variable `keycode' contains the keycode.
240 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
241 * We keep track of the up/down status of the key, and
242 * return the keycode if in MEDIUMRAW mode.
245 if (up_flag) {
246 rep = 0;
247 if(!test_and_clear_bit(keycode, key_down))
248 up_flag = kbd_unexpected_up(keycode);
249 } else
250 rep = test_and_set_bit(keycode, key_down);
252 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
253 if (keycode == SYSRQ_KEY) {
254 sysrq_pressed = !up_flag;
255 return;
256 } else if (sysrq_pressed) {
257 if (!up_flag) {
258 handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty);
259 return;
262 #endif
264 if (kbd->kbdmode == VC_MEDIUMRAW) {
265 /* soon keycodes will require more than one byte */
266 put_queue(keycode + up_flag);
267 raw_mode = 1; /* Most key classes will be ignored */
271 * Small change in philosophy: earlier we defined repetition by
272 * rep = keycode == prev_keycode;
273 * prev_keycode = keycode;
274 * but now by the fact that the depressed key was down already.
275 * Does this ever make a difference? Yes.
279 * Repeat a key only if the input buffers are empty or the
280 * characters get echoed locally. This makes key repeat usable
281 * with slow applications and under heavy loads.
283 if (!rep ||
284 (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
285 (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
286 u_short keysym;
287 u_char type;
289 /* the XOR below used to be an OR */
290 int shift_final = (shift_state | kbd->slockstate) ^
291 kbd->lockstate;
292 ushort *key_map = key_maps[shift_final];
294 if (key_map != NULL) {
295 keysym = key_map[keycode];
296 type = KTYP(keysym);
298 if (type >= 0xf0) {
299 type -= 0xf0;
300 if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type)))
301 return;
302 if (type == KT_LETTER) {
303 type = KT_LATIN;
304 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
305 key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
306 if (key_map)
307 keysym = key_map[keycode];
310 (*key_handler[type])(keysym & 0xff, up_flag);
311 if (type != KT_SLOCK)
312 kbd->slockstate = 0;
313 } else {
314 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
315 if (!up_flag && !raw_mode)
316 to_utf8(keysym);
318 } else {
319 /* maybe beep? */
320 /* we have at least to update shift_state */
321 #if 1 /* how? two almost equivalent choices follow */
322 compute_shiftstate();
323 kbd->slockstate = 0; /* play it safe */
324 #else
325 keysym = U(plain_map[keycode]);
326 type = KTYP(keysym);
327 if (type == KT_SHIFT)
328 (*key_handler[type])(keysym & 0xff, up_flag);
329 #endif
334 #ifdef CONFIG_FORWARD_KEYBOARD
335 extern int forward_chars;
337 void put_queue(int ch)
339 if (forward_chars == fg_console+1){
340 kbd_forward_char (ch);
341 } else {
342 wake_up(&keypress_wait);
343 if (tty) {
344 tty_insert_flip_char(tty, ch, 0);
345 con_schedule_flip(tty);
349 #else
350 void put_queue(int ch)
352 wake_up(&keypress_wait);
353 if (tty) {
354 tty_insert_flip_char(tty, ch, 0);
355 con_schedule_flip(tty);
358 #endif
360 static void puts_queue(char *cp)
362 wake_up(&keypress_wait);
363 if (!tty)
364 return;
366 while (*cp) {
367 tty_insert_flip_char(tty, *cp, 0);
368 cp++;
370 con_schedule_flip(tty);
373 static void applkey(int key, char mode)
375 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
377 buf[1] = (mode ? 'O' : '[');
378 buf[2] = key;
379 puts_queue(buf);
382 static void enter(void)
384 if (diacr) {
385 put_queue(diacr);
386 diacr = 0;
388 put_queue(13);
389 if (vc_kbd_mode(kbd,VC_CRLF))
390 put_queue(10);
393 static void caps_toggle(void)
395 if (rep)
396 return;
397 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
400 static void caps_on(void)
402 if (rep)
403 return;
404 set_vc_kbd_led(kbd, VC_CAPSLOCK);
407 static void show_ptregs(void)
409 if (kbd_pt_regs)
410 show_regs(kbd_pt_regs);
413 static void hold(void)
415 if (rep || !tty)
416 return;
419 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
420 * these routines are also activated by ^S/^Q.
421 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
423 if (tty->stopped)
424 start_tty(tty);
425 else
426 stop_tty(tty);
429 static void num(void)
431 if (vc_kbd_mode(kbd,VC_APPLIC))
432 applkey('P', 1);
433 else
434 bare_num();
438 * Bind this to Shift-NumLock if you work in application keypad mode
439 * but want to be able to change the NumLock flag.
440 * Bind this to NumLock if you prefer that the NumLock key always
441 * changes the NumLock flag.
443 static void bare_num(void)
445 if (!rep)
446 chg_vc_kbd_led(kbd,VC_NUMLOCK);
449 static void lastcons(void)
451 /* switch to the last used console, ChN */
452 set_console(last_console);
455 static void decr_console(void)
457 int i;
459 for (i = fg_console-1; i != fg_console; i--) {
460 if (i == -1)
461 i = MAX_NR_CONSOLES-1;
462 if (vc_cons_allocated(i))
463 break;
465 set_console(i);
468 static void incr_console(void)
470 int i;
472 for (i = fg_console+1; i != fg_console; i++) {
473 if (i == MAX_NR_CONSOLES)
474 i = 0;
475 if (vc_cons_allocated(i))
476 break;
478 set_console(i);
481 static void send_intr(void)
483 if (!tty)
484 return;
485 tty_insert_flip_char(tty, 0, TTY_BREAK);
486 con_schedule_flip(tty);
489 static void scroll_forw(void)
491 scrollfront(0);
494 static void scroll_back(void)
496 scrollback(0);
499 static void boot_it(void)
501 ctrl_alt_del();
504 static void compose(void)
506 dead_key_next = 1;
509 int spawnpid, spawnsig;
511 static void spawn_console(void)
513 if (spawnpid)
514 if(kill_proc(spawnpid, spawnsig, 1))
515 spawnpid = 0;
518 static void SAK(void)
521 * SAK should also work in all raw modes and reset
522 * them properly.
525 do_SAK(tty);
526 reset_vc(fg_console);
527 #if 0
528 do_unblank_screen(); /* not in interrupt routine? */
529 #endif
532 static void do_ignore(unsigned char value, char up_flag)
536 static void do_null()
538 compute_shiftstate();
541 static void do_spec(unsigned char value, char up_flag)
543 if (up_flag)
544 return;
545 if (value >= SIZE(spec_fn_table))
546 return;
547 if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
548 !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
549 return;
550 spec_fn_table[value]();
553 static void do_lowercase(unsigned char value, char up_flag)
555 printk(KERN_ERR "keyboard.c: do_lowercase was called - impossible\n");
558 static void do_self(unsigned char value, char up_flag)
560 if (up_flag)
561 return; /* no action, if this is a key release */
563 if (diacr)
564 value = handle_diacr(value);
566 if (dead_key_next) {
567 dead_key_next = 0;
568 diacr = value;
569 return;
572 put_queue(value);
575 #define A_GRAVE '`'
576 #define A_ACUTE '\''
577 #define A_CFLEX '^'
578 #define A_TILDE '~'
579 #define A_DIAER '"'
580 #define A_CEDIL ','
581 static unsigned char ret_diacr[NR_DEAD] =
582 {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
584 /* Obsolete - for backwards compatibility only */
585 static void do_dead(unsigned char value, char up_flag)
587 value = ret_diacr[value];
588 do_dead2(value,up_flag);
592 * Handle dead key. Note that we now may have several
593 * dead keys modifying the same character. Very useful
594 * for Vietnamese.
596 static void do_dead2(unsigned char value, char up_flag)
598 if (up_flag)
599 return;
601 diacr = (diacr ? handle_diacr(value) : value);
606 * We have a combining character DIACR here, followed by the character CH.
607 * If the combination occurs in the table, return the corresponding value.
608 * Otherwise, if CH is a space or equals DIACR, return DIACR.
609 * Otherwise, conclude that DIACR was not combining after all,
610 * queue it and return CH.
612 unsigned char handle_diacr(unsigned char ch)
614 int d = diacr;
615 int i;
617 diacr = 0;
619 for (i = 0; i < accent_table_size; i++) {
620 if (accent_table[i].diacr == d && accent_table[i].base == ch)
621 return accent_table[i].result;
624 if (ch == ' ' || ch == d)
625 return d;
627 put_queue(d);
628 return ch;
631 static void do_cons(unsigned char value, char up_flag)
633 if (up_flag)
634 return;
635 set_console(value);
638 static void do_fn(unsigned char value, char up_flag)
640 if (up_flag)
641 return;
642 if (value < SIZE(func_table)) {
643 if (func_table[value])
644 puts_queue(func_table[value]);
645 } else
646 printk(KERN_ERR "do_fn called with value=%d\n", value);
649 static void do_pad(unsigned char value, char up_flag)
651 static const char *pad_chars = "0123456789+-*/\015,.?()";
652 static const char *app_map = "pqrstuvwxylSRQMnnmPQ";
654 if (up_flag)
655 return; /* no action, if this is a key release */
657 /* kludge... shift forces cursor/number keys */
658 if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
659 applkey(app_map[value], 1);
660 return;
663 if (!vc_kbd_led(kbd,VC_NUMLOCK))
664 switch (value) {
665 case KVAL(K_PCOMMA):
666 case KVAL(K_PDOT):
667 do_fn(KVAL(K_REMOVE), 0);
668 return;
669 case KVAL(K_P0):
670 do_fn(KVAL(K_INSERT), 0);
671 return;
672 case KVAL(K_P1):
673 do_fn(KVAL(K_SELECT), 0);
674 return;
675 case KVAL(K_P2):
676 do_cur(KVAL(K_DOWN), 0);
677 return;
678 case KVAL(K_P3):
679 do_fn(KVAL(K_PGDN), 0);
680 return;
681 case KVAL(K_P4):
682 do_cur(KVAL(K_LEFT), 0);
683 return;
684 case KVAL(K_P6):
685 do_cur(KVAL(K_RIGHT), 0);
686 return;
687 case KVAL(K_P7):
688 do_fn(KVAL(K_FIND), 0);
689 return;
690 case KVAL(K_P8):
691 do_cur(KVAL(K_UP), 0);
692 return;
693 case KVAL(K_P9):
694 do_fn(KVAL(K_PGUP), 0);
695 return;
696 case KVAL(K_P5):
697 applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
698 return;
701 put_queue(pad_chars[value]);
702 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
703 put_queue(10);
706 static void do_cur(unsigned char value, char up_flag)
708 static const char *cur_chars = "BDCA";
709 if (up_flag)
710 return;
712 applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
715 static void do_shift(unsigned char value, char up_flag)
717 int old_state = shift_state;
719 if (rep)
720 return;
722 /* Mimic typewriter:
723 a CapsShift key acts like Shift but undoes CapsLock */
724 if (value == KVAL(K_CAPSSHIFT)) {
725 value = KVAL(K_SHIFT);
726 if (!up_flag)
727 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
730 if (up_flag) {
731 /* handle the case that two shift or control
732 keys are depressed simultaneously */
733 if (k_down[value])
734 k_down[value]--;
735 } else
736 k_down[value]++;
738 if (k_down[value])
739 shift_state |= (1 << value);
740 else
741 shift_state &= ~ (1 << value);
743 /* kludge */
744 if (up_flag && shift_state != old_state && npadch != -1) {
745 if (kbd->kbdmode == VC_UNICODE)
746 to_utf8(npadch & 0xffff);
747 else
748 put_queue(npadch & 0xff);
749 npadch = -1;
753 /* called after returning from RAW mode or when changing consoles -
754 recompute k_down[] and shift_state from key_down[] */
755 /* maybe called when keymap is undefined, so that shiftkey release is seen */
756 void compute_shiftstate(void)
758 int i, j, k, sym, val;
760 shift_state = 0;
761 for(i=0; i < SIZE(k_down); i++)
762 k_down[i] = 0;
764 for(i=0; i < SIZE(key_down); i++)
765 if(key_down[i]) { /* skip this word if not a single bit on */
766 k = i*BITS_PER_LONG;
767 for(j=0; j<BITS_PER_LONG; j++,k++)
768 if(test_bit(k, key_down)) {
769 sym = U(plain_map[k]);
770 if(KTYP(sym) == KT_SHIFT || KTYP(sym) == KT_SLOCK) {
771 val = KVAL(sym);
772 if (val == KVAL(K_CAPSSHIFT))
773 val = KVAL(K_SHIFT);
774 k_down[val]++;
775 shift_state |= (1<<val);
781 static void do_meta(unsigned char value, char up_flag)
783 if (up_flag)
784 return;
786 if (vc_kbd_mode(kbd, VC_META)) {
787 put_queue('\033');
788 put_queue(value);
789 } else
790 put_queue(value | 0x80);
793 static void do_ascii(unsigned char value, char up_flag)
795 int base;
797 if (up_flag)
798 return;
800 if (value < 10) /* decimal input of code, while Alt depressed */
801 base = 10;
802 else { /* hexadecimal input of code, while AltGr depressed */
803 value -= 10;
804 base = 16;
807 if (npadch == -1)
808 npadch = value;
809 else
810 npadch = npadch * base + value;
813 static void do_lock(unsigned char value, char up_flag)
815 if (up_flag || rep)
816 return;
817 chg_vc_kbd_lock(kbd, value);
820 static void do_slock(unsigned char value, char up_flag)
822 do_shift(value,up_flag);
823 if (up_flag || rep)
824 return;
825 chg_vc_kbd_slock(kbd, value);
826 /* try to make Alt, oops, AltGr and such work */
827 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
828 kbd->slockstate = 0;
829 chg_vc_kbd_slock(kbd, value);
834 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
835 * or (ii) whatever pattern of lights people want to show using KDSETLED,
836 * or (iii) specified bits of specified words in kernel memory.
839 static unsigned char ledstate = 0xff; /* undefined */
840 static unsigned char ledioctl;
842 unsigned char getledstate(void) {
843 return ledstate;
846 void setledstate(struct kbd_struct *kbd, unsigned int led) {
847 if (!(led & ~7)) {
848 ledioctl = led;
849 kbd->ledmode = LED_SHOW_IOCTL;
850 } else
851 kbd->ledmode = LED_SHOW_FLAGS;
852 set_leds();
855 static struct ledptr {
856 unsigned int *addr;
857 unsigned int mask;
858 unsigned char valid:1;
859 } ledptrs[3];
861 void register_leds(int console, unsigned int led,
862 unsigned int *addr, unsigned int mask) {
863 struct kbd_struct *kbd = kbd_table + console;
864 if (led < 3) {
865 ledptrs[led].addr = addr;
866 ledptrs[led].mask = mask;
867 ledptrs[led].valid = 1;
868 kbd->ledmode = LED_SHOW_MEM;
869 } else
870 kbd->ledmode = LED_SHOW_FLAGS;
873 static inline unsigned char getleds(void){
874 struct kbd_struct *kbd = kbd_table + fg_console;
875 unsigned char leds;
877 if (kbd->ledmode == LED_SHOW_IOCTL)
878 return ledioctl;
879 leds = kbd->ledflagstate;
880 if (kbd->ledmode == LED_SHOW_MEM) {
881 if (ledptrs[0].valid) {
882 if (*ledptrs[0].addr & ledptrs[0].mask)
883 leds |= 1;
884 else
885 leds &= ~1;
887 if (ledptrs[1].valid) {
888 if (*ledptrs[1].addr & ledptrs[1].mask)
889 leds |= 2;
890 else
891 leds &= ~2;
893 if (ledptrs[2].valid) {
894 if (*ledptrs[2].addr & ledptrs[2].mask)
895 leds |= 4;
896 else
897 leds &= ~4;
900 return leds;
904 * This routine is the bottom half of the keyboard interrupt
905 * routine, and runs with all interrupts enabled. It does
906 * console changing, led setting and copy_to_cooked, which can
907 * take a reasonably long time.
909 * Aside from timing (which isn't really that important for
910 * keyboard interrupts as they happen often), using the software
911 * interrupt routines for this thing allows us to easily mask
912 * this when we don't want any of the above to happen. Not yet
913 * used, but this allows for easy and efficient race-condition
914 * prevention later on.
916 static void kbd_bh(unsigned long dummy)
918 unsigned char leds = getleds();
920 if (leds != ledstate) {
921 ledstate = leds;
922 kbd_leds(leds);
923 if (kbd_ledfunc) kbd_ledfunc(leds);
927 EXPORT_SYMBOL(keyboard_tasklet);
928 DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
930 int __init kbd_init(void)
932 int i;
933 struct kbd_struct kbd0;
934 extern struct tty_driver console_driver;
936 kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
937 kbd0.ledmode = LED_SHOW_FLAGS;
938 kbd0.lockstate = KBD_DEFLOCK;
939 kbd0.slockstate = 0;
940 kbd0.modeflags = KBD_DEFMODE;
941 kbd0.kbdmode = VC_XLATE;
943 for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
944 kbd_table[i] = kbd0;
946 ttytab = console_driver.table;
948 kbd_init_hw();
950 tasklet_enable(&keyboard_tasklet);
951 tasklet_schedule(&keyboard_tasklet);
953 pm_kbd = pm_register(PM_SYS_DEV, PM_SYS_KBC, NULL);
955 return 0;