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

1     /*
2      * linux/drivers/char/pc_keyb.c
3      *
4      * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
5      * See keyboard.c for the whole history.
6      *
7      * Major cleanup by Martin Mares, May 1997
8      *
9      * Combined the keyboard and PS/2 mouse handling into one file,
10      * because they share the same hardware.
11      * Johan Myreen <jem@iki.fi> 1998-10-08.
12      *
13      * Code fixes to handle mouse ACKs properly.
14      * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
15      *
16      */
17     
18     #include <linux/config.h>
19     
20     #include <linux/spinlock.h>
21     #include <linux/sched.h>
22     #include <linux/interrupt.h>
23     #include <linux/tty.h>
24     #include <linux/mm.h>
25     #include <linux/signal.h>
26     #include <linux/init.h>
27     #include <linux/kbd_ll.h>
28     #include <linux/delay.h>
29     #include <linux/random.h>
30     #include <linux/poll.h>
31     #include <linux/miscdevice.h>
32     #include <linux/slab.h>
33     #include <linux/kbd_kern.h>
34     #include <linux/vt_kern.h>
35     #include <linux/smp_lock.h>
36     #include <linux/kd.h>
37     
38     #include <asm/keyboard.h>
39     #include <asm/bitops.h>
40     #include <asm/uaccess.h>
41     #include <asm/irq.h>
42     #include <asm/system.h>
43     
44     #include <asm/io.h>
45     
46     /* Some configuration switches are present in the include file... */
47     
48     #include <linux/pc_keyb.h>
49     
50     /* Simple translation table for the SysRq keys */
51     
52     #ifdef CONFIG_MAGIC_SYSRQ
53     unsigned char pckbd_sysrq_xlate[128] =
54     	"\000\0331234567890-=\177\t"			/* 0x00 - 0x0f */
55     	"qwertyuiop[]\r\000as"				/* 0x10 - 0x1f */
56     	"dfghjkl;'`\000\\zxcv"				/* 0x20 - 0x2f */
57     	"bnm,./\000*\000 \000\201\202\203\204\205"	/* 0x30 - 0x3f */
58     	"\206\207\210\211\212\000\000789-456+1"		/* 0x40 - 0x4f */
59     	"230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
60     	"\r\000/";					/* 0x60 - 0x6f */
61     #endif
62     
63     static void kbd_write_command_w(int data);
64     static void kbd_write_output_w(int data);
65     #ifdef CONFIG_PSMOUSE
66     static void aux_write_ack(int val);
67     static void __aux_write_ack(int val);
68     static int aux_reconnect = 0;
69     #endif
70     
71     static spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
72     static unsigned char handle_kbd_event(void);
73     
74     /* used only by send_data - set by keyboard_interrupt */
75     static volatile unsigned char reply_expected;
76     static volatile unsigned char acknowledge;
77     static volatile unsigned char resend;
78     
79     
80     #if defined CONFIG_PSMOUSE
81     /*
82      *	PS/2 Auxiliary Device
83      */
84     
85     static int __init psaux_init(void);
86     
87     #define AUX_RECONNECT1 0xaa	/* scancode1 when ps2 device is plugged (back) in */
88     #define AUX_RECONNECT2 0x00	/* scancode2 when ps2 device is plugged (back) in */
89      
90     static struct aux_queue *queue;	/* Mouse data buffer. */
91     static int aux_count;
92     /* used when we send commands to the mouse that expect an ACK. */
93     static unsigned char mouse_reply_expected;
94     
95     #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
96     #define AUX_INTS_ON  (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
97     
98     #define MAX_RETRIES	60		/* some aux operations take long time*/
99     #endif /* CONFIG_PSMOUSE */
100     
101     /*
102      * Wait for keyboard controller input buffer to drain.
103      *
104      * Don't use 'jiffies' so that we don't depend on
105      * interrupts..
106      *
107      * Quote from PS/2 System Reference Manual:
108      *
109      * "Address hex 0060 and address hex 0064 should be written only when
110      * the input-buffer-full bit and output-buffer-full bit in the
111      * Controller Status register are set 0."
112      */
113     
114     static void kb_wait(void)
115     {
116     	unsigned long timeout = KBC_TIMEOUT;
117     
118     	do {
119     		/*
120     		 * "handle_kbd_event()" will handle any incoming events
121     		 * while we wait - keypresses or mouse movement.
122     		 */
123     		unsigned char status = handle_kbd_event();
124     
125     		if (! (status & KBD_STAT_IBF))
126     			return;
127     		mdelay(1);
128     		timeout--;
129     	} while (timeout);
130     #ifdef KBD_REPORT_TIMEOUTS
131     	printk(KERN_WARNING "Keyboard timed out[1]\n");
132     #endif
133     }
134     
135     /*
136      * Translation of escaped scancodes to keycodes.
137      * This is now user-settable.
138      * The keycodes 1-88,96-111,119 are fairly standard, and
139      * should probably not be changed - changing might confuse X.
140      * X also interprets scancode 0x5d (KEY_Begin).
141      *
142      * For 1-88 keycode equals scancode.
143      */
144     
145     #define E0_KPENTER 96
146     #define E0_RCTRL   97
147     #define E0_KPSLASH 98
148     #define E0_PRSCR   99
149     #define E0_RALT    100
150     #define E0_BREAK   101  /* (control-pause) */
151     #define E0_HOME    102
152     #define E0_UP      103
153     #define E0_PGUP    104
154     #define E0_LEFT    105
155     #define E0_RIGHT   106
156     #define E0_END     107
157     #define E0_DOWN    108
158     #define E0_PGDN    109
159     #define E0_INS     110
160     #define E0_DEL     111
161     
162     #define E1_PAUSE   119
163     
164     /*
165      * The keycodes below are randomly located in 89-95,112-118,120-127.
166      * They could be thrown away (and all occurrences below replaced by 0),
167      * but that would force many users to use the `setkeycodes' utility, where
168      * they needed not before. It does not matter that there are duplicates, as
169      * long as no duplication occurs for any single keyboard.
170      */
171     #define SC_LIM 89
172     
173     #define FOCUS_PF1 85           /* actual code! */
174     #define FOCUS_PF2 89
175     #define FOCUS_PF3 90
176     #define FOCUS_PF4 91
177     #define FOCUS_PF5 92
178     #define FOCUS_PF6 93
179     #define FOCUS_PF7 94
180     #define FOCUS_PF8 95
181     #define FOCUS_PF9 120
182     #define FOCUS_PF10 121
183     #define FOCUS_PF11 122
184     #define FOCUS_PF12 123
185     
186     #define JAP_86     124
187     /* tfj@olivia.ping.dk:
188      * The four keys are located over the numeric keypad, and are
189      * labelled A1-A4. It's an rc930 keyboard, from
190      * Regnecentralen/RC International, Now ICL.
191      * Scancodes: 59, 5a, 5b, 5c.
192      */
193     #define RGN1 124
194     #define RGN2 125
195     #define RGN3 126
196     #define RGN4 127
197     
198     static unsigned char high_keys[128 - SC_LIM] = {
199       RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
200       0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
201       0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
202       0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
203       FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
204       FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
205     };
206     
207     /* BTC */
208     #define E0_MACRO   112
209     /* LK450 */
210     #define E0_F13     113
211     #define E0_F14     114
212     #define E0_HELP    115
213     #define E0_DO      116
214     #define E0_F17     117
215     #define E0_KPMINPLUS 118
216     /*
217      * My OmniKey generates e0 4c for  the "OMNI" key and the
218      * right alt key does nada. [kkoller@nyx10.cs.du.edu]
219      */
220     #define E0_OK	124
221     /*
222      * New microsoft keyboard is rumoured to have
223      * e0 5b (left window button), e0 5c (right window button),
224      * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
225      * [or: Windows_L, Windows_R, TaskMan]
226      */
227     #define E0_MSLW	125
228     #define E0_MSRW	126
229     #define E0_MSTM	127
230     
231     static unsigned char e0_keys[128] = {
232       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x00-0x07 */
233       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x08-0x0f */
234       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x10-0x17 */
235       0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,	      /* 0x18-0x1f */
236       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x20-0x27 */
237       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x28-0x2f */
238       0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,	      /* 0x30-0x37 */
239       E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,	      /* 0x38-0x3f */
240       E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,	      /* 0x40-0x47 */
241       E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
242       E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,	      /* 0x50-0x57 */
243       0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,	      /* 0x58-0x5f */
244       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x60-0x67 */
245       0, 0, 0, 0, 0, 0, 0, E0_MACRO,		      /* 0x68-0x6f */
246       0, 0, 0, 0, 0, 0, 0, 0,			      /* 0x70-0x77 */
247       0, 0, 0, 0, 0, 0, 0, 0			      /* 0x78-0x7f */
248     };
249     
250     int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
251     {
252     	if (scancode < SC_LIM || scancode > 255 || keycode > 127)
253     	  return -EINVAL;
254     	if (scancode < 128)
255     	  high_keys[scancode - SC_LIM] = keycode;
256     	else
257     	  e0_keys[scancode - 128] = keycode;
258     	return 0;
259     }
260     
261     int pckbd_getkeycode(unsigned int scancode)
262     {
263     	return
264     	  (scancode < SC_LIM || scancode > 255) ? -EINVAL :
265     	  (scancode < 128) ? high_keys[scancode - SC_LIM] :
266     	    e0_keys[scancode - 128];
267     }
268     
269     static int do_acknowledge(unsigned char scancode)
270     {
271     	if (reply_expected) {
272     	  /* Unfortunately, we must recognise these codes only if we know they
273     	   * are known to be valid (i.e., after sending a command), because there
274     	   * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
275     	   * keys with such codes :(
276     	   */
277     		if (scancode == KBD_REPLY_ACK) {
278     			acknowledge = 1;
279     			reply_expected = 0;
280     			return 0;
281     		} else if (scancode == KBD_REPLY_RESEND) {
282     			resend = 1;
283     			reply_expected = 0;
284     			return 0;
285     		}
286     		/* Should not happen... */
287     #if 0
288     		printk(KERN_DEBUG "keyboard reply expected - got %02x\n",
289     		       scancode);
290     #endif
291     	}
292     	return 1;
293     }
294     
295     int pckbd_translate(unsigned char scancode, unsigned char *keycode,
296     		    char raw_mode)
297     {
298     	static int prev_scancode;
299     
300     	/* special prefix scancodes.. */
301     	if (scancode == 0xe0 || scancode == 0xe1) {
302     		prev_scancode = scancode;
303     		return 0;
304     	}
305     
306     	/* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
307     	if (scancode == 0x00 || scancode == 0xff) {
308     		prev_scancode = 0;
309     		return 0;
310     	}
311     
312     	scancode &= 0x7f;
313     
314     	if (prev_scancode) {
315     	  /*
316     	   * usually it will be 0xe0, but a Pause key generates
317     	   * e1 1d 45 e1 9d c5 when pressed, and nothing when released
318     	   */
319     	  if (prev_scancode != 0xe0) {
320     	      if (prev_scancode == 0xe1 && scancode == 0x1d) {
321     		  prev_scancode = 0x100;
322     		  return 0;
323     	      } else if (prev_scancode == 0x100 && scancode == 0x45) {
324     		  *keycode = E1_PAUSE;
325     		  prev_scancode = 0;
326     	      } else {
327     #ifdef KBD_REPORT_UNKN
328     		  if (!raw_mode)
329     		    printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
330     #endif
331     		  prev_scancode = 0;
332     		  return 0;
333     	      }
334     	  } else {
335     	      prev_scancode = 0;
336     	      /*
337     	       *  The keyboard maintains its own internal caps lock and
338     	       *  num lock statuses. In caps lock mode E0 AA precedes make
339     	       *  code and E0 2A follows break code. In num lock mode,
340     	       *  E0 2A precedes make code and E0 AA follows break code.
341     	       *  We do our own book-keeping, so we will just ignore these.
342     	       */
343     	      /*
344     	       *  For my keyboard there is no caps lock mode, but there are
345     	       *  both Shift-L and Shift-R modes. The former mode generates
346     	       *  E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
347     	       *  So, we should also ignore the latter. - aeb@cwi.nl
348     	       */
349     	      if (scancode == 0x2a || scancode == 0x36)
350     		return 0;
351     
352     	      if (e0_keys[scancode])
353     		*keycode = e0_keys[scancode];
354     	      else {
355     #ifdef KBD_REPORT_UNKN
356     		  if (!raw_mode)
357     		    printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
358     			   scancode);
359     #endif
360     		  return 0;
361     	      }
362     	  }
363     	} else if (scancode >= SC_LIM) {
364     	    /* This happens with the FOCUS 9000 keyboard
365     	       Its keys PF1..PF12 are reported to generate
366     	       55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
367     	       Moreover, unless repeated, they do not generate
368     	       key-down events, so we have to zero up_flag below */
369     	    /* Also, Japanese 86/106 keyboards are reported to
370     	       generate 0x73 and 0x7d for \ - and \ | respectively. */
371     	    /* Also, some Brazilian keyboard is reported to produce
372     	       0x73 and 0x7e for \ ? and KP-dot, respectively. */
373     
374     	  *keycode = high_keys[scancode - SC_LIM];
375     
376     	  if (!*keycode) {
377     	      if (!raw_mode) {
378     #ifdef KBD_REPORT_UNKN
379     		  printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
380     			 " - ignored\n", scancode);
381     #endif
382     	      }
383     	      return 0;
384     	  }
385      	} else
386     	  *keycode = scancode;
387      	return 1;
388     }
389     
390     char pckbd_unexpected_up(unsigned char keycode)
391     {
392     	/* unexpected, but this can happen: maybe this was a key release for a
393     	   FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
394     	if (keycode >= SC_LIM || keycode == 85)
395     	    return 0;
396     	else
397     	    return 0200;
398     }
399     
400     void pckbd_pm_resume(void)
401     {
402     #if defined CONFIG_PSMOUSE
403            unsigned long flags;
404     
405            if (queue) {                    /* Aux port detected */
406                    if (aux_count == 0) {   /* Mouse not in use */ 
407                            spin_lock_irqsave(&kbd_controller_lock, flags);
408                            /*
409                             * Dell Lat. C600 A06 enables mouse after resume.
410                             * When user touches the pad, it posts IRQ 12
411                             * (which we do not process), thus holding keyboard.
412                             */
413                            kbd_write_command(KBD_CCMD_MOUSE_DISABLE);
414                            /* kbd_write_cmd(AUX_INTS_OFF); */ /* Config & lock */
415                            kb_wait();
416                            kbd_write_command(KBD_CCMD_WRITE_MODE);
417                            kb_wait();
418                            kbd_write_output(AUX_INTS_OFF);
419                            spin_unlock_irqrestore(&kbd_controller_lock, flags);
420                    }
421            }
422     #endif       
423     }
424     
425     
426     static inline void handle_mouse_event(unsigned char scancode)
427     {
428     #ifdef CONFIG_PSMOUSE
429     	static unsigned char prev_code;
430     	if (mouse_reply_expected) {
431     		if (scancode == AUX_ACK) {
432     			mouse_reply_expected--;
433     			return;
434     		}
435     		mouse_reply_expected = 0;
436     	}
437     	else if(scancode == AUX_RECONNECT2 && prev_code == AUX_RECONNECT1
438     		&& aux_reconnect) {
439     		printk (KERN_INFO "PS/2 mouse reconnect detected\n");
440     		queue->head = queue->tail = 0;	/* Flush input queue */
441     		__aux_write_ack(AUX_ENABLE_DEV);  /* ping the mouse :) */
442     		return;
443     	}
444     
445     	prev_code = scancode;
446     	add_mouse_randomness(scancode);
447     	if (aux_count) {
448     		int head = queue->head;
449     
450     		queue->buf[head] = scancode;
451     		head = (head + 1) & (AUX_BUF_SIZE-1);
452     		if (head != queue->tail) {
453     			queue->head = head;
454     			kill_fasync(&queue->fasync, SIGIO, POLL_IN);
455     			wake_up_interruptible(&queue->proc_list);
456     		}
457     	}
458     #endif
459     }
460     
461     static unsigned char kbd_exists = 1;
462     
463     static inline void handle_keyboard_event(unsigned char scancode)
464     {
465     #ifdef CONFIG_VT
466     	kbd_exists = 1;
467     	if (do_acknowledge(scancode))
468     		handle_scancode(scancode, !(scancode & 0x80));
469     #endif				
470     	tasklet_schedule(&keyboard_tasklet);
471     }	
472     
473     /*
474      * This reads the keyboard status port, and does the
475      * appropriate action.
476      *
477      * It requires that we hold the keyboard controller
478      * spinlock.
479      */
480     static unsigned char handle_kbd_event(void)
481     {
482     	unsigned char status = kbd_read_status();
483     	unsigned int work = 10000;
484     
485     	while ((--work > 0) && (status & KBD_STAT_OBF)) {
486     		unsigned char scancode;
487     
488     		scancode = kbd_read_input();
489     
490     		/* Error bytes must be ignored to make the 
491     		   Synaptics touchpads compaq use work */
492     #if 1
493     		/* Ignore error bytes */
494     		if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
495     #endif
496     		{
497     			if (status & KBD_STAT_MOUSE_OBF)
498     				handle_mouse_event(scancode);
499     			else
500     				handle_keyboard_event(scancode);
501     		}
502     
503     		status = kbd_read_status();
504     	}
505     		
506     	if (!work)
507     		printk(KERN_ERR "pc_keyb: controller jammed (0x%02X).\n", status);
508     
509     	return status;
510     }
511     
512     
513     static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
514     {
515     #ifdef CONFIG_VT
516     	kbd_pt_regs = regs;
517     #endif
518     
519     	spin_lock_irq(&kbd_controller_lock);
520     	handle_kbd_event();
521     	spin_unlock_irq(&kbd_controller_lock);
522     }
523     
524     /*
525      * send_data sends a character to the keyboard and waits
526      * for an acknowledge, possibly retrying if asked to. Returns
527      * the success status.
528      *
529      * Don't use 'jiffies', so that we don't depend on interrupts
530      */
531     static int send_data(unsigned char data)
532     {
533     	int retries = 3;
534     
535     	do {
536     		unsigned long timeout = KBD_TIMEOUT;
537     
538     		acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
539     		resend = 0;
540     		reply_expected = 1;
541     		kbd_write_output_w(data);
542     		for (;;) {
543     			if (acknowledge)
544     				return 1;
545     			if (resend)
546     				break;
547     			mdelay(1);
548     			if (!--timeout) {
549     #ifdef KBD_REPORT_TIMEOUTS
550     				printk(KERN_WARNING "keyboard: Timeout - AT keyboard not present?(%02x)\n", data);
551     #endif
552     				return 0;
553     			}
554     		}
555     	} while (retries-- > 0);
556     #ifdef KBD_REPORT_TIMEOUTS
557     	printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?\n");
558     #endif
559     	return 0;
560     }
561     
562     void pckbd_leds(unsigned char leds)
563     {
564     	if (kbd_exists && (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))) {
565     		send_data(KBD_CMD_ENABLE);	/* re-enable kbd if any errors */
566     		kbd_exists = 0;
567     	}
568     }
569     
570     #define DEFAULT_KEYB_REP_DELAY	250
571     #define DEFAULT_KEYB_REP_RATE	30	/* cps */
572     
573     static struct kbd_repeat kbdrate={
574     	DEFAULT_KEYB_REP_DELAY,
575     	DEFAULT_KEYB_REP_RATE
576     };
577     
578     static unsigned char parse_kbd_rate(struct kbd_repeat *r)
579     {
580     	static struct r2v{
581     		int rate;
582     		unsigned char val;
583     	} kbd_rates[]={	{5,0x14},
584     			{7,0x10},
585     			{10,0x0c},
586     			{15,0x08},
587     			{20,0x04},
588     			{25,0x02},
589     			{30,0x00}
590     	};
591     	static struct d2v{
592     		int delay;
593     		unsigned char val;
594     	} kbd_delays[]={{250,0},
595     			{500,1},
596     			{750,2},
597     			{1000,3}
598     	};
599     	int rate=0,delay=0;
600     	if (r != NULL){
601     		int i,new_rate=30,new_delay=250;
602     		if (r->rate <= 0)
603     			r->rate=kbdrate.rate;
604     		if (r->delay <= 0)
605     			r->delay=kbdrate.delay;
606     		for (i=0; i < sizeof(kbd_rates)/sizeof(struct r2v); i++)
607     			if (kbd_rates[i].rate == r->rate){
608     				new_rate=kbd_rates[i].rate;
609     				rate=kbd_rates[i].val;
610     				break;
611     			}
612     		for (i=0; i < sizeof(kbd_delays)/sizeof(struct d2v); i++)
613     			if (kbd_delays[i].delay == r->delay){
614     				new_delay=kbd_delays[i].delay;
615     				delay=kbd_delays[i].val;
616     				break;
617     			}
618     		r->rate=new_rate;
619     		r->delay=new_delay;
620     	}
621     	return (delay << 5) | rate;
622     }
623     
624     static int write_kbd_rate(unsigned char r)
625     {
626     	if (!send_data(KBD_CMD_SET_RATE) || !send_data(r)){
627     		send_data(KBD_CMD_ENABLE); 	/* re-enable kbd if any errors */
628     		return 0;
629     	}else
630     		return 1;
631     }
632     
633     static int pckbd_rate(struct kbd_repeat *rep)
634     {
635     	if (rep == NULL)
636     		return -EINVAL;
637     	else{
638     		unsigned char r=parse_kbd_rate(rep);
639     		struct kbd_repeat old_rep;
640     		memcpy(&old_rep,&kbdrate,sizeof(struct kbd_repeat));
641     		if (write_kbd_rate(r)){
642     			memcpy(&kbdrate,rep,sizeof(struct kbd_repeat));
643     			memcpy(rep,&old_rep,sizeof(struct kbd_repeat));
644     			return 0;
645     		}
646     	}
647     	return -EIO;
648     }
649     
650     /*
651      * In case we run on a non-x86 hardware we need to initialize both the
652      * keyboard controller and the keyboard.  On a x86, the BIOS will
653      * already have initialized them.
654      *
655      * Some x86 BIOSes do not correctly initialize the keyboard, so the
656      * "kbd-reset" command line options can be given to force a reset.
657      * [Ranger]
658      */
659     #ifdef __i386__
660      int kbd_startup_reset __initdata = 0;
661     #else
662      int kbd_startup_reset __initdata = 1;
663     #endif
664     
665     /* for "kbd-reset" cmdline param */
666     static int __init kbd_reset_setup(char *str)
667     {
668     	kbd_startup_reset = 1;
669     	return 1;
670     }
671     
672     __setup("kbd-reset", kbd_reset_setup);
673     
674     #define KBD_NO_DATA	(-1)	/* No data */
675     #define KBD_BAD_DATA	(-2)	/* Parity or other error */
676     
677     static int __init kbd_read_data(void)
678     {
679     	int retval = KBD_NO_DATA;
680     	unsigned char status;
681     
682     	status = kbd_read_status();
683     	if (status & KBD_STAT_OBF) {
684     		unsigned char data = kbd_read_input();
685     
686     		retval = data;
687     		if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
688     			retval = KBD_BAD_DATA;
689     	}
690     	return retval;
691     }
692     
693     static void __init kbd_clear_input(void)
694     {
695     	int maxread = 100;	/* Random number */
696     
697     	do {
698     		if (kbd_read_data() == KBD_NO_DATA)
699     			break;
700     	} while (--maxread);
701     }
702     
703     static int __init kbd_wait_for_input(void)
704     {
705     	long timeout = KBD_INIT_TIMEOUT;
706     
707     	do {
708     		int retval = kbd_read_data();
709     		if (retval >= 0)
710     			return retval;
711     		mdelay(1);
712     	} while (--timeout);
713     	return -1;
714     }
715     
716     static void kbd_write_command_w(int data)
717     {
718     	unsigned long flags;
719     
720     	spin_lock_irqsave(&kbd_controller_lock, flags);
721     	kb_wait();
722     	kbd_write_command(data);
723     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
724     }
725     
726     static void kbd_write_output_w(int data)
727     {
728     	unsigned long flags;
729     
730     	spin_lock_irqsave(&kbd_controller_lock, flags);
731     	kb_wait();
732     	kbd_write_output(data);
733     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
734     }
735     
736     #if defined(__alpha__)
737     /*
738      * Some Alphas cannot mask some/all interrupts, so we have to
739      * make sure not to allow interrupts AT ALL when polling for
740      * specific return values from the keyboard.
741      *
742      * I think this should work on any architecture, but for now, only Alpha.
743      */
744     static int kbd_write_command_w_and_wait(int data)
745     {
746     	unsigned long flags;
747     	int input;
748     
749     	spin_lock_irqsave(&kbd_controller_lock, flags);
750     	kb_wait();
751     	kbd_write_command(data);
752     	input = kbd_wait_for_input();
753     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
754     	return input;
755     }
756     
757     static int kbd_write_output_w_and_wait(int data)
758     {
759     	unsigned long flags;
760     	int input;
761     
762     	spin_lock_irqsave(&kbd_controller_lock, flags);
763     	kb_wait();
764     	kbd_write_output(data);
765     	input = kbd_wait_for_input();
766     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
767     	return input;
768     }
769     #else
770     static int kbd_write_command_w_and_wait(int data)
771     {
772     	kbd_write_command_w(data);
773     	return kbd_wait_for_input();
774     }
775     
776     static int kbd_write_output_w_and_wait(int data)
777     {
778     	kbd_write_output_w(data);
779     	return kbd_wait_for_input();
780     }
781     #endif /* __alpha__ */
782     
783     #if defined CONFIG_PSMOUSE
784     static void kbd_write_cmd(int cmd)
785     {
786     	unsigned long flags;
787     
788     	spin_lock_irqsave(&kbd_controller_lock, flags);
789     	kb_wait();
790     	kbd_write_command(KBD_CCMD_WRITE_MODE);
791     	kb_wait();
792     	kbd_write_output(cmd);
793     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
794     }
795     #endif /* CONFIG_PSMOUSE */
796     
797     static char * __init initialize_kbd(void)
798     {
799     	int status;
800     
801     	/*
802     	 * Test the keyboard interface.
803     	 * This seems to be the only way to get it going.
804     	 * If the test is successful a x55 is placed in the input buffer.
805     	 */
806     	kbd_write_command_w(KBD_CCMD_SELF_TEST);
807     	if (kbd_wait_for_input() != 0x55)
808     		return "Keyboard failed self test";
809     
810     	/*
811     	 * Perform a keyboard interface test.  This causes the controller
812     	 * to test the keyboard clock and data lines.  The results of the
813     	 * test are placed in the input buffer.
814     	 */
815     	kbd_write_command_w(KBD_CCMD_KBD_TEST);
816     	if (kbd_wait_for_input() != 0x00)
817     		return "Keyboard interface failed self test";
818     
819     	/*
820     	 * Enable the keyboard by allowing the keyboard clock to run.
821     	 */
822     	kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
823     
824     	/*
825     	 * Reset keyboard. If the read times out
826     	 * then the assumption is that no keyboard is
827     	 * plugged into the machine.
828     	 * This defaults the keyboard to scan-code set 2.
829     	 *
830     	 * Set up to try again if the keyboard asks for RESEND.
831     	 */
832     	do {
833     		kbd_write_output_w(KBD_CMD_RESET);
834     		status = kbd_wait_for_input();
835     		if (status == KBD_REPLY_ACK)
836     			break;
837     		if (status != KBD_REPLY_RESEND)
838     			return "Keyboard reset failed, no ACK";
839     	} while (1);
840     
841     	if (kbd_wait_for_input() != KBD_REPLY_POR)
842     		return "Keyboard reset failed, no POR";
843     
844     	/*
845     	 * Set keyboard controller mode. During this, the keyboard should be
846     	 * in the disabled state.
847     	 *
848     	 * Set up to try again if the keyboard asks for RESEND.
849     	 */
850     	do {
851     		kbd_write_output_w(KBD_CMD_DISABLE);
852     		status = kbd_wait_for_input();
853     		if (status == KBD_REPLY_ACK)
854     			break;
855     		if (status != KBD_REPLY_RESEND)
856     			return "Disable keyboard: no ACK";
857     	} while (1);
858     
859     	kbd_write_command_w(KBD_CCMD_WRITE_MODE);
860     	kbd_write_output_w(KBD_MODE_KBD_INT
861     			      | KBD_MODE_SYS
862     			      | KBD_MODE_DISABLE_MOUSE
863     			      | KBD_MODE_KCC);
864     
865     	/* ibm powerpc portables need this to use scan-code set 1 -- Cort */
866     	if (!(kbd_write_command_w_and_wait(KBD_CCMD_READ_MODE) & KBD_MODE_KCC))
867     	{
868     		/*
869     		 * If the controller does not support conversion,
870     		 * Set the keyboard to scan-code set 1.
871     		 */
872     		kbd_write_output_w(0xF0);
873     		kbd_wait_for_input();
874     		kbd_write_output_w(0x01);
875     		kbd_wait_for_input();
876     	}
877     
878     	if (kbd_write_output_w_and_wait(KBD_CMD_ENABLE) != KBD_REPLY_ACK)
879     		return "Enable keyboard: no ACK";
880     
881     	/*
882     	 * Finally, set the typematic rate to maximum.
883     	 */
884     	if (kbd_write_output_w_and_wait(KBD_CMD_SET_RATE) != KBD_REPLY_ACK)
885     		return "Set rate: no ACK";
886     	if (kbd_write_output_w_and_wait(0x00) != KBD_REPLY_ACK)
887     		return "Set rate: no 2nd ACK";
888     
889     	return NULL;
890     }
891     
892     void __init pckbd_init_hw(void)
893     {
894     	kbd_request_region();
895     
896     	/* Flush any pending input. */
897     	kbd_clear_input();
898     
899     	if (kbd_startup_reset) {
900     		char *msg = initialize_kbd();
901     		if (msg)
902     			printk(KERN_WARNING "initialize_kbd: %s\n", msg);
903     	}
904     
905     #if defined CONFIG_PSMOUSE
906     	psaux_init();
907     #endif
908     
909     	kbd_rate = pckbd_rate;
910     
911     	/* Ok, finally allocate the IRQ, and off we go.. */
912     	kbd_request_irq(keyboard_interrupt);
913     }
914     
915     #if defined CONFIG_PSMOUSE
916     
917     static int __init aux_reconnect_setup (char *str)
918     {
919     	aux_reconnect = 1;
920     	return 1;
921     }
922     
923     __setup("psaux-reconnect", aux_reconnect_setup);
924     
925     /*
926      * Check if this is a dual port controller.
927      */
928     static int __init detect_auxiliary_port(void)
929     {
930     	unsigned long flags;
931     	int loops = 10;
932     	int retval = 0;
933     
934     	/* Check if the BIOS detected a device on the auxiliary port. */
935     	if (aux_device_present == 0xaa)
936     		return 1;
937     
938     	spin_lock_irqsave(&kbd_controller_lock, flags);
939     
940     	/* Put the value 0x5A in the output buffer using the "Write
941     	 * Auxiliary Device Output Buffer" command (0xD3). Poll the
942     	 * Status Register for a while to see if the value really
943     	 * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
944     	 * bit is also set to 1 in the Status Register, we assume this
945     	 * controller has an Auxiliary Port (a.k.a. Mouse Port).
946     	 */
947     	kb_wait();
948     	kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
949     
950     	kb_wait();
951     	kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
952     
953     	do {
954     		unsigned char status = kbd_read_status();
955     
956     		if (status & KBD_STAT_OBF) {
957     			(void) kbd_read_input();
958     			if (status & KBD_STAT_MOUSE_OBF) {
959     				printk(KERN_INFO "Detected PS/2 Mouse Port.\n");
960     				retval = 1;
961     			}
962     			break;
963     		}
964     		mdelay(1);
965     	} while (--loops);
966     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
967     
968     	return retval;
969     }
970     
971     /*
972      * Send a byte to the mouse.
973      */
974     static void aux_write_dev(int val)
975     {
976     	unsigned long flags;
977     
978     	spin_lock_irqsave(&kbd_controller_lock, flags);
979     	kb_wait();
980     	kbd_write_command(KBD_CCMD_WRITE_MOUSE);
981     	kb_wait();
982     	kbd_write_output(val);
983     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
984     }
985     
986     /*
987      * Send a byte to the mouse & handle returned ack
988      */
989     static void __aux_write_ack(int val)
990     {
991     	kb_wait();
992     	kbd_write_command(KBD_CCMD_WRITE_MOUSE);
993     	kb_wait();
994     	kbd_write_output(val);
995     	/* we expect an ACK in response. */
996     	mouse_reply_expected++;
997     	kb_wait();
998     }
999     
1000     static void aux_write_ack(int val)
1001     {
1002     	unsigned long flags;
1003     
1004     	spin_lock_irqsave(&kbd_controller_lock, flags);
1005     	__aux_write_ack(val);
1006     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
1007     }
1008     
1009     static unsigned char get_from_queue(void)
1010     {
1011     	unsigned char result;
1012     	unsigned long flags;
1013     
1014     	spin_lock_irqsave(&kbd_controller_lock, flags);
1015     	result = queue->buf[queue->tail];
1016     	queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
1017     	spin_unlock_irqrestore(&kbd_controller_lock, flags);
1018     	return result;
1019     }
1020     
1021     
1022     static inline int queue_empty(void)
1023     {
1024     	return queue->head == queue->tail;
1025     }
1026     
1027     static int fasync_aux(int fd, struct file *filp, int on)
1028     {
1029     	int retval;
1030     
1031     	retval = fasync_helper(fd, filp, on, &queue->fasync);
1032     	if (retval < 0)
1033     		return retval;
1034     	return 0;
1035     }
1036     
1037     
1038     /*
1039      * Random magic cookie for the aux device
1040      */
1041     #define AUX_DEV ((void *)queue)
1042     
1043     static int release_aux(struct inode * inode, struct file * file)
1044     {
1045     	lock_kernel();
1046     	fasync_aux(-1, file, 0);
1047     	if (--aux_count) {
1048     		unlock_kernel();
1049     		return 0;
1050     	}
1051     	kbd_write_cmd(AUX_INTS_OFF);			    /* Disable controller ints */
1052     	kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
1053     	aux_free_irq(AUX_DEV);
1054     	unlock_kernel();
1055     	return 0;
1056     }
1057     
1058     /*
1059      * Install interrupt handler.
1060      * Enable auxiliary device.
1061      */
1062     
1063     static int open_aux(struct inode * inode, struct file * file)
1064     {
1065     	if (aux_count++) {
1066     		return 0;
1067     	}
1068     	queue->head = queue->tail = 0;		/* Flush input queue */
1069     	if (aux_request_irq(keyboard_interrupt, AUX_DEV)) {
1070     		aux_count--;
1071     		return -EBUSY;
1072     	}
1073     	kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE);	/* Enable the
1074     							   auxiliary port on
1075     							   controller. */
1076     	aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
1077     	kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
1078     	
1079     	mdelay(2);			/* Ensure we follow the kbc access delay rules.. */
1080     
1081     	send_data(KBD_CMD_ENABLE);	/* try to workaround toshiba4030cdt problem */
1082     
1083     	return 0;
1084     }
1085     
1086     /*
1087      * Put bytes from input queue to buffer.
1088      */
1089     
1090     static ssize_t read_aux(struct file * file, char * buffer,
1091     			size_t count, loff_t *ppos)
1092     {
1093     	DECLARE_WAITQUEUE(wait, current);
1094     	ssize_t i = count;
1095     	unsigned char c;
1096     
1097     	if (queue_empty()) {
1098     		if (file->f_flags & O_NONBLOCK)
1099     			return -EAGAIN;
1100     		add_wait_queue(&queue->proc_list, &wait);
1101     repeat:
1102     		set_current_state(TASK_INTERRUPTIBLE);
1103     		if (queue_empty() && !signal_pending(current)) {
1104     			schedule();
1105     			goto repeat;
1106     		}
1107     		current->state = TASK_RUNNING;
1108     		remove_wait_queue(&queue->proc_list, &wait);
1109     	}
1110     	while (i > 0 && !queue_empty()) {
1111     		c = get_from_queue();
1112     		put_user(c, buffer++);
1113     		i--;
1114     	}
1115     	if (count-i) {
1116     		file->f_dentry->d_inode->i_atime = CURRENT_TIME;
1117     		return count-i;
1118     	}
1119     	if (signal_pending(current))
1120     		return -ERESTARTSYS;
1121     	return 0;
1122     }
1123     
1124     /*
1125      * Write to the aux device.
1126      */
1127     
1128     static ssize_t write_aux(struct file * file, const char * buffer,
1129     			 size_t count, loff_t *ppos)
1130     {
1131     	ssize_t retval = 0;
1132     
1133     	if (count) {
1134     		ssize_t written = 0;
1135     
1136     		if (count > 32)
1137     			count = 32; /* Limit to 32 bytes. */
1138     		do {
1139     			char c;
1140     			get_user(c, buffer++);
1141     			aux_write_dev(c);
1142     			written++;
1143     		} while (--count);
1144     		retval = -EIO;
1145     		if (written) {
1146     			retval = written;
1147     			file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1148     		}
1149     	}
1150     
1151     	return retval;
1152     }
1153     
1154     /* No kernel lock held - fine */
1155     static unsigned int aux_poll(struct file *file, poll_table * wait)
1156     {
1157     	poll_wait(file, &queue->proc_list, wait);
1158     	if (!queue_empty())
1159     		return POLLIN | POLLRDNORM;
1160     	return 0;
1161     }
1162     
1163     struct file_operations psaux_fops = {
1164     	read:		read_aux,
1165     	write:		write_aux,
1166     	poll:		aux_poll,
1167     	open:		open_aux,
1168     	release:	release_aux,
1169     	fasync:		fasync_aux,
1170     };
1171     
1172     /*
1173      * Initialize driver.
1174      */
1175     static struct miscdevice psaux_mouse = {
1176     	PSMOUSE_MINOR, "psaux", &psaux_fops
1177     };
1178     
1179     static int __init psaux_init(void)
1180     {
1181     	int retval;
1182     
1183     	if (!detect_auxiliary_port())
1184     		return -EIO;
1185     
1186     	if ((retval = misc_register(&psaux_mouse)))
1187     		return retval;
1188     
1189     	queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
1190     	if (queue == NULL) {
1191     		printk(KERN_ERR "psaux_init(): out of memory\n");
1192     		misc_deregister(&psaux_mouse);
1193     		return -ENOMEM;
1194     	}
1195     	memset(queue, 0, sizeof(*queue));
1196     	queue->head = queue->tail = 0;
1197     	init_waitqueue_head(&queue->proc_list);
1198     
1199     #ifdef INITIALIZE_MOUSE
1200     	kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
1201     	aux_write_ack(AUX_SET_SAMPLE);
1202     	aux_write_ack(100);			/* 100 samples/sec */
1203     	aux_write_ack(AUX_SET_RES);
1204     	aux_write_ack(3);			/* 8 counts per mm */
1205     	aux_write_ack(AUX_SET_SCALE21);		/* 2:1 scaling */
1206     #endif /* INITIALIZE_MOUSE */
1207     	kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
1208     	kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
1209     
1210     	return 0;
1211     }
1212     
1213     #endif /* CONFIG_PSMOUSE */
1214