Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / keyboard.c
blob7e5a654c20f109b6df63fbcaf467a36fede6b3dc
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 int sysrq_enabled = 1;
162 #endif
164 static struct pm_dev *pm_kbd = NULL;
167 * Many other routines do put_queue, but I think either
168 * they produce ASCII, or they produce some user-assigned
169 * string, and in both cases we might assume that it is
170 * in utf-8 already.
172 void to_utf8(ushort c) {
173 if (c < 0x80)
174 put_queue(c); /* 0******* */
175 else if (c < 0x800) {
176 put_queue(0xc0 | (c >> 6)); /* 110***** 10****** */
177 put_queue(0x80 | (c & 0x3f));
178 } else {
179 put_queue(0xe0 | (c >> 12)); /* 1110**** 10****** 10****** */
180 put_queue(0x80 | ((c >> 6) & 0x3f));
181 put_queue(0x80 | (c & 0x3f));
183 /* UTF-8 is defined for words of up to 31 bits,
184 but we need only 16 bits here */
188 * Translation of escaped scancodes to keycodes.
189 * This is now user-settable (for machines were it makes sense).
192 int setkeycode(unsigned int scancode, unsigned int keycode)
194 return kbd_setkeycode(scancode, keycode);
197 int getkeycode(unsigned int scancode)
199 return kbd_getkeycode(scancode);
202 void handle_scancode(unsigned char scancode, int down)
204 unsigned char keycode;
205 char up_flag = down ? 0 : 0200;
206 char raw_mode;
208 pm_access(pm_kbd);
210 do_poke_blanked_console = 1;
211 tasklet_schedule(&console_tasklet);
212 add_keyboard_randomness(scancode | up_flag);
214 tty = ttytab? ttytab[fg_console]: NULL;
215 if (tty && (!tty->driver_data)) {
217 * We touch the tty structure via the the ttytab array
218 * without knowing whether or not tty is open, which
219 * is inherently dangerous. We currently rely on that
220 * fact that console_open sets tty->driver_data when
221 * it opens it, and clears it when it closes it.
223 tty = NULL;
225 kbd = kbd_table + fg_console;
226 if ((raw_mode = (kbd->kbdmode == VC_RAW))) {
227 put_queue(scancode | up_flag);
228 /* we do not return yet, because we want to maintain
229 the key_down array, so that we have the correct
230 values when finishing RAW mode or when changing VT's */
234 * Convert scancode to keycode
236 if (!kbd_translate(scancode, &keycode, raw_mode))
237 return;
240 * At this point the variable `keycode' contains the keycode.
241 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
242 * We keep track of the up/down status of the key, and
243 * return the keycode if in MEDIUMRAW mode.
246 if (up_flag) {
247 rep = 0;
248 if(!test_and_clear_bit(keycode, key_down))
249 up_flag = kbd_unexpected_up(keycode);
250 } else
251 rep = test_and_set_bit(keycode, key_down);
253 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
254 if (keycode == SYSRQ_KEY) {
255 sysrq_pressed = !up_flag;
256 return;
257 } else if (sysrq_pressed) {
258 if (!up_flag && sysrq_enabled) {
259 handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty);
260 return;
263 #endif
265 if (kbd->kbdmode == VC_MEDIUMRAW) {
266 /* soon keycodes will require more than one byte */
267 put_queue(keycode + up_flag);
268 raw_mode = 1; /* Most key classes will be ignored */
272 * Small change in philosophy: earlier we defined repetition by
273 * rep = keycode == prev_keycode;
274 * prev_keycode = keycode;
275 * but now by the fact that the depressed key was down already.
276 * Does this ever make a difference? Yes.
280 * Repeat a key only if the input buffers are empty or the
281 * characters get echoed locally. This makes key repeat usable
282 * with slow applications and under heavy loads.
284 if (!rep ||
285 (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
286 (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
287 u_short keysym;
288 u_char type;
290 /* the XOR below used to be an OR */
291 int shift_final = (shift_state | kbd->slockstate) ^
292 kbd->lockstate;
293 ushort *key_map = key_maps[shift_final];
295 if (key_map != NULL) {
296 keysym = key_map[keycode];
297 type = KTYP(keysym);
299 if (type >= 0xf0) {
300 type -= 0xf0;
301 if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type)))
302 return;
303 if (type == KT_LETTER) {
304 type = KT_LATIN;
305 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
306 key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
307 if (key_map)
308 keysym = key_map[keycode];
311 (*key_handler[type])(keysym & 0xff, up_flag);
312 if (type != KT_SLOCK)
313 kbd->slockstate = 0;
314 } else {
315 /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
316 if (!up_flag && !raw_mode)
317 to_utf8(keysym);
319 } else {
320 /* maybe beep? */
321 /* we have at least to update shift_state */
322 #if 1 /* how? two almost equivalent choices follow */
323 compute_shiftstate();
324 kbd->slockstate = 0; /* play it safe */
325 #else
326 keysym = U(plain_map[keycode]);
327 type = KTYP(keysym);
328 if (type == KT_SHIFT)
329 (*key_handler[type])(keysym & 0xff, up_flag);
330 #endif
335 #ifdef CONFIG_FORWARD_KEYBOARD
336 extern int forward_chars;
338 void put_queue(int ch)
340 if (forward_chars == fg_console+1){
341 kbd_forward_char (ch);
342 } else {
343 wake_up(&keypress_wait);
344 if (tty) {
345 tty_insert_flip_char(tty, ch, 0);
346 con_schedule_flip(tty);
350 #else
351 void put_queue(int ch)
353 wake_up(&keypress_wait);
354 if (tty) {
355 tty_insert_flip_char(tty, ch, 0);
356 con_schedule_flip(tty);
359 #endif
361 static void puts_queue(char *cp)
363 wake_up(&keypress_wait);
364 if (!tty)
365 return;
367 while (*cp) {
368 tty_insert_flip_char(tty, *cp, 0);
369 cp++;
371 con_schedule_flip(tty);
374 static void applkey(int key, char mode)
376 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
378 buf[1] = (mode ? 'O' : '[');
379 buf[2] = key;
380 puts_queue(buf);
383 static void enter(void)
385 if (diacr) {
386 put_queue(diacr);
387 diacr = 0;
389 put_queue(13);
390 if (vc_kbd_mode(kbd,VC_CRLF))
391 put_queue(10);
394 static void caps_toggle(void)
396 if (rep)
397 return;
398 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
401 static void caps_on(void)
403 if (rep)
404 return;
405 set_vc_kbd_led(kbd, VC_CAPSLOCK);
408 static void show_ptregs(void)
410 if (kbd_pt_regs)
411 show_regs(kbd_pt_regs);
414 static void hold(void)
416 if (rep || !tty)
417 return;
420 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
421 * these routines are also activated by ^S/^Q.
422 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
424 if (tty->stopped)
425 start_tty(tty);
426 else
427 stop_tty(tty);
430 static void num(void)
432 if (vc_kbd_mode(kbd,VC_APPLIC))
433 applkey('P', 1);
434 else
435 bare_num();
439 * Bind this to Shift-NumLock if you work in application keypad mode
440 * but want to be able to change the NumLock flag.
441 * Bind this to NumLock if you prefer that the NumLock key always
442 * changes the NumLock flag.
444 static void bare_num(void)
446 if (!rep)
447 chg_vc_kbd_led(kbd,VC_NUMLOCK);
450 static void lastcons(void)
452 /* switch to the last used console, ChN */
453 set_console(last_console);
456 static void decr_console(void)
458 int i;
460 for (i = fg_console-1; i != fg_console; i--) {
461 if (i == -1)
462 i = MAX_NR_CONSOLES-1;
463 if (vc_cons_allocated(i))
464 break;
466 set_console(i);
469 static void incr_console(void)
471 int i;
473 for (i = fg_console+1; i != fg_console; i++) {
474 if (i == MAX_NR_CONSOLES)
475 i = 0;
476 if (vc_cons_allocated(i))
477 break;
479 set_console(i);
482 static void send_intr(void)
484 if (!tty)
485 return;
486 tty_insert_flip_char(tty, 0, TTY_BREAK);
487 con_schedule_flip(tty);
490 static void scroll_forw(void)
492 scrollfront(0);
495 static void scroll_back(void)
497 scrollback(0);
500 static void boot_it(void)
502 ctrl_alt_del();
505 static void compose(void)
507 dead_key_next = 1;
510 int spawnpid, spawnsig;
512 static void spawn_console(void)
514 if (spawnpid)
515 if(kill_proc(spawnpid, spawnsig, 1))
516 spawnpid = 0;
519 static void SAK(void)
522 * SAK should also work in all raw modes and reset
523 * them properly.
526 do_SAK(tty);
527 reset_vc(fg_console);
528 #if 0
529 do_unblank_screen(); /* not in interrupt routine? */
530 #endif
533 static void do_ignore(unsigned char value, char up_flag)
537 static void do_null()
539 compute_shiftstate();
542 static void do_spec(unsigned char value, char up_flag)
544 if (up_flag)
545 return;
546 if (value >= SIZE(spec_fn_table))
547 return;
548 if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
549 !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
550 return;
551 spec_fn_table[value]();
554 static void do_lowercase(unsigned char value, char up_flag)
556 printk(KERN_ERR "keyboard.c: do_lowercase was called - impossible\n");
559 static void do_self(unsigned char value, char up_flag)
561 if (up_flag)
562 return; /* no action, if this is a key release */
564 if (diacr)
565 value = handle_diacr(value);
567 if (dead_key_next) {
568 dead_key_next = 0;
569 diacr = value;
570 return;
573 put_queue(value);
576 #define A_GRAVE '`'
577 #define A_ACUTE '\''
578 #define A_CFLEX '^'
579 #define A_TILDE '~'
580 #define A_DIAER '"'
581 #define A_CEDIL ','
582 static unsigned char ret_diacr[NR_DEAD] =
583 {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
585 /* Obsolete - for backwards compatibility only */
586 static void do_dead(unsigned char value, char up_flag)
588 value = ret_diacr[value];
589 do_dead2(value,up_flag);
593 * Handle dead key. Note that we now may have several
594 * dead keys modifying the same character. Very useful
595 * for Vietnamese.
597 static void do_dead2(unsigned char value, char up_flag)
599 if (up_flag)
600 return;
602 diacr = (diacr ? handle_diacr(value) : value);
607 * We have a combining character DIACR here, followed by the character CH.
608 * If the combination occurs in the table, return the corresponding value.
609 * Otherwise, if CH is a space or equals DIACR, return DIACR.
610 * Otherwise, conclude that DIACR was not combining after all,
611 * queue it and return CH.
613 unsigned char handle_diacr(unsigned char ch)
615 int d = diacr;
616 int i;
618 diacr = 0;
620 for (i = 0; i < accent_table_size; i++) {
621 if (accent_table[i].diacr == d && accent_table[i].base == ch)
622 return accent_table[i].result;
625 if (ch == ' ' || ch == d)
626 return d;
628 put_queue(d);
629 return ch;
632 static void do_cons(unsigned char value, char up_flag)
634 if (up_flag)
635 return;
636 set_console(value);
639 static void do_fn(unsigned char value, char up_flag)
641 if (up_flag)
642 return;
643 if (value < SIZE(func_table)) {
644 if (func_table[value])
645 puts_queue(func_table[value]);
646 } else
647 printk(KERN_ERR "do_fn called with value=%d\n", value);
650 static void do_pad(unsigned char value, char up_flag)
652 static const char *pad_chars = "0123456789+-*/\015,.?()";
653 static const char *app_map = "pqrstuvwxylSRQMnnmPQ";
655 if (up_flag)
656 return; /* no action, if this is a key release */
658 /* kludge... shift forces cursor/number keys */
659 if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
660 applkey(app_map[value], 1);
661 return;
664 if (!vc_kbd_led(kbd,VC_NUMLOCK))
665 switch (value) {
666 case KVAL(K_PCOMMA):
667 case KVAL(K_PDOT):
668 do_fn(KVAL(K_REMOVE), 0);
669 return;
670 case KVAL(K_P0):
671 do_fn(KVAL(K_INSERT), 0);
672 return;
673 case KVAL(K_P1):
674 do_fn(KVAL(K_SELECT), 0);
675 return;
676 case KVAL(K_P2):
677 do_cur(KVAL(K_DOWN), 0);
678 return;
679 case KVAL(K_P3):
680 do_fn(KVAL(K_PGDN), 0);
681 return;
682 case KVAL(K_P4):
683 do_cur(KVAL(K_LEFT), 0);
684 return;
685 case KVAL(K_P6):
686 do_cur(KVAL(K_RIGHT), 0);
687 return;
688 case KVAL(K_P7):
689 do_fn(KVAL(K_FIND), 0);
690 return;
691 case KVAL(K_P8):
692 do_cur(KVAL(K_UP), 0);
693 return;
694 case KVAL(K_P9):
695 do_fn(KVAL(K_PGUP), 0);
696 return;
697 case KVAL(K_P5):
698 applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
699 return;
702 put_queue(pad_chars[value]);
703 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
704 put_queue(10);
707 static void do_cur(unsigned char value, char up_flag)
709 static const char *cur_chars = "BDCA";
710 if (up_flag)
711 return;
713 applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
716 static void do_shift(unsigned char value, char up_flag)
718 int old_state = shift_state;
720 if (rep)
721 return;
723 /* Mimic typewriter:
724 a CapsShift key acts like Shift but undoes CapsLock */
725 if (value == KVAL(K_CAPSSHIFT)) {
726 value = KVAL(K_SHIFT);
727 if (!up_flag)
728 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
731 if (up_flag) {
732 /* handle the case that two shift or control
733 keys are depressed simultaneously */
734 if (k_down[value])
735 k_down[value]--;
736 } else
737 k_down[value]++;
739 if (k_down[value])
740 shift_state |= (1 << value);
741 else
742 shift_state &= ~ (1 << value);
744 /* kludge */
745 if (up_flag && shift_state != old_state && npadch != -1) {
746 if (kbd->kbdmode == VC_UNICODE)
747 to_utf8(npadch & 0xffff);
748 else
749 put_queue(npadch & 0xff);
750 npadch = -1;
754 /* called after returning from RAW mode or when changing consoles -
755 recompute k_down[] and shift_state from key_down[] */
756 /* maybe called when keymap is undefined, so that shiftkey release is seen */
757 void compute_shiftstate(void)
759 int i, j, k, sym, val;
761 shift_state = 0;
762 for(i=0; i < SIZE(k_down); i++)
763 k_down[i] = 0;
765 for(i=0; i < SIZE(key_down); i++)
766 if(key_down[i]) { /* skip this word if not a single bit on */
767 k = i*BITS_PER_LONG;
768 for(j=0; j<BITS_PER_LONG; j++,k++)
769 if(test_bit(k, key_down)) {
770 sym = U(plain_map[k]);
771 if(KTYP(sym) == KT_SHIFT || KTYP(sym) == KT_SLOCK) {
772 val = KVAL(sym);
773 if (val == KVAL(K_CAPSSHIFT))
774 val = KVAL(K_SHIFT);
775 k_down[val]++;
776 shift_state |= (1<<val);
782 static void do_meta(unsigned char value, char up_flag)
784 if (up_flag)
785 return;
787 if (vc_kbd_mode(kbd, VC_META)) {
788 put_queue('\033');
789 put_queue(value);
790 } else
791 put_queue(value | 0x80);
794 static void do_ascii(unsigned char value, char up_flag)
796 int base;
798 if (up_flag)
799 return;
801 if (value < 10) /* decimal input of code, while Alt depressed */
802 base = 10;
803 else { /* hexadecimal input of code, while AltGr depressed */
804 value -= 10;
805 base = 16;
808 if (npadch == -1)
809 npadch = value;
810 else
811 npadch = npadch * base + value;
814 static void do_lock(unsigned char value, char up_flag)
816 if (up_flag || rep)
817 return;
818 chg_vc_kbd_lock(kbd, value);
821 static void do_slock(unsigned char value, char up_flag)
823 do_shift(value,up_flag);
824 if (up_flag || rep)
825 return;
826 chg_vc_kbd_slock(kbd, value);
827 /* try to make Alt, oops, AltGr and such work */
828 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
829 kbd->slockstate = 0;
830 chg_vc_kbd_slock(kbd, value);
835 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
836 * or (ii) whatever pattern of lights people want to show using KDSETLED,
837 * or (iii) specified bits of specified words in kernel memory.
840 static unsigned char ledstate = 0xff; /* undefined */
841 static unsigned char ledioctl;
843 unsigned char getledstate(void) {
844 return ledstate;
847 void setledstate(struct kbd_struct *kbd, unsigned int led) {
848 if (!(led & ~7)) {
849 ledioctl = led;
850 kbd->ledmode = LED_SHOW_IOCTL;
851 } else
852 kbd->ledmode = LED_SHOW_FLAGS;
853 set_leds();
856 static struct ledptr {
857 unsigned int *addr;
858 unsigned int mask;
859 unsigned char valid:1;
860 } ledptrs[3];
862 void register_leds(int console, unsigned int led,
863 unsigned int *addr, unsigned int mask) {
864 struct kbd_struct *kbd = kbd_table + console;
865 if (led < 3) {
866 ledptrs[led].addr = addr;
867 ledptrs[led].mask = mask;
868 ledptrs[led].valid = 1;
869 kbd->ledmode = LED_SHOW_MEM;
870 } else
871 kbd->ledmode = LED_SHOW_FLAGS;
874 static inline unsigned char getleds(void){
875 struct kbd_struct *kbd = kbd_table + fg_console;
876 unsigned char leds;
878 if (kbd->ledmode == LED_SHOW_IOCTL)
879 return ledioctl;
880 leds = kbd->ledflagstate;
881 if (kbd->ledmode == LED_SHOW_MEM) {
882 if (ledptrs[0].valid) {
883 if (*ledptrs[0].addr & ledptrs[0].mask)
884 leds |= 1;
885 else
886 leds &= ~1;
888 if (ledptrs[1].valid) {
889 if (*ledptrs[1].addr & ledptrs[1].mask)
890 leds |= 2;
891 else
892 leds &= ~2;
894 if (ledptrs[2].valid) {
895 if (*ledptrs[2].addr & ledptrs[2].mask)
896 leds |= 4;
897 else
898 leds &= ~4;
901 return leds;
905 * This routine is the bottom half of the keyboard interrupt
906 * routine, and runs with all interrupts enabled. It does
907 * console changing, led setting and copy_to_cooked, which can
908 * take a reasonably long time.
910 * Aside from timing (which isn't really that important for
911 * keyboard interrupts as they happen often), using the software
912 * interrupt routines for this thing allows us to easily mask
913 * this when we don't want any of the above to happen. Not yet
914 * used, but this allows for easy and efficient race-condition
915 * prevention later on.
917 static void kbd_bh(unsigned long dummy)
919 unsigned char leds = getleds();
921 if (leds != ledstate) {
922 ledstate = leds;
923 kbd_leds(leds);
924 if (kbd_ledfunc) kbd_ledfunc(leds);
928 EXPORT_SYMBOL(keyboard_tasklet);
929 DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
931 int __init kbd_init(void)
933 int i;
934 struct kbd_struct kbd0;
935 extern struct tty_driver console_driver;
937 kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
938 kbd0.ledmode = LED_SHOW_FLAGS;
939 kbd0.lockstate = KBD_DEFLOCK;
940 kbd0.slockstate = 0;
941 kbd0.modeflags = KBD_DEFMODE;
942 kbd0.kbdmode = VC_XLATE;
944 for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
945 kbd_table[i] = kbd0;
947 ttytab = console_driver.table;
949 kbd_init_hw();
951 tasklet_enable(&keyboard_tasklet);
952 tasklet_schedule(&keyboard_tasklet);
954 pm_kbd = pm_register(PM_SYS_DEV, PM_SYS_KBC, NULL);
956 return 0;