Linux 2.4.0-test10pre4
[davej-history.git] / drivers / char / pc_keyb.c
blob117ae4ec1100cc1b55f045acff7d2b3f8e38d68e
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>
34 #include <linux/smp_lock.h>
36 #include <asm/keyboard.h>
37 #include <asm/bitops.h>
38 #include <asm/uaccess.h>
39 #include <asm/irq.h>
40 #include <asm/system.h>
42 #include <asm/io.h>
44 /* Some configuration switches are present in the include file... */
46 #include <linux/pc_keyb.h>
48 /* Simple translation table for the SysRq keys */
50 #ifdef CONFIG_MAGIC_SYSRQ
51 unsigned char pckbd_sysrq_xlate[128] =
52 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
53 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
54 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
55 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
56 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
57 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
58 "\r\000/"; /* 0x60 - 0x6f */
59 #endif
61 static void kbd_write_command_w(int data);
62 static void kbd_write_output_w(int data);
63 #ifdef CONFIG_PSMOUSE
64 static void aux_write_ack(int val);
65 static void __aux_write_ack(int val);
66 #endif
68 spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
69 static unsigned char handle_kbd_event(void);
71 /* used only by send_data - set by keyboard_interrupt */
72 static volatile unsigned char reply_expected;
73 static volatile unsigned char acknowledge;
74 static volatile unsigned char resend;
77 #if defined CONFIG_PSMOUSE
79 * PS/2 Auxiliary Device
82 static int __init psaux_init(void);
84 #define AUX_RECONNECT 170 /* scancode when ps2 device is plugged (back) in */
86 static struct aux_queue *queue; /* Mouse data buffer. */
87 static int aux_count;
88 /* used when we send commands to the mouse that expect an ACK. */
89 static unsigned char mouse_reply_expected;
91 #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
92 #define AUX_INTS_ON (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
94 #define MAX_RETRIES 60 /* some aux operations take long time*/
95 #endif /* CONFIG_PSMOUSE */
98 * Wait for keyboard controller input buffer to drain.
100 * Don't use 'jiffies' so that we don't depend on
101 * interrupts..
103 * Quote from PS/2 System Reference Manual:
105 * "Address hex 0060 and address hex 0064 should be written only when
106 * the input-buffer-full bit and output-buffer-full bit in the
107 * Controller Status register are set 0."
110 static void kb_wait(void)
112 unsigned long timeout = KBC_TIMEOUT;
114 do {
116 * "handle_kbd_event()" will handle any incoming events
117 * while we wait - keypresses or mouse movement.
119 unsigned char status = handle_kbd_event();
121 if (! (status & KBD_STAT_IBF))
122 return;
123 mdelay(1);
124 timeout--;
125 } while (timeout);
126 #ifdef KBD_REPORT_TIMEOUTS
127 printk(KERN_WARNING "Keyboard timed out[1]\n");
128 #endif
132 * Translation of escaped scancodes to keycodes.
133 * This is now user-settable.
134 * The keycodes 1-88,96-111,119 are fairly standard, and
135 * should probably not be changed - changing might confuse X.
136 * X also interprets scancode 0x5d (KEY_Begin).
138 * For 1-88 keycode equals scancode.
141 #define E0_KPENTER 96
142 #define E0_RCTRL 97
143 #define E0_KPSLASH 98
144 #define E0_PRSCR 99
145 #define E0_RALT 100
146 #define E0_BREAK 101 /* (control-pause) */
147 #define E0_HOME 102
148 #define E0_UP 103
149 #define E0_PGUP 104
150 #define E0_LEFT 105
151 #define E0_RIGHT 106
152 #define E0_END 107
153 #define E0_DOWN 108
154 #define E0_PGDN 109
155 #define E0_INS 110
156 #define E0_DEL 111
158 #define E1_PAUSE 119
161 * The keycodes below are randomly located in 89-95,112-118,120-127.
162 * They could be thrown away (and all occurrences below replaced by 0),
163 * but that would force many users to use the `setkeycodes' utility, where
164 * they needed not before. It does not matter that there are duplicates, as
165 * long as no duplication occurs for any single keyboard.
167 #define SC_LIM 89
169 #define FOCUS_PF1 85 /* actual code! */
170 #define FOCUS_PF2 89
171 #define FOCUS_PF3 90
172 #define FOCUS_PF4 91
173 #define FOCUS_PF5 92
174 #define FOCUS_PF6 93
175 #define FOCUS_PF7 94
176 #define FOCUS_PF8 95
177 #define FOCUS_PF9 120
178 #define FOCUS_PF10 121
179 #define FOCUS_PF11 122
180 #define FOCUS_PF12 123
182 #define JAP_86 124
183 /* tfj@olivia.ping.dk:
184 * The four keys are located over the numeric keypad, and are
185 * labelled A1-A4. It's an rc930 keyboard, from
186 * Regnecentralen/RC International, Now ICL.
187 * Scancodes: 59, 5a, 5b, 5c.
189 #define RGN1 124
190 #define RGN2 125
191 #define RGN3 126
192 #define RGN4 127
194 static unsigned char high_keys[128 - SC_LIM] = {
195 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
196 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
197 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
198 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
199 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
200 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
203 /* BTC */
204 #define E0_MACRO 112
205 /* LK450 */
206 #define E0_F13 113
207 #define E0_F14 114
208 #define E0_HELP 115
209 #define E0_DO 116
210 #define E0_F17 117
211 #define E0_KPMINPLUS 118
213 * My OmniKey generates e0 4c for the "OMNI" key and the
214 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
216 #define E0_OK 124
218 * New microsoft keyboard is rumoured to have
219 * e0 5b (left window button), e0 5c (right window button),
220 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
221 * [or: Windows_L, Windows_R, TaskMan]
223 #define E0_MSLW 125
224 #define E0_MSRW 126
225 #define E0_MSTM 127
227 static unsigned char e0_keys[128] = {
228 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
229 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
230 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
231 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
232 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
233 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
234 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
235 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
236 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
237 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
238 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
239 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
240 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
241 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
242 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
243 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
246 int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
248 if (scancode < SC_LIM || scancode > 255 || keycode > 127)
249 return -EINVAL;
250 if (scancode < 128)
251 high_keys[scancode - SC_LIM] = keycode;
252 else
253 e0_keys[scancode - 128] = keycode;
254 return 0;
257 int pckbd_getkeycode(unsigned int scancode)
259 return
260 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
261 (scancode < 128) ? high_keys[scancode - SC_LIM] :
262 e0_keys[scancode - 128];
265 static int do_acknowledge(unsigned char scancode)
267 if (reply_expected) {
268 /* Unfortunately, we must recognise these codes only if we know they
269 * are known to be valid (i.e., after sending a command), because there
270 * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
271 * keys with such codes :(
273 if (scancode == KBD_REPLY_ACK) {
274 acknowledge = 1;
275 reply_expected = 0;
276 return 0;
277 } else if (scancode == KBD_REPLY_RESEND) {
278 resend = 1;
279 reply_expected = 0;
280 return 0;
282 /* Should not happen... */
283 #if 0
284 printk(KERN_DEBUG "keyboard reply expected - got %02x\n",
285 scancode);
286 #endif
288 return 1;
291 int pckbd_translate(unsigned char scancode, unsigned char *keycode,
292 char raw_mode)
294 static int prev_scancode;
296 /* special prefix scancodes.. */
297 if (scancode == 0xe0 || scancode == 0xe1) {
298 prev_scancode = scancode;
299 return 0;
302 /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
303 if (scancode == 0x00 || scancode == 0xff) {
304 prev_scancode = 0;
305 return 0;
308 scancode &= 0x7f;
310 if (prev_scancode) {
312 * usually it will be 0xe0, but a Pause key generates
313 * e1 1d 45 e1 9d c5 when pressed, and nothing when released
315 if (prev_scancode != 0xe0) {
316 if (prev_scancode == 0xe1 && scancode == 0x1d) {
317 prev_scancode = 0x100;
318 return 0;
319 } else if (prev_scancode == 0x100 && scancode == 0x45) {
320 *keycode = E1_PAUSE;
321 prev_scancode = 0;
322 } else {
323 #ifdef KBD_REPORT_UNKN
324 if (!raw_mode)
325 printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
326 #endif
327 prev_scancode = 0;
328 return 0;
330 } else {
331 prev_scancode = 0;
333 * The keyboard maintains its own internal caps lock and
334 * num lock statuses. In caps lock mode E0 AA precedes make
335 * code and E0 2A follows break code. In num lock mode,
336 * E0 2A precedes make code and E0 AA follows break code.
337 * We do our own book-keeping, so we will just ignore these.
340 * For my keyboard there is no caps lock mode, but there are
341 * both Shift-L and Shift-R modes. The former mode generates
342 * E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
343 * So, we should also ignore the latter. - aeb@cwi.nl
345 if (scancode == 0x2a || scancode == 0x36)
346 return 0;
348 if (e0_keys[scancode])
349 *keycode = e0_keys[scancode];
350 else {
351 #ifdef KBD_REPORT_UNKN
352 if (!raw_mode)
353 printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
354 scancode);
355 #endif
356 return 0;
359 } else if (scancode >= SC_LIM) {
360 /* This happens with the FOCUS 9000 keyboard
361 Its keys PF1..PF12 are reported to generate
362 55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
363 Moreover, unless repeated, they do not generate
364 key-down events, so we have to zero up_flag below */
365 /* Also, Japanese 86/106 keyboards are reported to
366 generate 0x73 and 0x7d for \ - and \ | respectively. */
367 /* Also, some Brazilian keyboard is reported to produce
368 0x73 and 0x7e for \ ? and KP-dot, respectively. */
370 *keycode = high_keys[scancode - SC_LIM];
372 if (!*keycode) {
373 if (!raw_mode) {
374 #ifdef KBD_REPORT_UNKN
375 printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
376 " - ignored\n", scancode);
377 #endif
379 return 0;
381 } else
382 *keycode = scancode;
383 return 1;
386 char pckbd_unexpected_up(unsigned char keycode)
388 /* unexpected, but this can happen: maybe this was a key release for a
389 FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
390 if (keycode >= SC_LIM || keycode == 85)
391 return 0;
392 else
393 return 0200;
396 static inline void handle_mouse_event(unsigned char scancode)
398 #ifdef CONFIG_PSMOUSE
399 if (mouse_reply_expected) {
400 if (scancode == AUX_ACK) {
401 mouse_reply_expected--;
402 return;
404 mouse_reply_expected = 0;
406 else if(scancode == AUX_RECONNECT){
407 queue->head = queue->tail = 0; /* Flush input queue */
408 __aux_write_ack(AUX_ENABLE_DEV); /* ping the mouse :) */
409 return;
412 add_mouse_randomness(scancode);
413 if (aux_count) {
414 int head = queue->head;
416 queue->buf[head] = scancode;
417 head = (head + 1) & (AUX_BUF_SIZE-1);
418 if (head != queue->tail) {
419 queue->head = head;
420 kill_fasync(&queue->fasync, SIGIO, POLL_IN);
421 wake_up_interruptible(&queue->proc_list);
424 #endif
427 static unsigned char kbd_exists = 1;
429 static inline void handle_keyboard_event(unsigned char scancode)
431 #ifdef CONFIG_VT
432 kbd_exists = 1;
433 if (do_acknowledge(scancode))
434 handle_scancode(scancode, !(scancode & 0x80));
435 #endif
436 tasklet_schedule(&keyboard_tasklet);
440 * This reads the keyboard status port, and does the
441 * appropriate action.
443 * It requires that we hold the keyboard controller
444 * spinlock.
446 static unsigned char handle_kbd_event(void)
448 unsigned char status = kbd_read_status();
449 unsigned int work = 10000;
451 while (status & KBD_STAT_OBF) {
452 unsigned char scancode;
454 scancode = kbd_read_input();
456 /* Error bytes must be ignored to make the
457 Synaptics touchpads compaq use work */
458 #if 1
459 /* Ignore error bytes */
460 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
461 #endif
463 if (status & KBD_STAT_MOUSE_OBF)
464 handle_mouse_event(scancode);
465 else
466 handle_keyboard_event(scancode);
469 status = kbd_read_status();
471 if (!--work) {
472 printk(KERN_ERR "pc_keyb: controller jammed (0x%02X).\n",
473 status);
474 break;
478 return status;
482 static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
484 unsigned long flags;
486 #ifdef CONFIG_VT
487 kbd_pt_regs = regs;
488 #endif
489 spin_lock_irqsave(&kbd_controller_lock, flags);
490 handle_kbd_event();
491 spin_unlock_irqrestore(&kbd_controller_lock, flags);
495 * send_data sends a character to the keyboard and waits
496 * for an acknowledge, possibly retrying if asked to. Returns
497 * the success status.
499 * Don't use 'jiffies', so that we don't depend on interrupts
501 static int send_data(unsigned char data)
503 int retries = 3;
505 do {
506 unsigned long timeout = KBD_TIMEOUT;
508 acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
509 resend = 0;
510 reply_expected = 1;
511 kbd_write_output_w(data);
512 for (;;) {
513 if (acknowledge)
514 return 1;
515 if (resend)
516 break;
517 mdelay(1);
518 if (!--timeout) {
519 #ifdef KBD_REPORT_TIMEOUTS
520 printk(KERN_WARNING "keyboard: Timeout - AT keyboard not present?\n");
521 #endif
522 return 0;
525 } while (retries-- > 0);
526 #ifdef KBD_REPORT_TIMEOUTS
527 printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?\n");
528 #endif
529 return 0;
532 void pckbd_leds(unsigned char leds)
534 if (kbd_exists && (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))) {
535 send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
536 kbd_exists = 0;
541 * In case we run on a non-x86 hardware we need to initialize both the
542 * keyboard controller and the keyboard. On a x86, the BIOS will
543 * already have initialized them.
545 * Some x86 BIOSes do not correctly initialize the keyboard, so the
546 * "kbd-reset" command line options can be given to force a reset.
547 * [Ranger]
549 #ifdef __i386__
550 int kbd_startup_reset __initdata = 0;
551 #else
552 int kbd_startup_reset __initdata = 1;
553 #endif
555 /* for "kbd-reset" cmdline param */
556 static int __init kbd_reset_setup(char *str)
558 kbd_startup_reset = 1;
559 return 1;
562 __setup("kbd-reset", kbd_reset_setup);
564 #define KBD_NO_DATA (-1) /* No data */
565 #define KBD_BAD_DATA (-2) /* Parity or other error */
567 static int __init kbd_read_data(void)
569 int retval = KBD_NO_DATA;
570 unsigned char status;
572 status = kbd_read_status();
573 if (status & KBD_STAT_OBF) {
574 unsigned char data = kbd_read_input();
576 retval = data;
577 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
578 retval = KBD_BAD_DATA;
580 return retval;
583 static void __init kbd_clear_input(void)
585 int maxread = 100; /* Random number */
587 do {
588 if (kbd_read_data() == KBD_NO_DATA)
589 break;
590 } while (--maxread);
593 static int __init kbd_wait_for_input(void)
595 long timeout = KBD_INIT_TIMEOUT;
597 do {
598 int retval = kbd_read_data();
599 if (retval >= 0)
600 return retval;
601 mdelay(1);
602 } while (--timeout);
603 return -1;
606 static void kbd_write_command_w(int data)
608 unsigned long flags;
610 spin_lock_irqsave(&kbd_controller_lock, flags);
611 kb_wait();
612 kbd_write_command(data);
613 spin_unlock_irqrestore(&kbd_controller_lock, flags);
616 static void kbd_write_output_w(int data)
618 unsigned long flags;
620 spin_lock_irqsave(&kbd_controller_lock, flags);
621 kb_wait();
622 kbd_write_output(data);
623 spin_unlock_irqrestore(&kbd_controller_lock, flags);
626 #if defined CONFIG_PSMOUSE
627 static void kbd_write_cmd(int cmd)
629 unsigned long flags;
631 spin_lock_irqsave(&kbd_controller_lock, flags);
632 kb_wait();
633 kbd_write_command(KBD_CCMD_WRITE_MODE);
634 kb_wait();
635 kbd_write_output(cmd);
636 spin_unlock_irqrestore(&kbd_controller_lock, flags);
638 #endif /* CONFIG_PSMOUSE */
640 static char * __init initialize_kbd(void)
642 int status;
645 * Test the keyboard interface.
646 * This seems to be the only way to get it going.
647 * If the test is successful a x55 is placed in the input buffer.
649 kbd_write_command_w(KBD_CCMD_SELF_TEST);
650 if (kbd_wait_for_input() != 0x55)
651 return "Keyboard failed self test";
654 * Perform a keyboard interface test. This causes the controller
655 * to test the keyboard clock and data lines. The results of the
656 * test are placed in the input buffer.
658 kbd_write_command_w(KBD_CCMD_KBD_TEST);
659 if (kbd_wait_for_input() != 0x00)
660 return "Keyboard interface failed self test";
663 * Enable the keyboard by allowing the keyboard clock to run.
665 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
668 * Reset keyboard. If the read times out
669 * then the assumption is that no keyboard is
670 * plugged into the machine.
671 * This defaults the keyboard to scan-code set 2.
673 * Set up to try again if the keyboard asks for RESEND.
675 do {
676 kbd_write_output_w(KBD_CMD_RESET);
677 status = kbd_wait_for_input();
678 if (status == KBD_REPLY_ACK)
679 break;
680 if (status != KBD_REPLY_RESEND)
681 return "Keyboard reset failed, no ACK";
682 } while (1);
684 if (kbd_wait_for_input() != KBD_REPLY_POR)
685 return "Keyboard reset failed, no POR";
688 * Set keyboard controller mode. During this, the keyboard should be
689 * in the disabled state.
691 * Set up to try again if the keyboard asks for RESEND.
693 do {
694 kbd_write_output_w(KBD_CMD_DISABLE);
695 status = kbd_wait_for_input();
696 if (status == KBD_REPLY_ACK)
697 break;
698 if (status != KBD_REPLY_RESEND)
699 return "Disable keyboard: no ACK";
700 } while (1);
702 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
703 kbd_write_output_w(KBD_MODE_KBD_INT
704 | KBD_MODE_SYS
705 | KBD_MODE_DISABLE_MOUSE
706 | KBD_MODE_KCC);
708 /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
709 kbd_write_command_w(KBD_CCMD_READ_MODE);
710 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
712 * If the controller does not support conversion,
713 * Set the keyboard to scan-code set 1.
715 kbd_write_output_w(0xF0);
716 kbd_wait_for_input();
717 kbd_write_output_w(0x01);
718 kbd_wait_for_input();
722 kbd_write_output_w(KBD_CMD_ENABLE);
723 if (kbd_wait_for_input() != KBD_REPLY_ACK)
724 return "Enable keyboard: no ACK";
727 * Finally, set the typematic rate to maximum.
729 kbd_write_output_w(KBD_CMD_SET_RATE);
730 if (kbd_wait_for_input() != KBD_REPLY_ACK)
731 return "Set rate: no ACK";
732 kbd_write_output_w(0x00);
733 if (kbd_wait_for_input() != KBD_REPLY_ACK)
734 return "Set rate: no ACK";
736 return NULL;
739 void __init pckbd_init_hw(void)
741 kbd_request_region();
743 /* Flush any pending input. */
744 kbd_clear_input();
746 if (kbd_startup_reset) {
747 char *msg = initialize_kbd();
748 if (msg)
749 printk(KERN_WARNING "initialize_kbd: %s\n", msg);
752 #if defined CONFIG_PSMOUSE
753 psaux_init();
754 #endif
756 /* Ok, finally allocate the IRQ, and off we go.. */
757 kbd_request_irq(keyboard_interrupt);
760 #if defined CONFIG_PSMOUSE
763 * Check if this is a dual port controller.
765 static int __init detect_auxiliary_port(void)
767 unsigned long flags;
768 int loops = 10;
769 int retval = 0;
771 /* Check if the BIOS detected a device on the auxiliary port. */
772 if (aux_device_present == 0xaa)
773 return 1;
775 spin_lock_irqsave(&kbd_controller_lock, flags);
777 /* Put the value 0x5A in the output buffer using the "Write
778 * Auxiliary Device Output Buffer" command (0xD3). Poll the
779 * Status Register for a while to see if the value really
780 * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
781 * bit is also set to 1 in the Status Register, we assume this
782 * controller has an Auxiliary Port (a.k.a. Mouse Port).
784 kb_wait();
785 kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
787 kb_wait();
788 kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
790 do {
791 unsigned char status = kbd_read_status();
793 if (status & KBD_STAT_OBF) {
794 (void) kbd_read_input();
795 if (status & KBD_STAT_MOUSE_OBF) {
796 printk(KERN_INFO "Detected PS/2 Mouse Port.\n");
797 retval = 1;
799 break;
801 mdelay(1);
802 } while (--loops);
803 spin_unlock_irqrestore(&kbd_controller_lock, flags);
805 return retval;
809 * Send a byte to the mouse.
811 static void aux_write_dev(int val)
813 unsigned long flags;
815 spin_lock_irqsave(&kbd_controller_lock, flags);
816 kb_wait();
817 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
818 kb_wait();
819 kbd_write_output(val);
820 spin_unlock_irqrestore(&kbd_controller_lock, flags);
824 * Send a byte to the mouse & handle returned ack
826 static void __aux_write_ack(int val)
828 kb_wait();
829 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
830 kb_wait();
831 kbd_write_output(val);
832 /* we expect an ACK in response. */
833 mouse_reply_expected++;
834 kb_wait();
837 static void aux_write_ack(int val)
839 unsigned long flags;
841 spin_lock_irqsave(&kbd_controller_lock, flags);
842 __aux_write_ack(val);
843 spin_unlock_irqrestore(&kbd_controller_lock, flags);
846 static unsigned char get_from_queue(void)
848 unsigned char result;
849 unsigned long flags;
851 spin_lock_irqsave(&kbd_controller_lock, flags);
852 result = queue->buf[queue->tail];
853 queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
854 spin_unlock_irqrestore(&kbd_controller_lock, flags);
855 return result;
859 static inline int queue_empty(void)
861 return queue->head == queue->tail;
864 static int fasync_aux(int fd, struct file *filp, int on)
866 int retval;
868 retval = fasync_helper(fd, filp, on, &queue->fasync);
869 if (retval < 0)
870 return retval;
871 return 0;
876 * Random magic cookie for the aux device
878 #define AUX_DEV ((void *)queue)
880 static int release_aux(struct inode * inode, struct file * file)
882 lock_kernel();
883 fasync_aux(-1, file, 0);
884 if (--aux_count) {
885 unlock_kernel();
886 return 0;
888 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints */
889 kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
890 aux_free_irq(AUX_DEV);
891 unlock_kernel();
892 return 0;
896 * Install interrupt handler.
897 * Enable auxiliary device.
900 static int open_aux(struct inode * inode, struct file * file)
902 if (aux_count++) {
903 return 0;
905 queue->head = queue->tail = 0; /* Flush input queue */
906 if (aux_request_irq(keyboard_interrupt, AUX_DEV)) {
907 aux_count--;
908 return -EBUSY;
910 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable the
911 auxiliary port on
912 controller. */
913 aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
914 kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
916 send_data(KBD_CMD_ENABLE); /* try to workaround toshiba4030cdt problem */
918 return 0;
922 * Put bytes from input queue to buffer.
925 static ssize_t read_aux(struct file * file, char * buffer,
926 size_t count, loff_t *ppos)
928 DECLARE_WAITQUEUE(wait, current);
929 ssize_t i = count;
930 unsigned char c;
932 if (queue_empty()) {
933 if (file->f_flags & O_NONBLOCK)
934 return -EAGAIN;
935 add_wait_queue(&queue->proc_list, &wait);
936 repeat:
937 set_current_state(TASK_INTERRUPTIBLE);
938 if (queue_empty() && !signal_pending(current)) {
939 schedule();
940 goto repeat;
942 current->state = TASK_RUNNING;
943 remove_wait_queue(&queue->proc_list, &wait);
945 while (i > 0 && !queue_empty()) {
946 c = get_from_queue();
947 put_user(c, buffer++);
948 i--;
950 if (count-i) {
951 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
952 return count-i;
954 if (signal_pending(current))
955 return -ERESTARTSYS;
956 return 0;
960 * Write to the aux device.
963 static ssize_t write_aux(struct file * file, const char * buffer,
964 size_t count, loff_t *ppos)
966 ssize_t retval = 0;
968 if (count) {
969 ssize_t written = 0;
971 if (count > 32)
972 count = 32; /* Limit to 32 bytes. */
973 do {
974 char c;
975 get_user(c, buffer++);
976 aux_write_dev(c);
977 written++;
978 } while (--count);
979 retval = -EIO;
980 if (written) {
981 retval = written;
982 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
986 return retval;
989 /* No kernel lock held - fine */
990 static unsigned int aux_poll(struct file *file, poll_table * wait)
992 poll_wait(file, &queue->proc_list, wait);
993 if (!queue_empty())
994 return POLLIN | POLLRDNORM;
995 return 0;
998 struct file_operations psaux_fops = {
999 read: read_aux,
1000 write: write_aux,
1001 poll: aux_poll,
1002 open: open_aux,
1003 release: release_aux,
1004 fasync: fasync_aux,
1008 * Initialize driver.
1010 static struct miscdevice psaux_mouse = {
1011 PSMOUSE_MINOR, "psaux", &psaux_fops
1014 static int __init psaux_init(void)
1016 if (!detect_auxiliary_port())
1017 return -EIO;
1019 misc_register(&psaux_mouse);
1020 queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
1021 memset(queue, 0, sizeof(*queue));
1022 queue->head = queue->tail = 0;
1023 init_waitqueue_head(&queue->proc_list);
1025 #ifdef INITIALIZE_MOUSE
1026 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
1027 aux_write_ack(AUX_SET_SAMPLE);
1028 aux_write_ack(100); /* 100 samples/sec */
1029 aux_write_ack(AUX_SET_RES);
1030 aux_write_ack(3); /* 8 counts per mm */
1031 aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
1032 #endif /* INITIALIZE_MOUSE */
1033 kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
1034 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
1036 return 0;
1039 #endif /* CONFIG_PSMOUSE */