File: /usr/src/linux/drivers/sbus/char/sunkbd.c

1     /* keyboard.c: Sun keyboard driver.
2      *
3      * Copyright (C) 1995, 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
4      *
5      * Added vuid event generation and /dev/kbd device for SunOS
6      * compatibility - Miguel (miguel@nuclecu.unam.mx)
7      *
8      * Added PCI 8042 controller support -DaveM
9      * Added Magic SysRq support -MJ
10      */
11     
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>
17     #include <linux/mm.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>
30     
31     #include <asm/kbio.h>
32     #include <asm/vuid_event.h>
33     #include <asm/bitops.h>
34     #include <asm/oplib.h>
35     #include <asm/uaccess.h>
36     
37     #include <linux/kbd_kern.h>
38     #include <linux/kbd_diacr.h>
39     #include <linux/vt_kern.h>
40     
41     #ifdef CONFIG_PCI
42     #include <linux/pci.h>
43     #include <asm/pbm.h>
44     #include <asm/ebus.h>
45     #endif
46     
47     #include "sunkbd.h"
48     
49     #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
50     
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 */
54     
55     /* KBD device number, temporal */
56     #define KBD_MAJOR 11
57     
58     #define KBD_REPORT_ERR
59     #define KBD_REPORT_UNKN
60     
61     #ifndef KBD_DEFMODE
62     #define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
63     #endif
64     
65     #ifndef KBD_DEFLEDS
66     /*
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.
69      */
70     #define KBD_DEFLEDS 0
71     #endif
72     
73     #ifndef KBD_DEFLOCK
74     #define KBD_DEFLOCK 0
75     #endif
76     
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);
82     
83     struct l1a_kbd_state l1a_state;
84     
85     #ifndef CONFIG_PCI
86     DECLARE_WAIT_QUEUE_HEAD(keypress_wait);
87     #endif
88     
89     int keyboard_wait_for_keypress(struct console *co)
90     {
91     	sleep_on(&keypress_wait);
92     	return 0;
93     }
94     
95     static spinlock_t sunkbd_lock = SPIN_LOCK_UNLOCKED;
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     
103     /* shift state counters.. */
104     static unsigned char k_down[NR_SHIFT];
105     /* keyboard key bitmap */
106     static unsigned long key_down[256/BITS_PER_LONG];
107     
108     void push_kbd (int scan);
109     int kbd_redirected;
110     
111     static int dead_key_next;
112     /* 
113      * In order to retrieve the shift_state (for the mouse server), either
114      * the variable must be global, or a new procedure must be created to 
115      * return the value. I chose the former way.
116      */
117     #ifndef CONFIG_PCI
118     int shift_state;
119     struct kbd_struct kbd_table[MAX_NR_CONSOLES];
120     #endif
121     static int npadch = -1;			/* -1 or number assembled on pad */
122     static unsigned char diacr;
123     static char rep;			/* flag telling character repeat */
124     static struct tty_struct **ttytab;
125     static struct kbd_struct * kbd = kbd_table;
126     static struct tty_struct * tty;
127     static int compose_led_on;
128     static int kbd_delay_ticks = HZ / 5;
129     static int kbd_rate_ticks = HZ / 20;
130     
131     void sun_compute_shiftstate(void);
132     
133     typedef void (*k_hand)(unsigned char value, char up_flag);
134     typedef void (k_handfn)(unsigned char value, char up_flag);
135     
136     static k_handfn
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_ignore;
139     
140     static k_hand key_handler[16] = {
141     	do_self, do_fn, do_spec, do_pad, do_dead, do_cons, do_cur, do_shift,
142     	do_meta, do_ascii, do_lock, do_lowercase,
143     	do_ignore, do_ignore, do_ignore, do_ignore
144     };
145     
146     typedef void (*void_fnp)(void);
147     typedef void (void_fn)(void);
148     
149     static void_fn do_null, enter, show_ptregs, send_intr, lastcons, caps_toggle,
150     	num, hold, scroll_forw, scroll_back, boot_it, caps_on, compose,
151     	SAK, decr_console, incr_console, spawn_console, bare_num;
152     
153     static void_fnp spec_fn_table[] = {
154     	do_null,	enter,		show_ptregs,	show_mem,
155     	show_state,	send_intr,	lastcons,	caps_toggle,
156     	num,		hold,		scroll_forw,	scroll_back,
157     	boot_it,	caps_on,	compose,	SAK,
158     	decr_console,	incr_console,	spawn_console,	bare_num
159     };
160     
161     /* maximum values each key_handler can handle */
162     #ifndef CONFIG_PCI
163     const int max_vals[] = {
164     	255, SIZE(func_table) - 1, SIZE(spec_fn_table) - 1, NR_PAD - 1,
165     	NR_DEAD - 1, 255, 3, NR_SHIFT - 1,
166     	255, NR_ASCII - 1, NR_LOCK - 1, 255,
167     	NR_LOCK - 1
168     };
169     
170     const int NR_TYPES = SIZE(max_vals);
171     #endif
172     
173     static void put_queue(int);
174     static unsigned char handle_diacr(unsigned char);
175     
176     /* pt_regs - set by keyboard_interrupt(), used by show_ptregs() */
177     static struct pt_regs * pt_regs;
178     
179     #ifdef CONFIG_MAGIC_SYSRQ
180     unsigned char sun_sysrq_xlate[128] =
181     	"\0\0\0\0\0\201\202\212\203\213\204\214\205\0\206\0"	/* 0x00 - 0x0f */
182     	"\207\210\211\0\0\0\0\0\0\0\0\0\0\03312"		/* 0x10 - 0x1f */
183     	"34567890-=`\177\0=/*"					/* 0x20 - 0x2f */
184     	"\0\0.\0\0\011qwertyuiop"				/* 0x30 - 0x3f */
185     	"[]\177\000789-\0\0\0\0\0asd"				/* 0x40 - 0x4f */
186     	"fghjkl;'\\\015\0154560\0"				/* 0x50 - 0x5f */
187     	"\0\0\0\0zxcvbnm,./\0\012"				/* 0x60 - 0x6f */
188     	"123\0\0\0\0\0\0 \0\0\0\0\0\0";				/* 0x70 - 0x7f */
189     #endif
190     
191     volatile unsigned char sunkbd_layout;
192     volatile unsigned char sunkbd_type;
193     #define SUNKBD_TYPE2        0x02
194     #define SUNKBD_TYPE3        0x03
195     #define SUNKBD_TYPE4        0x04
196     
197     #define SUNKBD_LOUT_TYP4    	0x00
198     #define SUNKBD_LOUT_TYP5_MASK   0x20
199     
200     volatile int kbd_reset_pending;
201     volatile int kbd_layout_pending;
202     
203     /* commands */
204     #define SKBDCMD_RESET       0x1
205     #define SKBDCMD_GLAYOUT     0xf
206     #define SKBDCMD_BELLON      0x2
207     #define SKBDCMD_BELLOFF     0x3
208     #define SKBDCMD_SETLED      0xe
209     #define SKBDCMD_NOCLICK     0xb
210     #define SKBDCMD_CLICK       0xa
211     
212     static unsigned char sunkbd_clickp;
213     
214     /* The led set commands require sending the SETLED byte then
215      * a byte encoding which led's to have set.  Here are the bit
216      * values, a bit set = led-on.
217      */
218     #define LED_NLOCK           0x1   /* Num-lock */
219     #define LED_CMPOSE          0x2   /* Compose */
220     #define LED_SCRLCK          0x4   /* Scroll-lock */
221     #define LED_CLOCK           0x8   /* Caps-lock */
222     
223     /* Special state characters */
224     #define SKBD_RESET          0xff
225     #define SKBD_ALLUP          0x7f
226     #define SKBD_LYOUT          0xfe
227     
228     /* On the Sparc the keyboard could be one of two things.
229      * It could be a real keyboard speaking over one of the
230      * channels of the second zs8530 chip (other channel is
231      * used by the Sun mouse).  Else we have serial console
232      * going, and thus the other zs8530 chip is who we speak
233      * to.  Either way, we communicate through the zs8530
234      * driver for all our I/O.
235      */
236     
237     #define SUNKBD_UBIT     0x80      /* If set, key went up */
238     #define SUNKBD_KMASK    0x7f      /* Other bits are the keycode */
239     
240     #define KEY_LSHIFT      0x81
241     #define KEY_RSHIFT      0x82
242     #define KEY_CONTROL     0x83
243     #define KEY_NILL        0x84
244     #define KEY_CAPSLOCK    0x85
245     #define KEY_ALT         0x86
246     #define KEY_L1          0x87
247     
248     /* Due to sun_kbd_init() being called before rs_init(), and sun_kbd_init() doing:
249      *
250      *	tasklet_enable(&keyboard_tasklet);
251      *	tasklet_schedule(&keyboard_tasklet);
252      *
253      * this might well be called before some driver has claimed interest in
254      * handling the keyboard input/output. So we need to assign an initial nop.
255      */
256     static void nop_kbd_put_char(unsigned char c) { }
257     static void (*kbd_put_char)(unsigned char) = nop_kbd_put_char;
258     
259     /* Must be invoked under sunkbd_lock. */
260     static inline void send_cmd(unsigned char c)
261     {
262     	kbd_put_char(c);
263     }
264     
265     /* kbd_bh() calls this to send the SKBDCMD_SETLED to the sun keyboard
266      * with the proper bit pattern for the leds to be set.  It basically
267      * converts the kbd->ledflagstate values to corresponding sun kbd led
268      * bit value.
269      */
270     static inline unsigned char vcleds_to_sunkbd(unsigned char vcleds)
271     {
272     	unsigned char retval = 0;
273     
274     	if(vcleds & (1<<VC_SCROLLOCK))
275     		retval |= LED_SCRLCK;
276     	if(vcleds & (1<<VC_NUMLOCK))
277     		retval |= LED_NLOCK;
278     	if(vcleds & (1<<VC_CAPSLOCK))
279     		retval |= LED_CLOCK;
280     	if(compose_led_on)
281     		retval |= LED_CMPOSE;
282     	return retval;
283     }
284     
285     /*
286      * Translation of escaped scancodes to keycodes.
287      * This is now user-settable.
288      * The keycodes 1-88,96-111,119 are fairly standard, and
289      * should probably not be changed - changing might confuse X.
290      * X also interprets scancode 0x5d (KEY_Begin).
291      *
292      * For 1-88 keycode equals scancode.
293      */
294     
295     #define E0_KPENTER 96
296     #define E0_RCTRL   97
297     #define E0_KPSLASH 98
298     #define E0_PRSCR   99
299     #define E0_RALT    100
300     #define E0_BREAK   101  /* (control-pause) */
301     #define E0_HOME    102
302     #define E0_UP      103
303     #define E0_PGUP    104
304     #define E0_LEFT    105
305     #define E0_RIGHT   106
306     #define E0_END     107
307     #define E0_DOWN    108
308     #define E0_PGDN    109
309     #define E0_INS     110
310     #define E0_DEL     111
311     
312     #define E1_PAUSE   119
313     
314     /*
315      * The keycodes below are randomly located in 89-95,112-118,120-127.
316      * They could be thrown away (and all occurrences below replaced by 0),
317      * but that would force many users to use the `setkeycodes' utility, where
318      * they needed not before. It does not matter that there are duplicates, as
319      * long as no duplication occurs for any single keyboard.
320      */
321     #define SC_LIM 89
322     
323     #define FOCUS_PF1 85           /* actual code! */
324     #define FOCUS_PF2 89
325     #define FOCUS_PF3 90
326     #define FOCUS_PF4 91
327     #define FOCUS_PF5 92
328     #define FOCUS_PF6 93
329     #define FOCUS_PF7 94
330     #define FOCUS_PF8 95
331     #define FOCUS_PF9 120
332     #define FOCUS_PF10 121
333     #define FOCUS_PF11 122
334     #define FOCUS_PF12 123
335     
336     #define JAP_86     124
337     /* tfj@olivia.ping.dk:
338      * The four keys are located over the numeric keypad, and are
339      * labelled A1-A4. It's an rc930 keyboard, from
340      * Regnecentralen/RC International, Now ICL.
341      * Scancodes: 59, 5a, 5b, 5c.
342      */
343     #define RGN1 124
344     #define RGN2 125
345     #define RGN3 126
346     #define RGN4 127
347     
348     static unsigned char high_keys[128 - SC_LIM] = {
349       RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
350       0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
351       0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
352       0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
353       FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
354       FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
355     };
356     
357     /* BTC */
358     #define E0_MACRO   112
359     /* LK450 */
360     #define E0_F13     113
361     #define E0_F14     114
362     #define E0_HELP    115
363     #define E0_DO      116
364     #define E0_F17     117
365     #define E0_KPMINPLUS 118
366     /*
367      * My OmniKey generates e0 4c for  the "OMNI" key and the
368      * right alt key does nada. [kkoller@nyx10.cs.du.edu]
369      */
370     #define E0_OK	124
371     /*
372      * New microsoft keyboard is rumoured to have
373      * e0 5b (left window button), e0 5c (right window button),
374      * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
375      * [or: Windows_L, Windows_R, TaskMan]
376      */
377     #define E0_MSLW	125
378     #define E0_MSRW	126
379     #define E0_MSTM	127
380     
381     static unsigned char e0_keys[128] = {
382       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x00-0x07 */
383       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x08-0x0f */
384       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x10-0x17 */
385       0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,	      /* 0x18-0x1f */
386       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x20-0x27 */
387       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x28-0x2f */
388       0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,	      /* 0x30-0x37 */
389       E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,	      /* 0x38-0x3f */
390       E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,	      /* 0x40-0x47 */
391       E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
392       E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,	      /* 0x50-0x57 */
393       0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,	      /* 0x58-0x5f */
394       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x60-0x67 */
395       0, 0, 0, 0, 0, 0, 0, E0_MACRO,		      /* 0x68-0x6f */
396       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x70-0x77 */
397       0, 0, 0, 0, 0, 0, 0, 0			      /* 0x78-0x7f */
398     };
399     
400     /* we use this map to determine if a particular key should not be
401        autorepeated. We don't autorepeat CONTROL, LSHIFT, CAPS,
402        ALT, LMETA, RSHIFT, RMETA, ALTG and COMPOSE */
403     static unsigned char norepeat_keys[128] = {
404     	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  /* 0x00-0x0f */
405     	0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x10-0x1f */
406     	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x20-0x2f */
407     	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x30-0x3f */
408     	0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,  /* 0x40-0x4f */
409     	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x50-0x5f */
410     	0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,  /* 0x60-0x6f */
411     	0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0,  /* 0x70-0x7f */
412     };
413     
414     
415     int sun_setkeycode(unsigned int scancode, unsigned int keycode)
416     {
417     	if (scancode < SC_LIM || scancode > 255 || keycode > 127)
418     	  return -EINVAL;
419     	if (scancode < 128)
420     	  high_keys[scancode - SC_LIM] = keycode;
421     	else
422     	  e0_keys[scancode - 128] = keycode;
423     	return 0;
424     }
425     
426     int sun_getkeycode(unsigned int scancode)
427     {
428     	return
429     	  (scancode < SC_LIM || scancode > 255) ? -EINVAL :
430     	  (scancode < 128) ? high_keys[scancode - SC_LIM] :
431     	    e0_keys[scancode - 128];
432     }
433     
434     static void __sunkbd_inchar(unsigned char ch, struct pt_regs *regs);
435     void sunkbd_inchar(unsigned char ch, struct pt_regs *regs);
436     static void keyboard_timer (unsigned long ignored);
437     
438     static struct timer_list
439     auto_repeat_timer = { function: keyboard_timer };
440     
441     /* Keeps track of the last pressed key */
442     static unsigned char last_keycode;
443     
444     static void
445     keyboard_timer (unsigned long ignored)
446     {
447     	unsigned long flags;
448     
449     	spin_lock_irqsave(&sunkbd_lock, flags);
450     
451     	/* Auto repeat: send regs = 0 to indicate autorepeat */
452     	__sunkbd_inchar (last_keycode, 0);
453     	del_timer (&auto_repeat_timer);
454     	if (kbd_rate_ticks) {
455     		auto_repeat_timer.expires = jiffies + kbd_rate_ticks;
456     		add_timer (&auto_repeat_timer);
457     	}
458     
459     	spin_unlock_irqrestore(&sunkbd_lock, flags);
460     }
461     
462     #ifndef CONFIG_PCI
463     DECLARE_TASKLET_DISABLED(keyboard_tasklet, sun_kbd_bh, 0);
464     #endif
465     
466     /* #define SKBD_DEBUG */
467     /* This is our keyboard 'interrupt' routine.
468      * Must run under sunkbd_lock.
469      */
470     static void __sunkbd_inchar(unsigned char ch, struct pt_regs *regs)
471     {
472     	unsigned char keycode;
473     	char up_flag;                          /* 0 or SUNKBD_UBIT */
474     	char raw_mode;
475     
476     	if(ch == SKBD_RESET) {
477     		kbd_reset_pending = 1;
478     		goto out;
479     	}
480     	if(ch == SKBD_LYOUT) {
481     		kbd_layout_pending = 1;
482     		goto out;
483     	}
484     	if(kbd_reset_pending) {
485     		sunkbd_type = ch;
486     		kbd_reset_pending = 0;
487     		if(ch == SUNKBD_TYPE4)
488     			send_cmd(SKBDCMD_GLAYOUT);
489     		goto out;
490     	} else if(kbd_layout_pending) {
491     		sunkbd_layout = ch;
492     		kbd_layout_pending = 0;
493     		goto out;
494     	} else if(ch == SKBD_ALLUP) {
495     		del_timer (&auto_repeat_timer);
496     		memset(key_down, 0, sizeof(key_down));
497     		sun_compute_shiftstate();
498     		goto out;
499     	}
500     #ifdef SKBD_DEBUG
501     	if(ch == 0x7f)
502     		printk("KBD<ALL KEYS UP>");
503     	else
504     		printk("KBD<%x %s>", ch,
505     		       ((ch&0x80) ? "UP" : "DOWN"));
506     #endif
507     
508     	/* Whee, a real character. */
509     	if(regs) {
510     		pt_regs = regs;
511     		last_keycode = keycode = ch;
512     	} else {
513     		keycode = ch;
514     	}
515     	
516     	do_poke_blanked_console = 1;
517     	schedule_console_callback();
518     	add_keyboard_randomness(keycode);
519     
520     	tty = ttytab? ttytab[fg_console]: NULL;
521     	if (tty && (!tty->driver_data)) {
522     		/* This is to workaround ugly bug in tty_io.c, which
523                        does not do locking when it should */
524     		tty = NULL;
525     	}
526     	kbd = kbd_table + fg_console;
527     	if((raw_mode = (kbd->kbdmode == VC_RAW))) {
528     		if (kbd_redirected == fg_console+1)
529     			push_kbd (keycode);
530     		else
531     			put_queue(keycode);
532     		/* we do not return yet, because we want to maintain
533     		 * the key_down array, so that we have the correct
534     		 * values  when finishing RAW mode or when changing VT's.
535     		 */
536     	}
537     	up_flag = (keycode & SUNKBD_UBIT);  /* The 'up' bit */
538     	keycode &= SUNKBD_KMASK;            /* all the rest */
539     	del_timer (&auto_repeat_timer);
540     	if(up_flag) {
541     		rep = 0;
542     		clear_bit(keycode, key_down);
543     	} else {
544     		if (!norepeat_keys[keycode]) {
545     			if (kbd_rate_ticks) {
546     				auto_repeat_timer.expires =
547     						jiffies + kbd_delay_ticks;
548     				add_timer (&auto_repeat_timer);
549     			}
550     		}
551     		rep = test_and_set_bit(keycode, key_down);
552     	}
553     
554     #ifdef CONFIG_MAGIC_SYSRQ			/* Handle the SysRq hack */
555     	if (l1a_state.l1_down) {
556     		if (!up_flag)
557     			handle_sysrq(sun_sysrq_xlate[keycode], pt_regs, kbd, tty);
558     		goto out;
559     	}
560     #endif
561     
562     	if(raw_mode)
563     		goto out;
564     
565     	if(kbd->kbdmode == VC_MEDIUMRAW) {
566     		put_queue(keycode + up_flag);
567     		goto out;
568     	}
569     
570      	/*
571     	 * Small change in philosophy: earlier we defined repetition by
572     	 *	 rep = keycode == prev_keycode;
573     	 *	 prev_keycode = keycode;
574     	 * but now by the fact that the depressed key was down already.
575     	 * Does this ever make a difference? Yes.
576     	 */
577     
578     	/*
579      	 *  Repeat a key only if the input buffers are empty or the
580      	 *  characters get echoed locally. This makes key repeat usable
581      	 *  with slow applications and under heavy loads.
582     	 */
583     	if (!rep ||
584     	    (vc_kbd_mode(kbd,VC_REPEAT) && tty &&
585     	     (L_ECHO(tty) || (tty->driver.chars_in_buffer(tty) == 0)))) {
586     		u_short keysym;
587     		u_char type;
588     
589     		/* the XOR below used to be an OR */
590     		int shift_final = shift_state ^ kbd->lockstate ^ kbd->slockstate;
591     		ushort *key_map = key_maps[shift_final];
592     
593     		if (key_map != NULL) {
594     			keysym = key_map[keycode];
595     			type = KTYP(keysym);
596     
597     			if (type >= 0xf0) {
598     			    type -= 0xf0;
599     			    if (type == KT_LETTER) {
600     				type = KT_LATIN;
601     				if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
602     				    key_map = key_maps[shift_final ^ (1<<KG_SHIFT)];
603     				    if (key_map)
604     				      keysym = key_map[keycode];
605     				}
606     			    }
607     			    (*key_handler[type])(keysym & 0xff, up_flag);
608     			    if (type != KT_SLOCK)
609     			      kbd->slockstate = 0;
610     			}
611     		} else {
612     			/* maybe beep? */
613     			/* we have at least to update shift_state */
614     			sun_compute_shiftstate();
615     		}
616     	}
617     out:
618     	tasklet_schedule(&keyboard_tasklet);
619     }
620     
621     void sunkbd_inchar(unsigned char ch, struct pt_regs *regs)
622     {
623     	unsigned long flags;
624     
625     	spin_lock_irqsave(&sunkbd_lock, flags);
626     	__sunkbd_inchar(ch, regs);
627     	spin_unlock_irqrestore(&sunkbd_lock, flags);
628     }
629     
630     static void put_queue(int ch)
631     {
632     	wake_up(&keypress_wait);
633     	if (tty) {
634     		tty_insert_flip_char(tty, ch, 0);
635     		con_schedule_flip(tty);
636     	}
637     }
638     
639     static void puts_queue(char *cp)
640     {
641     	wake_up(&keypress_wait);
642     	if (!tty)
643     		return;
644     
645     	while (*cp) {
646     		tty_insert_flip_char(tty, *cp, 0);
647     		cp++;
648     	}
649     	con_schedule_flip(tty);
650     }
651     
652     static void applkey(int key, char mode)
653     {
654     	static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
655     
656     	buf[1] = (mode ? 'O' : '[');
657     	buf[2] = key;
658     	puts_queue(buf);
659     }
660     
661     static void enter(void)
662     {
663     	put_queue(13);
664     	if (vc_kbd_mode(kbd,VC_CRLF))
665     		put_queue(10);
666     }
667     
668     static void caps_toggle(void)
669     {
670     	if (rep)
671     		return;
672     	chg_vc_kbd_led(kbd, VC_CAPSLOCK);
673     }
674     
675     static void caps_on(void)
676     {
677     	if (rep)
678     		return;
679     	set_vc_kbd_led(kbd, VC_CAPSLOCK);
680     }
681     
682     static void show_ptregs(void)
683     {
684     	if (pt_regs)
685     		show_regs(pt_regs);
686     }
687     
688     static void hold(void)
689     {
690     	if (rep || !tty)
691     		return;
692     
693     	/*
694     	 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
695     	 * these routines are also activated by ^S/^Q.
696     	 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
697     	 */
698     	if (tty->stopped)
699     		start_tty(tty);
700     	else
701     		stop_tty(tty);
702     }
703     
704     static void num(void)
705     {
706     	if (vc_kbd_mode(kbd,VC_APPLIC))
707     		applkey('P', 1);
708     	else
709     		bare_num();
710     }
711     
712     /*
713      * Bind this to Shift-NumLock if you work in application keypad mode
714      * but want to be able to change the NumLock flag.
715      * Bind this to NumLock if you prefer that the NumLock key always
716      * changes the NumLock flag.
717      */
718     static void bare_num(void)
719     {
720     	if (!rep)
721     		chg_vc_kbd_led(kbd,VC_NUMLOCK);
722     }
723     
724     static void lastcons(void)
725     {
726     	/* switch to the last used console, ChN */
727     	set_console(last_console);
728     }
729     
730     static void decr_console(void)
731     {
732     	int i;
733      
734     	for (i = fg_console-1; i != fg_console; i--) {
735     		if (i == -1)
736     			i = MAX_NR_CONSOLES-1;
737     		if (vc_cons_allocated(i))
738     			break;
739     	}
740     	set_console(i);
741     }
742     
743     static void incr_console(void)
744     {
745     	int i;
746     
747     	for (i = fg_console+1; i != fg_console; i++) {
748     		if (i == MAX_NR_CONSOLES)
749     			i = 0;
750     		if (vc_cons_allocated(i))
751     			break;
752     	}
753     	set_console(i);
754     }
755     
756     static void send_intr(void)
757     {
758     	if (!tty)
759     		return;
760     	tty_insert_flip_char(tty, 0, TTY_BREAK);
761     	con_schedule_flip(tty);
762     }
763     
764     static void scroll_forw(void)
765     {
766     	scrollfront(0);
767     }
768     
769     static void scroll_back(void)
770     {
771     	scrollback(0);
772     }
773     
774     static void boot_it(void)
775     {
776     	extern int obp_system_intr(void);
777     
778     	if (!obp_system_intr())
779     		ctrl_alt_del();
780     	/* sigh.. attempt to prevent multiple entry */
781     	last_keycode=1;
782     	rep = 0;
783     }
784     
785     static void compose(void)
786     {
787     	dead_key_next = 1;
788     	compose_led_on = 1;
789     	set_leds();
790     }
791     
792     #ifdef CONFIG_PCI
793     extern int spawnpid, spawnsig;
794     #else
795     int spawnpid, spawnsig;
796     #endif
797     
798     
799     static void spawn_console(void)
800     {
801             if (spawnpid)
802     	   if(kill_proc(spawnpid, spawnsig, 1))
803     	     spawnpid = 0;
804     }
805     
806     static void SAK(void)
807     {
808     	do_SAK(tty);
809     #if 0
810     	/*
811     	 * Need to fix SAK handling to fix up RAW/MEDIUM_RAW and
812     	 * vt_cons modes before we can enable RAW/MEDIUM_RAW SAK
813     	 * handling.
814     	 * 
815     	 * We should do this some day --- the whole point of a secure
816     	 * attention key is that it should be guaranteed to always
817     	 * work.
818     	 */
819     	reset_vc(fg_console);
820     	do_unblank_screen();	/* not in interrupt routine? */
821     #endif
822     }
823     
824     static void do_ignore(unsigned char value, char up_flag)
825     {
826     }
827     
828     static void do_null()
829     {
830     	sun_compute_shiftstate();
831     }
832     
833     static void do_spec(unsigned char value, char up_flag)
834     {
835     	if (up_flag)
836     		return;
837     	if (value >= SIZE(spec_fn_table))
838     		return;
839     	spec_fn_table[value]();
840     }
841     
842     static void do_lowercase(unsigned char value, char up_flag)
843     {
844     	printk("keyboard.c: do_lowercase was called - impossible\n");
845     }
846     
847     static void do_self(unsigned char value, char up_flag)
848     {
849     	if (up_flag)
850     		return;		/* no action, if this is a key release */
851     
852     	if (diacr) {
853     		value = handle_diacr(value);
854     		compose_led_on = 0;
855     		set_leds();
856     	}
857     
858     	if (dead_key_next) {
859     		dead_key_next = 0;
860     		diacr = value;
861     		return;
862     	}
863     
864     	put_queue(value);
865     }
866     
867     #define A_GRAVE  '`'
868     #define A_ACUTE  '\''
869     #define A_CFLEX  '^'
870     #define A_TILDE  '~'
871     #define A_DIAER  '"'
872     #define A_CEDIL  ','
873     static unsigned char ret_diacr[NR_DEAD] =
874     	{A_GRAVE, A_ACUTE, A_CFLEX, A_TILDE, A_DIAER, A_CEDIL };
875     
876     /* If a dead key pressed twice, output a character corresponding to it,	*/
877     /* otherwise just remember the dead key.				*/
878     
879     static void do_dead(unsigned char value, char up_flag)
880     {
881     	if (up_flag)
882     		return;
883     
884     	value = ret_diacr[value];
885     	if (diacr == value) {   /* pressed twice */
886     		diacr = 0;
887     		put_queue(value);
888     		return;
889     	}
890     	diacr = value;
891     }
892     
893     
894     /* If space is pressed, return the character corresponding the pending	*/
895     /* dead key, otherwise try to combine the two.				*/
896     
897     unsigned char handle_diacr(unsigned char ch)
898     {
899     	int d = diacr;
900     	int i;
901     
902     	diacr = 0;
903     	if (ch == ' ')
904     		return d;
905     
906     	for (i = 0; i < accent_table_size; i++) {
907     		if (accent_table[i].diacr == d && accent_table[i].base == ch)
908     			return accent_table[i].result;
909     	}
910     
911     	put_queue(d);
912     	return ch;
913     }
914     
915     static void do_cons(unsigned char value, char up_flag)
916     {
917     	if (up_flag)
918     		return;
919     	set_console(value);
920     }
921     
922     static void do_fn(unsigned char value, char up_flag)
923     {
924     	if (up_flag)
925     		return;
926     	if (value < SIZE(func_table)) {
927     		if (func_table[value])
928     			puts_queue(func_table[value]);
929     	} else
930     		printk("do_fn called with value=%d\n", value);
931     }
932     
933     static void do_pad(unsigned char value, char up_flag)
934     {
935     	static const char *pad_chars = "0123456789+-*/\015,.?";
936     	static const char *app_map = "pqrstuvwxylSRQMnn?";
937     
938     	if (up_flag)
939     		return;		/* no action, if this is a key release */
940     
941     	/* kludge... shift forces cursor/number keys */
942     	if (vc_kbd_mode(kbd,VC_APPLIC) && !k_down[KG_SHIFT]) {
943     		applkey(app_map[value], 1);
944     		return;
945     	}
946     
947     	if (!vc_kbd_led(kbd,VC_NUMLOCK))
948     		switch (value) {
949     			case KVAL(K_PCOMMA):
950     			case KVAL(K_PDOT):
951     				do_fn(KVAL(K_REMOVE), 0);
952     				return;
953     			case KVAL(K_P0):
954     				do_fn(KVAL(K_INSERT), 0);
955     				return;
956     			case KVAL(K_P1):
957     				do_fn(KVAL(K_SELECT), 0);
958     				return;
959     			case KVAL(K_P2):
960     				do_cur(KVAL(K_DOWN), 0);
961     				return;
962     			case KVAL(K_P3):
963     				do_fn(KVAL(K_PGDN), 0);
964     				return;
965     			case KVAL(K_P4):
966     				do_cur(KVAL(K_LEFT), 0);
967     				return;
968     			case KVAL(K_P6):
969     				do_cur(KVAL(K_RIGHT), 0);
970     				return;
971     			case KVAL(K_P7):
972     				do_fn(KVAL(K_FIND), 0);
973     				return;
974     			case KVAL(K_P8):
975     				do_cur(KVAL(K_UP), 0);
976     				return;
977     			case KVAL(K_P9):
978     				do_fn(KVAL(K_PGUP), 0);
979     				return;
980     			case KVAL(K_P5):
981     				applkey('G', vc_kbd_mode(kbd, VC_APPLIC));
982     				return;
983     		}
984     
985     	put_queue(pad_chars[value]);
986     	if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
987     		put_queue(10);
988     }
989     
990     static void do_cur(unsigned char value, char up_flag)
991     {
992     	static const char *cur_chars = "BDCA";
993     	if (up_flag)
994     		return;
995     
996     	applkey(cur_chars[value], vc_kbd_mode(kbd,VC_CKMODE));
997     }
998     
999     static void do_shift(unsigned char value, char up_flag)
1000     {
1001     	int old_state = shift_state;
1002     
1003     	if (rep)
1004     		return;
1005     
1006     	/* Mimic typewriter:
1007     	   a CapsShift key acts like Shift but undoes CapsLock */
1008     	if (value == KVAL(K_CAPSSHIFT)) {
1009     		value = KVAL(K_SHIFT);
1010     		if (!up_flag)
1011     			clr_vc_kbd_led(kbd, VC_CAPSLOCK);
1012     	}
1013     
1014     	if (up_flag) {
1015     		/* handle the case that two shift or control
1016     		   keys are depressed simultaneously */
1017     		if (k_down[value])
1018     			k_down[value]--;
1019     	} else
1020     		k_down[value]++;
1021     
1022     	if (k_down[value])
1023     		shift_state |= (1 << value);
1024     	else
1025     		shift_state &= ~ (1 << value);
1026     
1027     	/* kludge, no joke... */
1028     	if (up_flag && shift_state != old_state && npadch != -1) {
1029     		put_queue(npadch & 0xff);
1030     		npadch = -1;
1031     	}
1032     }
1033     
1034     /* called after returning from RAW mode or when changing consoles -
1035        recompute k_down[] and shift_state from key_down[] */
1036     /* maybe called when keymap is undefined, so that shiftkey release is seen */
1037     void sun_compute_shiftstate(void)
1038     {
1039     	int i, j, k, sym, val;
1040     
1041     	shift_state = 0;
1042     	for(i=0; i < SIZE(k_down); i++)
1043     	  k_down[i] = 0;
1044     
1045     	for(i=0; i < SIZE(key_down); i++)
1046     	  if(key_down[i]) {	/* skip this word if not a single bit on */
1047     	    k = i*BITS_PER_LONG;
1048     	    for(j=0; j<BITS_PER_LONG; j++,k++)
1049     	      if(test_bit(k, key_down)) {
1050     		sym = U(plain_map[k]);
1051     		if(KTYP(sym) == KT_SHIFT) {
1052     		  val = KVAL(sym);
1053     		  if (val == KVAL(K_CAPSSHIFT))
1054     		    val = KVAL(K_SHIFT);
1055     		  k_down[val]++;
1056     		  shift_state |= (1<<val);
1057     		}
1058     	      }
1059     	  }
1060     }
1061     
1062     static void do_meta(unsigned char value, char up_flag)
1063     {
1064     	if (up_flag)
1065     		return;
1066     
1067     	if (vc_kbd_mode(kbd, VC_META)) {
1068     		put_queue('\033');
1069     		put_queue(value);
1070     	} else
1071     		put_queue(value | 0x80);
1072     }
1073     
1074     static void do_ascii(unsigned char value, char up_flag)
1075     {
1076     	int base;
1077     
1078     	if (up_flag)
1079     		return;
1080     
1081     	if (value < 10)    /* decimal input of code, while Alt depressed */
1082     	    base = 10;
1083     	else {       /* hexadecimal input of code, while AltGr depressed */
1084     	    value -= 10;
1085     	    base = 16;
1086     	}
1087     
1088     	if (npadch == -1)
1089     	  npadch = value;
1090     	else
1091     	  npadch = npadch * base + value;
1092     }
1093     
1094     static void do_lock(unsigned char value, char up_flag)
1095     {
1096     	if (up_flag || rep)
1097     		return;
1098     	chg_vc_kbd_lock(kbd, value);
1099     }
1100     
1101     /*
1102      * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
1103      * or (ii) whatever pattern of lights people want to show using KDSETLED,
1104      * or (iii) specified bits of specified words in kernel memory.
1105      */
1106     
1107     static unsigned char ledstate = 0xff; /* undefined */
1108     static unsigned char ledioctl;
1109     
1110     unsigned char sun_getledstate(void) {
1111         return ledstate;
1112     }
1113     
1114     void sun_setledstate(struct kbd_struct *kbd, unsigned int led) {
1115         if (!(led & ~7)) {
1116     	ledioctl = led;
1117     	kbd->ledmode = LED_SHOW_IOCTL;
1118         } else
1119     	kbd->ledmode = LED_SHOW_FLAGS;
1120         set_leds();
1121     }
1122     
1123     static struct ledptr {
1124         unsigned int *addr;
1125         unsigned int mask;
1126         unsigned char valid:1;
1127     } ledptrs[3];
1128     
1129     void register_leds(int console, unsigned int led,
1130     		   unsigned int *addr, unsigned int mask) {
1131         struct kbd_struct *kbd = kbd_table + console;
1132         if (led < 3) {
1133     	ledptrs[led].addr = addr;
1134     	ledptrs[led].mask = mask;
1135     	ledptrs[led].valid = 1;
1136     	kbd->ledmode = LED_SHOW_MEM;
1137         } else
1138     	kbd->ledmode = LED_SHOW_FLAGS;
1139     }
1140     
1141     static inline unsigned char getleds(void){
1142         struct kbd_struct *kbd = kbd_table + fg_console;
1143         unsigned char leds;
1144     
1145         if (kbd->ledmode == LED_SHOW_IOCTL)
1146           return ledioctl;
1147         leds = kbd->ledflagstate;
1148         if (kbd->ledmode == LED_SHOW_MEM) {
1149     	if (ledptrs[0].valid) {
1150     	    if (*ledptrs[0].addr & ledptrs[0].mask)
1151     	      leds |= 1;
1152     	    else
1153     	      leds &= ~1;
1154     	}
1155     	if (ledptrs[1].valid) {
1156     	    if (*ledptrs[1].addr & ledptrs[1].mask)
1157     	      leds |= 2;
1158     	    else
1159     	      leds &= ~2;
1160     	}
1161     	if (ledptrs[2].valid) {
1162     	    if (*ledptrs[2].addr & ledptrs[2].mask)
1163     	      leds |= 4;
1164     	    else
1165     	      leds &= ~4;
1166     	}
1167         }
1168         return leds;
1169     }
1170     
1171     /*
1172      * This routine is the bottom half of the keyboard interrupt
1173      * routine, and runs with all interrupts enabled. It does
1174      * console changing, led setting and copy_to_cooked, which can
1175      * take a reasonably long time.
1176      *
1177      * Aside from timing (which isn't really that important for
1178      * keyboard interrupts as they happen often), using the software
1179      * interrupt routines for this thing allows us to easily mask
1180      * this when we don't want any of the above to happen. Not yet
1181      * used, but this allows for easy and efficient race-condition
1182      * prevention later on.
1183      */
1184     static unsigned char sunkbd_ledstate = 0xff; /* undefined */
1185     void sun_kbd_bh(unsigned long dummy)
1186     {
1187     	unsigned long flags;
1188     	unsigned char leds, kbd_leds;
1189     
1190     	spin_lock_irqsave(&sunkbd_lock, flags);
1191     
1192     	leds = getleds();
1193     	kbd_leds = vcleds_to_sunkbd(leds);
1194     	if (kbd_leds != sunkbd_ledstate) {
1195     		ledstate = leds;
1196     		sunkbd_ledstate = kbd_leds;
1197     		send_cmd(SKBDCMD_SETLED);
1198     		send_cmd(kbd_leds);
1199     	}
1200     
1201     	spin_unlock_irqrestore(&sunkbd_lock, flags);
1202     }
1203     
1204     /* Support for keyboard "beeps". */ 
1205     
1206     /* Timer routine to turn off the beep after the interval expires. */
1207     static void sunkbd_kd_nosound(unsigned long __unused)
1208     {
1209     	unsigned long flags;
1210     
1211     	spin_lock_irqsave(&sunkbd_lock, flags);
1212     	send_cmd(SKBDCMD_BELLOFF);
1213     	spin_unlock_irqrestore(&sunkbd_lock, flags);
1214     }
1215     
1216     /*
1217      * Initiate a keyboard beep. If the frequency is zero, then we stop
1218      * the beep. Any other frequency will start a monotone beep. The beep
1219      * will be stopped by a timer after "ticks" jiffies. If ticks is 0,
1220      * then we do not start a timer.
1221      */
1222     static void sunkbd_kd_mksound(unsigned int hz, unsigned int ticks)
1223     {
1224     	unsigned long flags;
1225     	static struct timer_list sound_timer = { function: sunkbd_kd_nosound };
1226     
1227     	spin_lock_irqsave(&sunkbd_lock, flags);
1228     
1229     	del_timer(&sound_timer);
1230     
1231     	if (hz) {
1232     		send_cmd(SKBDCMD_BELLON);
1233     		if (ticks) {
1234     			sound_timer.expires = jiffies + ticks;
1235     			add_timer(&sound_timer);
1236     		}
1237     	} else
1238     		send_cmd(SKBDCMD_BELLOFF);
1239     
1240     	spin_unlock_irqrestore(&sunkbd_lock, flags);
1241     }
1242     
1243     extern void (*kd_mksound)(unsigned int hz, unsigned int ticks);
1244     
1245     int __init sun_kbd_init(void)
1246     {
1247     	int i, opt_node;
1248     	struct kbd_struct kbd0;
1249     	extern struct tty_driver console_driver;
1250     
1251     	kbd0.ledflagstate = kbd0.default_ledflagstate = KBD_DEFLEDS;
1252     	kbd0.ledmode = LED_SHOW_FLAGS;
1253     	kbd0.lockstate = KBD_DEFLOCK;
1254     	kbd0.slockstate = 0;
1255     	kbd0.modeflags = KBD_DEFMODE;
1256     	kbd0.kbdmode = VC_XLATE;
1257      
1258     	for (i = 0 ; i < MAX_NR_CONSOLES ; i++)
1259     		kbd_table[i] = kbd0;
1260     
1261     	ttytab = console_driver.table;
1262     
1263     	kd_mksound = sunkbd_kd_mksound;
1264     
1265     	/* XXX Check keyboard-click? property in 'options' PROM node XXX */
1266     	if(sparc_cpu_model != sun4) {
1267     		opt_node = prom_getchild(prom_root_node);
1268     		opt_node = prom_searchsiblings(opt_node, "options");
1269     		i = prom_getintdefault(opt_node, "keyboard-click?", -1);
1270     		if(i != -1)
1271     			sunkbd_clickp = 1;
1272     		else
1273     			sunkbd_clickp = 0;
1274     	} else {
1275     		sunkbd_clickp = 0;
1276     	}
1277     
1278     	keyboard_tasklet.func = sun_kbd_bh;
1279     
1280     	tasklet_enable(&keyboard_tasklet);
1281     	tasklet_schedule(&keyboard_tasklet);
1282     
1283     	return 0;
1284     }
1285     
1286     /* /dev/kbd support */
1287     
1288     #define KBD_QSIZE 32
1289     static Firm_event kbd_queue [KBD_QSIZE];
1290     static int kbd_head, kbd_tail;
1291     static spinlock_t kbd_queue_lock = SPIN_LOCK_UNLOCKED;
1292     char kbd_opened;
1293     static int kbd_active = 0;
1294     static DECLARE_WAIT_QUEUE_HEAD(kbd_wait);
1295     static struct fasync_struct *kb_fasync;
1296     
1297     void
1298     push_kbd (int scan)
1299     {
1300     	unsigned long flags;
1301     	int next;
1302     
1303     	if (scan == KBD_IDLE)
1304     		return;
1305     
1306     	spin_lock_irqsave(&kbd_queue_lock, flags);
1307     	next = (kbd_head + 1) % KBD_QSIZE;
1308     	if (next != kbd_tail){
1309     		kbd_queue [kbd_head].id = scan & KBD_KEYMASK;
1310     		kbd_queue [kbd_head].value=scan & KBD_UP ? VKEY_UP : VKEY_DOWN;
1311     		kbd_queue [kbd_head].time = xtime;
1312     		kbd_head = next;
1313     	}
1314     	spin_unlock_irqrestore(&kbd_queue_lock, flags);
1315     
1316     	kill_fasync (&kb_fasync, SIGIO, POLL_IN);
1317     	wake_up_interruptible (&kbd_wait);
1318     }
1319     
1320     static ssize_t
1321     kbd_read (struct file *f, char *buffer, size_t count, loff_t *ppos)
1322     {
1323     	DECLARE_WAITQUEUE(wait, current);
1324     	unsigned long flags;
1325     	char *end, *p;
1326     
1327     	/* Return EWOULDBLOCK, because this is what the X server expects */
1328     	if (kbd_head == kbd_tail){
1329     		if (f->f_flags & O_NONBLOCK)
1330     			return -EWOULDBLOCK;
1331     		add_wait_queue (&kbd_wait, &wait);
1332     repeat:
1333     		set_current_state(TASK_INTERRUPTIBLE);
1334     		if (kbd_head == kbd_tail && !signal_pending(current)) {
1335     			schedule();
1336     			goto repeat;
1337     		}
1338     		current->state = TASK_RUNNING;
1339     		remove_wait_queue (&kbd_wait, &wait);
1340     	}
1341     	/* There is data in the keyboard, fill the user buffer */
1342     	end = buffer+count;
1343     	p = buffer;
1344     	spin_lock_irqsave(&kbd_queue_lock, flags);
1345     	for (; p < end && kbd_head != kbd_tail;){
1346     		Firm_event this_event = kbd_queue[kbd_tail];
1347     
1348     		kbd_tail = (kbd_tail + 1) % KBD_QSIZE;
1349     
1350     		spin_unlock_irqrestore(&kbd_queue_lock, flags);
1351     
1352     #ifdef CONFIG_SPARC32_COMPAT
1353     		if (current->thread.flags & SPARC_FLAG_32BIT) {
1354     			if (copy_to_user((Firm_event *)p, &this_event,
1355     					 sizeof(Firm_event)-sizeof(struct timeval)))
1356     				return -EFAULT;
1357     			p += sizeof(Firm_event)-sizeof(struct timeval);
1358     			if (__put_user(this_event.time.tv_sec, (u32 *)p))
1359     				return -EFAULT;
1360     			p += sizeof(u32);
1361     			if (__put_user(this_event.time.tv_usec, (u32 *)p))
1362     				return -EFAULT;
1363     			p += sizeof(u32);
1364     		} else
1365     #endif
1366     		{
1367     			if (copy_to_user((Firm_event *)p, &this_event, 
1368     					 sizeof(Firm_event)))
1369     				return -EFAULT;
1370     			p += sizeof (Firm_event);
1371     		}
1372     #ifdef KBD_DEBUG
1373     		printk ("[%s]", this_event.value == VKEY_UP ? "UP" : "DOWN");
1374     #endif
1375     
1376     		spin_lock_irqsave(&kbd_queue_lock, flags);
1377     	}
1378     
1379     	spin_unlock_irqrestore(&kbd_queue_lock, flags);
1380     
1381     	return p-buffer;
1382     }
1383     
1384     /* Needed by X */
1385     static int kbd_fasync (int fd, struct file *filp, int on)
1386     {
1387     	int retval;
1388     
1389     	retval = fasync_helper (fd, filp, on, &kb_fasync);
1390     	if (retval < 0)
1391     		return retval;
1392     	return 0;
1393     }
1394     
1395     static unsigned int kbd_poll (struct file *f, poll_table *wait)
1396     {
1397     	poll_wait(f, &kbd_wait, wait);
1398     	if (kbd_head != kbd_tail)
1399     		return POLLIN | POLLRDNORM;
1400     	return 0;
1401     }
1402     
1403     static int
1404     kbd_ioctl (struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
1405     {
1406     	unsigned char c;
1407     	unsigned char leds = 0;
1408     	int value;
1409     
1410     	switch (cmd){
1411     	case KIOCTYPE:		  /* return keyboard type */
1412     		if (put_user(sunkbd_type, (int *) arg))
1413     			return -EFAULT;
1414     		break;
1415     	case KIOCGTRANS:
1416     		if (put_user(TR_UNTRANS_EVENT, (int *) arg))
1417     			return -EFAULT;
1418     		break;
1419     	case KIOCTRANS:
1420     		if (get_user(value, (int *) arg))
1421     			return -EFAULT;
1422     		if (value != TR_UNTRANS_EVENT)
1423     			return -EINVAL;
1424     		break;
1425     	case KIOCLAYOUT:
1426     		if (put_user(sunkbd_layout, (int *) arg))
1427     			return -EFAULT;
1428     		break;
1429     	case KIOCSDIRECT:
1430     #ifndef CODING_NEW_DRIVER
1431     		if (get_user(value, (int *) arg))
1432     			return -EFAULT;
1433     		if(value)
1434     			kbd_redirected = fg_console + 1;
1435     		else
1436     			kbd_redirected = 0;
1437     		kbd_table [fg_console].kbdmode = kbd_redirected ? VC_RAW : VC_XLATE;
1438     #endif
1439     		break;
1440     	case KIOCCMD:
1441     		if (get_user(value, (int *) arg))
1442     			return -EFAULT;
1443     		c = (unsigned char) value;
1444     		switch (c) {
1445     			case SKBDCMD_CLICK:
1446     			case SKBDCMD_NOCLICK:
1447     				spin_lock_irq(&sunkbd_lock);
1448     				send_cmd(c);
1449     				spin_unlock_irq(&sunkbd_lock);
1450     				return 0;
1451     			case SKBDCMD_BELLON:
1452     				kd_mksound(1,0);
1453     				return 0;
1454     			case SKBDCMD_BELLOFF:
1455     				kd_mksound(0,0);
1456     				return 0;
1457     			default:
1458     				return -EINVAL;
1459     		}
1460     	case KIOCSLED:
1461     		if (get_user(c, (unsigned char *) arg))
1462     			return -EFAULT;
1463     
1464     		if (c & LED_SCRLCK) leds |= (1 << VC_SCROLLOCK);
1465     		if (c & LED_NLOCK) leds |= (1 << VC_NUMLOCK);
1466     		if (c & LED_CLOCK) leds |= (1 << VC_CAPSLOCK);
1467     		compose_led_on = !!(c & LED_CMPOSE);
1468     		sun_setledstate(kbd_table + fg_console, leds);
1469     		break;
1470     	case KIOCGLED:
1471     		if (put_user(vcleds_to_sunkbd(getleds()), (unsigned char *) arg))
1472     			return -EFAULT;
1473     		break;
1474     	case KIOCGRATE:
1475     	{
1476     		struct kbd_rate rate;
1477     
1478     		rate.delay = kbd_delay_ticks;
1479     		if (kbd_rate_ticks)
1480     			rate.rate = HZ / kbd_rate_ticks;
1481     		else
1482     			rate.rate = 0;
1483     
1484     		if (copy_to_user((struct kbd_rate *)arg, &rate,
1485     				 sizeof(struct kbd_rate)))
1486     			return -EFAULT;
1487     
1488     		return 0;
1489     	}
1490     	case KIOCSRATE:
1491     	{
1492     		struct kbd_rate rate;
1493     
1494     		if (verify_area(VERIFY_READ, (void *)arg,
1495     				sizeof(struct kbd_rate)))
1496     			return -EFAULT;
1497     		copy_from_user(&rate, (struct kbd_rate *)arg,
1498     			       sizeof(struct kbd_rate));
1499     
1500     		if (rate.rate > 50)
1501     			return -EINVAL;
1502     		if (rate.rate == 0)
1503     			kbd_rate_ticks = 0;
1504     		else
1505     			kbd_rate_ticks = HZ / rate.rate;
1506     		kbd_delay_ticks = rate.delay;
1507     
1508     		return 0;
1509     	}
1510     	case FIONREAD:		/* return number of bytes in kbd queue */
1511     	{
1512     		int count;
1513     		
1514     		count = kbd_head - kbd_tail;
1515     		if (put_user((count < 0) ? KBD_QSIZE - count : count, (int *) arg))
1516     			return -EFAULT;
1517     		return 0;
1518     	}
1519     	default:
1520     		printk ("Unknown Keyboard ioctl: %8.8x\n", cmd);
1521     		return -EINVAL;
1522     	}
1523     	return 0;
1524     }
1525     
1526     static int
1527     kbd_open (struct inode *i, struct file *f)
1528     {
1529     	spin_lock_irq(&kbd_queue_lock);
1530     	kbd_active++;
1531     
1532     	if (kbd_opened)
1533     		goto out;
1534     
1535     	kbd_opened = fg_console + 1;
1536     
1537     	kbd_head = kbd_tail = 0;
1538     
1539      out:
1540     	spin_unlock_irq(&kbd_queue_lock);
1541     
1542     	return 0;
1543     }
1544     
1545     static int
1546     kbd_close (struct inode *i, struct file *f)
1547     {
1548     	spin_lock_irq(&kbd_queue_lock);
1549     	if (!--kbd_active) {
1550     		if (kbd_redirected)
1551     			kbd_table [kbd_redirected-1].kbdmode = VC_XLATE;
1552     		kbd_redirected = 0;
1553     		kbd_opened = 0;
1554     		kbd_fasync (-1, f, 0);
1555     	}
1556     	spin_unlock_irq(&kbd_queue_lock);
1557     
1558     	return 0;
1559     }
1560     
1561     static struct file_operations kbd_fops =
1562     {
1563     	read:		kbd_read,
1564     	poll:		kbd_poll,
1565     	ioctl:		kbd_ioctl,
1566     	open:		kbd_open,
1567     	release:	kbd_close,
1568     	fasync:		kbd_fasync,
1569     };
1570     
1571     void __init keyboard_zsinit(void (*put_char)(unsigned char))
1572     {
1573     	int timeout = 0;
1574     
1575     	kbd_put_char = put_char;
1576     	if (!kbd_put_char)
1577     		panic("keyboard_zsinit: no put_char parameter");
1578     
1579     	/* Test out the leds */
1580     	sunkbd_type = 255;
1581     	sunkbd_layout = 0;
1582     
1583     	send_cmd(SKBDCMD_RESET);
1584     	send_cmd(SKBDCMD_RESET);
1585     	while((sunkbd_type==255) && timeout++ < 25000) {
1586     		udelay(100);
1587     		barrier();
1588     	}
1589     
1590     	if(timeout>=25000) {
1591     		printk("keyboard: not present\n");
1592     		return;
1593     	}
1594     
1595     	if(sunkbd_type != SUNKBD_TYPE4) {
1596     		printk("Sun TYPE %d keyboard detected ", sunkbd_type);
1597     	} else {
1598     		timeout=0;
1599     		while((sunkbd_layout==0) && timeout++ < 10000) {
1600     			udelay(100);
1601     			barrier();
1602     		}
1603     		printk("Sun TYPE %d keyboard detected ",
1604     		       ((sunkbd_layout & SUNKBD_LOUT_TYP5_MASK) ? 5 : 4));
1605     	}
1606     	if(sunkbd_type == SUNKBD_TYPE2)
1607     		sunkbd_clickp = 0;
1608     
1609     	spin_lock_irq(&sunkbd_lock);
1610     
1611     	if(sunkbd_clickp) {
1612     		send_cmd(SKBDCMD_CLICK);
1613     		printk("with keyclick\n");
1614     	} else {
1615     		send_cmd(SKBDCMD_NOCLICK);
1616     		printk("without keyclick\n");
1617     	}
1618     
1619     	/* Dork with led lights, then turn them all off */
1620     	send_cmd(SKBDCMD_SETLED); send_cmd(0xf); /* All on */
1621     	send_cmd(SKBDCMD_SETLED); send_cmd(0x0); /* All off */
1622     
1623     	spin_unlock_irq(&sunkbd_lock);
1624     
1625     	/* Register the /dev/kbd interface */
1626     	devfs_register (NULL, "kbd", DEVFS_FL_DEFAULT,
1627     			KBD_MAJOR, 0,
1628     			S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
1629     			&kbd_fops, NULL);
1630     	if (devfs_register_chrdev (KBD_MAJOR, "kbd", &kbd_fops)){
1631     		printk ("Could not register /dev/kbd device\n");
1632     		return;
1633     	}
1634     	return;
1635     }
1636