tried to make ps/2 mouse work in "remote mode" (polling), but it doesn't
[bootcensus] / src / keyb.c
index 9a34d8f..3e86f43 100644 (file)
@@ -15,12 +15,18 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
+#include <stdio.h>
+#include <string.h>
 #include "keyb.h"
 #include "intr.h"
 #include "asmops.h"
+#include "kbregs.h"
 
-#define KB_IRQ 1
-#define KB_PORT                0x60
+#define delay7us() \
+       do { \
+               iodelay(); iodelay(); iodelay(); iodelay(); \
+               iodelay(); iodelay(); iodelay(); \
+       } while(0)
 
 /* table with rough translations from set 1 scancodes to ASCII-ish */
 static int scantbl[] = {
@@ -49,6 +55,10 @@ static unsigned char keystate[256];
 
 void kb_init(void)
 {
+       buf_ridx = buf_widx = 0;
+       num_pressed = 0;
+       memset(keystate, 0, sizeof keystate);
+
        interrupt(IRQ_TO_INTR(KB_IRQ), kbintr);
 }
 
@@ -110,12 +120,57 @@ void kb_putback(int key)
        buffer[buf_ridx] = key;
 }
 
+int kb_wait_write(void)
+{
+       int i;
+       for(i=0; i<32768; i++) {
+               if(!(inb(KB_STATUS_PORT) & KB_STAT_INBUF_FULL)) {
+                       return 1;
+               }
+               iodelay();
+       }
+       /*printf("kb_wait_write timeout\n");*/
+       return 0;
+}
+
+int kb_wait_read(void)
+{
+       int i;
+       for(i=0; i<32768; i++) {
+               if((inb(KB_STATUS_PORT) & KB_STAT_OUTBUF_FULL)) {
+                       return 1;
+               }
+               iodelay();
+       }
+       /*printf("kb_wait_read timeout\n");*/
+       return 0;
+}
+
+void kb_send_cmd(unsigned char cmd)
+{
+       kb_wait_write();
+       outb(cmd, KB_CMD_PORT);
+}
+
+void kb_send_data(unsigned char data)
+{
+       kb_wait_write();
+       outb(data, KB_DATA_PORT);
+}
+
+unsigned char kb_read_data(void)
+{
+       kb_wait_read();
+       delay7us();
+       return inb(KB_DATA_PORT);
+}
+
 static void kbintr()
 {
        unsigned char code;
        int key, press;
 
-       code = inb(KB_PORT);
+       code = inb(KB_DATA_PORT);
 
        if(code >= 128) {
                press = 0;