Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / pc_keyb.c
blobc265b927f4820afc509613fec89334ee45cc50d7
1 /*
2 * linux/drivers/char/pc_keyb.c
4 * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
5 * See keyboard.c for the whole history.
7 * Major cleanup by Martin Mares, May 1997
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.
13 * Code fixes to handle mouse ACKs properly.
14 * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
18 #include <linux/config.h>
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/malloc.h>
33 #include <linux/kbd_kern.h>
35 #include <asm/keyboard.h>
36 #include <asm/bitops.h>
37 #include <asm/uaccess.h>
38 #include <asm/irq.h>
39 #include <asm/system.h>
41 #include <asm/io.h>
43 /* Some configuration switches are present in the include file... */
45 #include <linux/pc_keyb.h>
47 /* Simple translation table for the SysRq keys */
49 #ifdef CONFIG_MAGIC_SYSRQ
50 unsigned char pckbd_sysrq_xlate[128] =
51 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
52 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
53 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
54 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
55 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
56 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
57 "\r\000/"; /* 0x60 - 0x6f */
58 #endif
60 static void kbd_write_command_w(int data);
61 static void kbd_write_output_w(int data);
62 #ifdef CONFIG_PSMOUSE
63 static void aux_write_ack(int val);
64 #endif
66 spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
67 static unsigned char handle_kbd_event(void);
69 /* used only by send_data - set by keyboard_interrupt */
70 static volatile unsigned char reply_expected = 0;
71 static volatile unsigned char acknowledge = 0;
72 static volatile unsigned char resend = 0;
75 #if defined CONFIG_PSMOUSE
77 * PS/2 Auxiliary Device
80 static int __init psaux_init(void);
82 #define AUX_RECONNECT 170 /* scancode when ps2 device is plugged (back) in */
84 static struct aux_queue *queue; /* Mouse data buffer. */
85 static int aux_count = 0;
86 /* used when we send commands to the mouse that expect an ACK. */
87 static unsigned char mouse_reply_expected = 0;
89 #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
90 #define AUX_INTS_ON (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
92 #define MAX_RETRIES 60 /* some aux operations take long time*/
93 #endif /* CONFIG_PSMOUSE */
96 * Wait for keyboard controller input buffer to drain.
98 * Don't use 'jiffies' so that we don't depend on
99 * interrupts..
101 * Quote from PS/2 System Reference Manual:
103 * "Address hex 0060 and address hex 0064 should be written only when
104 * the input-buffer-full bit and output-buffer-full bit in the
105 * Controller Status register are set 0."
108 static void kb_wait(void)
110 unsigned long timeout = KBC_TIMEOUT;
112 do {
114 * "handle_kbd_event()" will handle any incoming events
115 * while we wait - keypresses or mouse movement.
117 unsigned char status = handle_kbd_event();
119 if (! (status & KBD_STAT_IBF))
120 return;
121 mdelay(1);
122 timeout--;
123 } while (timeout);
124 #ifdef KBD_REPORT_TIMEOUTS
125 printk(KERN_WARNING "Keyboard timed out[1]\n");
126 #endif
130 * Translation of escaped scancodes to keycodes.
131 * This is now user-settable.
132 * The keycodes 1-88,96-111,119 are fairly standard, and
133 * should probably not be changed - changing might confuse X.
134 * X also interprets scancode 0x5d (KEY_Begin).
136 * For 1-88 keycode equals scancode.
139 #define E0_KPENTER 96
140 #define E0_RCTRL 97
141 #define E0_KPSLASH 98
142 #define E0_PRSCR 99
143 #define E0_RALT 100
144 #define E0_BREAK 101 /* (control-pause) */
145 #define E0_HOME 102
146 #define E0_UP 103
147 #define E0_PGUP 104
148 #define E0_LEFT 105
149 #define E0_RIGHT 106
150 #define E0_END 107
151 #define E0_DOWN 108
152 #define E0_PGDN 109
153 #define E0_INS 110
154 #define E0_DEL 111
156 #define E1_PAUSE 119
159 * The keycodes below are randomly located in 89-95,112-118,120-127.
160 * They could be thrown away (and all occurrences below replaced by 0),
161 * but that would force many users to use the `setkeycodes' utility, where
162 * they needed not before. It does not matter that there are duplicates, as
163 * long as no duplication occurs for any single keyboard.
165 #define SC_LIM 89
167 #define FOCUS_PF1 85 /* actual code! */
168 #define FOCUS_PF2 89
169 #define FOCUS_PF3 90
170 #define FOCUS_PF4 91
171 #define FOCUS_PF5 92
172 #define FOCUS_PF6 93
173 #define FOCUS_PF7 94
174 #define FOCUS_PF8 95
175 #define FOCUS_PF9 120
176 #define FOCUS_PF10 121
177 #define FOCUS_PF11 122
178 #define FOCUS_PF12 123
180 #define JAP_86 124
181 /* tfj@olivia.ping.dk:
182 * The four keys are located over the numeric keypad, and are
183 * labelled A1-A4. It's an rc930 keyboard, from
184 * Regnecentralen/RC International, Now ICL.
185 * Scancodes: 59, 5a, 5b, 5c.
187 #define RGN1 124
188 #define RGN2 125
189 #define RGN3 126
190 #define RGN4 127
192 static unsigned char high_keys[128 - SC_LIM] = {
193 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
194 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
195 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
196 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
197 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
198 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
201 /* BTC */
202 #define E0_MACRO 112
203 /* LK450 */
204 #define E0_F13 113
205 #define E0_F14 114
206 #define E0_HELP 115
207 #define E0_DO 116
208 #define E0_F17 117
209 #define E0_KPMINPLUS 118
211 * My OmniKey generates e0 4c for the "OMNI" key and the
212 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
214 #define E0_OK 124
216 * New microsoft keyboard is rumoured to have
217 * e0 5b (left window button), e0 5c (right window button),
218 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
219 * [or: Windows_L, Windows_R, TaskMan]
221 #define E0_MSLW 125
222 #define E0_MSRW 126
223 #define E0_MSTM 127
225 static unsigned char e0_keys[128] = {
226 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
227 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
228 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
229 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
230 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
231 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
232 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
233 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
234 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
235 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
236 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
237 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
238 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
239 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
240 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
241 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
244 int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
246 if (scancode < SC_LIM || scancode > 255 || keycode > 127)
247 return -EINVAL;
248 if (scancode < 128)
249 high_keys[scancode - SC_LIM] = keycode;
250 else
251 e0_keys[scancode - 128] = keycode;
252 return 0;
255 int pckbd_getkeycode(unsigned int scancode)
257 return
258 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
259 (scancode < 128) ? high_keys[scancode - SC_LIM] :
260 e0_keys[scancode - 128];
263 static int do_acknowledge(unsigned char scancode)
265 if (reply_expected) {
266 /* Unfortunately, we must recognise these codes only if we know they
267 * are known to be valid (i.e., after sending a command), because there
268 * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
269 * keys with such codes :(
271 if (scancode == KBD_REPLY_ACK) {
272 acknowledge = 1;
273 reply_expected = 0;
274 return 0;
275 } else if (scancode == KBD_REPLY_RESEND) {
276 resend = 1;
277 reply_expected = 0;
278 return 0;
280 /* Should not happen... */
281 #if 0
282 printk(KERN_DEBUG "keyboard reply expected - got %02x\n",
283 scancode);
284 #endif
286 return 1;
289 int pckbd_translate(unsigned char scancode, unsigned char *keycode,
290 char raw_mode)
292 static int prev_scancode = 0;
294 /* special prefix scancodes.. */
295 if (scancode == 0xe0 || scancode == 0xe1) {
296 prev_scancode = scancode;
297 return 0;
300 /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
301 if (scancode == 0x00 || scancode == 0xff) {
302 prev_scancode = 0;
303 return 0;
306 scancode &= 0x7f;
308 if (prev_scancode) {
310 * usually it will be 0xe0, but a Pause key generates
311 * e1 1d 45 e1 9d c5 when pressed, and nothing when released
313 if (prev_scancode != 0xe0) {
314 if (prev_scancode == 0xe1 && scancode == 0x1d) {
315 prev_scancode = 0x100;
316 return 0;
317 } else if (prev_scancode == 0x100 && scancode == 0x45) {
318 *keycode = E1_PAUSE;
319 prev_scancode = 0;
320 } else {
321 #ifdef KBD_REPORT_UNKN
322 if (!raw_mode)
323 printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
324 #endif
325 prev_scancode = 0;
326 return 0;
328 } else {
329 prev_scancode = 0;
331 * The keyboard maintains its own internal caps lock and
332 * num lock statuses. In caps lock mode E0 AA precedes make
333 * code and E0 2A follows break code. In num lock mode,
334 * E0 2A precedes make code and E0 AA follows break code.
335 * We do our own book-keeping, so we will just ignore these.
338 * For my keyboard there is no caps lock mode, but there are
339 * both Shift-L and Shift-R modes. The former mode generates
340 * E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
341 * So, we should also ignore the latter. - aeb@cwi.nl
343 if (scancode == 0x2a || scancode == 0x36)
344 return 0;
346 if (e0_keys[scancode])
347 *keycode = e0_keys[scancode];
348 else {
349 #ifdef KBD_REPORT_UNKN
350 if (!raw_mode)
351 printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
352 scancode);
353 #endif
354 return 0;
357 } else if (scancode >= SC_LIM) {
358 /* This happens with the FOCUS 9000 keyboard
359 Its keys PF1..PF12 are reported to generate
360 55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
361 Moreover, unless repeated, they do not generate
362 key-down events, so we have to zero up_flag below */
363 /* Also, Japanese 86/106 keyboards are reported to
364 generate 0x73 and 0x7d for \ - and \ | respectively. */
365 /* Also, some Brazilian keyboard is reported to produce
366 0x73 and 0x7e for \ ? and KP-dot, respectively. */
368 *keycode = high_keys[scancode - SC_LIM];
370 if (!*keycode) {
371 if (!raw_mode) {
372 #ifdef KBD_REPORT_UNKN
373 printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
374 " - ignored\n", scancode);
375 #endif
377 return 0;
379 } else
380 *keycode = scancode;
381 return 1;
384 char pckbd_unexpected_up(unsigned char keycode)
386 /* unexpected, but this can happen: maybe this was a key release for a
387 FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
388 if (keycode >= SC_LIM || keycode == 85)
389 return 0;
390 else
391 return 0200;
394 static inline void handle_mouse_event(unsigned char scancode)
396 #ifdef CONFIG_PSMOUSE
397 if (mouse_reply_expected) {
398 if (scancode == AUX_ACK) {
399 mouse_reply_expected--;
400 return;
402 mouse_reply_expected = 0;
404 else if(scancode == AUX_RECONNECT){
405 queue->head = queue->tail = 0; /* Flush input queue */
406 aux_write_ack(AUX_ENABLE_DEV); /* ping the mouse :) */
407 return;
410 add_mouse_randomness(scancode);
411 if (aux_count) {
412 int head = queue->head;
414 queue->buf[head] = scancode;
415 head = (head + 1) & (AUX_BUF_SIZE-1);
416 if (head != queue->tail) {
417 queue->head = head;
418 kill_fasync(&queue->fasync, SIGIO, POLL_IN);
419 wake_up_interruptible(&queue->proc_list);
422 #endif
425 static unsigned char kbd_exists = 1;
427 static inline void handle_keyboard_event(unsigned char scancode)
429 #ifdef CONFIG_VT
430 kbd_exists = 1;
431 if (do_acknowledge(scancode))
432 handle_scancode(scancode, !(scancode & 0x80));
433 #endif
434 tasklet_schedule(&keyboard_tasklet);
438 * This reads the keyboard status port, and does the
439 * appropriate action.
441 * It requires that we hold the keyboard controller
442 * spinlock.
444 static unsigned char handle_kbd_event(void)
446 unsigned char status = kbd_read_status();
447 unsigned int work = 10000;
449 while (status & KBD_STAT_OBF) {
450 unsigned char scancode;
452 scancode = kbd_read_input();
454 /* Error bytes must be ignored to make the
455 Synaptics touchpads compaq use work */
456 #if 1
457 /* Ignore error bytes */
458 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
459 #endif
461 if (status & KBD_STAT_MOUSE_OBF)
462 handle_mouse_event(scancode);
463 else
464 handle_keyboard_event(scancode);
467 status = kbd_read_status();
469 if (!--work) {
470 printk(KERN_ERR "pc_keyb: controller jammed (0x%02X).\n",
471 status);
472 break;
476 return status;
480 static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
482 unsigned long flags;
484 #ifdef CONFIG_VT
485 kbd_pt_regs = regs;
486 #endif
487 spin_lock_irqsave(&kbd_controller_lock, flags);
488 handle_kbd_event();
489 spin_unlock_irqrestore(&kbd_controller_lock, flags);
493 * send_data sends a character to the keyboard and waits
494 * for an acknowledge, possibly retrying if asked to. Returns
495 * the success status.
497 * Don't use 'jiffies', so that we don't depend on interrupts
499 static int send_data(unsigned char data)
501 int retries = 3;
503 do {
504 unsigned long timeout = KBD_TIMEOUT;
506 acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
507 resend = 0;
508 reply_expected = 1;
509 kbd_write_output_w(data);
510 for (;;) {
511 if (acknowledge)
512 return 1;
513 if (resend)
514 break;
515 mdelay(1);
516 if (!--timeout) {
517 #ifdef KBD_REPORT_TIMEOUTS
518 printk(KERN_WARNING "keyboard: Timeout - AT keyboard not present?\n");
519 #endif
520 return 0;
523 } while (retries-- > 0);
524 #ifdef KBD_REPORT_TIMEOUTS
525 printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?\n");
526 #endif
527 return 0;
530 void pckbd_leds(unsigned char leds)
532 if (kbd_exists && (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))) {
533 send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
534 kbd_exists = 0;
539 * In case we run on a non-x86 hardware we need to initialize both the
540 * keyboard controller and the keyboard. On a x86, the BIOS will
541 * already have initialized them.
543 * Some x86 BIOSes do not correctly initialize the keyboard, so the
544 * "kbd-reset" command line options can be given to force a reset.
545 * [Ranger]
547 #ifdef __i386__
548 int kbd_startup_reset __initdata = 0;
549 #else
550 int kbd_startup_reset __initdata = 1;
551 #endif
553 /* for "kbd-reset" cmdline param */
554 static int __init kbd_reset_setup(char *str)
556 kbd_startup_reset = 1;
557 return 1;
560 __setup("kbd-reset", kbd_reset_setup);
562 #define KBD_NO_DATA (-1) /* No data */
563 #define KBD_BAD_DATA (-2) /* Parity or other error */
565 static int __init kbd_read_data(void)
567 int retval = KBD_NO_DATA;
568 unsigned char status;
570 status = kbd_read_status();
571 if (status & KBD_STAT_OBF) {
572 unsigned char data = kbd_read_input();
574 retval = data;
575 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
576 retval = KBD_BAD_DATA;
578 return retval;
581 static void __init kbd_clear_input(void)
583 int maxread = 100; /* Random number */
585 do {
586 if (kbd_read_data() == KBD_NO_DATA)
587 break;
588 } while (--maxread);
591 static int __init kbd_wait_for_input(void)
593 long timeout = KBD_INIT_TIMEOUT;
595 do {
596 int retval = kbd_read_data();
597 if (retval >= 0)
598 return retval;
599 mdelay(1);
600 } while (--timeout);
601 return -1;
604 static void kbd_write_command_w(int data)
606 unsigned long flags;
608 spin_lock_irqsave(&kbd_controller_lock, flags);
609 kb_wait();
610 kbd_write_command(data);
611 spin_unlock_irqrestore(&kbd_controller_lock, flags);
614 static void kbd_write_output_w(int data)
616 unsigned long flags;
618 spin_lock_irqsave(&kbd_controller_lock, flags);
619 kb_wait();
620 kbd_write_output(data);
621 spin_unlock_irqrestore(&kbd_controller_lock, flags);
624 #if defined CONFIG_PSMOUSE
625 static void kbd_write_cmd(int cmd)
627 unsigned long flags;
629 spin_lock_irqsave(&kbd_controller_lock, flags);
630 kb_wait();
631 kbd_write_command(KBD_CCMD_WRITE_MODE);
632 kb_wait();
633 kbd_write_output(cmd);
634 spin_unlock_irqrestore(&kbd_controller_lock, flags);
636 #endif /* CONFIG_PSMOUSE */
638 static char * __init initialize_kbd(void)
640 int status;
643 * Test the keyboard interface.
644 * This seems to be the only way to get it going.
645 * If the test is successful a x55 is placed in the input buffer.
647 kbd_write_command_w(KBD_CCMD_SELF_TEST);
648 if (kbd_wait_for_input() != 0x55)
649 return "Keyboard failed self test";
652 * Perform a keyboard interface test. This causes the controller
653 * to test the keyboard clock and data lines. The results of the
654 * test are placed in the input buffer.
656 kbd_write_command_w(KBD_CCMD_KBD_TEST);
657 if (kbd_wait_for_input() != 0x00)
658 return "Keyboard interface failed self test";
661 * Enable the keyboard by allowing the keyboard clock to run.
663 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
666 * Reset keyboard. If the read times out
667 * then the assumption is that no keyboard is
668 * plugged into the machine.
669 * This defaults the keyboard to scan-code set 2.
671 * Set up to try again if the keyboard asks for RESEND.
673 do {
674 kbd_write_output_w(KBD_CMD_RESET);
675 status = kbd_wait_for_input();
676 if (status == KBD_REPLY_ACK)
677 break;
678 if (status != KBD_REPLY_RESEND)
679 return "Keyboard reset failed, no ACK";
680 } while (1);
682 if (kbd_wait_for_input() != KBD_REPLY_POR)
683 return "Keyboard reset failed, no POR";
686 * Set keyboard controller mode. During this, the keyboard should be
687 * in the disabled state.
689 * Set up to try again if the keyboard asks for RESEND.
691 do {
692 kbd_write_output_w(KBD_CMD_DISABLE);
693 status = kbd_wait_for_input();
694 if (status == KBD_REPLY_ACK)
695 break;
696 if (status != KBD_REPLY_RESEND)
697 return "Disable keyboard: no ACK";
698 } while (1);
700 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
701 kbd_write_output_w(KBD_MODE_KBD_INT
702 | KBD_MODE_SYS
703 | KBD_MODE_DISABLE_MOUSE
704 | KBD_MODE_KCC);
706 /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
707 kbd_write_command_w(KBD_CCMD_READ_MODE);
708 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
710 * If the controller does not support conversion,
711 * Set the keyboard to scan-code set 1.
713 kbd_write_output_w(0xF0);
714 kbd_wait_for_input();
715 kbd_write_output_w(0x01);
716 kbd_wait_for_input();
720 kbd_write_output_w(KBD_CMD_ENABLE);
721 if (kbd_wait_for_input() != KBD_REPLY_ACK)
722 return "Enable keyboard: no ACK";
725 * Finally, set the typematic rate to maximum.
727 kbd_write_output_w(KBD_CMD_SET_RATE);
728 if (kbd_wait_for_input() != KBD_REPLY_ACK)
729 return "Set rate: no ACK";
730 kbd_write_output_w(0x00);
731 if (kbd_wait_for_input() != KBD_REPLY_ACK)
732 return "Set rate: no ACK";
734 return NULL;
737 void __init pckbd_init_hw(void)
739 kbd_request_region();
741 /* Flush any pending input. */
742 kbd_clear_input();
744 if (kbd_startup_reset) {
745 char *msg = initialize_kbd();
746 if (msg)
747 printk(KERN_WARNING "initialize_kbd: %s\n", msg);
750 #if defined CONFIG_PSMOUSE
751 psaux_init();
752 #endif
754 /* Ok, finally allocate the IRQ, and off we go.. */
755 kbd_request_irq(keyboard_interrupt);
758 #if defined CONFIG_PSMOUSE
761 * Check if this is a dual port controller.
763 static int __init detect_auxiliary_port(void)
765 unsigned long flags;
766 int loops = 10;
767 int retval = 0;
769 /* Check if the BIOS detected a device on the auxiliary port. */
770 if (aux_device_present == 0xaa)
771 return 1;
773 spin_lock_irqsave(&kbd_controller_lock, flags);
775 /* Put the value 0x5A in the output buffer using the "Write
776 * Auxiliary Device Output Buffer" command (0xD3). Poll the
777 * Status Register for a while to see if the value really
778 * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
779 * bit is also set to 1 in the Status Register, we assume this
780 * controller has an Auxiliary Port (a.k.a. Mouse Port).
782 kb_wait();
783 kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
785 kb_wait();
786 kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
788 do {
789 unsigned char status = kbd_read_status();
791 if (status & KBD_STAT_OBF) {
792 (void) kbd_read_input();
793 if (status & KBD_STAT_MOUSE_OBF) {
794 printk(KERN_INFO "Detected PS/2 Mouse Port.\n");
795 retval = 1;
797 break;
799 mdelay(1);
800 } while (--loops);
801 spin_unlock_irqrestore(&kbd_controller_lock, flags);
803 return retval;
807 * Send a byte to the mouse.
809 static void aux_write_dev(int val)
811 unsigned long flags;
813 spin_lock_irqsave(&kbd_controller_lock, flags);
814 kb_wait();
815 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
816 kb_wait();
817 kbd_write_output(val);
818 spin_unlock_irqrestore(&kbd_controller_lock, flags);
822 * Send a byte to the mouse & handle returned ack
824 static void aux_write_ack(int val)
826 unsigned long flags;
828 spin_lock_irqsave(&kbd_controller_lock, flags);
829 kb_wait();
830 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
831 kb_wait();
832 kbd_write_output(val);
833 /* we expect an ACK in response. */
834 mouse_reply_expected++;
835 kb_wait();
836 spin_unlock_irqrestore(&kbd_controller_lock, flags);
839 static unsigned char get_from_queue(void)
841 unsigned char result;
842 unsigned long flags;
844 spin_lock_irqsave(&kbd_controller_lock, flags);
845 result = queue->buf[queue->tail];
846 queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
847 spin_unlock_irqrestore(&kbd_controller_lock, flags);
848 return result;
852 static inline int queue_empty(void)
854 return queue->head == queue->tail;
857 static int fasync_aux(int fd, struct file *filp, int on)
859 int retval;
861 retval = fasync_helper(fd, filp, on, &queue->fasync);
862 if (retval < 0)
863 return retval;
864 return 0;
869 * Random magic cookie for the aux device
871 #define AUX_DEV ((void *)queue)
873 static int release_aux(struct inode * inode, struct file * file)
875 fasync_aux(-1, file, 0);
876 if (--aux_count)
877 return 0;
878 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints */
879 kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
880 aux_free_irq(AUX_DEV);
881 return 0;
885 * Install interrupt handler.
886 * Enable auxiliary device.
889 static int open_aux(struct inode * inode, struct file * file)
891 if (aux_count++) {
892 return 0;
894 queue->head = queue->tail = 0; /* Flush input queue */
895 if (aux_request_irq(keyboard_interrupt, AUX_DEV)) {
896 aux_count--;
897 return -EBUSY;
899 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable the
900 auxiliary port on
901 controller. */
902 aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
903 kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
905 send_data(KBD_CMD_ENABLE); /* try to workaround toshiba4030cdt problem */
907 return 0;
911 * Put bytes from input queue to buffer.
914 static ssize_t read_aux(struct file * file, char * buffer,
915 size_t count, loff_t *ppos)
917 DECLARE_WAITQUEUE(wait, current);
918 ssize_t i = count;
919 unsigned char c;
921 if (queue_empty()) {
922 if (file->f_flags & O_NONBLOCK)
923 return -EAGAIN;
924 add_wait_queue(&queue->proc_list, &wait);
925 repeat:
926 set_current_state(TASK_INTERRUPTIBLE);
927 if (queue_empty() && !signal_pending(current)) {
928 schedule();
929 goto repeat;
931 current->state = TASK_RUNNING;
932 remove_wait_queue(&queue->proc_list, &wait);
934 while (i > 0 && !queue_empty()) {
935 c = get_from_queue();
936 put_user(c, buffer++);
937 i--;
939 if (count-i) {
940 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
941 return count-i;
943 if (signal_pending(current))
944 return -ERESTARTSYS;
945 return 0;
949 * Write to the aux device.
952 static ssize_t write_aux(struct file * file, const char * buffer,
953 size_t count, loff_t *ppos)
955 ssize_t retval = 0;
957 if (count) {
958 ssize_t written = 0;
960 if (count > 32)
961 count = 32; /* Limit to 32 bytes. */
962 do {
963 char c;
964 get_user(c, buffer++);
965 aux_write_dev(c);
966 written++;
967 } while (--count);
968 retval = -EIO;
969 if (written) {
970 retval = written;
971 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
975 return retval;
978 /* No kernel lock held - fine */
979 static unsigned int aux_poll(struct file *file, poll_table * wait)
981 poll_wait(file, &queue->proc_list, wait);
982 if (!queue_empty())
983 return POLLIN | POLLRDNORM;
984 return 0;
987 struct file_operations psaux_fops = {
988 read: read_aux,
989 write: write_aux,
990 poll: aux_poll,
991 open: open_aux,
992 release: release_aux,
993 fasync: fasync_aux,
997 * Initialize driver.
999 static struct miscdevice psaux_mouse = {
1000 PSMOUSE_MINOR, "psaux", &psaux_fops
1003 static int __init psaux_init(void)
1005 if (!detect_auxiliary_port())
1006 return -EIO;
1008 misc_register(&psaux_mouse);
1009 queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
1010 memset(queue, 0, sizeof(*queue));
1011 queue->head = queue->tail = 0;
1012 init_waitqueue_head(&queue->proc_list);
1014 #ifdef INITIALIZE_MOUSE
1015 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
1016 aux_write_ack(AUX_SET_SAMPLE);
1017 aux_write_ack(100); /* 100 samples/sec */
1018 aux_write_ack(AUX_SET_RES);
1019 aux_write_ack(3); /* 8 counts per mm */
1020 aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
1021 #endif /* INITIALIZE_MOUSE */
1022 kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
1023 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
1025 return 0;
1028 #endif /* CONFIG_PSMOUSE */