source: rtems/c/src/lib/libbsp/i386/pc386/console/keyboard.c @ f05b2ac

4.104.114.84.95
Last change on this file since f05b2ac was f05b2ac, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/21/04 at 16:01:48

Remove duplicate white lines.

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