1 /* keyboard.c: Sun keyboard driver.
3 * Copyright (C) 1995, 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Added vuid event generation and /dev/kbd device for SunOS
6 * compatibility - Miguel (miguel@nuclecu.unam.mx)
8 * Added PCI 8042 controller support -DaveM
9 * Added Magic SysRq support -MJ
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
18 #include <linux/ptrace.h>
19 #include <linux/signal.h>
20 #include <linux/string.h>
21 #include <linux/fcntl.h>
22 #include <linux/poll.h>
23 #include <linux/random.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/sysrq.h>
27 #include <linux/spinlock.h>
28 #include <linux/smp_lock.h>
29 #include <linux/devfs_fs_kernel.h>
32 #include <asm/vuid_event.h>
33 #include <asm/bitops.h>
34 #include <asm/oplib.h>
35 #include <asm/uaccess.h>
37 #include <linux/kbd_kern.h>
38 #include <linux/kbd_diacr.h>
39 #include <linux/vt_kern.h>
42 #include <linux/pci.h>
49 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
51 /* Define this one if you are making a new frame buffer driver */
52 /* it will not block the keyboard */
53 /* #define CODING_NEW_DRIVER */
55 /* KBD device number, temporal */
58 #define KBD_REPORT_ERR
59 #define KBD_REPORT_UNKN
62 #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
67 * Some laptops take the 789uiojklm,. keys as number pad when NumLock
68 * is on. This seems a good reason to start with NumLock off.
77 extern void poke_blanked_console(void);
78 extern void ctrl_alt_del(void);
79 extern void reset_vc(unsigned int new_console);
80 extern void scrollback(int);
81 extern void scrollfront(int);
83 struct l1a_kbd_state l1a_state;
85 static spinlock_t sunkbd_lock = SPIN_LOCK_UNLOCKED;
88 * global state includes the following, and various static variables
89 * in this module: prev_scancode, shift_state, diacr, npadch, dead_key_next.
90 * (last_console is now a global variable)
93 /* shift state counters.. */
94 static unsigned char k_down[NR_SHIFT];
95 /* keyboard key bitmap */
96 static unsigned long key_down[256/BITS_PER_LONG];
98 void push_kbd (int scan);
101 static int dead_key_next;
103 * In order to retrieve the shift_state (for the mouse server), either
104 * the variable must be global, or a new procedure must be created to
105 * return the value. I chose the former way.
109 struct kbd_struct kbd_table[MAX_NR_CONSOLES];
111 static int npadch = -1; /* -1 or number assembled on pad */
112 static unsigned char diacr;
113 static char rep; /* flag telling character repeat */
114 static struct tty_struct **ttytab;
115 static struct kbd_struct * kbd = kbd_table;
116 static struct tty_struct * tty;
117 static int compose_led_on;
118 static int kbd_delay_ticks = HZ / 5;
119 static int kbd_rate_ticks = HZ / 20;
121 void sun_compute_shiftstate(void);
123 typedef void (*k_hand)(unsigned char value, char up_flag);
124 typedef void (k_handfn)(unsigned char value, char up_flag);
127 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
128 do_meta, do_ascii, do_lock, do_lowercase, do_ignore;
130 static k_hand key_handler[16] = {
131 do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
132 do_meta, do_ascii, do_lock, do_lowercase,
133 do_ignore, do_ignore, do_ignore, do_ignore
136 typedef void (*void_fnp)(void);
137 typedef void (void_fn)(void);
139 static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle,
140 num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose,
141 SAK, decr_console, incr_console, spawn_console, bare_num;
143 static void_fnp spec_fn_table[] = {
144 do_null, enter, show_ptregs, show_mem,
145 show_state, send_intr, lastcons, caps_toggle,
146 num, hold, scroll_forw, scroll_back,
147 boot_it, caps_on, compose, SAK,
148 decr_console, incr_console, spawn_console, bare_num
151 /* maximum values each key_handler can handle */
153 const int max_vals[] = {
154 255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1,
155 NR_DEAD - 1, 255, 3, NR_SHIFT - 1,
156 255, NR_ASCII - 1, NR_LOCK - 1, 255,
160 const int NR_TYPES = SIZE(max_vals);
163 static void put_queue(int);
164 static unsigned char handle_diacr(unsigned char);
166 /* pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
167 static struct pt_regs * pt_regs;
169 #ifdef CONFIG_MAGIC_SYSRQ
170 unsigned char sun_sysrq_xlate[128] =
171 "\0\0\0\0\0\201\202\212\203\213\204\214\205\0\206\0" /* 0x00 - 0x0f */
172 "\207\210\211\0\0\0\0\0\0\0\0\0\0\03312" /* 0x10 - 0x1f */
173 "34567890-=`\177\0=/*" /* 0x20 - 0x2f */
174 "\0\0.\0\0\011qwertyuiop" /* 0x30 - 0x3f */
175 "[]\177\000789-\0\0\0\0\0asd" /* 0x40 - 0x4f */
176 "fghjkl;'\\\015\0154560\0" /* 0x50 - 0x5f */
177 "\0\0\0\0zxcvbnm,./\0\012" /* 0x60 - 0x6f */
178 "123\0\0\0\0\0\0 \0\0\0\0\0\0"; /* 0x70 - 0x7f */
181 volatile unsigned char sunkbd_layout;
182 volatile unsigned char sunkbd_type;
183 #define SUNKBD_TYPE2 0x02
184 #define SUNKBD_TYPE3 0x03
185 #define SUNKBD_TYPE4 0x04
187 #define SUNKBD_LOUT_TYP4 0x00
188 #define SUNKBD_LOUT_TYP5_MASK 0x20
190 volatile int kbd_reset_pending;
191 volatile int kbd_layout_pending;
194 #define SKBDCMD_RESET 0x1
195 #define SKBDCMD_GLAYOUT 0xf
196 #define SKBDCMD_BELLON 0x2
197 #define SKBDCMD_BELLOFF 0x3
198 #define SKBDCMD_SETLED 0xe
199 #define SKBDCMD_NOCLICK 0xb
200 #define SKBDCMD_CLICK 0xa
202 static unsigned char sunkbd_clickp;
204 /* The led set commands require sending the SETLED byte then
205 * a byte encoding which led's to have set. Here are the bit
206 * values, a bit set = led-on.
208 #define LED_NLOCK 0x1 /* Num-lock */
209 #define LED_CMPOSE 0x2 /* Compose */
210 #define LED_SCRLCK 0x4 /* Scroll-lock */
211 #define LED_CLOCK 0x8 /* Caps-lock */
213 /* Special state characters */
214 #define SKBD_RESET 0xff
215 #define SKBD_ALLUP 0x7f
216 #define SKBD_LYOUT 0xfe
218 /* On the Sparc the keyboard could be one of two things.
219 * It could be a real keyboard speaking over one of the
220 * channels of the second zs8530 chip (other channel is
221 * used by the Sun mouse). Else we have serial console
222 * going, and thus the other zs8530 chip is who we speak
223 * to. Either way, we communicate through the zs8530
224 * driver for all our I/O.
227 #define SUNKBD_UBIT 0x80 /* If set, key went up */
228 #define SUNKBD_KMASK 0x7f /* Other bits are the keycode */
230 #define KEY_LSHIFT 0x81
231 #define KEY_RSHIFT 0x82
232 #define KEY_CONTROL 0x83
233 #define KEY_NILL 0x84
234 #define KEY_CAPSLOCK 0x85
238 /* Due to sun_kbd_init() being called before rs_init(), and sun_kbd_init() doing:
240 * tasklet_enable(&keyboard_tasklet);
241 * tasklet_schedule(&keyboard_tasklet);
243 * this might well be called before some driver has claimed interest in
244 * handling the keyboard input/output. So we need to assign an initial nop.
246 static void nop_kbd_put_char(unsigned char c) { }
247 static void (*kbd_put_char)(unsigned char) = nop_kbd_put_char;
249 /* Must be invoked under sunkbd_lock. */
250 static inline void send_cmd(unsigned char c)
255 /* kbd_bh() calls this to send the SKBDCMD_SETLED to the sun keyboard
256 * with the proper bit pattern for the leds to be set. It basically
257 * converts the kbd->ledflagstate values to corresponding sun kbd led
260 static inline unsigned char vcleds_to_sunkbd(unsigned char vcleds)
262 unsigned char retval = 0;
264 if(vcleds & (1<<VC_SCROLLOCK))
265 retval |= LED_SCRLCK;
266 if(vcleds & (1<<VC_NUMLOCK))
268 if(vcleds & (1<<VC_CAPSLOCK))
271 retval |= LED_CMPOSE;
276 * Translation of escaped scancodes to keycodes.
277 * This is now user-settable.
278 * The keycodes 1-88,96-111,119 are fairly standard, and
279 * should probably not be changed - changing might confuse X.
280 * X also interprets scancode 0x5d (KEY_Begin).
282 * For 1-88 keycode equals scancode.
285 #define E0_KPENTER 96
287 #define E0_KPSLASH 98
290 #define E0_BREAK 101 /* (control-pause) */
305 * The keycodes below are randomly located in 89-95,112-118,120-127.
306 * They could be thrown away (and all occurrences below replaced by 0),
307 * but that would force many users to use the `setkeycodes' utility, where
308 * they needed not before. It does not matter that there are duplicates, as
309 * long as no duplication occurs for any single keyboard.
313 #define FOCUS_PF1 85 /* actual code! */
321 #define FOCUS_PF9 120
322 #define FOCUS_PF10 121
323 #define FOCUS_PF11 122
324 #define FOCUS_PF12 123
327 /* tfj@olivia.ping.dk:
328 * The four keys are located over the numeric keypad, and are
329 * labelled A1-A4. It's an rc930 keyboard, from
330 * Regnecentralen/RC International, Now ICL.
331 * Scancodes: 59, 5a, 5b, 5c.
338 static unsigned char high_keys[128 - SC_LIM] = {
339 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
340 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
341 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
342 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
343 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
344 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
355 #define E0_KPMINPLUS 118
357 * My OmniKey generates e0 4c for the "OMNI" key and the
358 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
362 * New microsoft keyboard is rumoured to have
363 * e0 5b (left window button), e0 5c (right window button),
364 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
365 * [or: Windows_L, Windows_R, TaskMan]
371 static unsigned char e0_keys[128] = {
372 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
373 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
374 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
375 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
376 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
377 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
378 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
379 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
380 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
381 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
382 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
383 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
384 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
385 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
386 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
387 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
390 /* we use this map to determine if a particular key should not be
391 autorepeated. We don't autorepeat CONTROL, LSHIFT, CAPS,
392 ALT, LMETA, RSHIFT, RMETA, ALTG and COMPOSE */
393 static unsigned char norepeat_keys[128] = {
394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 0x00-0x0f */
395 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x1f */
396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x2f */
397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30-0x3f */
398 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, /* 0x40-0x4f */
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50-0x5f */
400 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, /* 0x60-0x6f */
401 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, /* 0x70-0x7f */
405 int sun_setkeycode(unsigned int scancode, unsigned int keycode)
407 if (scancode < SC_LIM || scancode > 255 || keycode > 127)
410 high_keys[scancode - SC_LIM] = keycode;
412 e0_keys[scancode - 128] = keycode;
416 int sun_getkeycode(unsigned int scancode)
419 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
420 (scancode < 128) ? high_keys[scancode - SC_LIM] :
421 e0_keys[scancode - 128];
424 static void __sunkbd_inchar(unsigned char ch, struct pt_regs *regs);
425 void sunkbd_inchar(unsigned char ch, struct pt_regs *regs);
426 static void keyboard_timer (unsigned long ignored);
428 static struct timer_list
429 auto_repeat_timer = { function: keyboard_timer };
431 /* Keeps track of the last pressed key */
432 static unsigned char last_keycode;
435 keyboard_timer (unsigned long ignored)
439 spin_lock_irqsave(&sunkbd_lock, flags);
441 /* Auto repeat: send regs = 0 to indicate autorepeat */
442 __sunkbd_inchar (last_keycode, 0);
443 del_timer (&auto_repeat_timer);
444 if (kbd_rate_ticks) {
445 auto_repeat_timer.expires = jiffies + kbd_rate_ticks;
446 add_timer (&auto_repeat_timer);
449 spin_unlock_irqrestore(&sunkbd_lock, flags);
453 DECLARE_TASKLET_DISABLED(keyboard_tasklet, sun_kbd_bh, 0);
456 /* #define SKBD_DEBUG */
457 /* This is our keyboard 'interrupt' routine.
458 * Must run under sunkbd_lock.
460 static void __sunkbd_inchar(unsigned char ch, struct pt_regs *regs)
462 unsigned char keycode;
463 char up_flag; /* 0 or SUNKBD_UBIT */
466 if(ch == SKBD_RESET) {
467 kbd_reset_pending = 1;
470 if(ch == SKBD_LYOUT) {
471 kbd_layout_pending = 1;
474 if(kbd_reset_pending) {
476 kbd_reset_pending = 0;
477 if(ch == SUNKBD_TYPE4)
478 send_cmd(SKBDCMD_GLAYOUT);
480 } else if(kbd_layout_pending) {
482 kbd_layout_pending = 0;
484 } else if(ch == SKBD_ALLUP) {
485 del_timer (&auto_repeat_timer);
486 memset(key_down, 0, sizeof(key_down));
487 sun_compute_shiftstate();
492 printk("KBD<ALL KEYS UP>");
494 printk("KBD<%x %s>", ch,
495 ((ch&0x80) ? "UP" : "DOWN"));
498 /* Whee, a real character. */
501 last_keycode = keycode = ch;
506 do_poke_blanked_console = 1;
507 schedule_console_callback();
508 add_keyboard_randomness(keycode);
510 tty = ttytab? ttytab[fg_console]: NULL;
511 if (tty && (!tty->driver_data)) {
512 /* This is to workaround ugly bug in tty_io.c, which
513 does not do locking when it should */
516 kbd = kbd_table + fg_console;
517 if((raw_mode = (kbd->kbdmode == VC_RAW))) {
518 if (kbd_redirected == fg_console+1)
522 /* we do not return yet, because we want to maintain
523 * the key_down array, so that we have the correct
524 * values when finishing RAW mode or when changing VT's.
527 up_flag = (keycode & SUNKBD_UBIT); /* The 'up' bit */
528 keycode &= SUNKBD_KMASK; /* all the rest */
529 del_timer (&auto_repeat_timer);
532 clear_bit(keycode, key_down);
534 if (!norepeat_keys[keycode]) {
535 if (kbd_rate_ticks) {
536 auto_repeat_timer.expires =
537 jiffies + kbd_delay_ticks;
538 add_timer (&auto_repeat_timer);
541 rep = test_and_set_bit(keycode, key_down);
544 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq hack */
545 if (l1a_state.l1_down) {
547 handle_sysrq(sun_sysrq_xlate[keycode], pt_regs, kbd, tty);
555 if(kbd->kbdmode == VC_MEDIUMRAW) {
556 put_queue(keycode + up_flag);
561 * Small change in philosophy: earlier we defined repetition by
562 * rep = keycode == prev_keycode;
563 * prev_keycode = keycode;
564 * but now by the fact that the depressed key was down already.
565 * Does this ever make a difference? Yes.
569 * Repeat a key only if the input buffers are empty or the
570 * characters get echoed locally. This makes key repeat usable
571 * with slow applications and under heavy loads.
574 (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
575 (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
579 /* the XOR below used to be an OR */
580 int shift_final = shift_state ^ kbd->lockstate ^ kbd->slockstate;
581 ushort *key_map = key_maps[shift_final];
583 if (key_map != NULL) {
584 keysym = key_map[keycode];
589 if (type == KT_LETTER) {
591 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
592 key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
594 keysym = key_map[keycode];
597 (*key_handler[type])(keysym & 0xff, up_flag);
598 if (type != KT_SLOCK)
603 /* we have at least to update shift_state */
604 sun_compute_shiftstate();
608 tasklet_schedule(&keyboard_tasklet);
611 void sunkbd_inchar(unsigned char ch, struct pt_regs *regs)
615 spin_lock_irqsave(&sunkbd_lock, flags);
616 __sunkbd_inchar(ch, regs);
617 spin_unlock_irqrestore(&sunkbd_lock, flags);
620 static void put_queue(int ch)
623 tty_insert_flip_char(tty, ch, 0);
624 con_schedule_flip(tty);
628 static void puts_queue(char *cp)
634 tty_insert_flip_char(tty, *cp, 0);
637 con_schedule_flip(tty);
640 static void applkey(int key, char mode)
642 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
644 buf[1] = (mode ? 'O' : '[');
649 static void enter(void)
652 if (vc_kbd_mode(kbd,VC_CRLF))
656 static void caps_toggle(void)
660 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
663 static void caps_on(void)
667 set_vc_kbd_led(kbd, VC_CAPSLOCK);
670 static void show_ptregs(void)
676 static void hold(void)
682 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
683 * these routines are also activated by ^S/^Q.
684 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
692 static void num(void)
694 if (vc_kbd_mode(kbd,VC_APPLIC))
701 * Bind this to Shift-NumLock if you work in application keypad mode
702 * but want to be able to change the NumLock flag.
703 * Bind this to NumLock if you prefer that the NumLock key always
704 * changes the NumLock flag.
706 static void bare_num(void)
709 chg_vc_kbd_led(kbd,VC_NUMLOCK);
712 static void lastcons(void)
714 /* switch to the last used console, ChN */
715 set_console(last_console);
718 static void decr_console(void)
722 for (i = fg_console-1; i != fg_console; i--) {
724 i = MAX_NR_CONSOLES-1;
725 if (vc_cons_allocated(i))
731 static void incr_console(void)
735 for (i = fg_console+1; i != fg_console; i++) {
736 if (i == MAX_NR_CONSOLES)
738 if (vc_cons_allocated(i))
744 static void send_intr(void)
748 tty_insert_flip_char(tty, 0, TTY_BREAK);
749 con_schedule_flip(tty);
752 static void scroll_forw(void)
757 static void scroll_back(void)
762 static void boot_it(void)
764 extern int obp_system_intr(void);
766 if (!obp_system_intr())
768 /* sigh.. attempt to prevent multiple entry */
773 static void compose(void)
781 extern int spawnpid, spawnsig;
783 int spawnpid, spawnsig;
787 static void spawn_console(void)
790 if(kill_proc(spawnpid, spawnsig, 1))
794 static void SAK(void)
799 * Need to fix SAK handling to fix up RAW/MEDIUM_RAW and
800 * vt_cons modes before we can enable RAW/MEDIUM_RAW SAK
803 * We should do this some day --- the whole point of a secure
804 * attention key is that it should be guaranteed to always
807 reset_vc(fg_console);
808 do_unblank_screen(); /* not in interrupt routine? */
812 static void do_ignore(unsigned char value, char up_flag)
816 static void do_null()
818 sun_compute_shiftstate();
821 static void do_spec(unsigned char value, char up_flag)
825 if (value >= SIZE(spec_fn_table))
827 spec_fn_table[value]();
830 static void do_lowercase(unsigned char value, char up_flag)
832 printk("keyboard.c: do_lowercase was called - impossible\n");
835 static void do_self(unsigned char value, char up_flag)
838 return; /* no action, if this is a key release */
841 value = handle_diacr(value);
861 static unsigned char ret_diacr[NR_DEAD] =
862 {A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
864 /* If a dead key pressed twice, output a character corresponding to it, */
865 /* otherwise just remember the dead key. */
867 static void do_dead(unsigned char value, char up_flag)
872 value = ret_diacr[value];
873 if (diacr == value) { /* pressed twice */
882 /* If space is pressed, return the character corresponding the pending */
883 /* dead key, otherwise try to combine the two. */
885 unsigned char handle_diacr(unsigned char ch)
894 for (i = 0; i < accent_table_size; i++) {
895 if (accent_table[i].diacr == d && accent_table[i].base == ch)
896 return accent_table[i].result;
903 static void do_cons(unsigned char value, char up_flag)
910 static void do_fn(unsigned char value, char up_flag)
914 if (value < SIZE(func_table)) {
915 if (func_table[value])
916 puts_queue(func_table[value]);
918 printk("do_fn called with value=%d\n", value);
921 static void do_pad(unsigned char value, char up_flag)
923 static const char *pad_chars = "0123456789+-*/\015,.?";
924 static const char *app_map = "pqrstuvwxylSRQMnn?";
927 return; /* no action, if this is a key release */
929 /* kludge... shift forces cursor/number keys */
930 if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
931 applkey(app_map[value], 1);
935 if (!vc_kbd_led(kbd,VC_NUMLOCK))
939 do_fn(KVAL(K_REMOVE), 0);
942 do_fn(KVAL(K_INSERT), 0);
945 do_fn(KVAL(K_SELECT), 0);
948 do_cur(KVAL(K_DOWN), 0);
951 do_fn(KVAL(K_PGDN), 0);
954 do_cur(KVAL(K_LEFT), 0);
957 do_cur(KVAL(K_RIGHT), 0);
960 do_fn(KVAL(K_FIND), 0);
963 do_cur(KVAL(K_UP), 0);
966 do_fn(KVAL(K_PGUP), 0);
969 applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
973 put_queue(pad_chars[value]);
974 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
978 static void do_cur(unsigned char value, char up_flag)
980 static const char *cur_chars = "BDCA";
984 applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
987 static void do_shift(unsigned char value, char up_flag)
989 int old_state = shift_state;
995 a CapsShift key acts like Shift but undoes CapsLock */
996 if (value == KVAL(K_CAPSSHIFT)) {
997 value = KVAL(K_SHIFT);
999 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
1003 /* handle the case that two shift or control
1004 keys are depressed simultaneously */
1011 shift_state |= (1 << value);
1013 shift_state &= ~ (1 << value);
1015 /* kludge, no joke... */
1016 if (up_flag && shift_state != old_state && npadch != -1) {
1017 put_queue(npadch & 0xff);
1022 /* called after returning from RAW mode or when changing consoles -
1023 recompute k_down[] and shift_state from key_down[] */
1024 /* maybe called when keymap is undefined, so that shiftkey release is seen */
1025 void sun_compute_shiftstate(void)
1027 int i, j, k, sym, val;
1030 for(i=0; i < SIZE(k_down); i++)
1033 for(i=0; i < SIZE(key_down); i++)
1034 if(key_down[i]) { /* skip this word if not a single bit on */
1035 k = i*BITS_PER_LONG;
1036 for(j=0; j<BITS_PER_LONG; j++,k++)
1037 if(test_bit(k, key_down)) {
1038 sym = U(plain_map[k]);
1039 if(KTYP(sym) == KT_SHIFT) {
1041 if (val == KVAL(K_CAPSSHIFT))
1042 val = KVAL(K_SHIFT);
1044 shift_state |= (1<<val);
1050 static void do_meta(unsigned char value, char up_flag)
1055 if (vc_kbd_mode(kbd, VC_META)) {
1059 put_queue(value | 0x80);
1062 static void do_ascii(unsigned char value, char up_flag)
1069 if (value < 10) /* decimal input of code, while Alt depressed */
1071 else { /* hexadecimal input of code, while AltGr depressed */
1079 npadch = npadch * base + value;
1082 static void do_lock(unsigned char value, char up_flag)
1086 chg_vc_kbd_lock(kbd, value);
1090 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
1091 * or (ii) whatever pattern of lights people want to show using KDSETLED,
1092 * or (iii) specified bits of specified words in kernel memory.
1095 static unsigned char ledstate = 0xff; /* undefined */
1096 static unsigned char ledioctl;
1098 unsigned char sun_getledstate(void) {
1102 void sun_setledstate(struct kbd_struct *kbd, unsigned int led) {
1105 kbd->ledmode = LED_SHOW_IOCTL;
1107 kbd->ledmode = LED_SHOW_FLAGS;
1111 static struct ledptr {
1114 unsigned char valid:1;
1117 void register_leds(int console, unsigned int led,
1118 unsigned int *addr, unsigned int mask) {
1119 struct kbd_struct *kbd = kbd_table + console;
1121 ledptrs[led].addr = addr;
1122 ledptrs[led].mask = mask;
1123 ledptrs[led].valid = 1;
1124 kbd->ledmode = LED_SHOW_MEM;
1126 kbd->ledmode = LED_SHOW_FLAGS;
1129 static inline unsigned char getleds(void){
1130 struct kbd_struct *kbd = kbd_table + fg_console;
1133 if (kbd->ledmode == LED_SHOW_IOCTL)
1135 leds = kbd->ledflagstate;
1136 if (kbd->ledmode == LED_SHOW_MEM) {
1137 if (ledptrs[0].valid) {
1138 if (*ledptrs[0].addr & ledptrs[0].mask)
1143 if (ledptrs[1].valid) {
1144 if (*ledptrs[1].addr & ledptrs[1].mask)
1149 if (ledptrs[2].valid) {
1150 if (*ledptrs[2].addr & ledptrs[2].mask)
1160 * This routine is the bottom half of the keyboard interrupt
1161 * routine, and runs with all interrupts enabled. It does
1162 * console changing, led setting and copy_to_cooked, which can
1163 * take a reasonably long time.
1165 * Aside from timing (which isn't really that important for
1166 * keyboard interrupts as they happen often), using the software
1167 * interrupt routines for this thing allows us to easily mask
1168 * this when we don't want any of the above to happen. Not yet
1169 * used, but this allows for easy and efficient race-condition
1170 * prevention later on.
1172 static unsigned char sunkbd_ledstate = 0xff; /* undefined */
1173 void sun_kbd_bh(unsigned long dummy)
1175 unsigned long flags;
1176 unsigned char leds, kbd_leds;
1178 spin_lock_irqsave(&sunkbd_lock, flags);
1181 kbd_leds = vcleds_to_sunkbd(leds);
1182 if (kbd_leds != sunkbd_ledstate) {
1184 sunkbd_ledstate = kbd_leds;
1185 send_cmd(SKBDCMD_SETLED);
1189 spin_unlock_irqrestore(&sunkbd_lock, flags);
1192 /* Support for keyboard "beeps". */
1194 /* Timer routine to turn off the beep after the interval expires. */
1195 static void sunkbd_kd_nosound(unsigned long __unused)
1197 unsigned long flags;
1199 spin_lock_irqsave(&sunkbd_lock, flags);
1200 send_cmd(SKBDCMD_BELLOFF);
1201 spin_unlock_irqrestore(&sunkbd_lock, flags);
1205 * Initiate a keyboard beep. If the frequency is zero, then we stop
1206 * the beep. Any other frequency will start a monotone beep. The beep
1207 * will be stopped by a timer after "ticks" jiffies. If ticks is 0,
1208 * then we do not start a timer.
1210 static void sunkbd_kd_mksound(unsigned int hz, unsigned int ticks)
1212 unsigned long flags;
1213 static struct timer_list sound_timer = { function: sunkbd_kd_nosound };
1215 spin_lock_irqsave(&sunkbd_lock, flags);
1217 del_timer(&sound_timer);
1220 send_cmd(SKBDCMD_BELLON);
1222 sound_timer.expires = jiffies + ticks;
1223 add_timer(&sound_timer);
1226 send_cmd(SKBDCMD_BELLOFF);
1228 spin_unlock_irqrestore(&sunkbd_lock, flags);
1231 extern void (*kd_mksound)(unsigned int hz, unsigned int ticks);
1233 int __init sun_kbd_init(void)
1236 struct kbd_struct kbd0;
1237 extern struct tty_driver console_driver;
1239 kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
1240 kbd0.ledmode = LED_SHOW_FLAGS;
1241 kbd0.lockstate = KBD_DEFLOCK;
1242 kbd0.slockstate = 0;
1243 kbd0.modeflags = KBD_DEFMODE;
1244 kbd0.kbdmode = VC_XLATE;
1246 for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
1247 kbd_table[i] = kbd0;
1249 ttytab = console_driver.table;
1251 kd_mksound = sunkbd_kd_mksound;
1253 /* XXX Check keyboard-click? property in 'options' PROM node XXX */
1254 if(sparc_cpu_model != sun4) {
1255 opt_node = prom_getchild(prom_root_node);
1256 opt_node = prom_searchsiblings(opt_node, "options");
1257 i = prom_getintdefault(opt_node, "keyboard-click?", -1);
1266 keyboard_tasklet.func = sun_kbd_bh;
1268 tasklet_enable(&keyboard_tasklet);
1269 tasklet_schedule(&keyboard_tasklet);
1274 /* /dev/kbd support */
1276 #define KBD_QSIZE 32
1277 static Firm_event kbd_queue [KBD_QSIZE];
1278 static int kbd_head, kbd_tail;
1279 static spinlock_t kbd_queue_lock = SPIN_LOCK_UNLOCKED;
1281 static int kbd_active = 0;
1282 static DECLARE_WAIT_QUEUE_HEAD(kbd_wait);
1283 static struct fasync_struct *kb_fasync;
1288 unsigned long flags;
1291 if (scan == KBD_IDLE)
1294 spin_lock_irqsave(&kbd_queue_lock, flags);
1295 next = (kbd_head + 1) % KBD_QSIZE;
1296 if (next != kbd_tail){
1297 kbd_queue [kbd_head].id = scan & KBD_KEYMASK;
1298 kbd_queue [kbd_head].value=scan & KBD_UP ? VKEY_UP : VKEY_DOWN;
1299 kbd_queue [kbd_head].time = xtime;
1302 spin_unlock_irqrestore(&kbd_queue_lock, flags);
1304 kill_fasync (&kb_fasync, SIGIO, POLL_IN);
1305 wake_up_interruptible (&kbd_wait);
1309 kbd_read (struct file *f, char *buffer, size_t count, loff_t *ppos)
1311 DECLARE_WAITQUEUE(wait, current);
1312 unsigned long flags;
1315 /* Return EWOULDBLOCK, because this is what the X server expects */
1316 if (kbd_head == kbd_tail){
1317 if (f->f_flags & O_NONBLOCK)
1318 return -EWOULDBLOCK;
1319 add_wait_queue (&kbd_wait, &wait);
1321 set_current_state(TASK_INTERRUPTIBLE);
1322 if (kbd_head == kbd_tail && !signal_pending(current)) {
1326 current->state = TASK_RUNNING;
1327 remove_wait_queue (&kbd_wait, &wait);
1329 /* There is data in the keyboard, fill the user buffer */
1332 spin_lock_irqsave(&kbd_queue_lock, flags);
1333 for (; p < end && kbd_head != kbd_tail;){
1334 Firm_event this_event = kbd_queue[kbd_tail];
1336 kbd_tail = (kbd_tail + 1) % KBD_QSIZE;
1338 spin_unlock_irqrestore(&kbd_queue_lock, flags);
1340 #ifdef CONFIG_SPARC32_COMPAT
1341 if (current->thread.flags & SPARC_FLAG_32BIT) {
1342 if (copy_to_user((Firm_event *)p, &this_event,
1343 sizeof(Firm_event)-sizeof(struct timeval)))
1345 p += sizeof(Firm_event)-sizeof(struct timeval);
1346 if (__put_user(this_event.time.tv_sec, (u32 *)p))
1349 if (__put_user(this_event.time.tv_usec, (u32 *)p))
1355 if (copy_to_user((Firm_event *)p, &this_event,
1356 sizeof(Firm_event)))
1358 p += sizeof (Firm_event);
1361 printk ("[%s]", this_event.value == VKEY_UP ? "UP" : "DOWN");
1364 spin_lock_irqsave(&kbd_queue_lock, flags);
1367 spin_unlock_irqrestore(&kbd_queue_lock, flags);
1373 static int kbd_fasync (int fd, struct file *filp, int on)
1377 retval = fasync_helper (fd, filp, on, &kb_fasync);
1383 static unsigned int kbd_poll (struct file *f, poll_table *wait)
1385 poll_wait(f, &kbd_wait, wait);
1386 if (kbd_head != kbd_tail)
1387 return POLLIN | POLLRDNORM;
1392 kbd_ioctl (struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
1395 unsigned char leds = 0;
1399 case KIOCTYPE: /* return keyboard type */
1400 if (put_user(sunkbd_type, (int *) arg))
1404 if (put_user(TR_UNTRANS_EVENT, (int *) arg))
1408 if (get_user(value, (int *) arg))
1410 if (value != TR_UNTRANS_EVENT)
1414 if (put_user(sunkbd_layout, (int *) arg))
1418 #ifndef CODING_NEW_DRIVER
1419 if (get_user(value, (int *) arg))
1422 kbd_redirected = fg_console + 1;
1425 kbd_table [fg_console].kbdmode = kbd_redirected ? VC_RAW : VC_XLATE;
1429 if (get_user(value, (int *) arg))
1431 c = (unsigned char) value;
1434 case SKBDCMD_NOCLICK:
1435 spin_lock_irq(&sunkbd_lock);
1437 spin_unlock_irq(&sunkbd_lock);
1439 case SKBDCMD_BELLON:
1442 case SKBDCMD_BELLOFF:
1449 if (get_user(c, (unsigned char *) arg))
1452 if (c & LED_SCRLCK) leds |= (1 << VC_SCROLLOCK);
1453 if (c & LED_NLOCK) leds |= (1 << VC_NUMLOCK);
1454 if (c & LED_CLOCK) leds |= (1 << VC_CAPSLOCK);
1455 compose_led_on = !!(c & LED_CMPOSE);
1456 sun_setledstate(kbd_table + fg_console, leds);
1459 if (put_user(vcleds_to_sunkbd(getleds()), (unsigned char *) arg))
1464 struct kbd_rate rate;
1466 rate.delay = kbd_delay_ticks;
1468 rate.rate = HZ / kbd_rate_ticks;
1472 if (copy_to_user((struct kbd_rate *)arg, &rate,
1473 sizeof(struct kbd_rate)))
1480 struct kbd_rate rate;
1482 if (verify_area(VERIFY_READ, (void *)arg,
1483 sizeof(struct kbd_rate)))
1485 copy_from_user(&rate, (struct kbd_rate *)arg,
1486 sizeof(struct kbd_rate));
1493 kbd_rate_ticks = HZ / rate.rate;
1494 kbd_delay_ticks = rate.delay;
1498 case FIONREAD: /* return number of bytes in kbd queue */
1502 count = kbd_head - kbd_tail;
1503 if (put_user((count < 0) ? KBD_QSIZE - count : count, (int *) arg))
1508 printk ("Unknown Keyboard ioctl: %8.8x\n", cmd);
1515 kbd_open (struct inode *i, struct file *f)
1517 spin_lock_irq(&kbd_queue_lock);
1523 kbd_opened = fg_console + 1;
1525 kbd_head = kbd_tail = 0;
1528 spin_unlock_irq(&kbd_queue_lock);
1534 kbd_close (struct inode *i, struct file *f)
1536 spin_lock_irq(&kbd_queue_lock);
1537 if (!--kbd_active) {
1539 kbd_table [kbd_redirected-1].kbdmode = VC_XLATE;
1542 kbd_fasync (-1, f, 0);
1544 spin_unlock_irq(&kbd_queue_lock);
1549 static struct file_operations kbd_fops =
1559 void __init keyboard_zsinit(void (*put_char)(unsigned char))
1563 kbd_put_char = put_char;
1565 panic("keyboard_zsinit: no put_char parameter");
1567 /* Test out the leds */
1571 send_cmd(SKBDCMD_RESET);
1572 send_cmd(SKBDCMD_RESET);
1573 while((sunkbd_type==255) && timeout++ < 25000) {
1578 if(timeout>=25000) {
1579 printk("keyboard: not present\n");
1583 if(sunkbd_type != SUNKBD_TYPE4) {
1584 printk("Sun TYPE %d keyboard detected ", sunkbd_type);
1587 while((sunkbd_layout==0) && timeout++ < 10000) {
1591 printk("Sun TYPE %d keyboard detected ",
1592 ((sunkbd_layout & SUNKBD_LOUT_TYP5_MASK) ? 5 : 4));
1594 if(sunkbd_type == SUNKBD_TYPE2)
1597 spin_lock_irq(&sunkbd_lock);
1600 send_cmd(SKBDCMD_CLICK);
1601 printk("with keyclick\n");
1603 send_cmd(SKBDCMD_NOCLICK);
1604 printk("without keyclick\n");
1607 /* Dork with led lights, then turn them all off */
1608 send_cmd(SKBDCMD_SETLED); send_cmd(0xf); /* All on */
1609 send_cmd(SKBDCMD_SETLED); send_cmd(0x0); /* All off */
1611 spin_unlock_irq(&sunkbd_lock);
1613 /* Register the /dev/kbd interface */
1614 devfs_register (NULL, "kbd", DEVFS_FL_DEFAULT,
1616 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
1618 if (devfs_register_chrdev (KBD_MAJOR, "kbd", &kbd_fops)){
1619 printk ("Could not register /dev/kbd device\n");