Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:41

0001 /*
0002  *   Rosimildo da Silva:  rdasilva@connecttel.com
0003  */
0004 
0005 #include <limits.h>
0006 #include <sys/types.h>
0007 #include <rtems/keyboard.h>
0008 #include "i386kbd.h"
0009 #include <rtems/kd.h>
0010 #include <bsp.h>
0011 #include <bsp/bootcard.h>
0012 #include <stdatomic.h>
0013 
0014 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
0015 
0016 #ifndef KBD_DEFMODE
0017 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
0018 #endif
0019 
0020 #ifndef KBD_DEFLEDS
0021 /*
0022  * Some laptops take the 789uiojklm,. keys as number pad when NumLock
0023  * is on. This seems a good reason to start with NumLock off.
0024  */
0025 #define KBD_DEFLEDS 0
0026 #endif
0027 
0028 #ifndef KBD_DEFLOCK
0029 #define KBD_DEFLOCK 0
0030 #endif
0031 
0032 static int kbd_test_and_set_bit(int nr, atomic_uint_least32_t * addr)
0033 {
0034   uint_least32_t        mask;
0035   int                   retval;
0036 
0037   addr += nr >> 5;
0038   mask = 1UL << (nr & 0x1f);
0039 
0040   retval = (atomic_fetch_or(addr, mask) & mask) != 0;
0041 
0042   return retval;
0043 }
0044 
0045 static int kbd_test_and_clear_bit(int nr, atomic_uint_least32_t * addr)
0046 {
0047   uint_least32_t        mask;
0048   int                   retval;
0049 
0050   addr += nr >> 5;
0051   mask = 1UL << (nr & 0x1f);
0052 
0053   retval = (atomic_fetch_and(addr, ~mask) & mask) != 0;
0054 
0055   return retval;
0056 }
0057 
0058 static int kbd_test_bit(int nr, atomic_uint_least32_t * addr)
0059 {
0060   unsigned long  mask;
0061 
0062   addr += nr >> 5;
0063   mask = 1 << (nr & 0x1f);
0064   return ((mask & atomic_load(addr)) != 0);
0065 }
0066 
0067 /*
0068  * global state includes the following, and various static variables
0069  * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
0070  * (last_console is now a global variable)
0071  */
0072 #define  KBD_BITS_PER_ELEMENT (sizeof(atomic_uint_least32_t)*CHAR_BIT)
0073 
0074 /* shift state counters.. */
0075 static unsigned char k_down[NR_SHIFT] = {0, };
0076 /* keyboard key bitmap */
0077 static atomic_uint_least32_t
0078   key_down[(256 + KBD_BITS_PER_ELEMENT - 1) / KBD_BITS_PER_ELEMENT] = { 0, };
0079 
0080 static int dead_key_next = 0;
0081 /*
0082  * In order to retrieve the shift_state (for the mouse server), either
0083  * the variable must be global, or a new procedure must be created to
0084  * return the value. I chose the former way.
0085  */
0086 int shift_state = 0;
0087 static int npadch = -1;      /* -1 or number assembled on pad */
0088 static unsigned char diacr = 0;
0089 static char rep = 0;      /* flag telling character repeat */
0090 
0091 /* default console for RTEMS */
0092 static int  fg_console = 0;
0093 
0094 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
0095 static struct kbd_struct * kbd = kbd_table;
0096 
0097 void compute_shiftstate(void);
0098 
0099 typedef void (*k_hand)(unsigned char value, char up_flag);
0100 typedef void (k_handfn)(unsigned char value, char up_flag);
0101 
0102 static k_handfn
0103   do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
0104   do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
0105   do_ignore;
0106 
0107 static k_hand key_handler[16] = {
0108   do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
0109   do_meta, do_ascii, do_lock, do_lowercase, do_slock, do_dead2,
0110   do_ignore, do_ignore
0111 };
0112 
0113 /* Key types processed even in raw modes */
0114 
0115 #define TYPES_ALLOWED_IN_RAW_MODE ((1 << KT_SPEC) | (1 << KT_SHIFT))
0116 
0117 typedef void (*void_fnp)(void);
0118 typedef void (void_fn)(void);
0119 
0120 static void show_mem(void)
0121 {
0122 }
0123 static void show_state(void)
0124 {
0125 }
0126 
0127 static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle,
0128   num, hold, scroll_forw, scroll_back, caps_on, compose,
0129   SAK, decr_console, incr_console, spawn_console, bare_num;
0130 
0131 static void bsp_reset_wrapper(void)
0132 {
0133   bsp_reset(RTEMS_FATAL_SOURCE_BSP, 0);
0134 }
0135 
0136 static void_fnp spec_fn_table[] = {
0137   do_null,  enter,    show_ptregs,  show_mem,
0138   show_state,  send_intr,  lastcons,  caps_toggle,
0139   num,    hold,    scroll_forw,  scroll_back,
0140   bsp_reset_wrapper,  caps_on,  compose,  SAK,
0141   decr_console,  incr_console,  spawn_console,  bare_num
0142 };
0143 
0144 #define SPECIALS_ALLOWED_IN_RAW_MODE (1 << KVAL(K_SAK))
0145 
0146 /* maximum values each key_handler can handle */
0147 const int max_vals[] = {
0148   255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1,
0149   NR_DEAD - 1, 255, 3, NR_SHIFT - 1,
0150   255, NR_ASCII - 1, NR_LOCK - 1, 255,
0151   NR_LOCK - 1, 255
0152 };
0153 
0154 const int NR_TYPES = SIZE(max_vals);
0155 
0156 /* N.B. drivers/macintosh/mac_keyb.c needs to call put_queue */
0157 static void put_queue(int);
0158 static unsigned char handle_diacr(unsigned char);
0159 
0160 #ifdef CONFIG_MAGIC_SYSRQ
0161 static int sysrq_pressed;
0162 #endif
0163 
0164 /*
0165  * Many other routines do put_queue, but I think either
0166  * they produce ASCII, or they produce some user-assigned
0167  * string, and in both cases we might assume that it is
0168  * in utf-8 already.
0169  */
0170 static void to_utf8(ushort c)
0171 {
0172   if (c < 0x80)
0173     put_queue(c);                  /*  0*******  */
0174   else if (c < 0x800) {
0175     put_queue(0xc0 | (c >> 6));    /*  110***** 10******  */
0176     put_queue(0x80 | (c & 0x3f));
0177   } else {
0178     put_queue(0xe0 | (c >> 12));   /*  1110**** 10****** 10******  */
0179     put_queue(0x80 | ((c >> 6) & 0x3f));
0180     put_queue(0x80 | (c & 0x3f));
0181   }
0182   /* UTF-8 is defined for words of up to 31 bits,
0183      but we need only 16 bits here */
0184 }
0185 
0186 /*
0187  * Translation of escaped scancodes to keycodes.
0188  * This is now user-settable (for machines were it makes sense).
0189  */
0190 
0191 int setkeycode(unsigned int scancode, unsigned int keycode)
0192 {
0193     return kbd_setkeycode(scancode, keycode);
0194 }
0195 
0196 int getkeycode(unsigned int scancode)
0197 {
0198     return kbd_getkeycode(scancode);
0199 }
0200 
0201 void handle_scancode(unsigned char scancode, int down)
0202 {
0203   unsigned char keycode;
0204   char up_flag = down ? 0 : 0200;
0205   char raw_mode;
0206 
0207   mark_bh(CONSOLE_BH);
0208 
0209 #if 0
0210   tty = ttytab? ttytab[fg_console]: NULL;
0211   if (tty && (!tty->driver_data)) {
0212     /*
0213      * We touch the tty structure via the the ttytab array
0214      * without knowing whether or not tty is open, which
0215      * is inherently dangerous.  We currently rely on that
0216      * fact that console_open sets tty->driver_data when
0217      * it opens it, and clears it when it closes it.
0218      */
0219     tty = NULL;
0220   }
0221 #endif
0222 
0223   kbd = kbd_table + fg_console;
0224   if ((raw_mode = (kbd->kbdmode == VC_RAW))) {
0225     put_queue(scancode | up_flag);
0226     /* we do not return yet, because we want to maintain
0227        the key_down array, so that we have the correct
0228        values when finishing RAW mode or when changing VT's */
0229   }
0230 
0231   /*
0232    *  Convert scancode to keycode
0233    */
0234   if (!kbd_translate(scancode, &keycode, raw_mode))
0235       return;
0236 
0237   /*
0238    * At this point the variable `keycode' contains the keycode.
0239    * Note: the keycode must not be 0 (++Geert: on m68k 0 is valid).
0240    * We keep track of the up/down status of the key, and
0241    * return the keycode if in MEDIUMRAW mode.
0242    */
0243 
0244   if (up_flag) {
0245     rep = 0;
0246     if(!kbd_test_and_clear_bit(keycode, key_down))
0247         up_flag = kbd_unexpected_up(keycode);
0248   } else
0249     rep = kbd_test_and_set_bit(keycode, key_down);
0250 
0251 #ifdef CONFIG_MAGIC_SYSRQ    /* Handle the SysRq Hack */
0252   if (keycode == SYSRQ_KEY) {
0253     sysrq_pressed = !up_flag;
0254     return;
0255   } else if (sysrq_pressed) {
0256     if (!up_flag && sysrq_enabled)
0257       handle_sysrq(kbd_sysrq_xlate[keycode], kbd_pt_regs, kbd, tty);
0258     return;
0259   }
0260 #endif
0261 
0262   if (kbd->kbdmode == VC_MEDIUMRAW) {
0263     /* soon keycodes will require more than one byte */
0264     put_queue(keycode + up_flag);
0265     raw_mode = 1;  /* Most key classes will be ignored */
0266   }
0267   /*
0268    * Small change in philosophy: earlier we defined repetition by
0269    *   rep = keycode == prev_keycode;
0270    *   prev_keycode = keycode;
0271    * but now by the fact that the depressed key was down already.
0272    * Does this ever make a difference? Yes.
0273    */
0274 
0275   /*
0276    *  Repeat a key only if the input buffers are empty or the
0277    *  characters get echoed locally. This makes key repeat usable
0278    *  with slow applications and under heavy loads.
0279    */
0280   if (!rep || vc_kbd_mode(kbd,VC_REPEAT) ) {
0281 /*
0282   ||  (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
0283        (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
0284 */
0285     u_short keysym;
0286     u_char type;
0287 
0288     /* the XOR below used to be an OR */
0289     int shift_final = shift_state ^ kbd->lockstate ^ kbd->slockstate;
0290     ushort *key_map = key_maps[shift_final];
0291 
0292     if (key_map != NULL) {
0293       keysym = key_map[keycode];
0294       type = KTYP(keysym);
0295 
0296       if (type >= 0xf0) {
0297           type -= 0xf0;
0298           if (raw_mode && ! (TYPES_ALLOWED_IN_RAW_MODE & (1 << type)))
0299         return;
0300          if (type == KT_LETTER) {
0301         type = KT_LATIN;
0302         if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
0303             key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
0304             if (key_map)
0305               keysym = key_map[keycode];
0306         }
0307           }
0308 
0309           (*key_handler[type])(keysym & 0xff, up_flag);
0310 
0311           if (type != KT_SLOCK)
0312             kbd->slockstate = 0;
0313 
0314       } else {
0315           /* maybe only if (kbd->kbdmode == VC_UNICODE) ? */
0316           if (!up_flag && !raw_mode)
0317             to_utf8(keysym);
0318       }
0319     } else {
0320       /* maybe beep? */
0321       /* we have at least to update shift_state */
0322 #if 1      /* how? two almost equivalent choices follow */
0323       compute_shiftstate();
0324 #else
0325       keysym = U(plain_map[keycode]);
0326       type = KTYP(keysym);
0327       if (type == KT_SHIFT)
0328         (*key_handler[type])(keysym & 0xff, up_flag);
0329 #endif
0330     }
0331   }
0332 }
0333 
0334 static void ( *driver_input_handler_kbd )( void *, unsigned short, unsigned long ) = 0;
0335 /*
0336  */
0337 void kbd_set_driver_handler(
0338   void ( *handler )( void *, unsigned short, unsigned long )
0339 )
0340 {
0341   driver_input_handler_kbd = handler;
0342 }
0343 
0344 static void put_queue(int ch)
0345 {
0346   if ( driver_input_handler_kbd ) {
0347     driver_input_handler_kbd(  ( void *)kbd, (unsigned short)ch,  0 );
0348   } else {
0349     add_to_queue( ch );
0350   }
0351 }
0352 
0353 static void puts_queue(char *cp)
0354 {
0355   while (*cp) {
0356      put_queue( *cp );
0357     cp++;
0358   }
0359 }
0360 
0361 static void applkey(int key, char mode)
0362 {
0363   static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
0364 
0365   buf[1] = (mode ? 'O' : '[');
0366   buf[2] = key;
0367   puts_queue(buf);
0368 }
0369 
0370 static void enter(void)
0371 {
0372   if (diacr) {
0373     put_queue(diacr);
0374     diacr = 0;
0375   }
0376   put_queue(13);
0377 
0378   if (vc_kbd_mode(kbd,VC_CRLF))
0379     put_queue(10);
0380 }
0381 
0382 static void caps_toggle(void)
0383 {
0384   if (rep)
0385     return;
0386   chg_vc_kbd_led(kbd, VC_CAPSLOCK);
0387 }
0388 
0389 static void caps_on(void)
0390 {
0391   if (rep)
0392     return;
0393   set_vc_kbd_led(kbd, VC_CAPSLOCK);
0394 }
0395 
0396 static void show_ptregs(void)
0397 {
0398 }
0399 
0400 static void hold(void)
0401 {
0402   if (rep )
0403     return;
0404    chg_vc_kbd_led(kbd, VC_SCROLLOCK );
0405 }
0406 
0407 static void num(void)
0408 {
0409   if (vc_kbd_mode(kbd,VC_APPLIC))
0410     applkey('P', 1);
0411   else
0412     bare_num();
0413 }
0414 
0415 /*
0416  * Bind this to Shift-NumLock if you work in application keypad mode
0417  * but want to be able to change the NumLock flag.
0418  * Bind this to NumLock if you prefer that the NumLock key always
0419  * changes the NumLock flag.
0420  */
0421 static void bare_num(void)
0422 {
0423   if (!rep)
0424     chg_vc_kbd_led(kbd,VC_NUMLOCK);
0425 }
0426 
0427 static void lastcons(void)
0428 {
0429 }
0430 
0431 static void decr_console(void)
0432 {
0433 }
0434 
0435 static void incr_console(void)
0436 {
0437 }
0438 
0439 static void send_intr(void)
0440 {
0441 }
0442 
0443 static void scroll_forw(void)
0444 {
0445 }
0446 
0447 static void scroll_back(void)
0448 {
0449 }
0450 
0451 static void compose(void)
0452 {
0453   dead_key_next = 1;
0454 }
0455 
0456 int spawnpid, spawnsig;
0457 
0458 static void spawn_console(void)
0459 {
0460 }
0461 
0462 static void SAK(void)
0463 {
0464 }
0465 
0466 static void do_ignore(unsigned char value, char up_flag)
0467 {
0468 }
0469 
0470 static void do_null()
0471 {
0472   compute_shiftstate();
0473 }
0474 
0475 static void do_spec(unsigned char value, char up_flag)
0476 {
0477   if (up_flag)
0478     return;
0479   if (value >= SIZE(spec_fn_table))
0480     return;
0481 
0482   if ((kbd->kbdmode == VC_RAW || kbd->kbdmode == VC_MEDIUMRAW) &&
0483       !(SPECIALS_ALLOWED_IN_RAW_MODE & (1 << value)))
0484     return;
0485 
0486   spec_fn_table[value]();
0487 }
0488 
0489 static void do_lowercase(unsigned char value, char up_flag)
0490 {
0491 }
0492 
0493 static void do_self(unsigned char value, char up_flag)
0494 {
0495   if (up_flag)
0496     return;    /* no action, if this is a key release */
0497 
0498   if (diacr)
0499     value = handle_diacr(value);
0500 
0501   if (dead_key_next) {
0502     dead_key_next = 0;
0503     diacr = value;
0504     return;
0505   }
0506   put_queue(value);
0507 }
0508 
0509 #define A_GRAVE  '`'
0510 #define A_ACUTE  '\''
0511 #define A_CFLEX  '^'
0512 #define A_TILDE  '~'
0513 #define A_DIAER  '"'
0514 #define A_CEDIL  ','
0515 static unsigned char ret_diacr[NR_DEAD] =
0516   {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
0517 
0518 /* Obsolete - for backwards compatibility only */
0519 static void do_dead(unsigned char value, char up_flag)
0520 {
0521   value = ret_diacr[value];
0522    printk( " do_dead( %X ) ", value );
0523   do_dead2(value,up_flag);
0524 }
0525 
0526 /*
0527  * Handle dead key. Note that we now may have several
0528  * dead keys modifying the same character. Very useful
0529  * for Vietnamese.
0530  */
0531 static void do_dead2(unsigned char value, char up_flag)
0532 {
0533   if (up_flag)
0534     return;
0535   diacr = (diacr ? handle_diacr(value) : value);
0536 }
0537 
0538 /*
0539  * We have a combining character DIACR here, followed by the character CH.
0540  * If the combination occurs in the table, return the corresponding value.
0541  * Otherwise, if CH is a space or equals DIACR, return DIACR.
0542  * Otherwise, conclude that DIACR was not combining after all,
0543  * queue it and return CH.
0544  */
0545 unsigned char handle_diacr(unsigned char ch)
0546 {
0547   int d = diacr;
0548   int i;
0549 
0550   diacr = 0;
0551 
0552   for (i = 0; i < accent_table_size; i++) {
0553     if (accent_table[i].diacr == d && accent_table[i].base == ch)
0554       return accent_table[i].result;
0555   }
0556   if (ch == ' ' || ch == d)
0557     return d;
0558 
0559   put_queue(d);
0560   return ch;
0561 }
0562 
0563 static void do_cons(unsigned char value, char up_flag)
0564 {
0565   if (up_flag)
0566     return;
0567 }
0568 
0569 static void do_fn(unsigned char value, char up_flag)
0570 {
0571   if (up_flag)
0572     return;
0573 
0574   if (value < SIZE(func_table)) {
0575     if (func_table[value])
0576       puts_queue(func_table[value]);
0577   } else
0578     printk( "do_fn called with value=%d\n", value);
0579 }
0580 
0581 static void do_pad(unsigned char value, char up_flag)
0582 {
0583   static const char *pad_chars = "0123456789+-*/\015,.?()";
0584   static const char *app_map = "pqrstuvwxylSRQMnnmPQ";
0585 
0586   if (up_flag)
0587     return;    /* no action, if this is a key release */
0588 
0589   /* kludge... shift forces cursor/number keys */
0590   if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
0591     applkey(app_map[value], 1);
0592     return;
0593   }
0594   if (!vc_kbd_led(kbd,VC_NUMLOCK))
0595     switch (value) {
0596       case KVAL(K_PCOMMA):
0597       case KVAL(K_PDOT):
0598         do_fn(KVAL(K_REMOVE), 0);
0599         return;
0600       case KVAL(K_P0):
0601         do_fn(KVAL(K_INSERT), 0);
0602         return;
0603       case KVAL(K_P1):
0604         do_fn(KVAL(K_SELECT), 0);
0605         return;
0606       case KVAL(K_P2):
0607         do_cur(KVAL(K_DOWN), 0);
0608         return;
0609       case KVAL(K_P3):
0610         do_fn(KVAL(K_PGDN), 0);
0611         return;
0612       case KVAL(K_P4):
0613         do_cur(KVAL(K_LEFT), 0);
0614         return;
0615       case KVAL(K_P6):
0616         do_cur(KVAL(K_RIGHT), 0);
0617         return;
0618       case KVAL(K_P7):
0619         do_fn(KVAL(K_FIND), 0);
0620         return;
0621       case KVAL(K_P8):
0622         do_cur(KVAL(K_UP), 0);
0623         return;
0624       case KVAL(K_P9):
0625         do_fn(KVAL(K_PGUP), 0);
0626         return;
0627       case KVAL(K_P5):
0628         applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
0629         return;
0630     }
0631 
0632   put_queue(pad_chars[value]);
0633 
0634   if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
0635     put_queue(10);
0636 
0637 }
0638 
0639 static void do_cur(unsigned char value, char up_flag)
0640 {
0641   static const char *cur_chars = "BDCA";
0642   if (up_flag)
0643     return;
0644 
0645   applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
0646 }
0647 
0648 static void do_shift(unsigned char value, char up_flag)
0649 {
0650   int old_state = shift_state;
0651 
0652   if (rep)
0653     return;
0654 
0655   /* Mimic typewriter:
0656      a CapsShift key acts like Shift but undoes CapsLock */
0657   if (value == KVAL(K_CAPSSHIFT)) {
0658     value = KVAL(K_SHIFT);
0659     if (!up_flag)
0660       clr_vc_kbd_led(kbd, VC_CAPSLOCK);
0661   }
0662 
0663   if (up_flag) {
0664     /* handle the case that two shift or control
0665        keys are depressed simultaneously */
0666     if (k_down[value])
0667       k_down[value]--;
0668   } else
0669     k_down[value]++;
0670 
0671   if (k_down[value])
0672     shift_state |= (1 << value);
0673   else
0674     shift_state &= ~ (1 << value);
0675 
0676   /* kludge */
0677   if (up_flag && shift_state != old_state && npadch != -1) {
0678     if (kbd->kbdmode == VC_UNICODE)
0679       to_utf8(npadch & 0xffff);
0680     else
0681      put_queue(npadch & 0xff);
0682     npadch = -1;
0683   }
0684 }
0685 
0686 /* called after returning from RAW mode or when changing consoles -
0687    recompute k_down[] and shift_state from key_down[] */
0688 /* maybe called when keymap is undefined, so that shiftkey release is seen */
0689 void compute_shiftstate(void)
0690 {
0691   int i, j, k, sym, val;
0692 
0693   shift_state = 0;
0694   for(i=0; i < SIZE(k_down); i++)
0695     k_down[i] = 0;
0696 
0697   for(i=0; i < SIZE(key_down); i++)
0698     if(atomic_load(key_down + i)) {  /* skip this word if not a single bit on */
0699       k = i*KBD_BITS_PER_ELEMENT;
0700       for(j=0; j<KBD_BITS_PER_ELEMENT; j++,k++)
0701         if(kbd_test_bit(k, key_down)) {
0702     sym = U(plain_map[k]);
0703     if(KTYP(sym) == KT_SHIFT) {
0704       val = KVAL(sym);
0705       if (val == KVAL(K_CAPSSHIFT))
0706         val = KVAL(K_SHIFT);
0707       k_down[val]++;
0708       shift_state |= (1<<val);
0709     }
0710         }
0711     }
0712 }
0713 
0714 static void do_meta(unsigned char value, char up_flag)
0715 {
0716   if (up_flag)
0717     return;
0718 
0719   if (vc_kbd_mode(kbd, VC_META)) {
0720     put_queue('\033');
0721     put_queue(value);
0722   } else
0723     put_queue(value | 0x80);
0724 }
0725 
0726 static void do_ascii(unsigned char value, char up_flag)
0727 {
0728   int base;
0729 
0730   if (up_flag)
0731     return;
0732 
0733   if (value < 10)    /* decimal input of code, while Alt depressed */
0734       base = 10;
0735   else {       /* hexadecimal input of code, while AltGr depressed */
0736       value -= 10;
0737       base = 16;
0738   }
0739 
0740   if (npadch == -1)
0741     npadch = value;
0742   else
0743     npadch = npadch * base + value;
0744 }
0745 
0746 static void do_lock(unsigned char value, char up_flag)
0747 {
0748   if (up_flag || rep)
0749     return;
0750   chg_vc_kbd_lock(kbd, value);
0751 }
0752 
0753 static void do_slock(unsigned char value, char up_flag)
0754 {
0755   if (up_flag || rep)
0756     return;
0757 
0758   chg_vc_kbd_slock(kbd, value);
0759 }
0760 
0761 /*
0762  * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
0763  * or (ii) whatever pattern of lights people want to show using KDSETLED,
0764  * or (iii) specified bits of specified words in kernel memory.
0765  */
0766 
0767 static unsigned char ledstate = 0xff; /* undefined */
0768 static unsigned char ledioctl;
0769 
0770 unsigned char getledstate(void) {
0771   return ledstate;
0772 }
0773 
0774 void setledstate(struct kbd_struct *kbd, unsigned int led) {
0775   if (!(led & ~7)) {
0776     ledioctl = led;
0777      kbd->ledmode = LED_SHOW_IOCTL;
0778   } else
0779     ;
0780   kbd->ledmode = LED_SHOW_FLAGS;
0781   set_leds();
0782 }
0783 
0784 static struct ledptr {
0785   unsigned int *addr;
0786   unsigned int mask;
0787   unsigned char valid:1;
0788 } ledptrs[3];
0789 
0790 void register_leds(
0791   int console,
0792   unsigned int led,
0793   unsigned int *addr,
0794   unsigned int mask
0795 )
0796 {
0797   struct kbd_struct *kbd = kbd_table + console;
0798 
0799   if (led < 3) {
0800     ledptrs[led].addr = addr;
0801     ledptrs[led].mask = mask;
0802     ledptrs[led].valid = 1;
0803     kbd->ledmode = LED_SHOW_MEM;
0804   } else
0805     kbd->ledmode = LED_SHOW_FLAGS;
0806 }
0807 
0808 static inline unsigned char getleds(void)
0809 {
0810 
0811     struct kbd_struct *kbd = kbd_table + fg_console;
0812 
0813     unsigned char leds;
0814 
0815     if (kbd->ledmode == LED_SHOW_IOCTL)
0816       return ledioctl;
0817     leds = kbd->ledflagstate;
0818     if (kbd->ledmode == LED_SHOW_MEM) {
0819   if (ledptrs[0].valid) {
0820       if (*ledptrs[0].addr & ledptrs[0].mask)
0821         leds |= 1;
0822       else
0823         leds &= ~1;
0824   }
0825   if (ledptrs[1].valid) {
0826       if (*ledptrs[1].addr & ledptrs[1].mask)
0827         leds |= 2;
0828       else
0829         leds &= ~2;
0830   }
0831   if (ledptrs[2].valid) {
0832       if (*ledptrs[2].addr & ledptrs[2].mask)
0833         leds |= 4;
0834       else
0835         leds &= ~4;
0836   }
0837     }
0838    return leds;
0839 }
0840 
0841 /*
0842  * This routine is the bottom half of the keyboard interrupt
0843  * routine, and runs with all interrupts enabled. It does
0844  * console changing, led setting and copy_to_cooked, which can
0845  * take a reasonably long time.
0846  *
0847  * Aside from timing (which isn't really that important for
0848  * keyboard interrupts as they happen often), using the software
0849  * interrupt routines for this thing allows us to easily mask
0850  * this when we don't want any of the above to happen. Not yet
0851  * used, but this allows for easy and efficient race-condition
0852  * prevention later on.
0853  */
0854 static void kbd_bh(void)
0855 {
0856   unsigned char leds = getleds();
0857   if (leds != ledstate) {
0858     ledstate = leds;
0859     kbd_leds(leds);
0860   }
0861 }
0862 
0863 void set_leds(void)
0864 {
0865   kbd_bh();
0866 }
0867 
0868 int kbd_init(void)
0869 {
0870 
0871   int i;
0872   struct kbd_struct kbd0;
0873   kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
0874    kbd0.ledmode = LED_SHOW_MEM;
0875   kbd0.lockstate = KBD_DEFLOCK;
0876   kbd0.slockstate = 0;
0877   kbd0.modeflags = KBD_DEFMODE;
0878   kbd0.kbdmode = VC_XLATE;
0879 
0880   for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
0881     kbd_table[i] = kbd0;
0882 
0883   kbd_init_hw();
0884   mark_bh(KEYBOARD_BH);
0885   return 0;
0886 }