File: /usr/src/linux/drivers/char/keyboard.c

1     /*
2      * linux/drivers/char/keyboard.c
3      *
4      * Written for linux by Johan Myreen as a translation from
5      * the assembly version by Linus (with diacriticals added)
6      *
7      * Some additional features added by Christoph Niemann (ChN), March 1993
8      *
9      * Loadable keymaps by Risto Kankkunen, May 1993
10      *
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.
15      *
16      * 11-11-96: SAK should now work in the raw mode (Martin Mares)
17      * 
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
21      *
22      * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
23      * 30-07-98: Dead keys redone, aeb@cwi.nl.
24      */
25     
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>
35     
36     #include <asm/keyboard.h>
37     #include <asm/bitops.h>
38     
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>
45     
46     #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
47     
48     #ifndef KBD_DEFMODE
49     #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
50     #endif
51     
52     #ifndef KBD_DEFLEDS
53     /*
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.
56      */
57     #define KBD_DEFLEDS 0
58     #endif
59     
60     #ifndef KBD_DEFLOCK
61     #define KBD_DEFLOCK 0
62     #endif
63     
64     void (*kbd_ledfunc)(unsigned int led);
65     EXPORT_SYMBOL(handle_scancode);
66     EXPORT_SYMBOL(kbd_ledfunc);
67     
68     extern void ctrl_alt_del(void);
69     
70     DECLARE_WAIT_QUEUE_HEAD(keypress_wait);
71     struct console;
72     
73     int keyboard_wait_for_keypress(struct console *co)
74     {
75     	sleep_on(&keypress_wait);
76     	return 0;
77     }
78     
79     /*
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)
83      */
84     
85     /* shift state counters.. */
86     static unsigned char k_down[NR_SHIFT];
87     /* keyboard key bitmap */
88     static unsigned long key_down[256/BITS_PER_LONG];
89     
90     static int dead_key_next;
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.
95      */
96     int shift_state;
97     static int npadch = -1;			/* -1 or number assembled on pad */
98     static unsigned char diacr;
99     static char rep;			/* 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;
104     
105     void compute_shiftstate(void);
106     
107     typedef void (*k_hand)(unsigned char value, char up_flag);
108     typedef void (k_handfn)(unsigned char value, char up_flag);
109     
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;
114     
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
119     };
120     
121     /* Key types processed even in raw modes */
122     
123     #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
124     
125     typedef void (*void_fnp)(void);
126     typedef void (void_fn)(void);
127     
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;
131     
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
138     };
139     
140     #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
141     
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
148     };
149     
150     const int NR_TYPES = SIZE(max_vals);
151     
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);
155     
156     /* kbd_pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
157     struct pt_regs * kbd_pt_regs;
158     
159     #ifdef CONFIG_MAGIC_SYSRQ
160     static int sysrq_pressed;
161     #endif
162     
163     static struct pm_dev *pm_kbd;
164     
165     /*
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.
170      */
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));
181         }
182         /* UTF-8 is defined for words of up to 31 bits,
183            but we need only 16 bits here */
184     }
185     
186     /*
187      * Translation of escaped scancodes to keycodes.
188      * This is now user-settable (for machines were it makes sense).
189      */
190     
191     int setkeycode(unsigned int scancode, unsigned int keycode)
192     {
193         return kbd_setkeycode(scancode, keycode);
194     }
195     
196     int getkeycode(unsigned int scancode)
197     {
198         return kbd_getkeycode(scancode);
199     }
200     
201     void handle_scancode(unsigned char scancode, int down)
202     {
203     	unsigned char keycode;
204     	char up_flag = down ? 0 : 0200;
205     	char raw_mode;
206     
207     	pm_access(pm_kbd);
208     	add_keyboard_randomness(scancode | up_flag);
209     
210     	tty = ttytab? ttytab[fg_console]: NULL;
211     	if (tty && (!tty->driver_data)) {
212     		/*
213     		 * We touch the tty structure via the ttytab array
214     		 * without knowing whether or not tty is open, which
215     		 * is inherently dangerous.  We currently rely on that
216     		 * fact that console_open sets tty->driver_data when
217     		 * it opens it, and clears it when it closes it.
218     		 */
219     		tty = NULL;
220     	}
221     	kbd = kbd_table + fg_console;
222     	if ((raw_mode = (kbd->kbdmode == VC_RAW))) {
223     		put_queue(scancode | up_flag);
224     		/* we do not return yet, because we want to maintain
225     		   the key_down array, so that we have the correct
226     		   values when finishing RAW mode or when changing VT's */
227     	}
228     
229     	/*
230     	 *  Convert scancode to keycode
231     	 */
232     	if (!kbd_translate(scancode, &keycode, raw_mode))
233     		goto out;
234     
235     	/*
236     	 * At this point the variable `keycode' contains the keycode.
237     	 * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
238     	 * We keep track of the up/down status of the key, and
239     	 * return the keycode if in MEDIUMRAW mode.
240     	 */
241     
242     	if (up_flag) {
243     		rep = 0;
244     		if(!test_and_clear_bit(keycode, key_down))
245     		    up_flag = kbd_unexpected_up(keycode);
246     	} else
247     		rep = test_and_set_bit(keycode, key_down);
248     
249     #ifdef CONFIG_MAGIC_SYSRQ		/* Handle the SysRq Hack */
250     	if (keycode == SYSRQ_KEY) {
251     		sysrq_pressed = !up_flag;
252     		goto out;
253     	} else if (sysrq_pressed) {
254     		if (!up_flag) {
255     			handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty);
256     			goto out;
257     		}
258     	}
259     #endif
260     
261     	if (kbd->kbdmode == VC_MEDIUMRAW) {
262     		/* soon keycodes will require more than one byte */
263     		put_queue(keycode + up_flag);
264     		raw_mode = 1;	/* Most key classes will be ignored */
265     	}
266     
267     	/*
268     	 * Small change in philosophy: earlier we defined repetition by
269     	 *	 rep = keycode == prev_keycode;
270     	 *	 prev_keycode = keycode;
271     	 * but now by the fact that the depressed key was down already.
272     	 * Does this ever make a difference? Yes.
273     	 */
274     
275     	/*
276     	 *  Repeat a key only if the input buffers are empty or the
277     	 *  characters get echoed locally. This makes key repeat usable
278     	 *  with slow applications and under heavy loads.
279     	 */
280     	if (!rep ||
281     	    (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
282     	     (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
283     		u_short keysym;
284     		u_char type;
285     
286     		/* the XOR below used to be an OR */
287     		int shift_final = (shift_state | kbd->slockstate) ^
288     		    kbd->lockstate;
289     		ushort *key_map = key_maps[shift_final];
290     
291     		if (key_map != NULL) {
292     			keysym = key_map[keycode];
293     			type = KTYP(keysym);
294     
295     			if (type >= 0xf0) {
296     			    type -= 0xf0;
297     			    if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type)))
298     				goto out;
299     			    if (type == KT_LETTER) {
300     				type = KT_LATIN;
301     				if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
302     				    key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
303     				    if (key_map)
304     				      keysym = key_map[keycode];
305     				}
306     			    }
307     			    (*key_handler[type])(keysym & 0xff, up_flag);
308     			    if (type != KT_SLOCK)
309     			      kbd->slockstate = 0;
310     			} else {
311     			    /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
312     			    if (!up_flag && !raw_mode)
313     			      to_utf8(keysym);
314     			}
315     		} else {
316     			/* maybe beep? */
317     			/* we have at least to update shift_state */
318     #if 1			/* how? two almost equivalent choices follow */
319     			compute_shiftstate();
320     			kbd->slockstate = 0; /* play it safe */
321     #else
322     			keysym =  U(plain_map[keycode]);
323     			type = KTYP(keysym);
324     			if (type == KT_SHIFT)
325     			  (*key_handler[type])(keysym & 0xff, up_flag);
326     #endif
327     		}
328     	}
329     out:
330     	do_poke_blanked_console = 1;
331     	schedule_console_callback();
332     }
333     
334     
335     void put_queue(int ch)
336     {
337     	wake_up(&keypress_wait);
338     	if (tty) {
339     		tty_insert_flip_char(tty, ch, 0);
340     		con_schedule_flip(tty);
341     	}
342     }
343     
344     static void puts_queue(char *cp)
345     {
346     	wake_up(&keypress_wait);
347     	if (!tty)
348     		return;
349     
350     	while (*cp) {
351     		tty_insert_flip_char(tty, *cp, 0);
352     		cp++;
353     	}
354     	con_schedule_flip(tty);
355     }
356     
357     static void applkey(int key, char mode)
358     {
359     	static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
360     
361     	buf[1] = (mode ? 'O' : '[');
362     	buf[2] = key;
363     	puts_queue(buf);
364     }
365     
366     static void enter(void)
367     {
368     	if (diacr) {
369     		put_queue(diacr);
370     		diacr = 0;
371     	}
372     	put_queue(13);
373     	if (vc_kbd_mode(kbd,VC_CRLF))
374     		put_queue(10);
375     }
376     
377     static void caps_toggle(void)
378     {
379     	if (rep)
380     		return;
381     	chg_vc_kbd_led(kbd, VC_CAPSLOCK);
382     }
383     
384     static void caps_on(void)
385     {
386     	if (rep)
387     		return;
388     	set_vc_kbd_led(kbd, VC_CAPSLOCK);
389     }
390     
391     static void show_ptregs(void)
392     {
393     	if (kbd_pt_regs)
394     		show_regs(kbd_pt_regs);
395     }
396     
397     static void hold(void)
398     {
399     	if (rep || !tty)
400     		return;
401     
402     	/*
403     	 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
404     	 * these routines are also activated by ^S/^Q.
405     	 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
406     	 */
407     	if (tty->stopped)
408     		start_tty(tty);
409     	else
410     		stop_tty(tty);
411     }
412     
413     static void num(void)
414     {
415     	if (vc_kbd_mode(kbd,VC_APPLIC))
416     		applkey('P', 1);
417     	else
418     		bare_num();
419     }
420     
421     /*
422      * Bind this to Shift-NumLock if you work in application keypad mode
423      * but want to be able to change the NumLock flag.
424      * Bind this to NumLock if you prefer that the NumLock key always
425      * changes the NumLock flag.
426      */
427     static void bare_num(void)
428     {
429     	if (!rep)
430     		chg_vc_kbd_led(kbd,VC_NUMLOCK);
431     }
432     
433     static void lastcons(void)
434     {
435     	/* switch to the last used console, ChN */
436     	set_console(last_console);
437     }
438     
439     static void decr_console(void)
440     {
441     	int i;
442      
443     	for (i = fg_console-1; i != fg_console; i--) {
444     		if (i == -1)
445     			i = MAX_NR_CONSOLES-1;
446     		if (vc_cons_allocated(i))
447     			break;
448     	}
449     	set_console(i);
450     }
451     
452     static void incr_console(void)
453     {
454     	int i;
455     
456     	for (i = fg_console+1; i != fg_console; i++) {
457     		if (i == MAX_NR_CONSOLES)
458     			i = 0;
459     		if (vc_cons_allocated(i))
460     			break;
461     	}
462     	set_console(i);
463     }
464     
465     static void send_intr(void)
466     {
467     	if (!tty)
468     		return;
469     	tty_insert_flip_char(tty, 0, TTY_BREAK);
470     	con_schedule_flip(tty);
471     }
472     
473     static void scroll_forw(void)
474     {
475     	scrollfront(0);
476     }
477     
478     static void scroll_back(void)
479     {
480     	scrollback(0);
481     }
482     
483     static void boot_it(void)
484     {
485     	ctrl_alt_del();
486     }
487     
488     static void compose(void)
489     {
490     	dead_key_next = 1;
491     }
492     
493     int spawnpid, spawnsig;
494     
495     static void spawn_console(void)
496     {
497             if (spawnpid)
498     	   if(kill_proc(spawnpid, spawnsig, 1))
499     	     spawnpid = 0;
500     }
501     
502     static void SAK(void)
503     {
504     	/*
505     	 * SAK should also work in all raw modes and reset
506     	 * them properly.
507     	 */
508     
509     	do_SAK(tty);
510     	reset_vc(fg_console);
511     #if 0
512     	do_unblank_screen();	/* not in interrupt routine? */
513     #endif
514     }
515     
516     static void do_ignore(unsigned char value, char up_flag)
517     {
518     }
519     
520     static void do_null()
521     {
522     	compute_shiftstate();
523     }
524     
525     static void do_spec(unsigned char value, char up_flag)
526     {
527     	if (up_flag)
528     		return;
529     	if (value >= SIZE(spec_fn_table))
530     		return;
531     	if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
532     	    !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
533     		return;
534     	spec_fn_table[value]();
535     }
536     
537     static void do_lowercase(unsigned char value, char up_flag)
538     {
539     	printk(KERN_ERR "keyboard.c: do_lowercase was called - impossible\n");
540     }
541     
542     static void do_self(unsigned char value, char up_flag)
543     {
544     	if (up_flag)
545     		return;		/* no action, if this is a key release */
546     
547     	if (diacr)
548     		value = handle_diacr(value);
549     
550     	if (dead_key_next) {
551     		dead_key_next = 0;
552     		diacr = value;
553     		return;
554     	}
555     
556     	put_queue(value);
557     }
558     
559     #define A_GRAVE  '`'
560     #define A_ACUTE  '\''
561     #define A_CFLEX  '^'
562     #define A_TILDE  '~'
563     #define A_DIAER  '"'
564     #define A_CEDIL  ','
565     static unsigned char ret_diacr[NR_DEAD] =
566     	{A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
567     
568     /* Obsolete - for backwards compatibility only */
569     static void do_dead(unsigned char value, char up_flag)
570     {
571     	value = ret_diacr[value];
572     	do_dead2(value,up_flag);
573     }
574     
575     /*
576      * Handle dead key. Note that we now may have several
577      * dead keys modifying the same character. Very useful
578      * for Vietnamese.
579      */
580     static void do_dead2(unsigned char value, char up_flag)
581     {
582     	if (up_flag)
583     		return;
584     
585     	diacr = (diacr ? handle_diacr(value) : value);
586     }
587     
588     
589     /*
590      * We have a combining character DIACR here, followed by the character CH.
591      * If the combination occurs in the table, return the corresponding value.
592      * Otherwise, if CH is a space or equals DIACR, return DIACR.
593      * Otherwise, conclude that DIACR was not combining after all,
594      * queue it and return CH.
595      */
596     unsigned char handle_diacr(unsigned char ch)
597     {
598     	int d = diacr;
599     	int i;
600     
601     	diacr = 0;
602     
603     	for (i = 0; i < accent_table_size; i++) {
604     		if (accent_table[i].diacr == d && accent_table[i].base == ch)
605     			return accent_table[i].result;
606     	}
607     
608     	if (ch == ' ' || ch == d)
609     		return d;
610     
611     	put_queue(d);
612     	return ch;
613     }
614     
615     static void do_cons(unsigned char value, char up_flag)
616     {
617     	if (up_flag)
618     		return;
619     	set_console(value);
620     }
621     
622     static void do_fn(unsigned char value, char up_flag)
623     {
624     	if (up_flag)
625     		return;
626     	if (value < SIZE(func_table)) {
627     		if (func_table[value])
628     			puts_queue(func_table[value]);
629     	} else
630     		printk(KERN_ERR "do_fn called with value=%d\n", value);
631     }
632     
633     static void do_pad(unsigned char value, char up_flag)
634     {
635     	static const char *pad_chars = "0123456789+-*/\015,.?()";
636     	static const char *app_map = "pqrstuvwxylSRQMnnmPQ";
637     
638     	if (up_flag)
639     		return;		/* no action, if this is a key release */
640     
641     	/* kludge... shift forces cursor/number keys */
642     	if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
643     		applkey(app_map[value], 1);
644     		return;
645     	}
646     
647     	if (!vc_kbd_led(kbd,VC_NUMLOCK))
648     		switch (value) {
649     			case KVAL(K_PCOMMA):
650     			case KVAL(K_PDOT):
651     				do_fn(KVAL(K_REMOVE), 0);
652     				return;
653     			case KVAL(K_P0):
654     				do_fn(KVAL(K_INSERT), 0);
655     				return;
656     			case KVAL(K_P1):
657     				do_fn(KVAL(K_SELECT), 0);
658     				return;
659     			case KVAL(K_P2):
660     				do_cur(KVAL(K_DOWN), 0);
661     				return;
662     			case KVAL(K_P3):
663     				do_fn(KVAL(K_PGDN), 0);
664     				return;
665     			case KVAL(K_P4):
666     				do_cur(KVAL(K_LEFT), 0);
667     				return;
668     			case KVAL(K_P6):
669     				do_cur(KVAL(K_RIGHT), 0);
670     				return;
671     			case KVAL(K_P7):
672     				do_fn(KVAL(K_FIND), 0);
673     				return;
674     			case KVAL(K_P8):
675     				do_cur(KVAL(K_UP), 0);
676     				return;
677     			case KVAL(K_P9):
678     				do_fn(KVAL(K_PGUP), 0);
679     				return;
680     			case KVAL(K_P5):
681     				applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
682     				return;
683     		}
684     
685     	put_queue(pad_chars[value]);
686     	if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
687     		put_queue(10);
688     }
689     
690     static void do_cur(unsigned char value, char up_flag)
691     {
692     	static const char *cur_chars = "BDCA";
693     	if (up_flag)
694     		return;
695     
696     	applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
697     }
698     
699     static void do_shift(unsigned char value, char up_flag)
700     {
701     	int old_state = shift_state;
702     
703     	if (rep)
704     		return;
705     
706     	/* Mimic typewriter:
707     	   a CapsShift key acts like Shift but undoes CapsLock */
708     	if (value == KVAL(K_CAPSSHIFT)) {
709     		value = KVAL(K_SHIFT);
710     		if (!up_flag)
711     			clr_vc_kbd_led(kbd, VC_CAPSLOCK);
712     	}
713     
714     	if (up_flag) {
715     		/* handle the case that two shift or control
716     		   keys are depressed simultaneously */
717     		if (k_down[value])
718     			k_down[value]--;
719     	} else
720     		k_down[value]++;
721     
722     	if (k_down[value])
723     		shift_state |= (1 << value);
724     	else
725     		shift_state &= ~ (1 << value);
726     
727     	/* kludge */
728     	if (up_flag && shift_state != old_state && npadch != -1) {
729     		if (kbd->kbdmode == VC_UNICODE)
730     		  to_utf8(npadch & 0xffff);
731     		else
732     		  put_queue(npadch & 0xff);
733     		npadch = -1;
734     	}
735     }
736     
737     /* called after returning from RAW mode or when changing consoles -
738        recompute k_down[] and shift_state from key_down[] */
739     /* maybe called when keymap is undefined, so that shiftkey release is seen */
740     void compute_shiftstate(void)
741     {
742     	int i, j, k, sym, val;
743     
744     	shift_state = 0;
745     	for(i=0; i < SIZE(k_down); i++)
746     	  k_down[i] = 0;
747     
748     	for(i=0; i < SIZE(key_down); i++)
749     	  if(key_down[i]) {	/* skip this word if not a single bit on */
750     	    k = i*BITS_PER_LONG;
751     	    for(j=0; j<BITS_PER_LONG; j++,k++)
752     	      if(test_bit(k, key_down)) {
753     		sym = U(plain_map[k]);
754     		if(KTYP(sym) == KT_SHIFT || KTYP(sym) == KT_SLOCK) {
755     		  val = KVAL(sym);
756     		  if (val == KVAL(K_CAPSSHIFT))
757     		    val = KVAL(K_SHIFT);
758     		  k_down[val]++;
759     		  shift_state |= (1<<val);
760     		}
761     	      }
762     	  }
763     }
764     
765     static void do_meta(unsigned char value, char up_flag)
766     {
767     	if (up_flag)
768     		return;
769     
770     	if (vc_kbd_mode(kbd, VC_META)) {
771     		put_queue('\033');
772     		put_queue(value);
773     	} else
774     		put_queue(value | 0x80);
775     }
776     
777     static void do_ascii(unsigned char value, char up_flag)
778     {
779     	int base;
780     
781     	if (up_flag)
782     		return;
783     
784     	if (value < 10)    /* decimal input of code, while Alt depressed */
785     	    base = 10;
786     	else {       /* hexadecimal input of code, while AltGr depressed */
787     	    value -= 10;
788     	    base = 16;
789     	}
790     
791     	if (npadch == -1)
792     	  npadch = value;
793     	else
794     	  npadch = npadch * base + value;
795     }
796     
797     static void do_lock(unsigned char value, char up_flag)
798     {
799     	if (up_flag || rep)
800     		return;
801     	chg_vc_kbd_lock(kbd, value);
802     }
803     
804     static void do_slock(unsigned char value, char up_flag)
805     {
806     	do_shift(value,up_flag);
807     	if (up_flag || rep)
808     		return;
809     	chg_vc_kbd_slock(kbd, value);
810     	/* try to make Alt, oops, AltGr and such work */
811     	if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
812     		kbd->slockstate = 0;
813     		chg_vc_kbd_slock(kbd, value);
814     	}
815     }
816     
817     /*
818      * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
819      * or (ii) whatever pattern of lights people want to show using KDSETLED,
820      * or (iii) specified bits of specified words in kernel memory.
821      */
822     
823     static unsigned char ledstate = 0xff; /* undefined */
824     static unsigned char ledioctl;
825     
826     unsigned char getledstate(void) {
827         return ledstate;
828     }
829     
830     void setledstate(struct kbd_struct *kbd, unsigned int led) {
831         if (!(led & ~7)) {
832     	ledioctl = led;
833     	kbd->ledmode = LED_SHOW_IOCTL;
834         } else
835     	kbd->ledmode = LED_SHOW_FLAGS;
836         set_leds();
837     }
838     
839     static struct ledptr {
840         unsigned int *addr;
841         unsigned int mask;
842         unsigned char valid:1;
843     } ledptrs[3];
844     
845     void register_leds(int console, unsigned int led,
846     		   unsigned int *addr, unsigned int mask) {
847         struct kbd_struct *kbd = kbd_table + console;
848         if (led < 3) {
849     	ledptrs[led].addr = addr;
850     	ledptrs[led].mask = mask;
851     	ledptrs[led].valid = 1;
852     	kbd->ledmode = LED_SHOW_MEM;
853         } else
854     	kbd->ledmode = LED_SHOW_FLAGS;
855     }
856     
857     static inline unsigned char getleds(void){
858         struct kbd_struct *kbd = kbd_table + fg_console;
859         unsigned char leds;
860     
861         if (kbd->ledmode == LED_SHOW_IOCTL)
862           return ledioctl;
863         leds = kbd->ledflagstate;
864         if (kbd->ledmode == LED_SHOW_MEM) {
865     	if (ledptrs[0].valid) {
866     	    if (*ledptrs[0].addr & ledptrs[0].mask)
867     	      leds |= 1;
868     	    else
869     	      leds &= ~1;
870     	}
871     	if (ledptrs[1].valid) {
872     	    if (*ledptrs[1].addr & ledptrs[1].mask)
873     	      leds |= 2;
874     	    else
875     	      leds &= ~2;
876     	}
877     	if (ledptrs[2].valid) {
878     	    if (*ledptrs[2].addr & ledptrs[2].mask)
879     	      leds |= 4;
880     	    else
881     	      leds &= ~4;
882     	}
883         }
884         return leds;
885     }
886     
887     /*
888      * This routine is the bottom half of the keyboard interrupt
889      * routine, and runs with all interrupts enabled. It does
890      * console changing, led setting and copy_to_cooked, which can
891      * take a reasonably long time.
892      *
893      * Aside from timing (which isn't really that important for
894      * keyboard interrupts as they happen often), using the software
895      * interrupt routines for this thing allows us to easily mask
896      * this when we don't want any of the above to happen. Not yet
897      * used, but this allows for easy and efficient race-condition
898      * prevention later on.
899      */
900     static void kbd_bh(unsigned long dummy)
901     {
902     	unsigned char leds = getleds();
903     
904     	if (leds != ledstate) {
905     		ledstate = leds;
906     		kbd_leds(leds);
907     		if (kbd_ledfunc) kbd_ledfunc(leds);
908     	}
909     }
910     
911     EXPORT_SYMBOL(keyboard_tasklet);
912     DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
913     
914     typedef void (pm_kbd_func) (void);
915     
916     pm_callback pm_kbd_request_override = NULL;
917     
918     int __init kbd_init(void)
919     {
920     	int i;
921     	struct kbd_struct kbd0;
922     	extern struct tty_driver console_driver;
923     
924     	kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
925     	kbd0.ledmode = LED_SHOW_FLAGS;
926     	kbd0.lockstate = KBD_DEFLOCK;
927     	kbd0.slockstate = 0;
928     	kbd0.modeflags = KBD_DEFMODE;
929     	kbd0.kbdmode = VC_XLATE;
930      
931     	for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
932     		kbd_table[i] = kbd0;
933     
934     	ttytab = console_driver.table;
935     
936     	kbd_init_hw();
937     
938     	tasklet_enable(&keyboard_tasklet);
939     	tasklet_schedule(&keyboard_tasklet);
940     	
941     	pm_kbd = pm_register(PM_SYS_DEV, PM_SYS_KBC, pm_kbd_request_override);
942     
943     	return 0;
944     }
945