Page 1 of 1

Keyboard driver help [SOLVED]

Posted: Mon Jan 11, 2016 12:11 pm
by osdever
I have a problem with my keyboard gets function. My code is:

Code: Select all

const char* kbd_gets()
{
    char *cmd=malloc(128);
    uint8_t c=0;
    int i=0;
    while(true)
    {
        kbd_wait_irq();
        kbd_waitForBufToEmpty();
    c=inb(0x60);
    if(c>0)
    {
        if(c==28)
        {
            cmd[i]='\0';
            return cmd;
        }
        if(c==0x0e)
        {
            if(i>0)
            {
                i--;
                tty_goToPos(tty_x-1, tty_y);
                tty_putchar(' ');
                tty_goToPos(tty_x-1, tty_y);
            }
            continue;
        }
        if(c>0x01 && c<0x81)
        {
            tty_putchar(scancode[c+1]);
            cmd[i++]=scancode[c+1];
        }
    }
    }
    return NULL;
}
Very likely it's completely wrong. But please show me my error and mistakes that I made. There's a kbd_wait_irq, initKbdInt, kbd_handler and kbd_waitForBufForEmpty functions:

Code: Select all

void kbd_waitForBufToEmpty()
{
    char c=inb(0x60);
    while(inb(0x60)==c)
        c=inb(0x60);
}
bool kbd_irq_fired=false;
void kbd_wait_irq()
{
    while(kbd_irq_fired);
    kbd_irq_fired=false;
}
void kbd_handler(struct regs *UNUSED(r))
{
    kbd_irq_fired=true;
}
void initKbdInt()
{
    irq_install_handler(1,kbd_handler);
}
So, the problem is that: the keys sometimes are skipping. Like that:
Expected: helloworld
Result - helool
Without kbd_waitForBufToEmpty all is repeating a lot of times, because OS isn't waiting for keyboard buffer to empty.

Re: Keyboard driver help

Posted: Mon Jan 11, 2016 12:20 pm
by Techel
Check your condition in kbd_wait_irq and mark the 'fired' variable volatile. Note the kbc also notifies on key-up event.

Re: Keyboard driver help

Posted: Tue Jan 12, 2016 7:07 am
by osdever
Done, same problem.

Code: Select all

volatile bool kbd_irq_fired=false;
void kbd_wait_irq()
{
    if(!kbd_irq_fired)return;
    while(kbd_irq_fired);
    kbd_irq_fired=false;
}

Re: Keyboard driver help

Posted: Tue Jan 12, 2016 7:54 am
by osdever
Solved, just needed to improve the handler and remove kbd_waitforBufToEmpty().