added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / arch / i386-pc / Drivers / keyboard / kbd_common.c
blobc814884c8b76f5da767f22e21c6bf4f5c8abf304
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: PS/2 mouse driver.
6 Lang: English.
7 */
9 #include "kbd.h"
10 #define TIMER_RPROK 3599597124UL
12 static ULONG usec2tick(ULONG usec)
14 ULONG ret;
15 ULONG prok = TIMER_RPROK;
16 asm volatile("movl $0,%%eax; divl %2":"=a"(ret):"d"(usec),"m"(prok));
17 return ret;
20 static void mouse_usleep(LONG usec)
22 int oldtick, tick;
23 usec = usec2tick(usec);
25 outb(0x80, 0x43);
26 oldtick = inb(0x42);
27 oldtick += inb(0x42) << 8;
29 while (usec > 0)
31 outb(0x80, 0x43);
32 tick = inb(0x42);
33 tick += inb(0x42) << 8;
35 usec -= (oldtick - tick);
36 if (tick > oldtick) usec -= 0x10000;
37 oldtick = tick;
42 unsigned char handle_kbd_event(void)
44 unsigned char status = kbd_read_status();
45 unsigned int work = 10000;
47 while (status & KBD_STATUS_OBF)
49 unsigned char scancode;
51 scancode = kbd_read_input();
53 status = kbd_read_status();
54 if(!work--)
56 //printf(KERN_ERR "pc_keyb: controller jammed (0x%02X).\n",status);
57 break;
60 return status;
63 void kb_wait(void)
65 ULONG timeout = 1000; /* 1 sec should be enough */
69 unsigned char status = handle_kbd_event();
70 if (! (status & KBD_STATUS_IBF))
71 return;
73 mouse_usleep(1000);
74 timeout--;
75 } while (timeout);
78 void kbd_write_cmd(int cmd)
80 kb_wait();
81 kbd_write_command(KBD_CTRLCMD_WRITE_MODE);
82 kb_wait();
83 kbd_write_output(cmd);
86 void aux_write_ack(int val)
88 kb_wait();
89 kbd_write_command(KBD_CTRLCMD_WRITE_MOUSE);
90 kb_wait();;
91 kbd_write_output(val);
92 kb_wait();
95 void kbd_write_output_w(int data)
97 kb_wait();
98 kbd_write_output(data);
101 void kbd_write_command_w(int data)
103 kb_wait();
104 kbd_write_command(data);
107 #define KBD_NO_DATA (-1)
108 #define KBD_BAD_DATA (-2)
110 int kbd_read_data(void)
112 LONG retval = KBD_NO_DATA;
113 UBYTE status;
115 status = kbd_read_status();
116 if (status & KBD_STATUS_OBF)
118 UBYTE data = kbd_read_input();
120 retval = data;
121 if (status & (KBD_STATUS_GTO | KBD_STATUS_PERR))
122 retval = KBD_BAD_DATA;
125 return retval;
128 void kbd_clear_input(void)
130 int maxread = 100;
134 if (kbd_read_data() == KBD_NO_DATA)
135 break;
136 } while (--maxread);
139 int kbd_wait_for_input(void)
141 ULONG timeout = 1000;
145 int retval = kbd_read_data();
146 if (retval >= 0)
147 return retval;
148 mouse_usleep(1000);
149 } while(--timeout);
150 return -1;