Import 2.3.18pre1
[davej-history.git] / drivers / char / pc_keyb.c
blobb4b6069744f70ef49af7e95d6aefc746bd5a910a
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/ioport.h>
27 #include <linux/init.h>
28 #include <linux/kbd_ll.h>
29 #include <linux/delay.h>
30 #include <linux/random.h>
31 #include <linux/poll.h>
32 #include <linux/miscdevice.h>
33 #include <linux/malloc.h>
35 #include <asm/keyboard.h>
36 #include <asm/bitops.h>
37 #include <asm/io.h>
38 #include <asm/uaccess.h>
39 #include <asm/irq.h>
40 #include <asm/system.h>
42 /* Some configuration switches are present in the include file... */
44 #include "pc_keyb.h"
46 /* Simple translation table for the SysRq keys */
48 #ifdef CONFIG_MAGIC_SYSRQ
49 unsigned char pckbd_sysrq_xlate[128] =
50 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
51 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
52 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
53 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
54 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
55 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
56 "\r\000/"; /* 0x60 - 0x6f */
57 #endif
59 static void kbd_write(int address, int data);
60 static unsigned char handle_kbd_event(void);
62 spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
64 /* used only by send_data - set by keyboard_interrupt */
65 static volatile unsigned char reply_expected = 0;
66 static volatile unsigned char acknowledge = 0;
67 static volatile unsigned char resend = 0;
70 #if defined CONFIG_PSMOUSE
72 * PS/2 Auxiliary Device
75 static int __init psaux_init(void);
77 static struct aux_queue *queue; /* Mouse data buffer. */
78 static int aux_count = 0;
79 /* used when we send commands to the mouse that expect an ACK. */
80 static unsigned char mouse_reply_expected = 0;
82 #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
83 #define AUX_INTS_ON (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
85 #define MAX_RETRIES 60 /* some aux operations take long time*/
87 #ifndef AUX_IRQ
88 # define AUX_IRQ 12
89 #endif
91 #endif /* CONFIG_PSMOUSE */
94 * Wait for keyboard controller input buffer to drain.
96 * Don't use 'jiffies' so that we don't depend on
97 * interrupts..
99 * Quote from PS/2 System Reference Manual:
101 * "Address hex 0060 and address hex 0064 should be written only when
102 * the input-buffer-full bit and output-buffer-full bit in the
103 * Controller Status register are set 0."
106 static void kb_wait(void)
108 unsigned long timeout = KBC_TIMEOUT;
110 do {
112 * "handle_kbd_event()" will handle any incoming events
113 * while we wait - keypresses or mouse movement.
115 unsigned char status = handle_kbd_event();
117 if (! (status & KBD_STAT_IBF))
118 return;
119 mdelay(1);
120 timeout--;
121 } while (timeout);
122 #ifdef KBD_REPORT_TIMEOUTS
123 printk(KERN_WARNING "Keyboard timed out[1]\n");
124 #endif
128 * Translation of escaped scancodes to keycodes.
129 * This is now user-settable.
130 * The keycodes 1-88,96-111,119 are fairly standard, and
131 * should probably not be changed - changing might confuse X.
132 * X also interprets scancode 0x5d (KEY_Begin).
134 * For 1-88 keycode equals scancode.
137 #define E0_KPENTER 96
138 #define E0_RCTRL 97
139 #define E0_KPSLASH 98
140 #define E0_PRSCR 99
141 #define E0_RALT 100
142 #define E0_BREAK 101 /* (control-pause) */
143 #define E0_HOME 102
144 #define E0_UP 103
145 #define E0_PGUP 104
146 #define E0_LEFT 105
147 #define E0_RIGHT 106
148 #define E0_END 107
149 #define E0_DOWN 108
150 #define E0_PGDN 109
151 #define E0_INS 110
152 #define E0_DEL 111
154 #define E1_PAUSE 119
157 * The keycodes below are randomly located in 89-95,112-118,120-127.
158 * They could be thrown away (and all occurrences below replaced by 0),
159 * but that would force many users to use the `setkeycodes' utility, where
160 * they needed not before. It does not matter that there are duplicates, as
161 * long as no duplication occurs for any single keyboard.
163 #define SC_LIM 89
165 #define FOCUS_PF1 85 /* actual code! */
166 #define FOCUS_PF2 89
167 #define FOCUS_PF3 90
168 #define FOCUS_PF4 91
169 #define FOCUS_PF5 92
170 #define FOCUS_PF6 93
171 #define FOCUS_PF7 94
172 #define FOCUS_PF8 95
173 #define FOCUS_PF9 120
174 #define FOCUS_PF10 121
175 #define FOCUS_PF11 122
176 #define FOCUS_PF12 123
178 #define JAP_86 124
179 /* tfj@olivia.ping.dk:
180 * The four keys are located over the numeric keypad, and are
181 * labelled A1-A4. It's an rc930 keyboard, from
182 * Regnecentralen/RC International, Now ICL.
183 * Scancodes: 59, 5a, 5b, 5c.
185 #define RGN1 124
186 #define RGN2 125
187 #define RGN3 126
188 #define RGN4 127
190 static unsigned char high_keys[128 - SC_LIM] = {
191 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
192 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
193 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
194 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
195 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
196 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
199 /* BTC */
200 #define E0_MACRO 112
201 /* LK450 */
202 #define E0_F13 113
203 #define E0_F14 114
204 #define E0_HELP 115
205 #define E0_DO 116
206 #define E0_F17 117
207 #define E0_KPMINPLUS 118
209 * My OmniKey generates e0 4c for the "OMNI" key and the
210 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
212 #define E0_OK 124
214 * New microsoft keyboard is rumoured to have
215 * e0 5b (left window button), e0 5c (right window button),
216 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
217 * [or: Windows_L, Windows_R, TaskMan]
219 #define E0_MSLW 125
220 #define E0_MSRW 126
221 #define E0_MSTM 127
223 static unsigned char e0_keys[128] = {
224 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
225 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
226 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
227 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
228 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
229 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
230 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
231 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
232 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
233 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
234 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
235 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
236 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
237 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
238 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
239 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
242 int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
244 if (scancode < SC_LIM || scancode > 255 || keycode > 127)
245 return -EINVAL;
246 if (scancode < 128)
247 high_keys[scancode - SC_LIM] = keycode;
248 else
249 e0_keys[scancode - 128] = keycode;
250 return 0;
253 int pckbd_getkeycode(unsigned int scancode)
255 return
256 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
257 (scancode < 128) ? high_keys[scancode - SC_LIM] :
258 e0_keys[scancode - 128];
261 static int do_acknowledge(unsigned char scancode)
263 if (reply_expected) {
264 /* Unfortunately, we must recognise these codes only if we know they
265 * are known to be valid (i.e., after sending a command), because there
266 * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
267 * keys with such codes :(
269 if (scancode == KBD_REPLY_ACK) {
270 acknowledge = 1;
271 reply_expected = 0;
272 return 0;
273 } else if (scancode == KBD_REPLY_RESEND) {
274 resend = 1;
275 reply_expected = 0;
276 return 0;
278 /* Should not happen... */
279 #if 0
280 printk(KERN_DEBUG "keyboard reply expected - got %02x\n",
281 scancode);
282 #endif
284 return 1;
287 int pckbd_translate(unsigned char scancode, unsigned char *keycode,
288 char raw_mode)
290 static int prev_scancode = 0;
292 /* special prefix scancodes.. */
293 if (scancode == 0xe0 || scancode == 0xe1) {
294 prev_scancode = scancode;
295 return 0;
298 /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
299 if (scancode == 0x00 || scancode == 0xff) {
300 prev_scancode = 0;
301 return 0;
304 scancode &= 0x7f;
306 if (prev_scancode) {
308 * usually it will be 0xe0, but a Pause key generates
309 * e1 1d 45 e1 9d c5 when pressed, and nothing when released
311 if (prev_scancode != 0xe0) {
312 if (prev_scancode == 0xe1 && scancode == 0x1d) {
313 prev_scancode = 0x100;
314 return 0;
315 } else if (prev_scancode == 0x100 && scancode == 0x45) {
316 *keycode = E1_PAUSE;
317 prev_scancode = 0;
318 } else {
319 #ifdef KBD_REPORT_UNKN
320 if (!raw_mode)
321 printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
322 #endif
323 prev_scancode = 0;
324 return 0;
326 } else {
327 prev_scancode = 0;
329 * The keyboard maintains its own internal caps lock and
330 * num lock statuses. In caps lock mode E0 AA precedes make
331 * code and E0 2A follows break code. In num lock mode,
332 * E0 2A precedes make code and E0 AA follows break code.
333 * We do our own book-keeping, so we will just ignore these.
336 * For my keyboard there is no caps lock mode, but there are
337 * both Shift-L and Shift-R modes. The former mode generates
338 * E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
339 * So, we should also ignore the latter. - aeb@cwi.nl
341 if (scancode == 0x2a || scancode == 0x36)
342 return 0;
344 if (e0_keys[scancode])
345 *keycode = e0_keys[scancode];
346 else {
347 #ifdef KBD_REPORT_UNKN
348 if (!raw_mode)
349 printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
350 scancode);
351 #endif
352 return 0;
355 } else if (scancode >= SC_LIM) {
356 /* This happens with the FOCUS 9000 keyboard
357 Its keys PF1..PF12 are reported to generate
358 55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
359 Moreover, unless repeated, they do not generate
360 key-down events, so we have to zero up_flag below */
361 /* Also, Japanese 86/106 keyboards are reported to
362 generate 0x73 and 0x7d for \ - and \ | respectively. */
363 /* Also, some Brazilian keyboard is reported to produce
364 0x73 and 0x7e for \ ? and KP-dot, respectively. */
366 *keycode = high_keys[scancode - SC_LIM];
368 if (!*keycode) {
369 if (!raw_mode) {
370 #ifdef KBD_REPORT_UNKN
371 printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
372 " - ignored\n", scancode);
373 #endif
375 return 0;
377 } else
378 *keycode = scancode;
379 return 1;
382 char pckbd_unexpected_up(unsigned char keycode)
384 /* unexpected, but this can happen: maybe this was a key release for a
385 FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
386 if (keycode >= SC_LIM || keycode == 85)
387 return 0;
388 else
389 return 0200;
392 static inline void handle_mouse_event(unsigned char scancode)
394 #ifdef CONFIG_PSMOUSE
395 if (mouse_reply_expected) {
396 if (scancode == AUX_ACK) {
397 mouse_reply_expected--;
398 return;
400 mouse_reply_expected = 0;
403 add_mouse_randomness(scancode);
404 if (aux_count) {
405 int head = queue->head;
407 queue->buf[head] = scancode;
408 head = (head + 1) & (AUX_BUF_SIZE-1);
409 if (head != queue->tail) {
410 queue->head = head;
411 if (queue->fasync)
412 kill_fasync(queue->fasync, SIGIO);
413 wake_up_interruptible(&queue->proc_list);
416 #endif
420 * This reads the keyboard status port, and does the
421 * appropriate action.
423 * It requires that we hold the keyboard controller
424 * spinlock.
426 static unsigned char handle_kbd_event(void)
428 unsigned char status = inb(KBD_STATUS_REG);
430 while (status & KBD_STAT_OBF) {
431 unsigned char scancode;
433 scancode = inb(KBD_DATA_REG);
435 if (status & KBD_STAT_MOUSE_OBF) {
436 handle_mouse_event(scancode);
437 } else {
438 if (do_acknowledge(scancode))
439 handle_scancode(scancode, !(scancode & 0x80));
440 mark_bh(KEYBOARD_BH);
443 status = inb(KBD_STATUS_REG);
446 return status;
450 static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
452 unsigned long flags;
454 kbd_pt_regs = regs;
456 spin_lock_irqsave(&kbd_controller_lock, flags);
457 handle_kbd_event();
458 spin_unlock_irqrestore(&kbd_controller_lock, flags);
462 * send_data sends a character to the keyboard and waits
463 * for an acknowledge, possibly retrying if asked to. Returns
464 * the success status.
466 * Don't use 'jiffies', so that we don't depend on interrupts
468 static int send_data(unsigned char data)
470 int retries = 3;
472 do {
473 unsigned long timeout = KBD_TIMEOUT;
475 acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
476 resend = 0;
477 reply_expected = 1;
478 kbd_write(KBD_DATA_REG, data);
479 for (;;) {
480 if (acknowledge)
481 return 1;
482 if (resend)
483 break;
484 mdelay(1);
485 if (!--timeout) {
486 #ifdef KBD_REPORT_TIMEOUTS
487 printk(KERN_WARNING "Keyboard timeout[2]\n");
488 #endif
489 return 0;
492 } while (retries-- > 0);
493 #ifdef KBD_REPORT_TIMEOUTS
494 printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?\n");
495 #endif
496 return 0;
499 void pckbd_leds(unsigned char leds)
501 if (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))
502 send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
506 * In case we run on a non-x86 hardware we need to initialize both the
507 * keyboard controller and the keyboard. On a x86, the BIOS will
508 * already have initialized them.
510 * Some x86 BIOSes do not correctly initialize the keyboard, so the
511 * "kbd-reset" command line options can be given to force a reset.
512 * [Ranger]
514 #ifdef __i386__
515 int kbd_startup_reset __initdata = 0;
516 #else
517 int kbd_startup_reset __initdata = 1;
518 #endif
520 /* for "kbd-reset" cmdline param */
521 static int __init kbd_reset_setup(char *str)
523 kbd_startup_reset = 1;
524 return 1;
527 __setup("kbd-reset", kbd_reset_setup);
529 #define KBD_NO_DATA (-1) /* No data */
530 #define KBD_BAD_DATA (-2) /* Parity or other error */
532 static int __init kbd_read_input(void)
534 int retval = KBD_NO_DATA;
535 unsigned char status;
537 status = inb(KBD_STATUS_REG);
538 if (status & KBD_STAT_OBF) {
539 unsigned char data = inb(KBD_DATA_REG);
541 retval = data;
542 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
543 retval = KBD_BAD_DATA;
545 return retval;
548 static void __init kbd_clear_input(void)
550 int maxread = 100; /* Random number */
552 do {
553 if (kbd_read_input() == KBD_NO_DATA)
554 break;
555 } while (--maxread);
558 static int __init kbd_wait_for_input(void)
560 long timeout = KBD_INIT_TIMEOUT;
562 do {
563 int retval = kbd_read_input();
564 if (retval >= 0)
565 return retval;
566 mdelay(1);
567 } while (--timeout);
568 return -1;
571 static void kbd_write(int address, int data)
573 unsigned long flags;
575 spin_lock_irqsave(&kbd_controller_lock, flags);
576 kb_wait();
577 outb(data, address);
578 spin_unlock_irqrestore(&kbd_controller_lock, flags);
581 #if defined CONFIG_PSMOUSE
582 static void kbd_write_cmd(int cmd)
584 unsigned long flags;
586 spin_lock_irqsave(&kbd_controller_lock, flags);
587 kb_wait();
588 outb(KBD_CCMD_WRITE_MODE, KBD_CNTL_REG);
589 kb_wait();
590 outb(cmd, KBD_DATA_REG);
591 spin_unlock_irqrestore(&kbd_controller_lock, flags);
593 #endif /* CONFIG_PSMOUSE */
595 static char * __init initialize_kbd(void)
597 int status;
600 * Test the keyboard interface.
601 * This seems to be the only way to get it going.
602 * If the test is successful a x55 is placed in the input buffer.
604 kbd_write(KBD_CNTL_REG, KBD_CCMD_SELF_TEST);
605 if (kbd_wait_for_input() != 0x55)
606 return "Keyboard failed self test";
609 * Perform a keyboard interface test. This causes the controller
610 * to test the keyboard clock and data lines. The results of the
611 * test are placed in the input buffer.
613 kbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_TEST);
614 if (kbd_wait_for_input() != 0x00)
615 return "Keyboard interface failed self test";
618 * Enable the keyboard by allowing the keyboard clock to run.
620 kbd_write(KBD_CNTL_REG, KBD_CCMD_KBD_ENABLE);
623 * Reset keyboard. If the read times out
624 * then the assumption is that no keyboard is
625 * plugged into the machine.
626 * This defaults the keyboard to scan-code set 2.
628 * Set up to try again if the keyboard asks for RESEND.
630 do {
631 kbd_write(KBD_DATA_REG, KBD_CMD_RESET);
632 status = kbd_wait_for_input();
633 if (status == KBD_REPLY_ACK)
634 break;
635 if (status != KBD_REPLY_RESEND)
636 return "Keyboard reset failed, no ACK";
637 } while (1);
639 if (kbd_wait_for_input() != KBD_REPLY_POR)
640 return "Keyboard reset failed, no POR";
643 * Set keyboard controller mode. During this, the keyboard should be
644 * in the disabled state.
646 * Set up to try again if the keyboard asks for RESEND.
648 do {
649 kbd_write(KBD_DATA_REG, KBD_CMD_DISABLE);
650 status = kbd_wait_for_input();
651 if (status == KBD_REPLY_ACK)
652 break;
653 if (status != KBD_REPLY_RESEND)
654 return "Disable keyboard: no ACK";
655 } while (1);
657 kbd_write(KBD_CNTL_REG, KBD_CCMD_WRITE_MODE);
658 kbd_write(KBD_DATA_REG, KBD_MODE_KBD_INT
659 | KBD_MODE_SYS
660 | KBD_MODE_DISABLE_MOUSE
661 | KBD_MODE_KCC);
663 /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
664 kbd_write(KBD_CNTL_REG, KBD_CCMD_READ_MODE);
665 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
667 * If the controller does not support conversion,
668 * Set the keyboard to scan-code set 1.
670 kbd_write(KBD_DATA_REG, 0xF0);
671 kbd_wait_for_input();
672 kbd_write(KBD_DATA_REG, 0x01);
673 kbd_wait_for_input();
677 kbd_write(KBD_DATA_REG, KBD_CMD_ENABLE);
678 if (kbd_wait_for_input() != KBD_REPLY_ACK)
679 return "Enable keyboard: no ACK";
682 * Finally, set the typematic rate to maximum.
684 kbd_write(KBD_DATA_REG, KBD_CMD_SET_RATE);
685 if (kbd_wait_for_input() != KBD_REPLY_ACK)
686 return "Set rate: no ACK";
687 kbd_write(KBD_DATA_REG, 0x00);
688 if (kbd_wait_for_input() != KBD_REPLY_ACK)
689 return "Set rate: no ACK";
691 return NULL;
694 void __init pckbd_init_hw(void)
696 /* Flush any pending input. */
697 kbd_clear_input();
699 if (kbd_startup_reset) {
700 char *msg = initialize_kbd();
701 if (msg)
702 printk(KERN_WARNING "initialize_kbd: %s\n", msg);
705 #if defined CONFIG_PSMOUSE
706 psaux_init();
707 #endif
709 /* Ok, finally allocate the IRQ, and off we go.. */
710 request_irq(KEYBOARD_IRQ, keyboard_interrupt, 0, "keyboard", NULL);
713 #if defined CONFIG_PSMOUSE
716 * Check if this is a dual port controller.
718 static int __init detect_auxiliary_port(void)
720 unsigned long flags;
721 int loops = 10;
722 int retval = 0;
724 spin_lock_irqsave(&kbd_controller_lock, flags);
726 /* Put the value 0x5A in the output buffer using the "Write
727 * Auxiliary Device Output Buffer" command (0xD3). Poll the
728 * Status Register for a while to see if the value really
729 * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
730 * bit is also set to 1 in the Status Register, we assume this
731 * controller has an Auxiliary Port (a.k.a. Mouse Port).
733 kb_wait();
734 outb(KBD_CCMD_WRITE_AUX_OBUF, KBD_CNTL_REG);
736 kb_wait();
737 outb(0x5a, KBD_DATA_REG); /* 0x5a is a random dummy value. */
739 do {
740 unsigned char status = inb(KBD_STATUS_REG);
742 if (status & KBD_STAT_OBF) {
743 (void) inb(KBD_DATA_REG);
744 if (status & KBD_STAT_MOUSE_OBF) {
745 printk(KERN_INFO "Detected PS/2 Mouse Port.\n");
746 retval = 1;
748 break;
750 mdelay(1);
751 } while (--loops);
752 spin_unlock_irqrestore(&kbd_controller_lock, flags);
754 return retval;
758 * Send a byte to the mouse.
760 static void aux_write_dev(int val)
762 unsigned long flags;
764 spin_lock_irqsave(&kbd_controller_lock, flags);
765 kb_wait();
766 outb(KBD_CCMD_WRITE_MOUSE, KBD_CNTL_REG);
767 kb_wait();
768 outb(val, KBD_DATA_REG);
769 spin_unlock_irqrestore(&kbd_controller_lock, flags);
773 * Send a byte to the mouse & handle returned ack
775 static void aux_write_ack(int val)
777 unsigned long flags;
779 spin_lock_irqsave(&kbd_controller_lock, flags);
780 kb_wait();
781 outb(KBD_CCMD_WRITE_MOUSE, KBD_CNTL_REG);
782 kb_wait();
783 outb(val, KBD_DATA_REG);
784 /* we expect an ACK in response. */
785 mouse_reply_expected++;
786 kb_wait();
787 spin_unlock_irqrestore(&kbd_controller_lock, flags);
790 static unsigned char get_from_queue(void)
792 unsigned char result;
793 unsigned long flags;
795 spin_lock_irqsave(&kbd_controller_lock, flags);
796 result = queue->buf[queue->tail];
797 queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
798 spin_unlock_irqrestore(&kbd_controller_lock, flags);
799 return result;
803 static inline int queue_empty(void)
805 return queue->head == queue->tail;
808 static int fasync_aux(int fd, struct file *filp, int on)
810 int retval;
812 retval = fasync_helper(fd, filp, on, &queue->fasync);
813 if (retval < 0)
814 return retval;
815 return 0;
820 * Random magic cookie for the aux device
822 #define AUX_DEV ((void *)queue)
824 static int release_aux(struct inode * inode, struct file * file)
826 fasync_aux(-1, file, 0);
827 if (--aux_count)
828 return 0;
829 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints */
830 kbd_write(KBD_CNTL_REG, KBD_CCMD_MOUSE_DISABLE);
831 free_irq(AUX_IRQ, AUX_DEV);
832 return 0;
836 * Install interrupt handler.
837 * Enable auxiliary device.
840 static int open_aux(struct inode * inode, struct file * file)
842 if (aux_count++) {
843 return 0;
845 queue->head = queue->tail = 0; /* Flush input queue */
846 if (request_irq(AUX_IRQ, keyboard_interrupt, SA_SHIRQ, "PS/2 Mouse", AUX_DEV)) {
847 aux_count--;
848 return -EBUSY;
850 kbd_write(KBD_CNTL_REG, KBD_CCMD_MOUSE_ENABLE); /* Enable the
851 auxiliary port on
852 controller. */
853 aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
854 kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
856 return 0;
860 * Put bytes from input queue to buffer.
863 static ssize_t read_aux(struct file * file, char * buffer,
864 size_t count, loff_t *ppos)
866 DECLARE_WAITQUEUE(wait, current);
867 ssize_t i = count;
868 unsigned char c;
870 if (queue_empty()) {
871 if (file->f_flags & O_NONBLOCK)
872 return -EAGAIN;
873 add_wait_queue(&queue->proc_list, &wait);
874 repeat:
875 set_current_state(TASK_INTERRUPTIBLE);
876 if (queue_empty() && !signal_pending(current)) {
877 schedule();
878 goto repeat;
880 current->state = TASK_RUNNING;
881 remove_wait_queue(&queue->proc_list, &wait);
883 while (i > 0 && !queue_empty()) {
884 c = get_from_queue();
885 put_user(c, buffer++);
886 i--;
888 if (count-i) {
889 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
890 return count-i;
892 if (signal_pending(current))
893 return -ERESTARTSYS;
894 return 0;
898 * Write to the aux device.
901 static ssize_t write_aux(struct file * file, const char * buffer,
902 size_t count, loff_t *ppos)
904 ssize_t retval = 0;
906 if (count) {
907 ssize_t written = 0;
909 if (count > 32)
910 count = 32; /* Limit to 32 bytes. */
911 do {
912 char c;
913 get_user(c, buffer++);
914 aux_write_dev(c);
915 written++;
916 } while (--count);
917 retval = -EIO;
918 if (written) {
919 retval = written;
920 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
924 return retval;
927 static unsigned int aux_poll(struct file *file, poll_table * wait)
929 poll_wait(file, &queue->proc_list, wait);
930 if (!queue_empty())
931 return POLLIN | POLLRDNORM;
932 return 0;
935 struct file_operations psaux_fops = {
936 NULL, /* seek */
937 read_aux,
938 write_aux,
939 NULL, /* readdir */
940 aux_poll,
941 NULL, /* ioctl */
942 NULL, /* mmap */
943 open_aux,
944 NULL, /* flush */
945 release_aux,
946 NULL,
947 fasync_aux,
951 * Initialize driver.
953 static struct miscdevice psaux_mouse = {
954 PSMOUSE_MINOR, "psaux", &psaux_fops
957 static int __init psaux_init(void)
959 if (!detect_auxiliary_port())
960 return -EIO;
962 misc_register(&psaux_mouse);
963 queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
964 memset(queue, 0, sizeof(*queue));
965 queue->head = queue->tail = 0;
966 init_waitqueue_head(&queue->proc_list);
968 #ifdef INITIALIZE_MOUSE
969 kbd_write(KBD_CNTL_REG, KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
970 aux_write_ack(AUX_SET_SAMPLE);
971 aux_write_ack(100); /* 100 samples/sec */
972 aux_write_ack(AUX_SET_RES);
973 aux_write_ack(3); /* 8 counts per mm */
974 aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
975 #endif /* INITIALIZE_MOUSE */
976 kbd_write(KBD_CNTL_REG, KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
977 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
979 return 0;
982 #endif /* CONFIG_PSMOUSE */