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>
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>
39 #include <asm/keyboard.h>
40 #include <asm/bitops.h>
41 #include <asm/uaccess.h>
43 #include <asm/system.h>
47 /* Some configuration switches are present in the include file... */
49 #include <linux/pc_keyb.h>
51 /* Simple translation table for the SysRq keys */
53 #ifdef CONFIG_MAGIC_SYSRQ
54 unsigned char pckbd_sysrq_xlate[128] =
55 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
56 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
57 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
58 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
59 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
60 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
61 "\r\000/"; /* 0x60 - 0x6f */
64 static void kbd_write_command_w(int data);
65 static void kbd_write_output_w(int data);
67 static void aux_write_ack(int val);
68 static void __aux_write_ack(int val);
69 static int aux_reconnect = 0;
72 #ifndef kbd_controller_present
73 #define kbd_controller_present() 1
75 static spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
76 static unsigned char handle_kbd_event(void);
78 /* used only by send_data - set by keyboard_interrupt */
79 static volatile unsigned char reply_expected;
80 static volatile unsigned char acknowledge;
81 static volatile unsigned char resend;
84 #if defined CONFIG_PSMOUSE
86 * PS/2 Auxiliary Device
89 static int __init psaux_init(void);
91 #define AUX_RECONNECT1 0xaa /* scancode1 when ps2 device is plugged (back) in */
92 #define AUX_RECONNECT2 0x00 /* scancode2 when ps2 device is plugged (back) in */
94 static struct aux_queue *queue; /* Mouse data buffer. */
96 /* used when we send commands to the mouse that expect an ACK. */
97 static unsigned char mouse_reply_expected;
99 #define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
100 #define AUX_INTS_ON (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
102 #define MAX_RETRIES 60 /* some aux operations take long time*/
103 #endif /* CONFIG_PSMOUSE */
106 * Wait for keyboard controller input buffer to drain.
108 * Don't use 'jiffies' so that we don't depend on
111 * Quote from PS/2 System Reference Manual:
113 * "Address hex 0060 and address hex 0064 should be written only when
114 * the input-buffer-full bit and output-buffer-full bit in the
115 * Controller Status register are set 0."
118 static void kb_wait(void)
120 unsigned long timeout = KBC_TIMEOUT;
124 * "handle_kbd_event()" will handle any incoming events
125 * while we wait - keypresses or mouse movement.
127 unsigned char status = handle_kbd_event();
129 if (! (status & KBD_STAT_IBF))
134 #ifdef KBD_REPORT_TIMEOUTS
135 printk(KERN_WARNING "Keyboard timed out[1]\n");
140 * Translation of escaped scancodes to keycodes.
141 * This is now user-settable.
142 * The keycodes 1-88,96-111,119 are fairly standard, and
143 * should probably not be changed - changing might confuse X.
144 * X also interprets scancode 0x5d (KEY_Begin).
146 * For 1-88 keycode equals scancode.
149 #define E0_KPENTER 96
151 #define E0_KPSLASH 98
154 #define E0_BREAK 101 /* (control-pause) */
169 * The keycodes below are randomly located in 89-95,112-118,120-127.
170 * They could be thrown away (and all occurrences below replaced by 0),
171 * but that would force many users to use the `setkeycodes' utility, where
172 * they needed not before. It does not matter that there are duplicates, as
173 * long as no duplication occurs for any single keyboard.
177 #define FOCUS_PF1 85 /* actual code! */
185 #define FOCUS_PF9 120
186 #define FOCUS_PF10 121
187 #define FOCUS_PF11 122
188 #define FOCUS_PF12 123
191 /* tfj@olivia.ping.dk:
192 * The four keys are located over the numeric keypad, and are
193 * labelled A1-A4. It's an rc930 keyboard, from
194 * Regnecentralen/RC International, Now ICL.
195 * Scancodes: 59, 5a, 5b, 5c.
202 static unsigned char high_keys[128 - SC_LIM] = {
203 RGN1, RGN2, RGN3, RGN4, 0, 0, 0, /* 0x59-0x5f */
204 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
205 0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12, /* 0x68-0x6f */
206 0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3, /* 0x70-0x77 */
207 FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7, /* 0x78-0x7b */
208 FOCUS_PF8, JAP_86, FOCUS_PF10, 0 /* 0x7c-0x7f */
219 #define E0_KPMINPLUS 118
221 * My OmniKey generates e0 4c for the "OMNI" key and the
222 * right alt key does nada. [kkoller@nyx10.cs.du.edu]
226 * New microsoft keyboard is rumoured to have
227 * e0 5b (left window button), e0 5c (right window button),
228 * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
229 * [or: Windows_L, Windows_R, TaskMan]
235 static unsigned char e0_keys[128] = {
236 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00-0x07 */
237 0, 0, 0, 0, 0, 0, 0, 0, /* 0x08-0x0f */
238 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10-0x17 */
239 0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0, /* 0x18-0x1f */
240 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20-0x27 */
241 0, 0, 0, 0, 0, 0, 0, 0, /* 0x28-0x2f */
242 0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR, /* 0x30-0x37 */
243 E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP, /* 0x38-0x3f */
244 E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME, /* 0x40-0x47 */
245 E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
246 E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0, /* 0x50-0x57 */
247 0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0, /* 0x58-0x5f */
248 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60-0x67 */
249 0, 0, 0, 0, 0, 0, 0, E0_MACRO, /* 0x68-0x6f */
250 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77 */
251 0, 0, 0, 0, 0, 0, 0, 0 /* 0x78-0x7f */
254 int pckbd_setkeycode(unsigned int scancode, unsigned int keycode)
256 if (scancode < SC_LIM || scancode > 255 || keycode > 127)
259 high_keys[scancode - SC_LIM] = keycode;
261 e0_keys[scancode - 128] = keycode;
265 int pckbd_getkeycode(unsigned int scancode)
268 (scancode < SC_LIM || scancode > 255) ? -EINVAL :
269 (scancode < 128) ? high_keys[scancode - SC_LIM] :
270 e0_keys[scancode - 128];
273 static int do_acknowledge(unsigned char scancode)
275 if (reply_expected) {
276 /* Unfortunately, we must recognise these codes only if we know they
277 * are known to be valid (i.e., after sending a command), because there
278 * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
279 * keys with such codes :(
281 if (scancode == KBD_REPLY_ACK) {
285 } else if (scancode == KBD_REPLY_RESEND) {
290 /* Should not happen... */
292 printk(KERN_DEBUG "keyboard reply expected - got %02x\n",
299 int pckbd_translate(unsigned char scancode, unsigned char *keycode,
302 static int prev_scancode;
304 /* special prefix scancodes.. */
305 if (scancode == 0xe0 || scancode == 0xe1) {
306 prev_scancode = scancode;
310 /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
311 if (scancode == 0x00 || scancode == 0xff) {
320 * usually it will be 0xe0, but a Pause key generates
321 * e1 1d 45 e1 9d c5 when pressed, and nothing when released
323 if (prev_scancode != 0xe0) {
324 if (prev_scancode == 0xe1 && scancode == 0x1d) {
325 prev_scancode = 0x100;
327 } else if (prev_scancode == 0x100 && scancode == 0x45) {
331 #ifdef KBD_REPORT_UNKN
333 printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
341 * The keyboard maintains its own internal caps lock and
342 * num lock statuses. In caps lock mode E0 AA precedes make
343 * code and E0 2A follows break code. In num lock mode,
344 * E0 2A precedes make code and E0 AA follows break code.
345 * We do our own book-keeping, so we will just ignore these.
348 * For my keyboard there is no caps lock mode, but there are
349 * both Shift-L and Shift-R modes. The former mode generates
350 * E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
351 * So, we should also ignore the latter. - aeb@cwi.nl
353 if (scancode == 0x2a || scancode == 0x36)
356 if (e0_keys[scancode])
357 *keycode = e0_keys[scancode];
359 #ifdef KBD_REPORT_UNKN
361 printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
367 } else if (scancode >= SC_LIM) {
368 /* This happens with the FOCUS 9000 keyboard
369 Its keys PF1..PF12 are reported to generate
370 55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
371 Moreover, unless repeated, they do not generate
372 key-down events, so we have to zero up_flag below */
373 /* Also, Japanese 86/106 keyboards are reported to
374 generate 0x73 and 0x7d for \ - and \ | respectively. */
375 /* Also, some Brazilian keyboard is reported to produce
376 0x73 and 0x7e for \ ? and KP-dot, respectively. */
378 *keycode = high_keys[scancode - SC_LIM];
382 #ifdef KBD_REPORT_UNKN
383 printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
384 " - ignored\n", scancode);
394 char pckbd_unexpected_up(unsigned char keycode)
396 /* unexpected, but this can happen: maybe this was a key release for a
397 FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
398 if (keycode >= SC_LIM || keycode == 85)
404 int pckbd_pm_resume(struct pm_dev *dev, pm_request_t rqst, void *data)
406 #if defined CONFIG_PSMOUSE
409 if (rqst == PM_RESUME) {
410 if (queue) { /* Aux port detected */
411 if (aux_count == 0) { /* Mouse not in use */
412 spin_lock_irqsave(&kbd_controller_lock, flags);
414 * Dell Lat. C600 A06 enables mouse after resume.
415 * When user touches the pad, it posts IRQ 12
416 * (which we do not process), thus holding keyboard.
418 kbd_write_command(KBD_CCMD_MOUSE_DISABLE);
419 /* kbd_write_cmd(AUX_INTS_OFF); */ /* Config & lock */
421 kbd_write_command(KBD_CCMD_WRITE_MODE);
423 kbd_write_output(AUX_INTS_OFF);
424 spin_unlock_irqrestore(&kbd_controller_lock, flags);
433 static inline void handle_mouse_event(unsigned char scancode)
435 #ifdef CONFIG_PSMOUSE
436 static unsigned char prev_code;
437 if (mouse_reply_expected) {
438 if (scancode == AUX_ACK) {
439 mouse_reply_expected--;
442 mouse_reply_expected = 0;
444 else if(scancode == AUX_RECONNECT2 && prev_code == AUX_RECONNECT1
446 printk (KERN_INFO "PS/2 mouse reconnect detected\n");
447 queue->head = queue->tail = 0; /* Flush input queue */
448 __aux_write_ack(AUX_ENABLE_DEV); /* ping the mouse :) */
452 prev_code = scancode;
453 add_mouse_randomness(scancode);
455 int head = queue->head;
457 queue->buf[head] = scancode;
458 head = (head + 1) & (AUX_BUF_SIZE-1);
459 if (head != queue->tail) {
461 kill_fasync(&queue->fasync, SIGIO, POLL_IN);
462 wake_up_interruptible(&queue->proc_list);
468 static unsigned char kbd_exists = 1;
470 static inline void handle_keyboard_event(unsigned char scancode)
474 if (do_acknowledge(scancode))
475 handle_scancode(scancode, !(scancode & 0x80));
477 tasklet_schedule(&keyboard_tasklet);
481 * This reads the keyboard status port, and does the
482 * appropriate action.
484 * It requires that we hold the keyboard controller
487 static unsigned char handle_kbd_event(void)
489 unsigned char status = kbd_read_status();
490 unsigned int work = 10000;
492 while ((--work > 0) && (status & KBD_STAT_OBF)) {
493 unsigned char scancode;
495 scancode = kbd_read_input();
497 /* Error bytes must be ignored to make the
498 Synaptics touchpads compaq use work */
500 /* Ignore error bytes */
501 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
504 if (status & KBD_STAT_MOUSE_OBF)
505 handle_mouse_event(scancode);
507 handle_keyboard_event(scancode);
510 status = kbd_read_status();
514 printk(KERN_ERR "pc_keyb: controller jammed (0x%02X).\n", status);
520 static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
526 spin_lock_irq(&kbd_controller_lock);
528 spin_unlock_irq(&kbd_controller_lock);
532 * send_data sends a character to the keyboard and waits
533 * for an acknowledge, possibly retrying if asked to. Returns
534 * the success status.
536 * Don't use 'jiffies', so that we don't depend on interrupts
538 static int send_data(unsigned char data)
543 unsigned long timeout = KBD_TIMEOUT;
545 acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
548 kbd_write_output_w(data);
556 #ifdef KBD_REPORT_TIMEOUTS
557 printk(KERN_WARNING "keyboard: Timeout - AT keyboard not present?(%02x)\n", data);
562 } while (retries-- > 0);
563 #ifdef KBD_REPORT_TIMEOUTS
564 printk(KERN_WARNING "keyboard: Too many NACKs -- noisy kbd cable?\n");
569 void pckbd_leds(unsigned char leds)
571 if (kbd_exists && (!send_data(KBD_CMD_SET_LEDS) || !send_data(leds))) {
572 send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
577 #define DEFAULT_KEYB_REP_DELAY 250
578 #define DEFAULT_KEYB_REP_RATE 30 /* cps */
580 static struct kbd_repeat kbdrate={
581 DEFAULT_KEYB_REP_DELAY,
582 DEFAULT_KEYB_REP_RATE
585 static unsigned char parse_kbd_rate(struct kbd_repeat *r)
590 } kbd_rates[]={ {5,0x14},
601 } kbd_delays[]={{250,0},
608 int i,new_rate=30,new_delay=250;
610 r->rate=kbdrate.rate;
612 r->delay=kbdrate.delay;
613 for (i=0; i < sizeof(kbd_rates)/sizeof(struct r2v); i++)
614 if (kbd_rates[i].rate == r->rate){
615 new_rate=kbd_rates[i].rate;
616 rate=kbd_rates[i].val;
619 for (i=0; i < sizeof(kbd_delays)/sizeof(struct d2v); i++)
620 if (kbd_delays[i].delay == r->delay){
621 new_delay=kbd_delays[i].delay;
622 delay=kbd_delays[i].val;
628 return (delay << 5) | rate;
631 static int write_kbd_rate(unsigned char r)
633 if (!send_data(KBD_CMD_SET_RATE) || !send_data(r)){
634 send_data(KBD_CMD_ENABLE); /* re-enable kbd if any errors */
640 static int pckbd_rate(struct kbd_repeat *rep)
645 unsigned char r=parse_kbd_rate(rep);
646 struct kbd_repeat old_rep;
647 memcpy(&old_rep,&kbdrate,sizeof(struct kbd_repeat));
648 if (write_kbd_rate(r)){
649 memcpy(&kbdrate,rep,sizeof(struct kbd_repeat));
650 memcpy(rep,&old_rep,sizeof(struct kbd_repeat));
658 * In case we run on a non-x86 hardware we need to initialize both the
659 * keyboard controller and the keyboard. On a x86, the BIOS will
660 * already have initialized them.
662 * Some x86 BIOSes do not correctly initialize the keyboard, so the
663 * "kbd-reset" command line options can be given to force a reset.
667 int kbd_startup_reset __initdata = 0;
669 int kbd_startup_reset __initdata = 1;
672 /* for "kbd-reset" cmdline param */
673 static int __init kbd_reset_setup(char *str)
675 kbd_startup_reset = 1;
679 __setup("kbd-reset", kbd_reset_setup);
681 #define KBD_NO_DATA (-1) /* No data */
682 #define KBD_BAD_DATA (-2) /* Parity or other error */
684 static int __init kbd_read_data(void)
686 int retval = KBD_NO_DATA;
687 unsigned char status;
689 status = kbd_read_status();
690 if (status & KBD_STAT_OBF) {
691 unsigned char data = kbd_read_input();
694 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
695 retval = KBD_BAD_DATA;
700 static void __init kbd_clear_input(void)
702 int maxread = 100; /* Random number */
705 if (kbd_read_data() == KBD_NO_DATA)
710 static int __init kbd_wait_for_input(void)
712 long timeout = KBD_INIT_TIMEOUT;
715 int retval = kbd_read_data();
723 static void kbd_write_command_w(int data)
727 spin_lock_irqsave(&kbd_controller_lock, flags);
729 kbd_write_command(data);
730 spin_unlock_irqrestore(&kbd_controller_lock, flags);
733 static void kbd_write_output_w(int data)
737 spin_lock_irqsave(&kbd_controller_lock, flags);
739 kbd_write_output(data);
740 spin_unlock_irqrestore(&kbd_controller_lock, flags);
743 #if defined(__alpha__)
745 * Some Alphas cannot mask some/all interrupts, so we have to
746 * make sure not to allow interrupts AT ALL when polling for
747 * specific return values from the keyboard.
749 * I think this should work on any architecture, but for now, only Alpha.
751 static int kbd_write_command_w_and_wait(int data)
756 spin_lock_irqsave(&kbd_controller_lock, flags);
758 kbd_write_command(data);
759 input = kbd_wait_for_input();
760 spin_unlock_irqrestore(&kbd_controller_lock, flags);
764 static int kbd_write_output_w_and_wait(int data)
769 spin_lock_irqsave(&kbd_controller_lock, flags);
771 kbd_write_output(data);
772 input = kbd_wait_for_input();
773 spin_unlock_irqrestore(&kbd_controller_lock, flags);
777 static int kbd_write_command_w_and_wait(int data)
779 kbd_write_command_w(data);
780 return kbd_wait_for_input();
783 static int kbd_write_output_w_and_wait(int data)
785 kbd_write_output_w(data);
786 return kbd_wait_for_input();
788 #endif /* __alpha__ */
790 #if defined CONFIG_PSMOUSE
791 static void kbd_write_cmd(int cmd)
795 spin_lock_irqsave(&kbd_controller_lock, flags);
797 kbd_write_command(KBD_CCMD_WRITE_MODE);
799 kbd_write_output(cmd);
800 spin_unlock_irqrestore(&kbd_controller_lock, flags);
802 #endif /* CONFIG_PSMOUSE */
804 static char * __init initialize_kbd(void)
809 * Test the keyboard interface.
810 * This seems to be the only way to get it going.
811 * If the test is successful a x55 is placed in the input buffer.
813 kbd_write_command_w(KBD_CCMD_SELF_TEST);
814 if (kbd_wait_for_input() != 0x55)
815 return "Keyboard failed self test";
818 * Perform a keyboard interface test. This causes the controller
819 * to test the keyboard clock and data lines. The results of the
820 * test are placed in the input buffer.
822 kbd_write_command_w(KBD_CCMD_KBD_TEST);
823 if (kbd_wait_for_input() != 0x00)
824 return "Keyboard interface failed self test";
827 * Enable the keyboard by allowing the keyboard clock to run.
829 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
832 * Reset keyboard. If the read times out
833 * then the assumption is that no keyboard is
834 * plugged into the machine.
835 * This defaults the keyboard to scan-code set 2.
837 * Set up to try again if the keyboard asks for RESEND.
840 kbd_write_output_w(KBD_CMD_RESET);
841 status = kbd_wait_for_input();
842 if (status == KBD_REPLY_ACK)
844 if (status != KBD_REPLY_RESEND)
845 return "Keyboard reset failed, no ACK";
848 if (kbd_wait_for_input() != KBD_REPLY_POR)
849 return "Keyboard reset failed, no POR";
852 * Set keyboard controller mode. During this, the keyboard should be
853 * in the disabled state.
855 * Set up to try again if the keyboard asks for RESEND.
858 kbd_write_output_w(KBD_CMD_DISABLE);
859 status = kbd_wait_for_input();
860 if (status == KBD_REPLY_ACK)
862 if (status != KBD_REPLY_RESEND)
863 return "Disable keyboard: no ACK";
866 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
867 kbd_write_output_w(KBD_MODE_KBD_INT
869 | KBD_MODE_DISABLE_MOUSE
872 /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
873 if (!(kbd_write_command_w_and_wait(KBD_CCMD_READ_MODE) & KBD_MODE_KCC))
876 * If the controller does not support conversion,
877 * Set the keyboard to scan-code set 1.
879 kbd_write_output_w(0xF0);
880 kbd_wait_for_input();
881 kbd_write_output_w(0x01);
882 kbd_wait_for_input();
885 if (kbd_write_output_w_and_wait(KBD_CMD_ENABLE) != KBD_REPLY_ACK)
886 return "Enable keyboard: no ACK";
889 * Finally, set the typematic rate to maximum.
891 if (kbd_write_output_w_and_wait(KBD_CMD_SET_RATE) != KBD_REPLY_ACK)
892 return "Set rate: no ACK";
893 if (kbd_write_output_w_and_wait(0x00) != KBD_REPLY_ACK)
894 return "Set rate: no 2nd ACK";
899 void __init pckbd_init_hw(void)
901 if (!kbd_controller_present()) {
906 kbd_request_region();
908 /* Flush any pending input. */
911 if (kbd_startup_reset) {
912 char *msg = initialize_kbd();
914 printk(KERN_WARNING "initialize_kbd: %s\n", msg);
917 #if defined CONFIG_PSMOUSE
921 kbd_rate = pckbd_rate;
923 /* Ok, finally allocate the IRQ, and off we go.. */
924 kbd_request_irq(keyboard_interrupt);
927 #if defined CONFIG_PSMOUSE
929 static int __init aux_reconnect_setup (char *str)
935 __setup("psaux-reconnect", aux_reconnect_setup);
938 * Check if this is a dual port controller.
940 static int __init detect_auxiliary_port(void)
946 /* Check if the BIOS detected a device on the auxiliary port. */
947 if (aux_device_present == 0xaa)
950 spin_lock_irqsave(&kbd_controller_lock, flags);
952 /* Put the value 0x5A in the output buffer using the "Write
953 * Auxiliary Device Output Buffer" command (0xD3). Poll the
954 * Status Register for a while to see if the value really
955 * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
956 * bit is also set to 1 in the Status Register, we assume this
957 * controller has an Auxiliary Port (a.k.a. Mouse Port).
960 kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
963 kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
966 unsigned char status = kbd_read_status();
968 if (status & KBD_STAT_OBF) {
969 (void) kbd_read_input();
970 if (status & KBD_STAT_MOUSE_OBF) {
971 printk(KERN_INFO "Detected PS/2 Mouse Port.\n");
978 spin_unlock_irqrestore(&kbd_controller_lock, flags);
984 * Send a byte to the mouse.
986 static void aux_write_dev(int val)
990 spin_lock_irqsave(&kbd_controller_lock, flags);
992 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
994 kbd_write_output(val);
995 spin_unlock_irqrestore(&kbd_controller_lock, flags);
999 * Send a byte to the mouse & handle returned ack
1001 static void __aux_write_ack(int val)
1004 kbd_write_command(KBD_CCMD_WRITE_MOUSE);
1006 kbd_write_output(val);
1007 /* we expect an ACK in response. */
1008 mouse_reply_expected++;
1012 static void aux_write_ack(int val)
1014 unsigned long flags;
1016 spin_lock_irqsave(&kbd_controller_lock, flags);
1017 __aux_write_ack(val);
1018 spin_unlock_irqrestore(&kbd_controller_lock, flags);
1021 static unsigned char get_from_queue(void)
1023 unsigned char result;
1024 unsigned long flags;
1026 spin_lock_irqsave(&kbd_controller_lock, flags);
1027 result = queue->buf[queue->tail];
1028 queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
1029 spin_unlock_irqrestore(&kbd_controller_lock, flags);
1034 static inline int queue_empty(void)
1036 return queue->head == queue->tail;
1039 static int fasync_aux(int fd, struct file *filp, int on)
1043 retval = fasync_helper(fd, filp, on, &queue->fasync);
1051 * Random magic cookie for the aux device
1053 #define AUX_DEV ((void *)queue)
1055 static int release_aux(struct inode * inode, struct file * file)
1058 fasync_aux(-1, file, 0);
1063 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints */
1064 kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
1065 aux_free_irq(AUX_DEV);
1071 * Install interrupt handler.
1072 * Enable auxiliary device.
1075 static int open_aux(struct inode * inode, struct file * file)
1080 queue->head = queue->tail = 0; /* Flush input queue */
1081 if (aux_request_irq(keyboard_interrupt, AUX_DEV)) {
1085 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable the
1088 aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
1089 kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
1091 mdelay(2); /* Ensure we follow the kbc access delay rules.. */
1093 send_data(KBD_CMD_ENABLE); /* try to workaround toshiba4030cdt problem */
1099 * Put bytes from input queue to buffer.
1102 static ssize_t read_aux(struct file * file, char * buffer,
1103 size_t count, loff_t *ppos)
1105 DECLARE_WAITQUEUE(wait, current);
1109 if (queue_empty()) {
1110 if (file->f_flags & O_NONBLOCK)
1112 add_wait_queue(&queue->proc_list, &wait);
1114 set_current_state(TASK_INTERRUPTIBLE);
1115 if (queue_empty() && !signal_pending(current)) {
1119 current->state = TASK_RUNNING;
1120 remove_wait_queue(&queue->proc_list, &wait);
1122 while (i > 0 && !queue_empty()) {
1123 c = get_from_queue();
1124 put_user(c, buffer++);
1128 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
1131 if (signal_pending(current))
1132 return -ERESTARTSYS;
1137 * Write to the aux device.
1140 static ssize_t write_aux(struct file * file, const char * buffer,
1141 size_t count, loff_t *ppos)
1146 ssize_t written = 0;
1149 count = 32; /* Limit to 32 bytes. */
1152 get_user(c, buffer++);
1159 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1166 /* No kernel lock held - fine */
1167 static unsigned int aux_poll(struct file *file, poll_table * wait)
1169 poll_wait(file, &queue->proc_list, wait);
1171 return POLLIN | POLLRDNORM;
1175 struct file_operations psaux_fops = {
1180 release: release_aux,
1185 * Initialize driver.
1187 static struct miscdevice psaux_mouse = {
1188 PSMOUSE_MINOR, "psaux", &psaux_fops
1191 static int __init psaux_init(void)
1195 if (!detect_auxiliary_port())
1198 if ((retval = misc_register(&psaux_mouse)))
1201 queue = (struct aux_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
1202 if (queue == NULL) {
1203 printk(KERN_ERR "psaux_init(): out of memory\n");
1204 misc_deregister(&psaux_mouse);
1207 memset(queue, 0, sizeof(*queue));
1208 queue->head = queue->tail = 0;
1209 init_waitqueue_head(&queue->proc_list);
1211 #ifdef INITIALIZE_MOUSE
1212 kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
1213 aux_write_ack(AUX_SET_SAMPLE);
1214 aux_write_ack(100); /* 100 samples/sec */
1215 aux_write_ack(AUX_SET_RES);
1216 aux_write_ack(3); /* 8 counts per mm */
1217 aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
1218 #endif /* INITIALIZE_MOUSE */
1219 kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
1220 kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
1225 #endif /* CONFIG_PSMOUSE */
1228 static int blink_frequency = HZ/2;
1230 /* Tell the user who may be running in X and not see the console that we have
1231 panic'ed. This is to distingush panics from "real" lockups.
1232 Could in theory send the panic message as morse, but that is left as an
1233 exercise for the reader. */
1234 void panic_blink(void)
1236 static unsigned long last_jiffie;
1238 /* Roughly 1/2s frequency. KDB uses about 1s. Make sure it is
1240 if (!blink_frequency)
1242 if (jiffies - last_jiffie > blink_frequency) {
1244 while (kbd_read_status() & KBD_STAT_IBF) mdelay(1);
1245 kbd_write_output(KBD_CMD_SET_LEDS);
1247 while (kbd_read_status() & KBD_STAT_IBF) mdelay(1);
1249 kbd_write_output(led);
1250 last_jiffie = jiffies;
1254 static int __init panicblink_setup(char *str)
1257 if (get_option(&str,&par))
1258 blink_frequency = par*(1000/HZ);
1262 /* panicblink=0 disables the blinking as it caused problems with some console
1263 switches. otherwise argument is ms of a blink period. */
1264 __setup("panicblink=", panicblink_setup);